I am wanting the "May 1" of invoice amount column to be dynamic based on the bcd.processeddate '5/1/2020 between '5/1/2021' A year worth of data - sql

I want the "May 1" of invoice amount column to be dynamic based on the bcd.processeddate '5/1/2020 between '5/1/2021' A year worth of data so for example if I choose bcd.processeddate 5/15/2020 to 5/15/2021, the columns should say May 15 invoice amount, june 15 invoice amount and so on.
Can someone guide me in the right direction?
select
case when cagp.amb_memberid is not null then cagp.amb_memberid else case when cap.amb_memberid is not null then cap.amb_memberid else ca.amb_memberid end end as [ParentID],
case when cagp.amb_memberid is not null then cagp.name else case when cap.amb_memberid is not null then cap.name else ca.name end end as [ParentName],
bcd.MemberID, bcd.MemberStationCode, ca.name,
sum(case when year(processeddate)=2020 and month(processeddate)=5 then (invoiceAmount) else 0 end) as "May 1 invoiceAmount",
sum(case when year(processeddate)=2020 and month(processeddate)=6 then (invoiceAmount) else 0 end) as "Jun 1 invoiceAmount",
sum(case when year(processeddate)=2020 and month(processeddate)=7 then (invoiceAmount) else 0 end) as "Jul 1 invoiceAmount",
sum(case when year(processeddate)=2020 and month(processeddate)=8 then (invoiceAmount) else 0 end) as "Aug 1 invoiceAmount",
sum(case when year(processeddate)=2020 and month(processeddate)=9 then (invoiceAmount) else 0 end) as "Sep 1 invoiceAmount",
sum(case when year(processeddate)=2020 and month(processeddate)=10 then (invoiceAmount) else 0 end) as "Oct 1 invoiceAmount",
sum(case when year(processeddate)=2020 and month(processeddate)=11 then (invoiceAmount) else 0 end) as "Nov 1 invoiceAmount",
sum(case when year(processeddate)=2020 and month(processeddate)=12 then (invoiceAmount) else 0 end) as "Dec 1 invoiceAmount",
sum(case when year(processeddate)=2021 and month(processeddate)=1 then (invoiceAmount) else 0 end) as "Jan 1 invoiceAmount",
sum(case when year(processeddate)=2021 and month(processeddate)=2 then (invoiceAmount) else 0 end) as "Feb 1 invoiceAmount",
sum(case when year(processeddate)=2021 and month(processeddate)=3 then (invoiceAmount) else 0 end) as "Mar 1 invoiceAmount",
sum(case when year(processeddate)=2021 and month(processeddate)=4 then (invoiceAmount) else 0 end) as "Apr 1 invoiceAmount",
sum(case when year(processeddate)=2021 and month(processeddate)=5 then (invoiceAmount) else 0 end) as "May 1 invoiceAmount"
from serviceconnection_Hist bcd
left outer join CRM_Account ca
on bcd.MemberID=ca.amb_memberid and ca.statecodename='Active' and ca.statecode=0 and ca.statuscode=1
left outer join CRM_Account cap on ca.parentaccountid=cap.accountid and cap.statecode=0 and cap.statuscode=1
left outer join CRM_Account cagp on cap.parentaccountid=cagp.accountid and cagp.statecode=0 and cagp.statuscode=1
where bcd.processeddate >= '5/1/2020' and bcd.processeddate< '5/1/2021' and MemberID<>'' and ca.statecode=0 and ca.statuscode=1
group by
case when cagp.amb_memberid is not null then cagp.amb_memberid else case when cap.amb_memberid is not null then cap.amb_memberid else ca.amb_memberid end end,
case when cagp.amb_memberid is not null then cagp.name else case when cap.amb_memberid is not null then cap.name else ca.name end end,
bcd.MemberID, bcd.MemberStationCode, ca.name
order by 1, 2, bcd.MemberStationCode, bcd.MemberID

Related

how to make pivot more than one column value sql

I have this table
country weeek quantity 1 quantity 2 quantity 3
0 1 sa 3235 365 123
1 1 su 6698 32135 1234
2 1 mo 1565 5689 12345
Desired output:
country sa_q1 su_q1 mo_q1 sa_q2 su_q2 mo_q2 sa_q3 su_q3 mo_q3
0 1 3235 6698 1565 365 32135 5689 123 1234 12345
Just use conditional aggregation:
select country,
sum(case when week = 'sa' then quantity1 else 0 end) as sa1,
sum(case when week = 'su' then quantity1 else 0 end) as su1,
sum(case when week = 'mo' then quantity1 else 0 end) as mo1,
sum(case when week = 'sa' then quantity2 else 0 end) as sa2,
sum(case when week = 'su' then quantity2 else 0 end) as su2,
sum(case when week = 'mo' then quantity2 else 0 end) as mo2,
sum(case when week = 'sa' then quantity3 else 0 end) as sa3,
sum(case when week = 'su' then quantity3 else 0 end) as su3,
sum(case when week = 'mo' then quantity3 else 0 end) as mo3
from t
group by country;

SQL How to do substraction based on my below case?

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)

Calculation in CASE WHEN statement

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

SQL Grand Total last ROW

Is there a way I could get the grand total of each month. I've looked into Rollup but cant seem to figure it out.
Query I have is
SELECT t.city,f.fname,f.lname,
SUM(CASE datepart(month,ddate) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,ddate) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,ddate) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,ddate) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,ddate) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,ddate) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,ddate) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,ddate) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,ddate) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,ddate) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,ddate) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,ddate) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
SUM(CASE datepart(year,ddate) WHEN 2014 THEN 1 ELSE 0 END) AS 'TOTAL'
from world T
INNER JOIN sales F ON T.ID=F.ID
where t.city = ROME
group by t.city,f.fname,f.lname
Output example
t.city, f.fname, f.lname January total
ROME John Doe 5 5
Grand Total 5 5
Did you try this?
group by GROUPING SETS((t.city, f.fname, f.lname), ())
To get Grand Total you'll need to change the select as well.
And, as a note: use single quotes only for string and date constants. Using them for column identifiers can lead to confusing and problems. Either drop the quotes altogether or use square braces or double quotes.
EDIT:
Without group by extensions or CTEs this is a pain. There is a trick to doing it with minimal modifications:
SELECT (case when which = 'normal' then t.city else 'Grand Total' end),
(case when which = 'normal' then f.fname end),
(case when which = 'normal' then f.lname end),
SUM(CASE datepart(month,ddate) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,ddate) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,ddate) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,ddate) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,ddate) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,ddate) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,ddate) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,ddate) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,ddate) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,ddate) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,ddate) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,ddate) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
SUM(CASE datepart(year,ddate) WHEN 2014 THEN 1 ELSE 0 END) AS 'TOTAL'
from world T INNER JOIN
sales F
ON T.ID=F.ID cross join
(select 'normal' as which union all select 'total') as which
where t.city = ROME
group by (case when which = 'normal' then t.city else 'Grand Total' end),
(case when which = 'normal' then f.fname end),
(case when which = 'normal' then f.lname end);
(I'm not reformatting the rest of the query, but you should not use single quotes for column identifiers. Only use single quotes for string and date constants.)
Edit: Using GROUP BY ROLLUP(grouping columns) this will calculate the sum of the aggregated columns. By default it will return 'NULL' for all grouped columns but you can put an ISNULL wrapper around to get rid of it, or return a specific value.
SELECT ISNULL(t.city, 'Grand Total') AS [City],f.fname AS [Fname],f.lname AS [Lname],
SUM(CASE datepart(month,ddate) WHEN 1 THEN 1 ELSE 0 END) AS [January],
SUM(CASE datepart(month,ddate) WHEN 2 THEN 1 ELSE 0 END) AS [February],
SUM(CASE datepart(month,ddate) WHEN 3 THEN 1 ELSE 0 END) AS [March],
SUM(CASE datepart(month,ddate) WHEN 4 THEN 1 ELSE 0 END) AS [April],
SUM(CASE datepart(month,ddate) WHEN 5 THEN 1 ELSE 0 END) AS [May],
SUM(CASE datepart(month,ddate) WHEN 6 THEN 1 ELSE 0 END) AS [June],
SUM(CASE datepart(month,ddate) WHEN 7 THEN 1 ELSE 0 END) AS [July],
SUM(CASE datepart(month,ddate) WHEN 8 THEN 1 ELSE 0 END) AS [August],
SUM(CASE datepart(month,ddate) WHEN 9 THEN 1 ELSE 0 END) AS [September],
SUM(CASE datepart(month,ddate) WHEN 10 THEN 1 ELSE 0 END) AS [October],
SUM(CASE datepart(month,ddate) WHEN 11 THEN 1 ELSE 0 END) AS [November],
SUM(CASE datepart(month,ddate) WHEN 12 THEN 1 ELSE 0 END) AS [December],
SUM(CASE datepart(year,ddate) WHEN 2014 THEN 1 ELSE 0 END) AS [TOTAL]
FROM world T
INNER JOIN sales F ON T.ID=F.ID
WHERE t.city = ROME
GROUP BY ROLLUP(t.city,f.fname,f.lname)

Grouping by Month of TimeStamp

I am attempting to group by twice, once by an individual level field and then by the month of the timestamp field.
Little new to SQL but here's what I came up with after reading another SO post here: SQL query to group by month part of timestamp
SELECT
VwNIMUserDim.USER_EMAIL_ADDRESS,
VwNIMEventFct.NIM_USER_ID,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 880 THEN 1 ELSE 0 END) AS APP_OPEN,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 881 THEN 1 ELSE 0 END) AS AUTODL_SETTINGS_SAVE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 882 THEN 1 ELSE 0 END) AS AUTO_QUERY_CONFIRM,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 883 THEN 1 ELSE 0 END) AS ISSUE_CLOSE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 884 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 885 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD_COMPLETE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 886 THEN 1 ELSE 0 END) AS PICKER_SEND_PICKS
FROM RDMAVWSANDBOX.VwNIMEventFct
GROUP BY NIM_USER_ID, MONTH('EVENT_GMT_TIMESTAMP')
The error message returned by my SQL client, Teradata, says:
"SELECT FAILED Error 3706 Syntax error expected something in between ',' and the MONTH keyword.
This is my first time doing two things: Grouping By twice and using the Month function.
What am I doing wrong here? How do I group by email address users in each month?
There's no MONTH function in Teradata/Standard SQL, it's EXTRACT(YEAR/MONTH/DAY/HOUR/MINUTE/SECOND):
EXTRACT(MONTH FROM EVENT_GMT_TIMESTAMP)
Your query as written will result in a PRODUCT (Cartesian) JOIN between the VwNIMUserDIm and VwNIMEventFct tables (views). I have taken the liberty to modify the SQL based on your comments to the previous response:
SELECT
User_.USER_EMAIL_ADDRESS,
Event_.NIM_USER_ID,
Event_.EVENT_GMT_TIMESTAMP(FORMAT 'yyyy-mm')(char(7)) AS EVENT_MONTH,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 880 THEN 1 ELSE 0 END) AS APP_OPEN,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 881 THEN 1 ELSE 0 END) AS AUTODL_SETTINGS_SAVE,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 882 THEN 1 ELSE 0 END) AS AUTO_QUERY_CONFIRM,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 883 THEN 1 ELSE 0 END) AS ISSUE_CLOSE,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 884 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 885 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD_COMPLETE,
SUM(CASE WHEN Event_.NIM_EVENT_TYPE_ID = 886 THEN 1 ELSE 0 END) AS PICKER_SEND_PICKS
FROM RDMAVWSANDBOX.VwNIMEventFct Event_
JOIN RDMAVWSANDBOX.VwNIMUserDim User_
ON Event_.NIM_USER_ID = User_.NIM_USER_ID
GROUP BY 1,2;
Possibly depending on database origin (MSSQL, mySQL, Oracle, DB2, Etc)
SELECT
VwNIMUserDim.USER_EMAIL_ADDRESS,
VwNIMEventFct.NIM_USER_ID,
MONTH(EVENT_GMT_TIMESTAMP),
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 880 THEN 1 ELSE 0 END) AS APP_OPEN,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 881 THEN 1 ELSE 0 END) AS AUTODL_SETTINGS_SAVE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 882 THEN 1 ELSE 0 END) AS AUTO_QUERY_CONFIRM,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 883 THEN 1 ELSE 0 END) AS ISSUE_CLOSE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 884 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 885 THEN 1 ELSE 0 END) AS ISSUE_DOWNLOAD_COMPLETE,
SUM(CASE WHEN NIM_EVENT_TYPE_ID = 886 THEN 1 ELSE 0 END) AS PICKER_SEND_PICKS
FROM RDMAVWSANDBOX.VwNIMEventFct
GROUP BY NIM_USER_ID, MONTH(EVENT_GMT_TIMESTAMP)