关于Sql server中的Group by子句的问题

时间:2010-08-19 05:51:54

标签: sql sql-server tsql

表:客户

Name    Type    Amount

James   P   125.00
James   P   125.00
James   P   125.00
James   R   225.00
James   R   225.00
Rajiv   R   155.00
Rajiv   R   155.00
Rajiv   R   155.00
Rajiv   P   150.00
Rajiv   P   150.00
Saran   R   175.00

在这个表结构中,我想要一个输出,它将给出每个人的P计数,R的计数,其中type = P的金额之和,type = R的金额之和

在这种情况下,任何与我相处的线索都没有帮助我。

3 个答案:

答案 0 :(得分:3)

如果您希望将结果作为单独的记录,只需将名称分组并输入:

select Name, Type, count(*) as Cnt, sum(Amount) as AmountSum
from Customer
Group by Name, Type
order by Name, Type

结果:

Name    Type  Cnt  AmountSum
James   P     3    375.00
James   R     2    450.00
Rajiv   P     2    300.00
Rajiv   R     3    465.00
Saran   R     1    175.00

如果你想要一个人在同一记录中的计数和总和,你必须做一些比较:

select
  Name,
  count(case Type when 'P' then 1 else null end) as CntP,
  sum(case Type when 'P' then Amount else 0 end) as AmountSumP,
  count(case Type when 'R' then 1 else null end) as CntR,
  sum(case Type when 'R' then Amount else 0 end) as AmountSumR,
from Customer
Group by Name
order by Name

结果:

Name    CntP  AmountSumP  CntR  AmountSumR
James   3     375.00      2     450.00
Rajiv   2     300.00      3     465.00
Saran   0     0.00        1     175.00

答案 1 :(得分:0)

一个查询,没有CTE,没有派生表:

SELECT
    Name,
    SUM(CASE WHEN Type = 'P' THEN 1 ELSE 0 END) AS PCount,
    SUM(CASE WHEN Type = 'R' THEN 1 ELSE 0 END) AS RCount,
    SUM(CASE WHEN Type = 'P' THEN Amount ELSE 0 END) AS PAmount,
    SUM(CASE WHEN Type = 'R' THEN Amount ELSE 0 END) AS RAmount
FROM yourTable 
GROUP BY Name

使用

CREATE TABLE customer (name varchar(50), type char(1), amount decimal(6,2));

INSERT INTO customer VALUES ('James', 'P', 125.00);
INSERT INTO customer VALUES ('James', 'P', 125.00);
INSERT INTO customer VALUES ('James', 'P', 125.00);
INSERT INTO customer VALUES ('James', 'R', 225.00);
INSERT INTO customer VALUES ('James', 'R', 225.00);
INSERT INTO customer VALUES ('Rajiv', 'R', 155.00);
INSERT INTO customer VALUES ('Rajiv', 'R', 155.00);
INSERT INTO customer VALUES ('Rajiv', 'R', 155.00);
INSERT INTO customer VALUES ('Rajiv', 'P', 150.00);
INSERT INTO customer VALUES ('Rajiv', 'P', 150.00);
INSERT INTO customer VALUES ('Saran', 'R', 175.00);
SELECT
    Name,
    SUM(CASE WHEN Type = 'P' THEN 1 ELSE 0 END) AS PCount,
    SUM(CASE WHEN Type = 'R' THEN 1 ELSE 0 END) AS RCount,
    SUM(CASE WHEN Type = 'P' THEN Amount ELSE 0 END) AS PAmount,
    SUM(CASE WHEN Type = 'R' THEN Amount ELSE 0 END) AS RAmount
FROM customer 
GROUP BY Name

James   3   2   375.00  450.00
Rajiv   2   3   300.00  465.00
Saran   0   1   0.00    175.00

答案 2 :(得分:0)

编辑答案:gbn在我的原始答案中指出错误后

SELECT  name,
        SUM( CASE WHEN [type] = 'P' THEN 1 ELSE 0 END) CountOfP , 
        SUM( CASE WHEN [type] = 'R' THEN 1 ELSE 0 END) CountOfR,
        SUM( CASE WHEN [type] = 'P' THEN Amount End) SumOfP , 
        SUM( CASE WHEN [type] = 'R' THEN Amount END) SumOfR

FROM customer 
GROUP BY name

原始答案

SELECT  name,
        COUNT( CASE WHEN [type] = 'P' THEN 1 ELSE 0 END) CountOfP , 
        COUNT( CASE WHEN [type] = 'R' THEN 1 ELSE 0 END) CountOfR,
        SUM( CASE WHEN [type] = 'P' THEN Amount End) SumOfP , 
        SUM( CASE WHEN [type] = 'R' THEN Amount END) SumOfR

FROM customer 
GROUP BY name