I have this extract form a query im writing.
What I want to do is sum only the successful payments
something along the lines of
, SUM(CASE WHEN (LOWER(result) = 'successful') THEN (SUM(amount) as amt_paid) ELSE 0 END) AS Successful_Payed
Here is the query extract.
Select
resultdate
, SUM(amount) as amt_paid
, SUM(CASE WHEN (LOWER(result) = 'successful') THEN 1 ELSE 0 END) AS Successful
, SUM(CASE WHEN (LOWER(result) = 'unsuccessful') THEN 1 ELSE 0 END) AS Unsuccessful
, SUM(CASE WHEN (LOWER(result) = 'tracking') THEN 1 ELSE 0 END) AS Tracking
, SUM(CASE WHEN (LOWER(result) = 'dispute') THEN 1 ELSE 0 END) AS Dispute
From paysoft_results
Group By resultdate
Order By resultdate
change like this
Select
resultdate
, SUM(CASE WHEN (LOWER(result) = 'successful') THEN amount ELSE 0 END) AS
Successful_Payed
, SUM(CASE WHEN (LOWER(result) = 'successful') THEN 1 ELSE 0 END) AS Successful
, SUM(CASE WHEN (LOWER(result) = 'unsuccessful') THEN 1 ELSE 0 END) AS Unsuccessful
, SUM(CASE WHEN (LOWER(result) = 'tracking') THEN 1 ELSE 0 END) AS Tracking
, SUM(CASE WHEN (LOWER(result) = 'dispute') THEN 1 ELSE 0 END) AS Dispute
From paysoft_results
Group By resultdate
Order By resultdate
Related
How to make persen_sukses or persen_gagal only get 2 decimal ?
I try use round but get some error.
May be anyone can help me.
Here is my sql
SELECT * ,(CASE WHEN all_trx.trx_gagal = 0 THEN 100 WHEN all_trx.trx_sukses = 0 THEN 0 ELSE (CAST(all_trx.trx_sukses AS float)/CAST(all_trx.trx_total AS float)*100) END) AS persen_sukses,
(CASE WHEN all_trx.trx_sukses = 0 THEN 100 WHEN all_trx.trx_gagal = 0 THEN 0 ELSE (CAST(all_trx.trx_gagal AS float)/CAST(all_trx.trx_total AS float)*100) END) AS persen_gagal
FROM
(SELECT kode_produk AS Produk,
COUNT(CASE WHEN status = '20' THEN 1 END) AS trx_sukses,
COUNT(CASE WHEN status > '20' THEN 1 END) AS trx_gagal,
COUNT(CASE WHEN status >= '20' THEN 1 END) AS trx_total
FROM transaksi WHERE CAST(tgl_entri AS DATE) = CAST(GETDATE() AS DATE) GROUP BY kode_produk)
AS all_trx
SELECT *
,(CASE
WHEN all_trx.trx_gagal = 0 THEN 100
WHEN all_trx.trx_sukses = 0 THEN 0
--ELSE (CAST(all_trx.trx_sukses AS float)/CAST(all_trx.trx_total AS float)*100)
ELSE CAST(all_trx.trx_sukses*100.0 / all_trx.trx_total as numeric(18,2) )
END) AS persen_sukses
,(CASE
WHEN all_trx.trx_sukses = 0 THEN 100
WHEN all_trx.trx_gagal = 0 THEN 0
--ELSE (CAST(all_trx.trx_gagal AS float)/CAST(all_trx.trx_total AS float)*100)
ELSE CAST(all_trx.trx_gagal*100.0 / all_trx.trx_total as numeric(18,2) )
END) AS persen_gagal
FROM
(
SELECT
kode_produk AS Produk,
COUNT(CASE WHEN status = '20' THEN 1 END) AS trx_sukses,
COUNT(CASE WHEN status > '20' THEN 1 END) AS trx_gagal,
COUNT(CASE WHEN status >= '20' THEN 1 END) AS trx_total
FROM transaksi
WHERE CAST(tgl_entri AS DATE) = CAST(GETDATE() AS DATE)
GROUP BY kode_produk
) AS all_trx
Dear i am able to fix the issue in your query, Pleae note the only change i did is ELSE part of Case Statement. I executed the query and now returning the value upto two decimal places only. If you have to increase or decrease the digits after the decimal sign update the value of numeric(18,2) here 2 is number of digit after decimal.
The below SQL is returning 'Cannot perform an aggregate function on an expression containing an aggregate or a subquery.', can anyone help?
SELECT
sum(case when Frequency = 'Monthly' then ISNULL(SUM(Amount),0.0) else 0 end) +
sum(case when Frequency = '4 Weekly' then ISNULL(SUM(Amount),0.0) / 2 else 0 end) +
sum(case when Frequency = 'Fortnightly' then ISNULL(SUM(Amount),0.0) / 3 else 0 end) +
sum(case when Frequency = 'Weekly' then ISNULL(SUM(Amount),0.0) / 5 else 0 end)
FROM TableWHERE Id = 1
If you want conditional aggregation, you only want one sum():
SELECT sum(case when Frequency = 'Monthly' then Amount else 0 end) +
sum(case when Frequency = '4 Weekly' then Amount / 2 else 0 end) +
sum(case when Frequency = 'Fortnightly' then Amount / 3 else 0 end) +
sum(case when Frequency = 'Weekly' then Amount,0.0) / 5 else 0 end)
FROM Table
WHERE Id = 1;
I think you want to do something like
sum(case when Frequency = 'Monthly' then ISNULL(Amount,0.0) else 0 end)
SELECT
sum(case when Frequency = 'Monthly' then ISNULL(Amount,0.0) else 0 end) +
sum(case when Frequency = '4 Weekly' then ISNULL(Amount,0.0) / 2 else 0 end) +
sum(case when Frequency = 'Fortnightly' then ISNULL(Amount,0.0) / 3 else 0 end) +
sum(case when Frequency = 'Weekly' then ISNULL(Amount,0.0) / 5 else 0 end)
FROM Table
WHERE Id = 1
How to I set a column called NEW_BAL, result are from strResponseTextNull minus of the 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)
Try this
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)
I have following query:
SELECT
datediff(d, 0, sauda_date),
Scrip_Code,
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty ELSE 0 END) AS BuyQty,
SUM(CASE WHEN Buy_sell = 1 THEN Market_Rate ELSE 0 END) AS BuyRate,
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty * Market_Rate ELSE 0 END) AS BuyAmount,
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty ELSE 0 END) AS SellQty,
SUM(CASE WHEN Buy_sell = 2 THEN Market_Rate ELSE 0 END) AS SellRate,
(CASE WHEN SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty ELSE 0 END) >
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty ELSE 0 END) THEN 'BF'
ELSE 'BT' END ) as TradeType,
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty * Market_Rate ELSE 0 END) AS SellAmount,
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty ELSE 0 END) -
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty ELSE 0 END) as NETQTY,
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty * Market_Rate ELSE 0 END) -
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty * Market_Rate ELSE 0 END)as NetAmt,
SUM(CASE WHEN Buy_sell = 2 THEN Trade_Qty * Market_Rate ELSE 0 END) -
SUM(CASE WHEN Buy_sell = 1 THEN Trade_Qty * Market_Rate ELSE 0 END) as PNLAmt
FROM tradeFile
where Inst_Type = 'FUTIDX'
OR Inst_Type='FUTSTK'
and Sauda_Date = convert(datetime,'1 Mar 2013')
group by Scrip_Code, ExpiryDate, datediff(d,0,sauda_date)
Result:
This query is for taking sum of Buy_Qty,Sell_Qty datewise.[With ref to this quest:not able to get individual date in query result
But as we can see datediff(d,0,sauda_date) column (i.e. first column) is giving me some result, but i wanted to add sauda_date also in this result as a date.
what changes i should make in this query.
I tried it by adding directly sauda_date in query but it gives me error.
Please guid me.
If you use a GROUP BY in your query then (in case of T-SQL) in the field's part of a query you can use only fields from GROUP BY or aggregate functions (SUM, COUNT,....). So you can't add just sauda_date. You should add it with aggregate function. For example MIN(sauda_date). OR you can add it to GROUP BY part.
Not sure with your result requirement..
simply adding sauda_date in select list should not give error if you includ it in group by list, i have tried belwo and its working.
Select datediff(d,0,f.Date),
sum(f.price),
f.OrderID,
f.Date
from test f
group by f.Date,OrderID,datediff(d,0,f.Date)
Else
you can use self join to get only sauda date joining main result on your key column
I have a table with total no of 1000 records in it.It has the following structure:
EMP_ID EMP_NAME PHONE_NO ARR_DATE
1 A 545454 2012/03/12
I want to calculate no of records for every month in year-2012
Is there any way that should solve my issue in a single shot?
I tried:
select count(*)
from table_emp
where year(ARR_DATE) = '2012' and month(ARR_DATE) = '01'
SELECT COUNT(*)
FROM table_emp
WHERE YEAR(ARR_DATE) = '2012'
GROUP BY MONTH(ARR_DATE)
This will give you the count per month for 2012;
SELECT MONTH(ARR_DATE) MONTH, COUNT(*) COUNT
FROM table_emp
WHERE YEAR(arr_date)=2012
GROUP BY MONTH(ARR_DATE);
Demo here.
Try This query:
SELECT
SUM(CASE datepart(month,ARR_DATE) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,ARR_DATE) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,ARR_DATE) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,ARR_DATE) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,ARR_DATE) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,ARR_DATE) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,ARR_DATE) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,ARR_DATE) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,ARR_DATE) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,ARR_DATE) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,ARR_DATE) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,ARR_DATE) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
SUM(CASE datepart(year,ARR_DATE) WHEN 2012 THEN 1 ELSE 0 END) AS 'TOTAL'
FROM
sometable
WHERE
ARR_DATE BETWEEN '2012/01/01' AND '2012/12/31'
select count(*)
from table_emp
where DATEPART(YEAR, ARR_DATE) = '2012' AND DATEPART(MONTH, ARR_DATE) = '01'