SQL - when lineid is '01', use newid, otherwise use id
P粉394812277
P粉394812277 2024-04-02 12:08:51
0
1
377

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粉394812277
P粉394812277

reply all(1)
P粉648469285

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]
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!