SQL Grand Total last ROW - sql

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)

Related

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

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

How pivot month name in SQL Server after a date

I have a table in SQL Server with these columns:
id int pk
date datetime
value numeric
This is my select query
SELECT
O.Date AS DATE,
O.Value AS VALUE
FROM
Orders O
WHERE
YEAR(Date) = #Year
and this is my data
I want this output:
JAN FEB MAR APR MAY JUNE JULY AUG SEPT OCT NOV DEC
-----------------------------------------------------------------------
600 1200 600 600 0 0 0 0 0 0 0 0
Doesn't need a subquery. Something like this should work
select sum(case when dt.mo=1 then o.[Value] else 0 end) 'Jan',
sum(case when dt.mo=2 then o.[Value] else 0 end) 'Feb',
sum(case when dt.mo=3 then o.[Value] else 0 end) 'Mar',
sum(case when dt.mo=4 then o.[Value] else 0 end) 'Apr',
sum(case when dt.mo=5 then o.[Value] else 0 end) 'May',
sum(case when dt.mo=6 then o.[Value] else 0 end) 'Jun',
sum(case when dt.mo=7 then o.[Value] else 0 end) 'Jul',
sum(case when dt.mo=8 then o.[Value] else 0 end) 'Aug',
sum(case when dt.mo=9 then o.[Value] else 0 end) 'Sep',
sum(case when dt.mo=10 then o.[Value] else 0 end) 'Oct',
sum(case when dt.mo=11 then o.[Value] else 0 end) 'Nov',
sum(case when dt.mo=12 then o.[Value] else 0 end) 'Dec'
from Orders o
cross apply (select month(o.[Date]) mo) dt
where year(o.[DAte])=#Year;
You may try this-
Select
max(case when [Month] = 'Jan' then Value end) As "JAN"
,max(case when [Month] = 'Feb' then Value end) As "FEB"
,.....
,max(case when [Month] = 'Dec' then Value end) As "DEC"
from
(SELECT
Format(O.Date, 'MMM') as [Month],
Max(O.Value) AS VALUE
FROM Orders O
WHERE YEAR(Date) = #Year
Group by
Format(O.Date, 'MMM'))T;

SQL count and group by date and type

I have a quality system table. There are 2 types of quality points NCR and RMA.
RMA is external and NCR is internal. We log transactions to the table either as a NCR or a RMA. I would like to have a query that will count all of the RMA's and all of the NCR's then group them by monthly count. For example:
MONTH RMA NCR
JANUARY 10 54
FEBRUARY 48 22
MARCH 25 55
If the value is zero for the month or the month has not yet come up, I don't want to see it on the report.
the table. this is what I have.
SELECT MONTH(QualityControl.CreateDate) MONTH, COUNT(*) AS 'NCR'
FROM QualityControl
WHERE YEAR(QualityControl.CreateDate)=2015
and
QualityControl.NCR is not null
GROUP BY MONTH(QualityControl.CreateDate)
This only gives me the month number (1=January) and the NCR count for that month.
Then I tried the following:
SELECT
SUM(CASE datepart(month,CreateDate) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,CreateDate) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,CreateDate) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,CreateDate) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,CreateDate) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,CreateDate) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,CreateDate) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,CreateDate) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,CreateDate) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,CreateDate) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,CreateDate) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,CreateDate) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
SUM(CASE datepart(year,CreateDate) WHEN 2015 THEN 1 ELSE 0 END) AS 'TOTAL'
FROM
QualityControl
WHERE
CreateDate BETWEEN '2015/01/01' AND '2015/12/30'
And it gave me the count for NCR and RMA
Let's go with your first attempt, which is one row per value. You want to count the valid "NCR" and "RMA" values. You can use a COUNT() like this:
SELECT MONTH(qc.CreateDate) as MON, COUNT(NCR) as NCR, COUNT(RMA) as RMA
FROM QualityControl qc
WHERE CreateDate >= '2015-01-01' AND CreateDate < '2016-01-01'
GROUP BY MONTH(qc.CreateDate)
ORDER BY MON;
With a column (or other expression), COUNT() counts the number of non-NULL values. That seems to be exactly what you want.
If you wanted the SUM() of the values, then you would use SUM() instead of COUNT().
Note about the dates:
The version using comparisons is better than the one using YEAR(), because it can make use of indexes.
I changed the BETWEEN to >= and <. This version works correctly, if your date/time column has a time component. It also works for dates.
December has 31 days.
in sql server you can do all of these :
SELECT
CASE datepart(month,getdate()) WHEN 1 THEN 1 ELSE 0 END AS January,
CASE datepart(month,getdate()) WHEN 2 THEN 1 ELSE 0 END AS "February",
CASE datepart(month,getdate()) WHEN 3 THEN 1 ELSE 0 END AS [March]
the best method would be
SELECT datename(month, getdate())

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)

Count records for every month in a year

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'