Home  >  Q&A  >  body text

SQL - when lineid is '01', use newid, otherwise use id

There is such data in my table

Line number 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

I hope my output is like this

document Line number 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 invoice data and having lineid='01' for each row means that the information is for different invoices so I have to tag it with new documentID and I would like your help to create it using command. < /p>

This might be simple but I searched like a madman here and can't find a solution.

EDIT: Yes, what I want is "increment docid every time lineid equals 01"

P粉394812277P粉394812277180 days ago378

reply all(1)I'll reply

  • P粉648469285

    P粉6484692852024-04-03 09:50:47

    You can use run count 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]

    reply
    0
  • Cancelreply