带分组的案例陈述

时间:2015-05-26 12:05:26

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

您能否帮我写一个SELECT语句,该语句返回一行,其中包含每个涉及事务编号55的TypeId的列?

CREATE TABLE Types
(
Id INT IDENTITY(1,1) NOT NULL,
Name VARCHAR(100) NOT NULL,
CONSTRAINT PK_Types PRIMARY KEY CLUSTERED(Id)
)
GO

INSERT INTO Types
VALUES
 ('Type1')
,('Type2')
,('Type3')
,('Type4')
,('Type5')
GO

CREATE TABLE Transactions
(
Id INT IDENTITY(1000,1) NOT NULL,
TypeId INT NULL,
TransactionNumber INT NOT NULL,
Amount MONEY NULL,
DateRecorded DATETIME2 NOT NULL,
CONSTRAINT PK_Transactions PRIMARY KEY CLUSTERED(Id),
CONSTRAINT FK_Types FOREIGN KEY(TypeId) REFERENCES Types(Id)
)
GO

INSERT INTO Transactions
VALUES
 (1,55,2555.50,SYSDATETIME())
,(3,55,3555.50,SYSDATETIME())
,(4,55,4555.50,SYSDATETIME())
,(5,55,5555.50,SYSDATETIME())
GO

我需要在Transactions表中为每个交易号返回一行。

我已经尝试过:

SELECT TransactionNumber
    ,(CASE WHEN TypeId = 1 THEN Amount ELSE 0 END) AS Type1
    ,(CASE WHEN TypeId = 2 THEN Amount ELSE 0 END) AS Type2
    ,(CASE WHEN TypeId = 3 THEN Amount ELSE 0 END) AS Type3
    ,(CASE WHEN TypeId = 4 THEN Amount ELSE 0 END) AS Type4
    ,(CASE WHEN TypeId = 5 THEN Amount ELSE 0 END) AS Type5
FROM Transactions

4 个答案:

答案 0 :(得分:1)

我认为您可以PIVOT来获取所需的输出:

SELECT *
FROM (
    SELECT ty.NAME, ISNULL(Amount,0)Amount
    FROM TRANSACTIONS t
    INNER JOIN Types ty ON ty.Id = t.TypeId
    WHERE TransactionNumber = 55
    ) sub
PIVOT(
      SUM(Amount) FOR [Name] IN ([Type1], [Type2], [Type3], [Type4], [Type5])
     ) AS pivottable

SQL Fiddle

答案 1 :(得分:1)

SELECT TransactionNumber
    ,sum(CASE WHEN TypeId = 1 THEN Amount ELSE 0 END) AS Type1
    ,sum(CASE WHEN TypeId = 2 THEN Amount ELSE 0 END) AS Type2
    ,sum(CASE WHEN TypeId = 3 THEN Amount ELSE 0 END) AS Type3
    ,sum(CASE WHEN TypeId = 4 THEN Amount ELSE 0 END) AS Type4
    ,sum(CASE WHEN TypeId = 5 THEN Amount ELSE 0 END) AS Type5
FROM Transactions group by TransactionNumber;

答案 2 :(得分:0)

我认为这是您正在寻找的查询:

SELECT 
    COUNT(CASE WHEN TypeID = 1 THEN 1 ELSE NULL END) '# Transactions Type 1'
    , COUNT(CASE WHEN TypeID = 2 THEN 1 ELSE NULL END) '# Transactions Type 2'
    , COUNT(CASE WHEN TypeID = 3 THEN 1 ELSE NULL END) '# Transactions Type 3'
    , COUNT(CASE WHEN TypeID = 4 THEN 1 ELSE NULL END) '# Transactions Type 4'
    , COUNT(CASE WHEN TypeID = 5 THEN 1 ELSE NULL END) '# Transactions Type 5'
FROM TRANSACTIONS
Where TransactionNumber = 55

您可以找到有效的 SQLFiddle here

修改

在您发表评论后,我认为您实际需要的是SUM()

SELECT TransactionNumber
    , SUM(CASE WHEN TypeId = 1 THEN Amount ELSE 0 END) AS Type1
    , SUM(CASE WHEN TypeId = 2 THEN Amount ELSE 0 END) AS Type2
    , SUM(CASE WHEN TypeId = 3 THEN Amount ELSE 0 END) AS Type3
    , SUM(CASE WHEN TypeId = 4 THEN Amount ELSE 0 END) AS Type4
    , SUM(CASE WHEN TypeId = 5 THEN Amount ELSE 0 END) AS Type5
FROM Transactions
GROUP BY TransactionNumber

SQLFiddle for 2nd query here

答案 3 :(得分:0)

使用条件聚合:

select Id,
       sum(case TypeId when 1 then Amount end) Type1,
       sum(case TypeId when 2 then Amount end) Type2,
       ...
from Transactions
group by Id