SQL查询中的计数器列

时间:2016-05-18 09:26:02

标签: sql-server count inner-join

我有一个与INNER JOIN链接的两个表。这些是Invoice Header表和Invoice Lines表。

我想添加的是某种计数器列,它会为每个唯一的发票号码提供1,而对于任何重复的发票号码,如果发票包含多个订单项,则为0。然后,当我稍后分析我的数据时,它将允许我对Excel中的各个列进行计数。基本上它是一个计数,但只在左表的记录上,忽略了正确的表。

我无法弄清楚如何使用COUNT()或CASE来实现这一点。它有可能吗?

这是我的疑问:

SELECT P.DocNum
     , L.LineNum
     , P.DocCur
     , P.DocTotalFC
     , P.DocTotal
     , CASE 
         WHEN LEFT(L.AcctCode, 1) = '7'  THEN 'Admin'
         WHEN LEFT(L.AcctCode, 2) = '15' THEN 'Admin'
         WHEN L.AcctCode = '11501'       THEN 'MX'
         WHEN LEFT(L.Project, 1) = 'W'   THEN 'MX'
         WHEN L.OcrCode2 = '570'         THEN 'MEC'
         WHEN L.OcrCode2 = '590'         THEN 'ZHU'
         ELSE 'Flight Ops'
       END AS 'Type'
     , L.AcctCode AS 'Account'
     , L.OcrCode  AS 'Aircraft'
     , L.OcrCode2 AS 'Department'
     , L.OcrCode3 AS 'Business Unit'
     , L.Project  AS 'Project'               
     , L.LineTotal
FROM OPCH P
   INNER JOIN PCH1 L ON P.DocEntry = L.DocEntry
WHERE P.DocDate BETWEEN '2016-01-01' AND '2016-03-31'  
ORDER BY P.DocNum, L.LineNum

任何提示都受到高度赞赏。

提前致谢。

3 个答案:

答案 0 :(得分:2)

我认为您需要的是ROW_NUMBER()而不是COUNT()

SELECT P.DocNum
     , L.LineNum
     , P.DocCur
     , P.DocTotalFC
     , P.DocTotal
     , CASE 
         WHEN LEFT(L.AcctCode, 1) = '7'  THEN 'Admin'
         WHEN LEFT(L.AcctCode, 2) = '15' THEN 'Admin'
         WHEN L.AcctCode = '11501'       THEN 'MX'
         WHEN LEFT(L.Project, 1) = 'W'   THEN 'MX'
         WHEN L.OcrCode2 = '570'         THEN 'MEC'
         WHEN L.OcrCode2 = '590'         THEN 'ZHU'
         ELSE 'Flight Ops'
       END AS 'Type'
     , L.AcctCode AS 'Account'
     , L.OcrCode  AS 'Aircraft'
     , L.OcrCode2 AS 'Department'
     , L.OcrCode3 AS 'Business Unit'
     , L.Project  AS 'Project'               
     , L.LineTotal
     , CASE WHEN ROW_NUMBER() OVER(PARTITION BY p.docnum ORDER BY p.docDate) > 1
            then 0
            else 1
       end as rnk
FROM OPCH P
   INNER JOIN PCH1 L ON P.DocEntry = L.DocEntry
WHERE P.DocDate BETWEEN '2016-01-01' AND '2016-03-31'  
ORDER BY P.DocNum, L.LineNum

您没有提供足够的详细信息来了解哪一列是唯一的发票编号,以及哪一列是第一个(获得值1)和重复(获取值0)的dicide。我使用docnum作为发票编号,使用docdate作为列来决定哪些是重复项,然后根据需要进行调整。

如果订单无关紧要 - 它是完全重复的,无论哪个记录是重复的,那么按部分的顺序无关紧要。

答案 1 :(得分:1)

由于我还没有根据我使用过Row_number和Case条件的假设知道确切的数据,我们可以得到1表示正常值,0表示重复值

SELECT P.DocNum
     , L.LineNum
     , P.DocCur
     , P.DocTotalFC
     , P.DocTotal
     , CASE 
         WHEN LEFT(L.AcctCode, 1) = '7'  THEN 'Admin'
         WHEN LEFT(L.AcctCode, 2) = '15' THEN 'Admin'
         WHEN L.AcctCode = '11501'       THEN 'MX'
         WHEN LEFT(L.Project, 1) = 'W'   THEN 'MX'
         WHEN L.OcrCode2 = '570'         THEN 'MEC'
         WHEN L.OcrCode2 = '590'         THEN 'ZHU'
         ELSE 'Flight Ops'
       END AS 'Type'
     , L.AcctCode AS 'Account'
     , L.OcrCode  AS 'Aircraft'
     , L.OcrCode2 AS 'Department'
     , L.OcrCode3 AS 'Business Unit'
     , L.Project  AS 'Project'               
     , L.LineTotal
     , CASE WHEN ROW_NUMBER()OVER(PARTITION BY  P.DocNum,L.LineNum ORDER BY (SELECT NULL)) = 1 THEN 1 ELSE 0 END SummaryValues
FROM OPCH P
   INNER JOIN PCH1 L ON P.DocEntry = L.DocEntry
WHERE P.DocDate BETWEEN '2016-01-01' AND '2016-03-31'  
ORDER BY P.DocNum, L.LineNum

答案 2 :(得分:0)

;with cte as (
    SELECT
        a.DocEntry, joincounter = count(b.DocEntry)
    FROM
        OPCH P AS a 
        INNER JOIN PCH1 AS b ON a.DocEntry = b.DocEntry
    group by a.DocEntry
)
SELECT 
    case 
        when cte.joincounter > 1 then 0
        else cte.joincounter 
    end as joincounter

    ,P.DocNum
    , L.LineNum
    , P.DocCur
    , P.DocTotalFC
    , P.DocTotal
    , CASE 
        WHEN LEFT(L.AcctCode, 1) = '7'  THEN 'Admin'
        WHEN LEFT(L.AcctCode, 2) = '15' THEN 'Admin'
        WHEN L.AcctCode = '11501'       THEN 'MX'
        WHEN LEFT(L.Project, 1) = 'W'   THEN 'MX'
        WHEN L.OcrCode2 = '570'         THEN 'MEC'
        WHEN L.OcrCode2 = '590'         THEN 'ZHU'
        ELSE 'Flight Ops'
    END AS 'Type'
    , L.AcctCode AS 'Account'
    , L.OcrCode  AS 'Aircraft'
    , L.OcrCode2 AS 'Department'
    , L.OcrCode3 AS 'Business Unit'
    , L.Project  AS 'Project'               
    , L.LineTotal
FROM 
    OPCH P
    INNER JOIN PCH1 L ON P.DocEntry = L.DocEntry
    inner join cte on cte.DocEntry = P.DocEntry
WHERE
    P.DocDate >= '2016-01-01' 
    AND P.DocDate < '2016-03-31'  
ORDER BY
    P.DocNum, L.LineNum