IBM DB2 PIVOT A TABLE FULL OF DATES - database

I have this table in DB2:
DATE
----------
09/11/2021
06/10/2021
28/11/2021
17/11/2021
11/10/2021
24/11/2021
07/11/2021
30/11/2021
I want to count how many times a date appeared in the table and group it by year and month, and display it like this:
| YEAR | OCTOBER | NOVEMBER |
----------------------------
| 2021 | 2 | 6 |

As months are a known quantity you could use a sum of a case statement:
select year(datecol) as year
,sum(case when month(datecol) = 1 then 1 else 0 end) as jan
,sum(case when month(datecol) = 2 then 1 else 0 end) as feb
,sum(case when month(datecol) = 3 then 1 else 0 end) as mar
,sum(case when month(datecol) = 4 then 1 else 0 end) as apr
,sum(case when month(datecol) = 5 then 1 else 0 end) as may
,sum(case when month(datecol) = 6 then 1 else 0 end) as jun
,sum(case when month(datecol) = 7 then 1 else 0 end) as jul
,sum(case when month(datecol) = 8 then 1 else 0 end) as aug
,sum(case when month(datecol) = 9 then 1 else 0 end) as sep
,sum(case when month(datecol) = 10 then 1 else 0 end) as oct
,sum(case when month(datecol) = 11 then 1 else 0 end) as nov
,sum(case when month(datecol) = 12 then 1 else 0 end) as dec
from datetest
group by year(datecol)
order by 1;
That will give you output similar to this:
YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
2018 0 0 0 0 0 0 0 0 0 0 3 0
2019 0 0 0 0 0 0 0 0 0 1 2 0
2020 0 0 0 0 0 0 0 0 0 1 1 0
2021 0 0 0 0 0 0 0 0 0 2 6 0

You may use a generic routine described at the link dynamic pivot SQL Query in Db2.
Use the following call to get the desired result set for your case:
CALL PIVOT
(
'SELECT YEAR (DATE) AS YEAR, TO_CHAR (DATE, ''MONTH'') AS MONTH FROM DATES'
, 'YEAR'
, 'MONTH'
, 'MONTH'
, 'count'
, 'SESSION.PIVOT'
, '-'
, ?, ?, ?
);
The result is:
YEAR
NOVEMBER
OCTOBER
2021
6
2

Related

Displaying full years worth of data when part way through

I am attempting to display a full years worth of data when only part way through the year and it has been requested to display these future months of the year as 0.
I do not want to add these further months manually post query as when we enter March, March will start displaying data.
My query:
SELECT
FORMAT(a.CreatedDateTime, 'MMM') AS 'month',
SUM(b.NewContactCount) AS 'new_total',
SUM(b.TotalContactCount) AS 'all_total'
FROM database.dbo.ImportBatch a
LEFT JOIN ImportBatch_File b ON a.id = b.BatchID
WHERE a.CreatedDateTime >= CAST(YEAR(GETDATE()) AS VARCHAR) + '/01/01'
GROUP BY FORMAT(a.CreatedDateTime, 'MMM'), MONTH(a.CreatedDateTime)
ORDER BY MONTH(a.CreatedDateTime)
My results:
month new_total all_total
1 Jan 337 1000
2 Feb 146 497
My desired outcome:
month new_total all_total
1 Jan 337 1000
2 Feb 146 497
3 Mar 0 0
4 Apr 0 0
5 May 0 0
6 Jun 0 0
7 Jul 0 0
8 Aug 0 0
9 Sep 0 0
10 Oct 0 0
11 Nov 0 0
12 Dec 0 0

How can I pivot data based on Month and year wise client Report

I'm trying to retrieve data based on Month of every year. It will get but sorting is not correct.
SELECT Client,DispacthDate,Models,Model1,Model2,Model3
FROM (
SELECT Client
,Concat(year(Audit_Date),' ',datename(month,Audit_Date)) as DispatchDate
,Models
,Count(Models) 'Units'
FROM dbo.ABC
GROUP BY Client,Concat(year(Audit_Date),' ',datename(month,Audit_Date)),Models
) abcd
PIVOT(SUM(Units) FOR Models IN (Model1,Model2,Model3)) as P
ORDER BY Client,DispatchDate
I'll get result set like this:
Client DispatchDate Model1 Model2 Model3
Client1 2017 December 0 0 0
Client1 2017 November 25 0 0
Client1 2018 January 21 0 0
Client1 2018 March 9 0 0
Client2 2017 December 8 0 0
Client2 2018 April 5 0 0
Client2 2018 August 0 0 0
Client2 2018 July 0 0 0
Client2 2018 March 0 0 0
Client3 2017 December 4 0 0
Client3 2017 November 687 0 0
Client3 2018 April 8 0 0
Client3 2018 January 0 0 0
But Required Data Like:
Client DispatchDate Model1 Model2 Model3
Client1 2018 January 21 0 0
Client1 2018 March 9 0 0
Client1 2017 November 25 0 0
Client1 2017 December 0 0 0
Client2 2018 March 0 0 0
Client2 2018 April 5 0 0
Client2 2018 July 0 0 0
Client2 2018 August 0 0 0
Client2 2017 December 8 0 0
Client3 2018 January 0 0 0
Client3 2018 April 8 0 0
Client3 2017 November 687 0 0
Client3 2017 December 4 0 0
Please Any one suggest me for my query.
From my understanding,
SELECT Client,DispacthDate,Models,Model1,Model2,Model3
FROM (
SELECT Client
,Concat(year(Audit_Date),' ',datename(month,Audit_Date)) as DispatchDate
,Models
,Count(Models) 'Units'
FROM dbo.ABC
GROUP BY Client,Concat(year(Audit_Date),' ',datename(month,Audit_Date)),Models
) abcd
PIVOT(SUM(Units) FOR Models IN (Model1,Model2,Model3)) as P
ORDER BY Client,
, DATEPART (YYYY, convert(date, DispacthDate)) desc -- here query has been changed.
, DATEPART (MM, convert(date, DispacthDate)) -- here query has been changed.
Don't convert the DispatchDate to string in the inner query. Keep it in date or datetime type. And then convert it at the outer query
SELECT Client,
DATENAME(YEAR, Audit_Date) + ' ' + DATENAME(MONTH, Audit_Date) AS DispatchDate,
Models, Model1, Model2, Model3
FROM (
SELECT Client
,DATEADD(MONTH, DATEDIFF(MONTH, 0, Audit_Date), 0) as Audit_Date
,Models
,Count(Models) AS Units
FROM dbo.ABC
GROUP BY Client,
DATEADD(MONTH, DATEDIFF(MONTH, 0, Audit_Date), 0), month(Audit_Date)),
Models
) abcd
PIVOT(SUM(Units) FOR Models IN (Model1,Model2,Model3)) as P
ORDER BY Client, Audit_Date

SQL Server tabulating survey data for every month

I have a survey table with many columns but i am focusing on these 2, survey_date and over_rating. i am not sure if it is possible to be done in a single query. I am using sql server 2012. This is my sample data.
select survey_date, overall_rating from survey
survey_date overall_rating
2017-01-06 15:09:51.940 6
2017-02-06 14:18:18.620 4
2017-05-07 16:03:12.037 7
2017-05-23 10:41:30.357 7
2017-05-23 10:41:30.357 5
2017-05-24 12:05:25.217 8
2017-06-01 09:03:47.727 7
2017-06-05 09:01:07.283 9
2017-06-05 09:28:12.597 6
2017-06-15 09:47:29.407 7
2017-07-06 12:10:50.003 2
2017-07-06 13:45:52.997 7
2017-08-06 14:00:35.403 5
2017-08-09 12:21:17.367 8
I need to count the occurrence for each rating 1-10, for each month, and sum it up. Example June 15, rating 10 have 1, rating 9 have 10, ...
This is the result table:
Month 10 9 8 7 6 5 4 3 2 1 Avg Score Total Total >=6 CSI
June'15 1 10 20 3 0 0 0 0 0 0 8 34 34 100%
July'15 1 16 14 0 0 0 0 0 1 0 9 32 31 99%
August'15 7 6 6 0 0 0 0 0 0 0 9 19 19 100%
September'15 0 2 2 0 0 0 0 0 0 0 9 4 4 100%
November'15 0 1 2 0 0 0 0 0 0 0 8 3 3 100%
December'15 0 7 3 4 2 0 0 0 0 0 8 16 16 100%
i have tried this query but is partly wrong as there is duplicate month for each rating:
select si.yr, si.mn,
case when si.overall_rating = 10 then count(si.overall_rating) else 0 end as '10',
case when si.overall_rating = 9 then count(si.overall_rating) else 0 end as '9',
case when si.overall_rating = 8 then count(si.overall_rating) else 0 end as '8',
case when si.overall_rating = 7 then count(si.overall_rating) else 0 end as '7',
case when si.overall_rating = 6 then count(si.overall_rating) else 0 end as '6',
case when si.overall_rating = 5 then count(si.overall_rating) else 0 end as '5',
case when si.overall_rating = 4 then count(si.overall_rating) else 0 end as '4',
case when si.overall_rating = 3 then count(si.overall_rating) else 0 end as '3',
case when si.overall_rating = 2 then count(si.overall_rating) else 0 end as '2',
case when si.overall_rating = 1 then count(si.overall_rating) else 0 end as '1',
sum(si.overall_rating) as month_count
from
(select YEAR(s.survey_date) yr, MONTH(s.survey_date) mn, s.overall_rating
from survey s where s.status='Submitted' and s.survey_date >= '2017-01-01' AND s.survey_date <= '2017-12-31'
group by YEAR(s.survey_date), MONTH(s.survey_date), s.overall_rating) si group by si.yr, si.mn, si.overall_rating;
Results:
yr mm 10 9 8 7 6 5 4 3 2 1 total
2017 1 0 0 0 0 1 0 0 0 0 0 6
2017 2 0 0 0 0 0 0 1 0 0 0 4
2017 5 0 0 0 0 0 1 0 0 0 0 5
2017 5 0 0 0 1 0 0 0 0 0 0 7
2017 5 0 0 1 0 0 0 0 0 0 0 8
2017 6 0 0 0 0 1 0 0 0 0 0 6
2017 6 0 0 0 1 0 0 0 0 0 0 7
2017 6 0 1 0 0 0 0 0 0 0 0 9
2017 7 0 0 0 0 0 0 0 0 1 0 2
2017 7 0 0 0 1 0 0 0 0 0 0 7
2017 8 0 0 0 0 0 1 0 0 0 0 5
2017 8 0 0 1 0 0 0 0 0 0 0 8
As you can see 5 and 6 are repeated for different rating. If anyone could tell me is it possible to be done in a single query. Thanks
I think I understand what you are trying to achieve here and you'll be pleased to know you are not far off. You have the right idea in using a conditional aggregate, but you need to wrap your conditional case expression in the aggregate, not the other way around. To do a conditional count, you can simply return 1 for a condition match and 0 for a no match and then sum up the result.
Doing this allows your group by to remain nice and simple:
declare #t table(survey_date datetime,overall_rating int);
insert into #t values ('2017-01-06 15:09:51.940',6),('2017-02-06 14:18:18.620',4),('2017-05-07 16:03:12.037',7),('2017-05-23 10:41:30.357',7),('2017-05-23 10:41:30.357',5),('2017-05-24 12:05:25.217',8),('2017-06-01 09:03:47.727',7),('2017-06-05 09:01:07.283',9),('2017-06-05 09:28:12.597',6),('2017-06-15 09:47:29.407',7),('2017-07-06 12:10:50.003',2),('2017-07-06 13:45:52.997',7),('2017-08-06 14:00:35.403',5),('2017-08-09 12:21:17.367',8);
select dateadd(m,datediff(m,0,survey_date),0) as [Month]
,sum(case when overall_rating = 10 then 1 else 0 end) as [10]
,sum(case when overall_rating = 9 then 1 else 0 end) as [9]
,sum(case when overall_rating = 8 then 1 else 0 end) as [8]
,sum(case when overall_rating = 7 then 1 else 0 end) as [7]
,sum(case when overall_rating = 6 then 1 else 0 end) as [6]
,sum(case when overall_rating = 5 then 1 else 0 end) as [5]
,sum(case when overall_rating = 4 then 1 else 0 end) as [4]
,sum(case when overall_rating = 3 then 1 else 0 end) as [3]
,sum(case when overall_rating = 2 then 1 else 0 end) as [2]
,sum(case when overall_rating = 1 then 1 else 0 end) as [1]
,count(overall_rating) as ScoresReturned
,sum(overall_rating) as TotalScore
,avg(cast(overall_rating as decimal(10,0))) as Average
from #t
group by dateadd(m,datediff(m,0,survey_date),0)
order by [Month];
Output:
+-------------------------+----+---+---+---+---+---+---+---+---+---+----------------+------------+----------+
| Month | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | ScoresReturned | TotalScore | Average |
+-------------------------+----+---+---+---+---+---+---+---+---+---+----------------+------------+----------+
| 2017-01-01 00:00:00.000 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 6 | 6.000000 |
| 2017-02-01 00:00:00.000 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 4 | 4.000000 |
| 2017-05-01 00:00:00.000 | 0 | 0 | 1 | 2 | 0 | 1 | 0 | 0 | 0 | 0 | 4 | 27 | 6.750000 |
| 2017-06-01 00:00:00.000 | 0 | 1 | 0 | 2 | 1 | 0 | 0 | 0 | 0 | 0 | 4 | 29 | 7.250000 |
| 2017-07-01 00:00:00.000 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 2 | 9 | 4.500000 |
| 2017-08-01 00:00:00.000 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 2 | 13 | 6.500000 |
+-------------------------+----+---+---+---+---+---+---+---+---+---+----------------+------------+----------+
select si.yr, si.mn,
case when si.overall_rating = 10 then count(si.overall_rating) else 0 end as '10',
case when si.overall_rating = 9 then count(si.overall_rating) else 0 end as '9',
case when si.overall_rating = 8 then count(si.overall_rating) else 0 end as '8',
case when si.overall_rating = 7 then count(si.overall_rating) else 0 end as '7',
case when si.overall_rating = 6 then count(si.overall_rating) else 0 end as '6',
case when si.overall_rating = 5 then count(si.overall_rating) else 0 end as '5',
case when si.overall_rating = 4 then count(si.overall_rating) else 0 end as '4',
case when si.overall_rating = 3 then count(si.overall_rating) else 0 end as '3',
case when si.overall_rating = 2 then count(si.overall_rating) else 0 end as '2',
case when si.overall_rating = 1 then count(si.overall_rating) else 0 end as '1',
sum(si.overall_rating) as month_count
from
(select YEAR(s.survey_date) yr, MONTH(s.survey_date) mn, s.overall_rating
from survey s where s.status='Submitted' and s.survey_date >= '2017-01-01' AND s.survey_date <= '2017-12-31'
group by YEAR(s.survey_date), MONTH(s.survey_date), s.overall_rating) si group by si.yr, si.mn;
You must remove si.overall_rating in Group By
Oh! I posted late, anyway you can try this otherwise.
DATA:
IF ( OBJECT_ID('tempdb..#temptable') IS NOT NULL )
BEGIN
DROP TABLE #temptable
END
CREATE TABLE #temptable
(
survey_date DATETIME ,
overall_rating NUMERIC(22,2)
)
INSERT INTO #temptable
( survey_date, overall_rating )
VALUES ( '2017-01-06 15:09:51.940', 6 ),
( '2017-02-06 14:18:18.620', 4 ),
( '2017-05-07 16:03:12.037', 7 ),
( '2017-05-23 10:41:30.357', 7 ),
( '2017-05-23 10:41:30.357', 5 ),
( '2017-05-24 12:05:25.217', 8 ),
( '2017-06-01 09:03:47.727', 7 ),
( '2017-06-05 09:01:07.283', 9 ),
( '2017-06-05 09:28:12.597', 6 ),
( '2017-06-15 09:47:29.407', 7 ),
( '2017-07-06 12:10:50.003', 2 ),
( '2017-07-06 13:45:52.997', 7 ),
( '2017-08-06 14:00:35.403', 5 ),
( '2017-08-09 12:21:17.367', 8 )
QUERY:
;
WITH CTE
AS ( SELECT DATENAME(month, survey_date) + ' '''
+ RIGHT(CAST(YEAR(survey_date) AS NVARCHAR(4)),
2) AS [Month] ,
ISNULL([1], 0) [1] ,
ISNULL([2], 0) [2] ,
ISNULL([3], 0) [3] ,
ISNULL([4], 0) [4] ,
ISNULL([5], 0) [5] ,
ISNULL([6], 0) [6] ,
ISNULL([7], 0) [7] ,
ISNULL([8], 0) [8] ,
ISNULL([9], 0) [9] ,
ISNULL([10], 0) [10],
Total,
Average
FROM ( SELECT survey_date ,
COUNT(overall_rating) overall_rating,
CAST(SUM(overall_rating) AS INT) Total,
AVG(overall_rating) Average
FROM ( SELECT DATEADD(MONTH,
DATEDIFF(MONTH,
0, survey_date),
0) survey_date ,
overall_rating
FROM #temptable
) T
GROUP BY t.survey_date
) PVT PIVOT ( SUM(overall_rating) FOR overall_rating IN ( [1],
[2], [3], [4],
[5], [6], [7],
[8], [9], [10] ) ) P
)
SELECT [Month] ,
ISNULL([1], 0) [1] ,
ISNULL([2], 0) [2] ,
ISNULL([3], 0) [3] ,
ISNULL([4], 0) [4] ,
ISNULL([5], 0) [5] ,
ISNULL([6], 0) [6] ,
ISNULL([7], 0) [7] ,
ISNULL([8], 0) [8] ,
ISNULL([9], 0) [9] ,
ISNULL([10], 0) [10],
Total,
Average
FROM CTE
RESULT:
Month 1 2 3 4 5 6 7 8 9 10 Total Average
----------------------- ------ ---- ----- ----- ----- ----- ---- ---- ----- ----- ----------- -------------
January '17 1 0 0 0 0 0 0 0 0 0 6 6.000000
February '17 1 0 0 0 0 0 0 0 0 0 4 4.000000
May '17 0 0 0 4 0 0 0 0 0 0 27 6.750000
June '17 0 0 0 4 0 0 0 0 0 0 29 7.250000
July '17 0 2 0 0 0 0 0 0 0 0 9 4.500000
August '17 0 2 0 0 0 0 0 0 0 0 13 6.500000
(6 row(s) affected)

Multiple PIVOTS? Need to count by Hour Per Month

This should be pretty simple. I have other PIVOT SQL queries working fine. I want to count logins: by hour, by month. I am thinking two PIVOTs or UNPIVOT and then PIVOT? Yes, I have dug around here, other sites, Google, etc. I am pretty stuck.
SELECT
loginid
,DATEPART(MONTH,logtime) Month
, DATEPART(HOUR, logtime) Hour
FROM somelog (nolock)
) temp
PIVOT (
COUNT(loginid)
FOR Month in (JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC)
) AS Pvt
What I want the results to be..
HOUR,JAN,FEB,MAR
00
01
02
..
23
and I don't need 8760 (365 x 24) of them
I have tried GROUP BY HOUR
I have tried GROUP BY temp.hour
I have also tried this as well.. It does seem to work, but I get Hour 00 for example 365 times.. Again, the GROUP BY issue..
SELECT
TimeOfDay
, [1] JAN
, [2] FEB
, [3] MAR
, [4] APR
, [5] MAY
, [6] JUN
, [7] JUL
, [8] AUG
, [9] SEP
, [10] OCT
, [11] NOV
, [12] DEC
FROM (SELECT logintime
, loginid
, datepart(month, logintime) [month]
, DatePart(hour, logintime) TimeOfDay
FROM sometable (nolock)) x
PIVOT (
COUNT(loginid)
for [month] in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS pvt
ORDER BY 1, 2
Thanks,
Kent
One way to do it
WITH hour_list AS (
SELECT 0 hour
UNION ALL
SELECT hour + 1 FROM hour_list WHERE hour < 23
)
SELECT h.hour,
COALESCE(jan, 0) jan,
COALESCE(feb, 0) feb,
COALESCE(mar, 0) mar,
COALESCE(apr, 0) apr,
COALESCE(may, 0) may,
COALESCE(jun, 0) jun,
COALESCE(jul, 0) jul,
COALESCE(aug, 0) aug,
COALESCE(sep, 0) sep,
COALESCE(oct, 0) oct,
COALESCE(nov, 0) nov,
COALESCE(dec, 0) dec
FROM hour_list h LEFT JOIN
(
SELECT DATEPART(HOUR, logtime) hour,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 1 THEN 1 END) jan,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 2 THEN 1 END) feb,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 3 THEN 1 END) mar,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 4 THEN 1 END) apr,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 5 THEN 1 END) may,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 6 THEN 1 END) jun,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 7 THEN 1 END) jul,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 8 THEN 1 END) aug,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 9 THEN 1 END) sep,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 10 THEN 1 END) oct,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 11 THEN 1 END) nov,
SUM(CASE WHEN DATEPART(MONTH, logtime) = 12 THEN 1 END) dec
FROM somelog (NOLOCK)
GROUP BY DATEPART(HOUR, logtime)
) l
ON h.hour = l.hour
or with PIVOT
WITH hour_list AS (
SELECT 0 hour
UNION ALL
SELECT hour + 1 FROM hour_list WHERE hour < 23
)
SELECT h.hour,
COALESCE([1], 0) jan,
COALESCE([2], 0) feb,
COALESCE([3], 0) mar,
COALESCE([4], 0) apr,
COALESCE([5], 0) may,
COALESCE([6], 0) jun,
COALESCE([7], 0) jul,
COALESCE([8], 0) aug,
COALESCE([9], 0) sep,
COALESCE([10], 0) oct,
COALESCE([11], 0) nov,
COALESCE([12], 0) dec
FROM hour_list h LEFT JOIN
(
SELECT DATEPART(MONTH, logtime) month,
DATEPART(HOUR, logtime) hour,
COUNT(*) log_count
FROM somelog (NOLOCK)
GROUP BY DATEPART(MONTH, logtime), DATEPART(HOUR, logtime)
) s
PIVOT
(
SUM(log_count) FOR month IN([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) p
ON h.hour = p.hour
Sample output for both queries:
| HOUR | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
|------|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
| 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 3 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 4 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
...
Here is SQLFiddle demo (using CASE)
Here is SQLFiddle demo (using PIVOT)

Need help to get sql query output like this

This is my query
select
dtfromdate, dttodate,
(SELECT DATEDIFF(day, dtfromdate, dtTodate)) AS NumberOfDays,
fltspl
from dbo.tblHR_SpecialLeaveTransaction
where
nvrempcode = 'MCL1218' and nvrstatus = 1
order by
dtfromdate
Result :
dtfromdate dttodate NumberOfDays fltspl
----------------------- ----------------------- ------------ ----------------------
2012-05-01 00:00:00 2012-05-31 00:00:00 30 30
Another query
select
dtfromdate, dtTodate,
(SELECT DATEDIFF(day, dtfromdate, dtTodate) ) AS NumberOfDays,
fltcl, fltsl, fltpl, fltcompoff, fltod, fltlop,
isnull(fltflexiL, 0) as fltflexiL
from
tblhr_leavetransaction
where
nvrempcode = 'MCL1218' and nvrstatus = 1
order by
dtfromdate
Result:
dtfromdate dtTodate NumberOfDays fltcl fltsl fltpl fltcompoff fltod fltlop fltflexiL
----------------------- ----------------------- ------------ ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ----------------------
2011-01-14 00:00:00 2011-01-14 00:00:00 0 1 0 0 0 0 0 0
2011-01-17 00:00:00 2011-01-17 00:00:00 0 1 0 0 0 0 0 0
2011-01-25 00:00:00 2011-01-25 00:00:00 0 0 0 0 0 1 0 0
2011-04-01 00:00:00 2011-04-02 00:00:00 1 0 0 0 0 2 0 0
2011-05-14 00:00:00 2011-05-14 00:00:00 0 0 0 0 0 1 0 0
2011-05-16 00:00:00 2011-05-16 00:00:00 0 0 0 0 1 0 0 0
2011-05-18 00:00:00 2011-05-18 00:00:00 0 1 0 0 0 0 0 0
2011-05-19 00:00:00 2011-05-20 00:00:00 1 0 2 0 0 0 0 0
2011-05-21 00:00:00 2011-05-21 00:00:00 0 1 0 0 0 0 0 0
2011-05-23 00:00:00 2011-05-23 00:00:00 0 0 0 0 1 0 0 0
I need the output like this,
dtfromdate dtTodate NumberOfDays fltcl fltsl fltpl fltcompoff fltod fltlop fltflexiL fltspl
----------------------- ----------------------- ------------ ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ----------
2011-01-14 00:00:00 2011-01-14 00:00:00 0 1 0 0 0 0 0 0 0
2011-01-17 00:00:00 2011-01-17 00:00:00 0 1 0 0 0 0 0 0 0
2011-01-25 00:00:00 2011-01-25 00:00:00 0 0 0 0 0 1 0 0 0
2011-04-01 00:00:00 2011-04-02 00:00:00 1 0 0 0 0 2 0 0 0
2011-05-14 00:00:00 2011-05-14 00:00:00 0 0 0 0 0 1 0 0 0
2011-05-16 00:00:00 2011-05-16 00:00:00 0 0 0 0 1 0 0 0 0
2011-05-18 00:00:00 2011-05-18 00:00:00 0 1 0 0 0 0 0 0 0
2011-05-19 00:00:00 2011-05-20 00:00:00 1 0 2 0 0 0 0 0 0
2011-05-21 00:00:00 2011-05-21 00:00:00 0 1 0 0 0 0 0 0 0
2011-05-23 00:00:00 2011-05-23 00:00:00 0 0 0 0 1 0 0 0 0
2012-05-01 00:00:00 2012-05-31 00:00:00 30 0 0 0 0 0 0 0 30
Looks like you just want to union two queries together. To do this both queries must include all columns, just set those to null/zero as necessary. One other slight difficulty is that you cant use order by when unioning, unless you subselect the whole thing.
Without the order by:
select
dtfromdate, dttodate,
(SELECT DATEDIFF(day, dtfromdate, dtTodate)) AS NumberOfDays,
0 as fltcl,
0 as fltsl,
0 as fltpl,
0 as fltcompoff,
0 as fltod,
0 as fltlop,
0 as fltflexiL,
fltspl
from dbo.tblHR_SpecialLeaveTransaction
where
nvrempcode = 'MCL1218' and nvrstatus = 1
union
select
dtfromdate, dtTodate,
(SELECT DATEDIFF(day, dtfromdate, dtTodate) ) AS NumberOfDays,
fltcl, fltsl, fltpl, fltcompoff, fltod, fltlop,
isnull(fltflexiL, 0) as fltflexiL ,
0 as fltspl
from
tblhr_leavetransaction
where
nvrempcode = 'MCL1218' and nvrstatus = 1
If you need a specific order:
SELECT * FROM
(
select
dtfromdate, dttodate,
(SELECT DATEDIFF(day, dtfromdate, dtTodate)) AS NumberOfDays,
0 as fltcl,
0 as fltsl,
0 as fltpl,
0 as fltcompoff,
0 as fltod,
0 as fltlop,
0 as fltflexiL,
fltspl
from dbo.tblHR_SpecialLeaveTransaction
where
nvrempcode = 'MCL1218' and nvrstatus = 1
union
select
dtfromdate, dtTodate,
(SELECT DATEDIFF(day, dtfromdate, dtTodate) ) AS NumberOfDays,
fltcl, fltsl, fltpl, fltcompoff, fltod, fltlop,
isnull(fltflexiL, 0) as fltflexiL ,
0 as fltspl
from
tblhr_leavetransaction
where
nvrempcode = 'MCL1218' and nvrstatus = 1
) src
order by dtfromdate

Resources