SQL查询基于2个列的唯一值

时间:2016-02-05 12:18:57

标签: sql-server-2008 ssms common-table-expression sql-view ssms-2012

我需要帮助解决这个问题。

目前我有这个问题:

SELECT Table1.Column1 AS Company
    ,SUM(DATEDIFF(Day, Table2.Column2, Table1.Column3)) AS DiffDate
FROM Table1
INNER JOIN Table2 ON Table1.Column6 = Table2.Column6
    AND Table1.Column5 = Table2.Column5
GROUP BY Company

结果是......

Company        DiffDate  

company1          8

但这不是我想要的,因为有一些值是column5和column6的组合是相同的。见下文。

Company  |  Column6  |  Column5  |  DiffDate  
Company1 |   5782    |     10    |      2  
Company1 |   5782    |     10    |      2  
Company1 |   5782    |     20    |      2  
Company1 |   5782    |     30    |      2 

所以我之后的结果是6,而不是8。

我尝试使用SELECT DISTINCT,但这并没有做任何事情。

提前谢谢你。

编辑2016-02-09

我在SSMS中进行了以下查询。

;WITH cte AS (SELECT DISTINCT bestlevdat, bestradnr, bestnr FROM dbo.bpa)
SELECT  dbo.bp.ftgnr AS Företagsnr,SUM(DATEDIFF(Day,cte.bestlevdat, dbo.bp.bestberlevdat)) AS Diff_Bekräftat_dat, 
        SUM(DATEDIFF(Day,cte.bestlevdat, dbo.bp.bestbeglevdat)) AS Diff_Önskat_dat, COUNT(dbo.bp.bestradnr) AS AntalRader, SUM(CASE WHEN datediff(day, cte.bestlevdat, dbo.bp.bestberlevdat) < - 0 THEN 1 ELSE 0 END) AS Antal_avvikande_rader, ROUND((COUNT(dbo.bp.bestradnr) - SUM(CASE WHEN datediff(day, cte.bestlevdat, dbo.bp.bestberlevdat) < - 0 THEN 1.0 ELSE 0.0 END)) * 100 / COUNT(dbo.bp.bestradnr), 1) AS Levsäk
FROM    dbo.bp INNER JOIN
        cte ON dbo.bp.bestnr = cte.bestnr AND dbo.bp.bestradnr = cte.bestradnr
WHERE     (YEAR(dbo.bp.bestberlevdat) = '2015')
GROUP BY dbo.bp.ftgnr
ORDER BY AntalRader DESC

这很有魅力。但是当我创建一个新视图并在那里粘贴此查询并尝试保存时,我会在&#34 ;;&#34;上收到错误。再次。如果我删除&#34;;&#34;在SSMS中我得到错误&#34;多部分标识符&#34; cte.bestlevdat&#34;无法受约束的#34;并将我的查询转换为以下内容:

WITH cte AS (SELECT DISTINCT bestlevdat, bestradnr, bestnr
                         FROM         dbo.bpa AS bpa_1)
SELECT     TOP (100) PERCENT dbo.bp.ftgnr AS Företagsnr, SUM(DATEDIFF(Day, cte_1.bestlevdat, dbo.bp.bestberlevdat)) AS Diff_Bekräftat_dat, SUM(DATEDIFF(Day, 
                        cte_1.bestlevdat, dbo.bp.bestbeglevdat)) AS Diff_Önskat_dat, COUNT(dbo.bp.bestradnr) AS AntalRader, SUM(CASE WHEN datediff(day, cte.bestlevdat, 
                        dbo.bp.bestberlevdat) < - 0 THEN 1 ELSE 0 END) AS Antal_avvikande_rader, ROUND((COUNT(dbo.bp.bestradnr) - SUM(CASE WHEN datediff(day, cte.bestlevdat, 
                        dbo.bp.bestberlevdat) < - 0 THEN 1.0 ELSE 0.0 END)) * 100 / COUNT(dbo.bp.bestradnr), 1) AS Levsäk
 FROM         dbo.bp INNER JOIN
                        cte AS cte_1 ON dbo.bp.bestnr = cte_1.bestnr AND dbo.bp.bestradnr = cte_1.bestradnr
 WHERE     (YEAR(dbo.bp.bestberlevdat) = '2015')
 GROUP BY dbo.bp.ftgnr
 ORDER BY AntalRader DESC

很抱歉,如果我错过了一些显而易见的事情,但我对查询很新,这是我第一次尝试&#34;查看&#34;。

1 个答案:

答案 0 :(得分:1)

听起来你想要做类似于你想要Table2中不同行的事情,并从Table1加入/聚合它们,所以使用CTE可能会有所帮助,所以像这样:

;WITH cte AS (
    SELECT DISTINCT
                Column2,
                Column5,
                Column6
    FROM        Table2
)
SELECT
            Table1.Column1 AS Company, 
            SUM(DATEDIFF(Day, cte.Column2, Table1.Column3)) AS DiffDate

FROM        Table1 

INNER JOIN  cte
    ON      Table1.Column6 = cte.Column6 
    AND     Table1.Column5 = cte.Column5

GROUP BY Company