SQL - Case when lineid='01' then newid else id

72 Views Asked by At

I have this kind of data in my table

lineid price €
01 100.00
02 200.00
01 10.34
01 311.12
01 14.33
02 36.44
03 89.70
04 11.33

and i would like my output to be like this

docid lineid price €
1 01 100
1 02 200.00
2 01 10.34
3 01 311.12
4 01 14.33
4 02 36.44
4 03 89.70
4 04 11.33

Its data for invoices and for every line that has lineid='01' it means that the info is for different invoice so i have to mark it with new documentID that i want you to help me create it with a command.

Its probably something easy but i am searching like a maniac here and i cant find the solution.

EDIT: Yes , it Is "increment docid each time lineid equals 01" what i want

1

There are 1 best solutions below

0
Danish On

You could use running counts using something like below (assuming this is MS SQL you are talking about)

SELECT ROW_NUMBER() over(partition by [LineId] order by [LineId]) as DocId,
      [LineId],
      [Price]
  FROM [StackOverflow].[dbo].[RunningCount] order by [LineId]