Sql server查询结果

时间:2018-01-03 12:13:40

标签: sql sql-server

任何人都可以帮我解决我在Sql Server 2014中遇到的问题吗?

我将这些数据放在一个表中:

ID    Service   Price    Paid
1      Test1     100      
1      Test1     100      N
2      Test2     200      N
2      Test2     200       
2      Test2     200       

我尝试的是提供这样的数据:

ID     Service    CountOfPaid  CountOfUnPaid Difference
 1      Test1          1             1            0
 2      Test2          2             1            1

我可以通过分组找到支付计数或未支付计数,但我无法找到如何呈现我想要的数据。我也试过了Union和sum来计算(*),但这也没有帮助。

提前感谢您的帮助

以下是我的尝试:

SELECT  ID ,
        Service ,
        SUM(t.CountOfPaid)
FROM    ( 

(SELECT   ID ,
                    Service ,
                    COUNT(*) AS CountOfPaid
           FROM     TEST
           WHERE    Paid IS NULL
                    OR Paid <> N'N'
           GROUP BY id ,
                    Service)

          UNION

          ( SELECT  id ,
                    Service ,
                    COUNT(*) AS CountOfUnPaid
            FROM    TEST
            WHERE   Paid IS NOT NULL
                    OR Paid = N'N'
            GROUP BY id ,
                    Service
          )
        ) t

GROUP BY t.ID ,
        t.Service
ORDER BY t.ID ASC;

5 个答案:

答案 0 :(得分:2)

这是工作代码

SELECT
    *
   ,ABS(Z.CountOfPaid - Z.CountOfUnPaid)
FROM (SELECT
        ID
       ,Z.SERVICE
       ,SUM(CASE
            WHEN paid = '' THEN 1
            ELSE 0
        END) AS CountOfPaid
       ,SUM(CASE
            WHEN paid = 'N' THEN 1
            ELSE 0
        END) AS CountOfUnPaid
    FROM #table Z
    GROUP BY id
            ,service) Z

答案 1 :(得分:1)

Try This it will help you

DECLARE @table AS TABLE ( id INT, service VARCHAR(50), price DECIMAL(18,2), paid varchar(1) ) INSERT @table SELECT '1' ,'Test1', 100, null UNION ALL SELECT 1,'Test1', 100 ,'N' UNION ALL SELECT 2,'Test2', 200 ,'N' UNION ALL SELECT 2,'Test2', 200 ,null UNION ALL SELECT 2,'Test2', 200 ,null

; WITH temp AS ( SELECT id , service , price , ISNULL(paid, 'Y') paid FROM @table ), temp2 AS ( SELECT id , temp.service , COUNT(*) Total, 'PaidTotal' AS recordtype FROM temp WHERE temp.paid = 'Y' GROUP BY temp.id , temp.service UNION ALL SELECT id , temp.service , COUNT(*) Total, 'UnpaidTotal' AS recordtype FROM temp WHERE temp.paid = 'N' GROUP BY temp.id , temp.service )
SELECT Id, pvt.service, ISNULL(PaidTotal,0) PaidTotal,ISNULL(UnpaidTotal,0) UnpaidTotal, (ISNULL(PaidTotal,0) - ISNULL(UnpaidTotal,0)) AS Difference FROM temp2 PIVOT (SUM(temp2.Total) FOR temp2.recordtype IN ([PaidTotal],[UnpaidTotal] )) AS pvt

答案 2 :(得分:0)

进行条件聚合

select id, service,
       sum(case when paid is null then 1 else 0 end) [CountOfPaid],
       sum(case when paid = 'N' then 1 else 0 end) [CountOfUnPaid],
       (sum(case when paid is null then 1 else 0 end) - sum(case when paid = 'N' then 1 else 0 end)) [Difference] 
from table
group by id, service 

答案 3 :(得分:0)

试试这个:

Select ID, Service ,
,Sum(Case When Paid='N' Then 0 Else 1 End) CountOfPaid
,Sum(Case When Paid='N' Then 1 Else 0 End) CountOfUnPaid
,Sum(Case When Paid='N' Then 0 Else 1 End) -
 Sum(Case When Paid='N' Then 1 Else 0 End) Difference
From Table 
Group by ID, Service 
Order by ID, Service 

答案 4 :(得分:0)

如果付费为“N”或为空: count(*)计算所有行,count(column)计算列不为空的行:

select id, service,
       [CountAll] - [CountOfUnPaid] [CountOfPaid],
       [CountOfUnPaid],
       [CountAll] - 2 * [CountOfUnPaid] [Difference] 
from
(
    select id, service, count(*) [CountAll], count(paid) [CountOfUnPaid]
    from tablename
    group by id, service
) dt