如何将一行分成两行?

时间:2013-12-18 16:28:57

标签: sql sql-server

MerchantFee有一个信用卡或债券的交易类型,它在同一行中计算,但是我需要将这两个金额分开来提供信用和借记金额。 有一行表示debitcard的Creditcard,它被称为TenderType,但我不能将tendertype指定给一个,否则它不起作用,如果我使用CASE,则总和在脚本中不起作用。

有人可以帮忙吗?

我的代码:

USE [PAYAT]
SELECT month(t.DateCreated) AS Month,
    c.name AS [Retailer],
    h.name AS [Bill Issuer],
    count(t.RecID) AS [Total_Transactions],
    sum(t.RetailFee) AS [RetailFee],
    sum(t.Pay@fee) AS [Pay@_Fee (Incl)],
    sum(t.Pay@Fee / 1.14)AS [Pay@_Fee (excl)],
    sum(t.CashHandlingFee) AS [Cash_Fee],
    sum(t.MerchantFee) AS [Merchant_Fee]
FROM dbo.tblPay@transactions t (nolock)
    LEFT OUTER JOIN dbo.tblPay@company c (nolock) ON t.RetailID = c.RecID
    LEFT OUTER JOIN dbo.tblPay@company h (nolock) ON t.ClientID = h.RecID
    WHERE t.DateCreated > '2013-10-01 00:10:00.000'
    AND t.DateCreated < '2013-10-01 23:59:59.000'
    Group BY c.name,h.name, month(t.DateCreated)
    ORDER BY c.name

2 个答案:

答案 0 :(得分:0)

获取两个不同的结果集并将它们联合起来。像这样:

USE [PAYAT]
SELECT * FROM 
(
SELECT month(t.DateCreated) AS Month,
    c.name AS [Retailer],
    h.name AS [Bill Issuer],
    count(t.RecID) AS [Total_Transactions],
    sum(t.RetailFee) AS [RetailFee],
    sum(t.Pay@fee) AS [Pay@_Fee (Incl)],
    sum(t.Pay@Fee / 1.14)AS [Pay@_Fee (excl)],
    sum(t.CashHandlingFee) AS [Cash_Fee],
    sum(t.MerchantFee) AS [Merchant_Fee]
FROM dbo.tblPay@transactions t (nolock)
    LEFT OUTER JOIN dbo.tblPay@company c (nolock) ON t.RetailID = c.RecID
    LEFT OUTER JOIN dbo.tblPay@company h (nolock) ON t.ClientID = h.RecID
WHERE TenderType='CreditCard'

UNION

SELECT month(t.DateCreated) AS Month,
    c.name AS [Retailer],
    h.name AS [Bill Issuer],
    count(t.RecID) AS [Total_Transactions],
    sum(t.RetailFee) AS [RetailFee],
    sum(t.Pay@fee) AS [Pay@_Fee (Incl)],
    sum(t.Pay@Fee / 1.14)AS [Pay@_Fee (excl)],
    sum(t.CashHandlingFee) AS [Cash_Fee],
    sum(t.MerchantFee) AS [Merchant_Fee]
FROM dbo.tblPay@transactions t (nolock)
    LEFT OUTER JOIN dbo.tblPay@company c (nolock) ON t.RetailID = c.RecID
    LEFT OUTER JOIN dbo.tblPay@company h (nolock) ON t.ClientID = h.RecID
WHERE TenderType='DebitCard'
) A

答案 1 :(得分:0)

你的问题是“将一行分成两行”,但根据描述,我假设你的意思是“将一列分成两列”...如果是这样,我相信这就是你想要的:

USE [PAYAT]

SELECT 
    month(t.DateCreated) AS Month,
    c.name AS [Retailer],
    h.name AS [Bill Issuer],
    count(t.RecID) AS [Total_Transactions],
    sum(t.RetailFee) AS [RetailFee],
    sum(t.Pay@fee) AS [Pay@_Fee (Incl)],
    sum(t.Pay@Fee / 1.14)AS [Pay@_Fee (excl)],
    sum(t.CashHandlingFee) AS [Cash_Fee],

    -- removed
    --sum(t.MerchantFee) AS [Merchant_Fee],

    -- added case statements to separate merchant fees into two categories
    sum(CASE WHEN TenderType='CreditCard' THEN t.MerchantFee ELSE 0 END) AS [Merchant_Fee_Credit_Card],
    sum(CASE WHEN TenderType='DebitCard' THEN t.MerchantFee ELSE 0 END) AS [Merchant_Fee_Debit_Card]

FROM
    dbo.tblPay@transactions t (nolock)
    LEFT OUTER JOIN dbo.tblPay@company c (nolock) ON t.RetailID = c.RecID
    LEFT OUTER JOIN dbo.tblPay@company h (nolock) ON t.ClientID = h.RecID
WHERE
    t.DateCreated > '2013-10-01 00:10:00.000'
    AND t.DateCreated < '2013-10-01 23:59:59.000'
GROUP BY
    c.name,h.name, month(t.DateCreated)
ORDER BY
    c.name
    ; --added