2013-12-18 97 views
0

该商家有一个tendertype类型的信用卡或借记卡,计算在同一行,但我需要分割这两个金额,以提供信用和借记金额。 有一行表示名为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([email protected]) AS [[email protected]_Fee (Incl)], 
    sum([email protected]/1.14)AS [[email protected]_Fee (excl)], 
    sum(t.CashHandlingFee) AS [Cash_Fee], 
    sum(t.MerchantFee) AS [Merchant_Fee] 
FROM [email protected] t (nolock) 
    LEFT OUTER JOIN [email protected] c (nolock) ON t.RetailID = c.RecID 
    LEFT OUTER JOIN [email protected] 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 
+0

这是用于MySQL还是MS SQL Server? –

+0

对不起,我仍然使用该网站,它不断给我错误,当我把我的代码在评论部分 –

+0

我调整了代码,因为我有它在这一刻,结果是正确的我只需要拆分MerchantFee –

回答

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([email protected]) AS [[email protected]_Fee (Incl)], 
    sum([email protected]/1.14)AS [[email protected]_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 
    [email protected] t (nolock) 
    LEFT OUTER JOIN [email protected] c (nolock) ON t.RetailID = c.RecID 
    LEFT OUTER JOIN [email protected] 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 
+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([email protected]) AS [[email protected]_Fee (Incl)], 
    sum([email protected]/1.14)AS [[email protected]_Fee (excl)], 
    sum(t.CashHandlingFee) AS [Cash_Fee], 
    sum(t.MerchantFee) AS [Merchant_Fee] 
FROM [email protected] t (nolock) 
    LEFT OUTER JOIN [email protected] c (nolock) ON t.RetailID = c.RecID 
    LEFT OUTER JOIN [email protected] 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([email protected]) AS [[email protected]_Fee (Incl)], 
    sum([email protected]/1.14)AS [[email protected]_Fee (excl)], 
    sum(t.CashHandlingFee) AS [Cash_Fee], 
    sum(t.MerchantFee) AS [Merchant_Fee] 
FROM [email protected] t (nolock) 
    LEFT OUTER JOIN [email protected] c (nolock) ON t.RetailID = c.RecID 
    LEFT OUTER JOIN [email protected] h (nolock) ON t.ClientID = h.RecID 
WHERE TenderType='DebitCard' 
) A 
+0

我不断收到以下错误:“列'[email protected]'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。”我试着把它放在组中,但它不起作用。 –

+0

好吧,我明白了,Thanx –

+0

仍然没有工作,它给我空行 –