Monthly and Yearly Report in one row - sql-server

I have a table whose output is this
i have written my query like this
SELECT
c.DivisionNameEn,
a.LF_CompanyDivisionID,
0 as code,
SUM(a.totInvoiceAmnt) AS Sales,
'Sales' Salesperson,
sum(a.totInvoiceAmnt) as Sum ,
sum(a.totInvoiceAmnt) /sum(a.totInvoiceAmnt) *100 as MonthPercent ,
YEAR(a.TransactionDate) [Year],
MONTH(a.TransactionDate) [Month],
CASE
WHEN datepart(month,a.TransactionDate) = 1 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 1 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 2 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 2 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 3 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 4 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 5 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 6 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 7 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 8 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 9 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 10 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 10 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 11 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 12 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
ELSE 0
END as MonthTotal
from POS_salesHeader a
INNER JOIN dbo.STR_CompanyDivision AS c ON c.id = a.LF_CompanyDivisionID
--WHERE YEAR(a.TransactionDate) = YEAR(GETDATE()) AND MONTH(a.TransactionDate) = MONTH(GETDATE()) /*To filter by month*/
GROUP BY YEAR(a.TransactionDate), MONTH(a.TransactionDate) , c.DivisionNameEn ,LF_CompanyDivisionID
it is giving me out put like this
I want a row which give me month Total totInvoiceAmnt and till now Year Total totInvoiceAmnt for example if today its 15th April it gives me total totInvoiceAmnt of only April with the column name MonthsTotal and in another column it give me till now yearly totInvoiceAmnt total from month of jan + Feb + March +April

Related

How can separate morning and evening shifts patients with gender wise using SQL stored procedure

I want to separate the morning and evening patient checked-in count gender-wise and also if gender age less then show the Female child and M child on the basis of gender.
SELECT
COUNT(ch.EnteredOn) AS counter,
CONVERT(date, ch.EnteredOn) AS sessionDay,
SUM(CASE WHEN CAST(CONVERT(CHAR(2), ch.EnteredOn, 108) AS INT) < 12 THEN 1 ELSE 0 END) AS 'Morning',
SUM(CASE WHEN CAST(CONVERT(CHAR(2), ch.EnteredOn, 108) AS INT) >= 12 THEN 1 ELSE 0 END) AS 'Evening',
SUM(CASE WHEN p.Gender = 1 THEN 1 ELSE 0 END) AS Male,
SUM(CASE WHEN p.Gender = 2 THEN 1 ELSE 0 END) AS Fmale,
SUM(CASE WHEN DATEDIFF(hour, P.DOB, GETDATE()) / 8766 <= 18
AND P.Gender = 1 THEN 1 ELSE 0 END) AS MChild,
SUM(CASE WHEN DATEDIFF(hour, P.DOB, GETDATE()) / 8766 <= 18
AND P.Gender = 2 THEN 1 ELSE 0 END) AS FChild
FROM
Patient.CheckIn ch
INNER JOIN
Patient.Patients P ON ch.PatientId = p.PatientId
GROUP BY
Ch.EnteredOn
I have only three columns like Gender, Time and DOB. Gender id 1 shows the male and Gender id 2 is using for females.
enter image description here
SELECT convert(date, ch.EnteredOn) AS sessionDay, CAST( CONVERT(CHAR(2), ch.EnteredOn, 108) AS INT) <12 as morning, count(ch.EnteredOn) AS counter
,sum(case when p.Gender=1 Then 1 ELSE 0 end) as Male
,sum(case when p.Gender=2 Then 1 ELSE 0 end) as Fmale
,Sum( case when DATEDIFF(hour,P.DOB,GETDATE())/8766<=18 AND P.Gender=1 Then 1 ELSE 0 end ) as MChiled
,Sum( case when DATEDIFF(hour,P.DOB,GETDATE())/8766<=18 AND P.Gender=2 Then 1 ELSE 0 end ) as FChiled
FROM Patient.CheckIn ch
inner join Patient.Patients P on ch.PatientId=p.PatientId
Group by Ch.EnteredOn
Group BY can be
Group by convert(date, ch.EnteredOn) AS sessionDay, CAST( CONVERT(CHAR(2), ch.EnteredOn, 108) AS INT) <12

Sql sum of new columns

I have a part of a query below where I have created new columns based on conditions:
select
sum(case when Overall_Time_Spent < 0 then 1 else 0 end) as Errors,
sum(case when Overall_Time_Spent between 0 and 3 then 1 else 0 end) as _0_3_days,
sum(case when Overall_Time_Spent = 4 then 1 else 0 end) as _4_days,
sum(case when Overall_Time_Spent = 5 then 1 else 0 end) as _5_days,
sum(case when Overall_Time_Spent between 6 and 8 then 1 else 0 end) as _6_8_days,
sum(case when Overall_Time_Spent >= 9 then 1 else 0 end) as more_than_9_days,
avg(case when Overall_Time_Spent < 0 then 100.0 else 0 end) as Errors_percent,
avg(case when Overall_Time_Spent between 0 and 3 then 100.0 else 0 end) as _0_3_percent,
avg(case when Overall_Time_Spent = 4 then 100.0 else 0 end) as _4_percent,
avg(case when Overall_Time_Spent = 5 then 100.0 else 0 end) as _5_percent,
avg(case when Overall_Time_Spent between 6 and 8 then 100.0 else 0 end) as _6_8_percent,
avg(case when Overall_Time_Spent >= 9 then 100.0 else 0 end) as more_than_9_days_percent,
How can I add a query within this one where I can add TWO MORE columns which give me sum of all the sums and sum of all of avg
Thanks in advance
The CASE expressions you have covers <0, between 0 and 8 and >=9, which is every possibility, provided that your data is an int. Thus, all you need to add is
COUNT(Overall_Time_Spent) AS DaysTotal
If it isn't an int, then your CASE expressions will miss values between 3 and 4, 4 and 5, 5 and 6 and 8 and 9, however, the COUNT would include them.
Try to use subquery:
SELECT a.*, (Errors + _0_3_days...) as Total FROM (
select
sum(case when Overall_Time_Spent < 0 then 1 else 0 end) as Errors,
sum(case when Overall_Time_Spent between 0 and 3 then 1 else 0 end) as _0_3_days,
sum(case when Overall_Time_Spent = 4 then 1 else 0 end) as _4_days,
sum(case when Overall_Time_Spent = 5 then 1 else 0 end) as _5_days,
sum(case when Overall_Time_Spent between 6 and 8 then 1 else 0 end) as _6_8_days,
sum(case when Overall_Time_Spent >= 9 then 1 else 0 end) as more_than_9_days
) as a

T-SQL Pivot multiple columns duplicates results

I have a table with aggregated data that I would like to pivot. Here is a sample of the data:
Unit# EffectiveDay RequestCount ResponseCount
5 1 0 0
5 2 8 8
5 3 4 4
5 4 4 4
5 5 2 2
5 6 0 0
5 7 2 2
6 1 0 0
6 2 0 0
6 3 0 0
6 4 0 0
6 5 0 0
6 6 0 0
6 7 0 0
I can successfully pivot the RequestCount column, but when I add the ResponseCount column it's as if the rows increased exponentially. Instead of two rows (one for each unit#), I get 14.
select *
from (
select Unit#
,concat('Request', EffectiveDay) AS RequestDay
,RequestCount
,concat('Response', EffectiveDay) As ResponseDay
,ResponseCount
from #tmpLogPayConnexion
) as P
pivot (
sum(RequestCount)
for RequestDay in ([Request1], [Request2], [Request3], [Request4], [Request5], [Request6], [Request7])
) as pvtRequest
pivot (
sum(ResponseCount)
for ResponseDay in ([Response1], [Response2], [Response3], [Response4], [Response5], [Response6], [Response7])
) as pvtResponse
I know this is possible, but I am stumped.
Thank you.
This seems like a time for a crosstab, or conditional aggregation.
select [Unit#]
, Request1 = max(case when EffectiveDay = 1 then RequestCount end)
, Request2 = max(case when EffectiveDay = 2 then RequestCount end)
, Request3 = max(case when EffectiveDay = 3 then RequestCount end)
, Request4 = max(case when EffectiveDay = 4 then RequestCount end)
, Request5 = max(case when EffectiveDay = 5 then RequestCount end)
, Request6 = max(case when EffectiveDay = 6 then RequestCount end)
, Request7 = max(case when EffectiveDay = 7 then RequestCount end)
, Response1 = max(case when EffectiveDay = 1 then ResponseCount end)
, Response2 = max(case when EffectiveDay = 2 then ResponseCount end)
, Response3 = max(case when EffectiveDay = 3 then ResponseCount end)
, Response4 = max(case when EffectiveDay = 4 then ResponseCount end)
, Response5 = max(case when EffectiveDay = 5 then ResponseCount end)
, Response6 = max(case when EffectiveDay = 6 then ResponseCount end)
, Response7 = max(case when EffectiveDay = 7 then ResponseCount end)
from #tmpLogPayConnexion
group by [Unit#]
order by [Unit#]

I want to append year of date in alias, is it possible? and how?

My query looks like this:
SELECT
u.Title,
SUM(CASE datepart(month,VISIT_DATE) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,VISIT_DATE) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,VISIT_DATE) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,VISIT_DATE) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,VISIT_DATE) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,VISIT_DATE) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,VISIT_DATE) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,VISIT_DATE) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,VISIT_DATE) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,VISIT_DATE) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,VISIT_DATE) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,VISIT_DATE) WHEN 12 THEN 1 ELSE 0 END) AS 'December'
FROM
pidilite_feedback_app_dev_latest.dbo.visit v, pidilite_feedback_app_dev_latest.dbo.user_master u
WHERE
v.VISIT_DATE BETWEEN '2017-01-30 12:45:43.673' and '2017-05-24 12:45:43.673'
and u.ID = v.USER_ID
and v.VISIT_STATUS = 'submitted'
and u.delete_flag = 'F'
and v.delete_flag = 'F'
and v.DIVISION_CODE = '10'
GROUP BY v.USER_ID, u.Title
where I want something like this
SUM(CASE datepart(month,VISIT_DATE) WHEN 1 THEN 1 ELSE 0 END) AS concat(YEAR(VISIT_DATE),'January'),
OR
SUM(CASE datepart(month,VISIT_DATE) WHEN 2 THEN 1 ELSE 0 END) AS 'February'+''+YEAR(VISIT_DATE),
But it is not working, need guidance, thank you

SQL - AVG call rate by time of day and day of week

I have a table with records for each telephone call.
I want to create a matrix where I have the average call rate per 30 minute time segment over the day by day of week. Each call has a start time which is a date/time
i.e.
I want to display Mon-Sun as row headers and 30 min intervals as column headers. The content is the average count of calls for each day (Mon - Sun) in each particular 30 minute segment. This is where I have got but it isn't what I want. It sums the number of calls by time segment and I cant work out how to get averages.
I hope some can point me in the right direction -
SQL is not something I use often so be gentle :-)
set dateformat dmy
SET DATEFIRST 1 -- Start with Monday
SELECT datepart(dw,starttime) as 'Day',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS 'Midnight-12:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '12:30am-1am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '1am-1:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '1:30am-2am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '2am-2:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '2:30am-3am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '3am-3:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '3:30am-4am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '4am-4:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '4:30am-5am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '5am-5:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '5:30am-6am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '6am-6:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '6:30am-7am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '7am-7:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '7:30am-8am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '8:30am-9am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '9am-9:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '10:30am-11am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '11am-11:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '11:30am-Noon',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS 'Noon-12:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '1pm-1:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '1:30pm-2pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '2pm-2:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '2:30pm-3pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '3pm-3:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '4:30pm-4pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '4pm-5:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '5:30pm-6pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '6pm-6:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '6:30pm-7pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '7pm-7:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '7:30pm-8pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '8pm-8:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '8:30pm-9pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '9pm-9:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '9:30pm-10pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '10pm-10:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '10:30pm-11pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '11pm-11:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '11:30pm-Midnight'
FROM [dbo].[CallList]
where StartTime>'1/6/2015'
and StartTime<'30/6/2015'
GROUP BY datepart(dw,starttime)
ORDER BY datepart(dw,starttime)
Creating a WHILE loop to insert into a temp table then doing the aggregation after the fact is a viable option. See below:
CREATE TABLE #Data
(
ID UNIQUEIDENTIFIER
,DateIn DATETIME
)
INSERT INTO #Data
VALUES
(NEWID(), '2016-05-17 00:01:00'),
(NEWID(), '2016-05-17 00:21:00'),
(NEWID(), '2016-05-17 01:01:00'),
(NEWID(), '2016-05-17 05:41:00'),
(NEWID(), '2016-05-17 06:51:00'),
(NEWID(), '2016-05-17 13:21:00'),
(NEWID(), '2016-05-17 18:01:00'),
(NEWID(), '2016-05-17 21:11:00'),
(NEWID(), '2016-05-17 23:41:00'),
(NEWID(), '2016-05-17 23:51:00')
DECLARE #Start DATETIME
DECLARE #End DATETIME
SET #Start = '2016-05-17 00:00:00'
SET #End = '2016-05-17 00:30:00'
CREATE TABLE #Temp
(
ID UNIQUEIDENTIFIER
,[Time Increment] VARCHAR(100)
)
WHILE #End <= '2016-05-17 23:30:00'
BEGIN
INSERT INTO #Temp
SELECT
ID
,CAST(#Start AS VARCHAR) + ' - ' + CAST(#End AS VARCHAR) [Time Increment]
FROM #Data
WHERE DateIn >= #Start
AND DateIn < #End
SET #Start = DATEADD(Minute, 30, #Start)
SET #End = DATEADD(Minute, 30, #End)
END
SELECT
[Time Increment]
,COUNT(ID) [Count]
FROM #Temp
GROUP BY [Time Increment]
DROP TABLE #Temp
DROP TABLE #Data
In this example, the result is the following:
Time Increment |Count
May 17 2016 1:00AM - May 17 2016 1:30AM | 1
May 17 2016 1:00PM - May 17 2016 1:30PM | 1
May 17 2016 5:30AM - May 17 2016 6:00AM | 1
May 17 2016 6:00PM - May 17 2016 6:30PM | 1
May 17 2016 6:30AM - May 17 2016 7:00AM | 1
May 17 2016 9:00PM - May 17 2016 9:30PM | 1
May 17 2016 12:00AM - May 17 2016 12:30AM | 2

Resources