[英]How to find count of column, based on 3 distinct values of 3 columns in Sql server?
[英]How to find distinct sum of a column in sql server?
我有一個像這樣的結構表:
lineID lineItemPrice
1 20
2 25
3 27
4 30
4 30
4 30
我想得到lineIdPrice的總和,其中lineId是不同的。 我不確定sql查詢應該是什么? 請幫忙。
輸出應為102。
我不太清楚你是否正在尋找這個:
select
sum(lineItemPrice), lineID
from
table
group by lineID
或這個:
select
sum(lineItemPrice)
from
(select distinct lineID, lineItemPrice from table)
如果要整個表的總和:
select
sum(lineItemPrice)
from
table
第一個給出的結果將匯總所有lineItemPrice與其各自的lineID的值
lineID lineItemPrice
1 20
2 25
3 27
4 90
第二個將匯總所有這些不同的記錄,並給出102作為答案
lineID lineItemPrice
1 20
2 25
3 27
4 30
第三:
lineItemPrice
162
嘗試這個:
SELECT SUM(lineItemPrice) as TotalSum FROM
(SELECT lineItemPrice
FROM TableName
GROUP BY lineID,lineITemPrice) T
結果:
TOTALSUM
102
在SQL Fiddle中查看結果。
請試試:
select
SUM(lineItemPrice) lineItemPrice
from(
select
distinct lineID, lineItemPrice
From tbl
)x
您可以在下面的查詢中使用lineID lineITemPrice是否具有相同的值對。
select
SUM(DISTINCT lineItemPrice) lineItemPrice
From tbl
這應該工作:
Select lineID, SUM(lineItemPrice) Total From YourTableName Group By lineID
很簡單。
GROUP BY語句與聚合函數(在此我們使用SUM函數)結合使用,以按一個或多個列對結果集進行分組。
為什么不這樣
SELECT * FROM
(
Select lineID, SUM(lineItemPrice) Over(Partition By lineID) Total
From TableName
) T
Group By lineID,Total
輸出:
lineID lineItemPrice
1 20
2 25
3 27
4 90
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.