2014-05-06 62 views
0

我的表看起來像這樣:需要編寫SQL Server查詢返回唯一值的總和(基於一列)

Supplier Reference  Description  Total  
-------------------------------------------------- 
smiths  BP657869510L NULL    42 
smiths  BP657869510L NULL    42 
smiths  BP654669510L No. 5621   13 
smiths  BP654669510L No. 5621   13 
corrigan 15:51   Order 23542  23 
corrigan 15:51   Order 23542  23 
williams 14015   Block B   19 
williams 14015   Block B   19 

我想一個T-SQL查詢寫入

  • 返回每個供應商的交易清單,根據Reference列清除重複條目。
  • 返回與每個供應商的交易總額,再次根據Reference列消除重複的條目。

所以結果我想基於數據返回上面會

Supplier Reference  Description  Total  
    --------------------------------------------------- 
    smiths  BP657869510L NULL    42 
    smiths  BP654669510L No. 5621   13 
    corrigan 15:51   Order 23542  23 
    williams 14015   Block B   19 

和第二個要求:

Supplier Total 
    --------------------- 
    smiths  55 
    corrigan 23 
    williams 19 

這可能嗎?請注意,即使Reference列包含相同的值,其他列中的值也可能不同。如果發生這種情況並不重要,我只關心包含不同或獨特值的行。

+1

是有可能 – cyan

+1

你的解釋是混亂。每個'供應商'有可能有不同的參考?如果有不同的'Reference'與'Total'相同,你還想只考慮一行嗎?如果「描述」不同,可以隨機選擇哪一個? – dnoeth

+0

是的,每個'供應商'都可以有不同的'參考' - 我通過在不同'參考'條目下的樣品表中爲'供應商''史密斯'分配條目來證明這一點。我希望每一行都有一個唯一的'Reference'來考慮數量是否相同。如果'Reference'中的'Description'不同,那麼'Reference'是相同的行,我沒有偏好選擇哪一個,但理想情況下我想返回其中的一個。 – James

回答

0

請嘗試以下sql
假設@tempData是您的表名稱。

declare @tempData table 
(
    supplier nvarchar(20), 
    reference nvarchar (20), 
    xDescription nvarchar(20), 
    total int 
); 

insert into @tempData 
select 'smiths',  'BP657869510L' ,NULL,    42 union all 
select 'smiths',  'BP657869510L' ,NULL,    42 union all 
select 'smiths',  'BP654669510L' ,'No. 5621',   13 union all 
select 'smiths',  'BP654669510L' ,'No. 5621',   13 union all 
select 'corrigan', '15:51'   ,'Order 23542',  23 union all 
select 'corrigan', '15:51'   ,'Order 23542',  23 union all 
select 'williams', '14015'   ,'Block B',   19 union all 
select 'williams', '14015'   ,'Block B',   19 
; 

select 
    a.supplier 
    , a.reference 
    , a.xDescription 
    , a.total 
from @tempData a 
group by a.supplier 
    , a.reference 
    , a.xDescription 
    , a.total 
; 

/* 
supplier    reference   xDescription   total 
-------------------- -------------------- -------------------- ----------- 
corrigan    15:51    Order 23542   23 
smiths    BP654669510L   No. 5621    13 
smiths    BP657869510L   NULL     42 
williams    14015    Block B    19 
*/ 


with cte as 
(
select 
    a.supplier 
    , a.reference 
    , a.xDescription 
    , a.total 
from @tempData a 
group by a.supplier 
    , a.reference 
    , a.xDescription 
    , a.total 
) 
select 
    distinct c.supplier, sum(c.total) over(partition by c.supplier) as total 
from cte c 
; 

/* 
supplier    total 
-------------------- ----------- 
corrigan    23 
smiths    55 
williams    19 
*/ 

UPDATE
的請求,該查詢的目的是包括具有不同的描述中,相同的供應商獨立的記錄:例如供應商史密斯

DENSE_RANK()將滿足該請求(http://technet.microsoft.com/en-us/library/ms173825(v=sql.90).aspx

with cte as 
(
select 
    a.supplier 
    , a.reference 
    , a.xDescription 
    , a.total 
    ,dense_rank() over(partition by a.supplier order by a.supplier, a.xDescription) as dRow 
from @tempData a 
group by a.supplier 
    , a.reference 
    , a.xDescription 
    , a.total 
) 
select 
    distinct c.supplier, sum(c.total) over(partition by c.supplier,drow) as total 
from cte c 
; 

/* 
supplier    total 
-------------------- ----------- 
corrigan    23 
smiths    13 
smiths    42 
williams    19 
*/ 

查看全部現場

with cte as 
(
select 
    a.supplier 
    , a.reference 
    , a.xDescription 
    , a.total 
    ,dense_rank() over(partition by a.supplier order by a.supplier, a.xDescription) as dRow 
from @tempData a 
group by a.supplier 
    , a.reference 
    , a.xDescription 
    , a.total 
) 
select 
    distinct c.supplier, c.reference,c.xDescription, sum(c.total) over(partition by c.supplier,drow) as total 
from cte c 
; 
+0

此查詢返回任何列包含不同的數據的所有行。例如,如果'Description'在每行中略有不同,它將返回'Reference'的多行。我只想返回參考值不同的行。 – James

+0

@james此查詢提供了第一個和第二個問題的結果。您能否通過添加更多數據樣本來進一步描述問題?並且還請更新您所需的數據結果。 – cyan

+0

謝謝你,但是如果你在上面的表中將「Description」的一個值改爲unique,那麼你的查詢將不會返回上面描述的結果。 – James

1
declare @tempData table 
(
    supplier nvarchar(20), 
    reference nvarchar (20), 
    xDescription nvarchar(20), 
    total int 
); 

insert into @tempData 
select 'smiths',  'BP657869510L' ,NULL,    42 union all 
select 'smiths',  'BP657869510L' ,NULL,    42 union all 
select 'smiths',  'BP654669510L' ,'No. 5621',   13 union all 
select 'smiths',  'BP654669510L' ,'No. 5621',   13 union all 
select 'corrigan', '15:51'   ,'Order 23542',  23 union all 
select 'corrigan', '15:51'   ,'Order 23542',  23 union all 
select 'williams', '14015'   ,'Block B',   19 union all 
select 'williams', '14015'   ,'Block B',   19 
; 

select distinct x.supplier, 
SUM(X.total)OVER(PARTITION BY x.supplier)As Total from 
(Select a.supplier,a.reference,a.xDescription,a.total from @tempData a 
GROUP BY a.supplier,a.reference,a.xDescription,a.total) X 
GROUP BY x.supplier,X.total 
+0

謝謝,我不得不對這個查詢做一個小小的調整,但它引導我回答我的第二個問題(即交易總和)。這是工作的查詢:select distinct x.supplier, SUM(X.total)OVER(PARTITION BY x.supplier)As Total from (Select a.supplier,a.reference,a.description,a.total from @tempData a GROUP BY a.supplier,a.reference,a.description,a.total)X GROUP BY x.supplier,x.reference,X.total – James

+0

我的答案LOL如何?老兄,我應該至少投票了,mohan111正在使用我的@tempdata查詢LOL。 – cyan

1

按照從OP Total評論永遠是Reference相同,但Description可以改變。 DISTINCT相當於在SELECT

一個GROUP BY所有列爲了得到第一個需求的不同是不夠的,如果它能夠丟棄Description

SELECT DISTINCT 
     Supplier 
    , Reference 
    , Total 
FROM myTable 

,如果它是不可能的,那麼空,一MAX或在同一直線上的東西可以做到的,在一個NULL下面的查詢是否存在該組一個以上的值被返回,否則該單一值被輸出

SELECT Supplier 
    , Reference 
    , Description = CASE WHEN COUNT(DISTINCT Description) > 1 THEN NULL 
          ELSE MAX(Description) 
        END 
    , Total 
FROM myTable 
GROUP BY Supplier, Reference, Total 

要獲得第二個,上面的查詢可以用作CTE作爲主查詢添加GROUP BY,在這種情況下,Description列是不需要的,因此被刪除。

With dValue AS (
    SELECT DISTINCT 
     Supplier 
     , Reference 
     , Total 
    FROM myTable 
) 
SELECT Supplier 
    , SUM(Total) Total 
FROM dValue 
GROUP BY Supplier 

如果你有一個版本的SQLServer的其中CTE是不可能的第一個查詢可以作爲一個子查詢來獲得相同的結果

SELECT Supplier 
    , SUM(Total) Total 
FROM (SELECT DISTINCT Supplier, Reference, Total 
     FROM myTable) dValue 
GROUP BY Supplier 
+0

據我所知,僅使用不同的(如在您的第一個響應中)將返回任何列不同的所有行。例如,如果描述稍有不同,它將返回引用相同的行。我只想要引用不同的行。 – James

+0

道歉 - 引用可能在2行或更多行中相同,其他列包含不同的值。任何關於查詢的建議只返回Reference不同的行嗎? – James

+0

爲了我的目的,如果'Reference'是相同的'Total'將是相同的。 「說明」可能不同。 – James