Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Get Table conditions SQL

I have this table in SQL:

Date Price Quantity
16/11/2020 45.2 -1000
16/11/2020 45.2 -500
17/11/2020 48 800
16/11/2020 46 200
16/11/2020 46 200

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

1 Answer

0 votes
by (71.8m points)

Looking at the description, it seems like you want a group based on negative and positive quantity along with date.

You have not tagged your question with any database but you can use the standard sql case .. when as follows:

Select date, price, sum(quantity) as quantity
  From your_table t
Group by date, price, case when quantity < 0 the 1 else 2 end;

If you dont want the result to be groupped by price then you can remove the price from the group by clause and use max(price) or any aggregate function as per your requirement in the select clause.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...