SQL将count()特定记录放在另一个* AND *相同的表中

时间:2012-09-15 16:15:35

标签: sql sql-server sql-server-2008

这与我最近提出的另一个问题类似,但更进一步。

我之前的问题询问了如何使用tblCurrent构建视图,同时从tblHistorical检索特定相关记录的计数。

现在想要知道如何做同样的事情,但要添加两个额外的列,这些列根据同一tblCurrent表中的特定条件计算记录。

我有两张桌子:

tblCurrent 
 Ref  | CustomerID | Category | Subcategory | Resolved | ... .. .. .  . |
X001  | 001        | Sales  |  Major      |  TRUE  
X002  | 002        | Sales  | Minor      |  FALSE
X003  | 001        | Marketing | Corp      |  TRUE  


tblHistorical 
 Ref   | ... .. .. .  . |  Missing | Matched
X001   | ... .. .. .  . |  TRUE    | FALSE
X001   | ... .. .. .  . |  FALSE   | FALSE
X002   | ... .. .. .  . |  TRUE    | TRUE
X002   | ... .. .. .  . |  TRUE    | FALSE
X003   | ... .. .. .  . |  FALSE   | FALSE
X003   | ... .. .. .  . |  TRUE    | TRUE

Ref在tblCurrent中是唯一的,但在历史中不是。

我被告知并成功完成了上一个问题的以下工作:

SELECT  a.ref,
    SUM(CASE WHEN b.Missing = 'True' THEN 1 ELSE 0 END) missingTrue,
    SUM(CASE WHEN b.Missing = 'False' THEN 1 ELSE 0 END) missingFalse,
    SUM(CASE WHEN b.Matched = 'True' THEN 1 ELSE 0 END) matchTrue
FROM    tblCurrent a
    LEFT JOIN tblHistorical b
        on a.ref = b.ref
GROUP BY a.ref

问:如何添加两个额外的列(对于上面的解决方案):

  • tblCurrent中匹配相同CustomerID + Category(但不是当前记录)且已解决= False的记录数。
  • tblCurrent中匹配相同CustomerID + Category + SubCategory(但不是当前记录)且已解决= False的记录数。

4 个答案:

答案 0 :(得分:1)

您可以使用CTE解决此问题:

WITH tmp AS (
   SELECT CustomerId, COUNT(*) AS cnt
   FROM tblCurrent)
SELECT a.ref, tmp.cnt, ...
FROM tblCurrent a
LEFT JOIN tblHistorical b
   ON a.ref = b.ref
LEFT JOIN tmp
   ON tmp.CustomerId = a.CustomerId
...

答案 1 :(得分:1)

试试这个:

<击>

<击>

选择a.ref,     SUM(例如b.Missing ='True'THEN 1 ELSE 0 END)missingTrue,     SUM(例如b.Missing ='False'那么1 ELSE 0 END)missFalse,     SUM(例如b.Matched ='True'THEN 1 ELSE 0 END)matchTrue,     COUNT(DISTINCT a.CustomerID)as CustomerID_count - &lt;添加此行 来自tblCurrent a     LEFT JOIN tblHistorical b         在a.ref = b.ref上 GROUP BY a.ref

<击>

修改

根据您更新的问题,如果我理解您的问题,

;WITH 
CATEGORY   AS 
        (SELECT CustomerID ,Category,COUNT(*) AS CNT
        FROM tblCurrent
        GROUP BY CustomerID ,Category),

SUBCATEGORY AS 
        (SELECT CustomerID ,Category,SubCategory,COUNT(*) AS CNT
        FROM tblCurrent
        GROUP BY CustomerID ,Category,SubCategory),

MAIN        AS (
SELECT  a.ref,
        SUM(CASE WHEN b.Missing = 'True' THEN 1 ELSE 0 END) missingTrue,
        SUM(CASE WHEN b.Missing = 'False' THEN 1 ELSE 0 END) missingFalse,
        SUM(CASE WHEN b.Matched = 'True' THEN 1 ELSE 0 END) matchTrue
        FROM    tblCurrent a
        LEFT JOIN tblHistorical b
            on a.ref = b.ref
        GROUP BY a.ref
)
SELECT M.* ,C.CNT AS CATEGORY_COUNT,
       S.CNT AS SUBCATEGORY_COUNT
FROM    MAIN M
JOIN    tblCurrent T
ON      M.ref=T.ref
JOIN    CATEGORY C
ON      C.CustomerID=T.CustomerID
AND     C.Category=T.Category
JOIN    SUBCATEGORY S
ON      S.CustomerID=T.CustomerID
AND     C.Category=T.Category
AND     S.SubCategory=T.SubCategory

答案 2 :(得分:0)

COUNT(DISTINCT )添加到tblCurrent的唯一ID - 即:

SELECT  a.ref, 
    COUNT(DISTINCT tblCurrent.Ref),
    SUM(CASE WHEN b.Missing = 'True' THEN 1 ELSE 0 END) missingTrue, 
    SUM(CASE WHEN b.Missing = 'False' THEN 1 ELSE 0 END) missingFalse, 
    SUM(CASE WHEN b.Matched = 'True' THEN 1 ELSE 0 END) matchTrue 
FROM    tblCurrent a 
    LEFT JOIN tblHistorical b 
        on a.ref = b.ref 
GROUP BY a.ref 

答案 3 :(得分:0)

您可以在CASE语句中使用多个条件。所以你可以做“当a = b和c = d然后1个0结束时的情况”。这有帮助吗?