SQL如何基于我的下面的情况做减法?

时间:2015-05-14 14:58:50

标签: sql sql-server

如何设置名为NEW_BAL的列,结果来自strResponseTextNull减去MIGS_STATUS_Approved

SELECT  
    Convert(char(8), WebPayH_dtmRequest, 112)as MIGSPaymentRequestDate,
    SUM(case when WebPayH_strResponseText IS NULL then 1 else 0 end) as strResponseTextNull,
    SUM(case when WebPayH_strApproved like 'Y' then 1 else 0 end) as MIGS_STATUS_Approved,
    SUM(case when WebPayH_strResponseText like 'Transaction+was+blocked+by+the+Payment+Server+because+it+did+not+pass+all+risk+checks.' then 1 else 0 end) as RiskCheckNotPass,
    SUM(case when WebPayH_strResponseText like 'The+card+holder+was+not+authorised.+This+is+used+in+3-D+Secure+Authentication.' then 1 else 0 end) as ThreeDNotEnabled,
    SUM(case when WebPayH_strResponseText like 'Expired+Card' then 1 else 0 end) as ExpiredCard,
    SUM(case when WebPayH_strResponseText like 'Declined' then 1 else 0 end) as DeclinedByBank,
    SUM(case when WebPayH_strResponseText like 'Insufficient+Funds' then 1 else 0 end) as InsufficientFunds,
    SUM(case when WebPayH_strResponseText like 'Timed+Out' then 1 else 0 end) as TimerOut,

**-- SELECT strResponseTextNull - SUM MIGS_STATUS_Approved AS NEW_BAL**

    count(WebPayH_strResponseCode) AS TotalMIGSPaymentRequest
FROM 
    [VISTAIT].[dbo].[tblWebPaymentHistory]
WHERE 
    WebPayH_dtmRequest >= '2015-05-07'
GROUP BY 
    Convert(char(8), WebPayH_dtmRequest, 112) 
ORDER BY 
    Convert(char(8), WebPayH_dtmRequest, 112) 

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT  
    Convert(char(8), WebPayH_dtmRequest, 112)as MIGSPaymentRequestDate,
    SUM(case when WebPayH_strResponseText IS NULL then 1 else 0 end) as strResponseTextNull,
    SUM(case when WebPayH_strApproved like 'Y' then 1 else 0 end) as MIGS_STATUS_Approved,
    SUM(case when WebPayH_strResponseText like 'Transaction+was+blocked+by+the+Payment+Server+because+it+did+not+pass+all+risk+checks.' then 1 else 0 end) as RiskCheckNotPass,
    SUM(case when WebPayH_strResponseText like 'The+card+holder+was+not+authorised.+This+is+used+in+3-D+Secure+Authentication.' then 1 else 0 end) as ThreeDNotEnabled,
    SUM(case when WebPayH_strResponseText like 'Expired+Card' then 1 else 0 end) as ExpiredCard,
    SUM(case when WebPayH_strResponseText like 'Declined' then 1 else 0 end) as DeclinedByBank,
    SUM(case when WebPayH_strResponseText like 'Insufficient+Funds' then 1 else 0 end) as InsufficientFunds,
    SUM(case when WebPayH_strResponseText like 'Timed+Out' then 1 else 0 end) as TimerOut,
    SUM(case when WebPayH_strResponseText IS NULL then 1 else 0 end)-SUM(case when WebPayH_strApproved like 'Y' then 1 else 0 end) as NEW_BAL,
    count(WebPayH_strResponseCode) AS TotalMIGSPaymentRequest
FROM 
    [VISTAIT].[dbo].[tblWebPaymentHistory]
WHERE 
    WebPayH_dtmRequest >= '2015-05-07'
GROUP BY 
    Convert(char(8), WebPayH_dtmRequest, 112) 
ORDER BY 
    Convert(char(8), WebPayH_dtmRequest, 112)