Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

sql server - SQL - combine rows conditionally on the value of previous row

My issue I believe is best explained by using an example:

Sample Data

SO Customer Product Description Quantity Extension Country
01 xxx 123 Product A 01 20.00 USA
02 yyy 456 Product B 02 25.00 China
02 yyy 456 Product B 01 12.50 China
02 yyy 789 Product C 01 50.00 Mexico
03 zzz 789 Product C 01 50.00 Mexico
03 zzz 789 Product C 01 50.00 Mexico
question from:https://stackoverflow.com/questions/65847478/sql-combine-rows-conditionally-on-the-value-of-previous-row

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This looks like group by:

select SO, Customer, Product, Description, sum(Quantity), sum(Extension),   Country
from t
group by SO, Customer, Product, Description, Country;

Note that quantity looks strange. Numbers in SQL are not shown with leading zeros -- and numbers should be stored as numbers, not strings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...