带有条件的SQL QUERY案例

时间:2015-09-14 14:47:18

标签: sql sql-server tsql

我有一个sql查询,我必须得到记录计数,我正在使用当前查询正确计数,但我只想获得“未检查计数”不为零的记录计数。

   SELECT        
    dbo.Customer.AccountNo AS Cust_Acc_No, 
    dbo.Customer.Name AS [Customer Name], 
    dbo.Customer.Adrs1 AS Cust_Address_1,    
    dbo.Customer.Adrs2 AS Cust_Address_2, 
    dbo.Customer.City AS Cust_City, 
    dbo.Customer.Province, 
    dbo.Customer.PostalCode AS Cust_Postal_Code, 
    dbo.Customer.Email1 AS Email, 
    CAST(dbo.Customer.AccStatus AS int) AS [Account Status], 
    dbo.Customer.ID AS CID, 
    case when  COUNT(dbo.Manifest.ID) <  
         sum( CASE WHEN (dbo.Manifest.CheckedBy IS NULL OR
            (LTRIM(RTRIM(dbo.Manifest.CheckedBy)) = '')) THEN 1 ELSE 0 END) 
    then  COUNT(dbo.Manifest.ID) else
         sum( CASE WHEN (dbo.Manifest.CheckedBy IS NULL OR
         (LTRIM(RTRIM(dbo.Manifest.CheckedBy)) = '')) THEN 1 ELSE 0 END) 
    end as [Unchecked Count],
    case when  COUNT(dbo.Manifest.ID) <  
         sum( CASE WHEN (dbo.Manifest.CheckedBy IS not NULL OR
            (LTRIM(RTRIM(dbo.Manifest.CheckedBy))! = '')) THEN 1 ELSE 0 END) 
    then  COUNT(dbo.Manifest.ID) else
         sum( CASE WHEN (dbo.Manifest.CheckedBy IS not NULL OR
            (LTRIM(RTRIM(dbo.Manifest.CheckedBy))! = '')) THEN 1 ELSE 0 END) 
    end as [Checked_Count],
    COUNT(dbo.Manifest.ID) as Total

   FROM            
    dbo.Customer inner JOIN dbo.Manifest ON dbo.Customer.AccountNo = dbo.Manifest.FKAccountNo

   GROUP BY 
    dbo.Customer.AccountNo, 
    dbo.Customer.Name, 
    dbo.Customer.Adrs1, 
    dbo.Customer.City, 
    dbo.Customer.Province, 
    dbo.Customer.Adrs2, 
    dbo.Customer.Email1, 
    dbo.Customer.PostalCode, 
    dbo.Customer.AccStatus, 
    dbo.Customer.ID,Manifest.FKAccountNo

3 个答案:

答案 0 :(得分:2)

Having子句

之后尝试group by子句
having [Unchecked Count] > 0

完整查询

SELECT        dbo.Customer.AccountNo AS Cust_Acc_No, dbo.Customer.Name AS [Customer Name], dbo.Customer.Adrs1 AS Cust_Address_1, dbo.Customer.Adrs2 AS Cust_Address_2, dbo.Customer.City AS Cust_City, 
                     dbo.Customer.Province, dbo.Customer.PostalCode AS Cust_Postal_Code, dbo.Customer.Email1 AS Email, CAST(dbo.Customer.AccStatus AS int) AS [Account Status], dbo.Customer.ID AS CID, 
                    case when  COUNT(dbo.Manifest.ID) <  sum( CASE WHEN (dbo.Manifest.CheckedBy IS NULL OR
                     (LTRIM(RTRIM(dbo.Manifest.CheckedBy)) = '')) THEN 1 ELSE 0 END) 
                     then  COUNT(dbo.Manifest.ID) else
                     sum( CASE WHEN (dbo.Manifest.CheckedBy IS NULL OR
                     (LTRIM(RTRIM(dbo.Manifest.CheckedBy)) = '')) THEN 1 ELSE 0 END) 
                     end
                     as [Unchecked Count],
                      case when  COUNT(dbo.Manifest.ID) <  sum( CASE WHEN (dbo.Manifest.CheckedBy IS not NULL OR
                     (LTRIM(RTRIM(dbo.Manifest.CheckedBy))! = '')) THEN 1 ELSE 0 END) 
                     then  COUNT(dbo.Manifest.ID) else
                     sum( CASE WHEN (dbo.Manifest.CheckedBy IS not NULL OR
                     (LTRIM(RTRIM(dbo.Manifest.CheckedBy))! = '')) THEN 1 ELSE 0 END) 
                     end
                     as [Checked_Count],
                     COUNT(dbo.Manifest.ID) as Total

                     FROM            dbo.Customer inner JOIN
                     dbo.Manifest ON dbo.Customer.AccountNo = dbo.Manifest.FKAccountNo
                     GROUP BY dbo.Customer.AccountNo, dbo.Customer.Name, dbo.Customer.Adrs1, dbo.Customer.City, dbo.Customer.Province, dbo.Customer.Adrs2, dbo.Customer.Email1, dbo.Customer.PostalCode, dbo.Customer.AccStatus, 
                     dbo.Customer.ID,Manifest.FKAccountNo
    having [Unchecked Count] > 0

答案 1 :(得分:1)

您可以查询其他查询的结果。 SELECT * FROM ( <your query> ) AS T WHERE [Unchecked Count] > 0

答案 2 :(得分:0)

Thank you AlexanderW and Shakti for your answers.I tried both of your suggestions but 'Unchecked Count' is a alias, that is why I was getting 'invalid Column error'. But your answers helped me to solve the problem. Here is how it is working now, please let me know if you have better suggestion/s..

WITH OnlyUnchecked AS(<MyQuery>)
SELECT * 
FROM OnlyUnchecked 
WHERE [Unchecked Count]>0