How use count for value from another table - sql

How to use count joining with different tables. Please have a look at my queries. Here i am using CROSS APPLY. But i am not getting the actual result.
how i can to get all the item from item table not in incident table.
Tabel : Inc_cat
+------------+--------------+--+
| inc_cat_id | inc_cat_n | |
+------------+--------------+--+
| 1 | Support | |
| 2 | PM | |
| 3 | Installation | |
+------------+--------------+--+
Table:incident
+-------------+---------+------------+-----------------+
| incident_id | item_id | inc_cat_id | date_logged |
+-------------+---------+------------+-----------------+
| 100 | 555 | 1 | 2016-01-01 |
| 101 | 555 | 2 | 2016-01-18 |
| 103 | 444 | 3 | 2016-02-10 |
| 104 | 444 | 2 | 2016-04-01 |
| 105 | 666 | 1 | 2016-04-09 |
| 106 | 555 | 2 | 2016-04-20 |
+-------------+---------+------------+-----------------+
Table:item
+---------+---------+--+
| item_id | cust_id | |
+---------+---------+--+
| 444 | 34 | |
| 555 | 34 | |
| 666 | 76 | |
| 333 | 34 | |
| 222 | 34 | |
| 111 | 34 | |
+---------+---------+--+
Result:
+---------+----------------+-----------+---------------------+
| item_id | count(Support) | count(PM) | count(Installation) |
+---------+----------------+-----------+---------------------+
| 555 | 0 | 1 | 0 |
| 444 | 0 | 1 | 0 |
| 666 | 0 | 0 | 0 |
| 333 | 0 | 0 | 0 |
| 222 | 0 | 0 | 0 |
| 111 | 0 | 0 | 0 |
+---------+----------------+-----------+---------------------+
My Query:
SELECT i.item_ID,
COUNT(CASE WHEN i.inc_cat_id = ic.inc_cat_id AND i.inc_cat_id = 1 THEN 1 END) AS cntSupport,
COUNT(CASE WHEN i.inc_cat_id = ic.inc_cat_id AND i.inc_cat_id = 2 THEN 1 END) AS cntPM,
COUNT(CASE WHEN i.inc_cat_id = ic.inc_cat_id AND i.inc_cat_id = 3 THEN 1 END) AS cntInstallation
FROM #incident i
CROSS APPLY #incCat ic
WHERE (i.date_logged BETWEEN '2016-04-01' AND '2016-04-30')AND i.cust_id='34'
GROUP BY i.item_ID

You don't need a CROSS APPLY. A simple LEFT JOIN will do:
SELECT i.item_id,
COUNT(CASE WHEN inc.inc_cat_id = 1 THEN 1 END) AS cntSupport,
COUNT(CASE WHEN inc.inc_cat_id = 2 THEN 1 END) AS cntPM,
COUNT(CASE WHEN inc.inc_cat_id = 3 THEN 1 END) AS cntInstallation
FROM Item AS i
LEFT JOIN Incident AS inc ON i.item_id = inc.item_id AND
inc.date_logged BETWEEN '2016-04-01' AND '2016-04-30'
WHERE i.cust_id = 34
GROUP BY i.item_id
You just need to start by table Item, so as to get all items returned, as in the expected result set in the OP.
Demo here

Unless I'm missing something, your query is over complicated:
SELECT item_ID,
COUNT(CASE WHEN .inc_cat_id = 1 THEN 1 END) AS cntSupport,
COUNT(CASE WHEN inc_cat_id = 2 THEN 1 END) AS cntPM,
COUNT(CASE WHEN inc_cat_id = 3 THEN 1 END) AS cntInstallation
FROM #incident
GROUP BY item_ID

UPDATE:
With the addition of logged_date column, you just need to add it in the ON clause. You need to use sp_executesql instead of EXEC now to prevent sql injection:
DECLARE #fromDate DATE = '2016-04-01',
#toDate DATE = '2016-04-30';
DECLARE #sql NVARCHAR(MAX) = N'';
SELECT #sql =
'SELECT
i.item_id' + CHAR(10) +
(SELECT
' , COUNT(CASE WHEN inc.inc_cat_id = ' + CONVERT(VARCHAR(10), inc_cat_id) +
' THEN 1 END) AS ' + QUOTENAME('count(' + inc_cat_n + ')') + CHAR(10)
FROM #Inc_cat
ORDER BY inc_cat_id
FOR XML PATH('')
) +
'FROM #item AS i
LEFT JOIN #incident AS inc
ON i.item_id = inc.item_id
AND inc.date_logged BETWEEN #fromDate AND #toDate
GROUP BY i.item_id;';
PRINT (#sql);
EXEC sp_executesql
#sql,
N'#fromDate DATE, #toDate DATE',
#fromDate,
#toDate
ONLINE DEMO
Giorgos answer is good if you only have that 3 Inc_cats. However, if you have unknown number of Inc_cats, you need to do it dynamically. Here is a method using a dynamic crosstab:
DECLARE #sql NVARCHAR(MAX) = N'';
SELECT #sql =
'SELECT
i.item_id' + CHAR(10) +
(SELECT
' , COUNT(CASE WHEN inc.inc_cat_id = ' + CONVERT(VARCHAR(10), inc_cat_id) +
' THEN 1 END) AS ' + QUOTENAME('count(' + inc_cat_n + ')') + CHAR(10)
FROM Inc_cat
ORDER BY inc_cat_id
FOR XML PATH('')
) +
'FROM Item AS i
LEFT JOIN Incident AS inc
ON i.item_id = inc.item_id
GROUP BY i.item_id;';
PRINT (#sql);
EXEC (#sql);
ONLINE DEMO
Basically, it's just a dynamic version of Giorgos' answer.

Related

Convert SQL Query to Table Using Pivot Table

Can I know how can I modify the SQL Query below so that the result will turn into expected result using pivot table where () in For MonthYear = Start Date and End Date selected by user ?
SQL Query : SQL Query Result (Image)
Expected Result : Table (Image)
SELECT * FROM
(
SELECT WR.Work_Type,
LEFT(DATENAME(MONTH,WR.Request_Date),3)+' '+STR(YEAR(WR.Request_Date),4) AS MonthYear,
CONVERT(INT,STR(YEAR(WR.Request_Date),4)+REPLACE(STR(MONTH(WR.Request_Date),2),' ','0') ) AS MonthYearOrder,
COUNT(ISNULL(WR.Request_ID,0)) AS Total
FROM tblWork_Request WR
INNER JOIN vWorkTypeByPropertyLevel WT ON WR.CoID=WT.CoID AND WR.Work_Type=WT.Work_Type AND WT.IsWorkRequest=1
LEFT JOIN [UBERIQ_1.5_Property].[dbo].[tblProperty] P ON WR.Property_ID=P.Property_ID
WHERE WR.CoID='59' and WR.Property_ID='MCST001'
GROUP BY WR.Work_Type,LEFT(DATENAME(MONTH,WR.Request_Date),3)+' '+STR(YEAR(WR.Request_Date),4),
CONVERT(INT,STR(YEAR(WR.Request_Date),4)+REPLACE(STR(MONTH(WR.Request_Date),2),' ','0') )
) t
pivot (
SUM(Total)
FOR MonthYear IN ()
) as pivot_table
Result :-
Work_Type | MonthYear | MonthYearOrder | Total
----------------------------------------------------------
A.1 Problem Code 1 | Jun 2020 | 202006 | 3
AHU faulty | Nov 2020 | 202011 | 6
AirCondition | May 2020 | 202005 | 1
Exterior | Jun 2020 | 202006 | 2
Expected Result :-
Work Type | Jan 2020 | Feb 2020 | Mar 2020 | Apr 2020 | May 2020 | Jun 2020 | Jun 2020 | Jul 2020 | Aug 2020 | Sep 2020 | Oct 2020 | Nov 2020 | Dec 2020 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------
A.1 Problem Code 1 | 0 | 0 | 0 | 0 | 0 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
AHU Faulty | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 | 0 |
AirCondition | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Exterior | 0 | 0 | 0 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Please advise. Thank you.
--Variables for dynamic columns and query
DECLARE #columns NVARCHAR(MAX) = ''
DECLARE #ifnullcolumns NVARCHAR(MAX) = ''
DECLARE #query NVARCHAR(MAX)
;WITH months(YearMonth) AS
(
SELECT 202001
UNION ALL
SELECT YearMonth + 1
FROM months
WHERE YearMonth < 202012
)
SELECT
#columns += QUOTENAME(YearMonth) + ',',
#ifnullcolumns += 'ISNULL(' + QUOTENAME(YearMonth) + ', 0) AS ' + QUOTENAME(YearMonth)+','
FROM
months
-- remove last , from the column list
SET #columns = LEFT(#columns, LEN(#columns) - 1)
SET #ifnullcolumns = LEFT(#ifnullcolumns, LEN(#ifnullcolumns) - 1)
-- Building dynamic query with pivot
SET #query ='
SELECT Work_Type, '+ #ifnullcolumns +' FROM (
select Work_Type, MonthYerOrder, Total
from Sample) t
PIVOT(
SUM(Total)
FOR MonthYerOrder IN ('+ #columns +')
) AS pivot_table;'
EXECUTE sp_executesql #query
Result of this query: http://sqlfiddle.com/#!18/96333/63

order by a string as number sql

I have a table like follows, i need to pivot the table with weeks. i could able to create pivot table but the columns order getting shuffled since im ordering a string column. and getting output like 3nd table.
MRN Weeks
--------------------------------
GIRFTR1H0461081 52
GIRFTR1H0461083 5 - 6
GIRFTR1H0461084 0 - 1
GIRFTR1H0461085 1 - 2
GIRFTR1H0461086 11 - 12
GIRFTR1H0461087 1 - 2
I need to get output as a pivot table order by Weeks Like,
MRN | 0 - 1 | 1 - 2 | 5 - 6 |11 - 12| 52
---------------------------------------------------------------
GIRFTR1H0461081 | 0 | 0 | 0 | 0 | 0
GIRFTR1H0461083 | 0 | 0 | 1 | 0 | 0
GIRFTR1H0461084 | 1 | 0 | 0 | 0 | 0
GIRFTR1H0461085 | 0 | 1 | 0 | 0 | 0
GIRFTR1H0461086 | 0 | 0 | 0 | 1 | 0
GIRFTR1H0461087 0 | 1 | 0 | 0 | 0
When i tried to create this table, the columns order getting shuffled like bellow,
MRN | 0 - 1 | 1 - 2 |11 - 12| 5 - 6 | 52
---------------------------------------------------------------
GIRFTR1H0461081 | 0 | 0 | 0 | 0 | 0
GIRFTR1H0461083 | 0 | 0 | 0 | 1 | 0
GIRFTR1H0461084 | 1 | 0 | 0 | 0 | 0
GIRFTR1H0461085 | 0 | 1 | 0 | 0 | 0
GIRFTR1H0461086 | 0 | 0 | 1 | 0 | 0
GIRFTR1H0461087 | 0 | 1 | 0 | 0 | 0
My code is,
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME(WeeksBand)
from [test_Tbl]
group by WeeksBand
order by WeeksBand
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT CurrentSpecialty,' + #cols + ' from
(
select CurrentSpecialty, WeeksBand, EncounterId
from [Tbl_Current_PTL_Test]
) x
pivot
(
count(EncounterId)
for WeeksBand in (' + #cols + ')
) p '
execute(#query);
Please help me to sort out my issue,
It's doing a string (alpha) sort, and you actually want to be doing a numeric sort.
I'd change the order line from:
order by WeeksBand
to:
order by Cast(SubString(WeeksBand, 1, CASE WHEN CHARINDEX('-', WeeksBand) != 0 THEN CHARINDEX('-', WeeksBand) -1 ELSE LEN(WeeksBand) END) AS INT)
which will convert the first number into an integer value (not string) and then should order correctly.
I don't see why a standard pivot query should not work here. From your expected output it seems you want a 1 as a placeholder for an MRN entry which is present in that week, and 0 otherwise.
SELECT [MRN],
MAX(CASE WHEN [Weeks] = '0 - 1' THEN 1 ELSE 0 END) AS [0 - 1],
MAX(CASE WHEN [Weeks] = '1 - 2' THEN 1 ELSE 0 END) AS [1 - 2],
MAX(CASE WHEN [Weeks] = '5 - 6' THEN 1 ELSE 0 END) AS [5 - 6],
MAX(CASE WHEN [Weeks] = '11 - 12' THEN 1 ELSE 0 END) AS [11 - 12],
MAX(CASE WHEN [Weeks] = '52' THEN 1 ELSE 0 END) AS [52]
FROM [yourTable]
GROUP BY [MRN]
You should format Week field of first table using 2 digits as:
MRN Weeks
--------------------------------
GIRFTR1H0461081 52
GIRFTR1H0461083 05 - 06
GIRFTR1H0461084 00 - 01
GIRFTR1H0461085 01 - 02
GIRFTR1H0461086 11 - 12
GIRFTR1H0461087 01 - 02

Need a select query to get the output as shown below.?

I Have a SQL Table as shown below,
| Loc | Date | Id | Sts |
-------------------------
| Hyd | 15-01-2016 | 1 | A |
| Vjd | 16-01-2016 | 2 | B |
| Viz | 15-01-2016 | 3 | C |
| Hyd | 15-03-2016 | 4 | A |
| Vjd | 15-03-2016 | 5 | B |
| Viz | 15-03-2016 | 6 | C |
| Hyd | 15-03-2016 | 4 | A |
| Vjd | 15-05-2016 | 5 | B |
| Viz | 15-05-2016 | 6 | C |
And i need output like,
**| Loc | Jan-16 | Mar-16 | May-16 |**
**|-------|A |B |C |A |B |C |A |B |C |**
----------
|Hyd | 1 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 |
|Vjd | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 |
|Viz | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
Can anyone help me out please..
Thanks in Advance.
You will basically need to aggregate based on CASE statements, like this:
DECLARE #table TABLE (loc VARCHAR(3), [date] DATE, id INT, sts CHAR(1));
INSERT INTO #table SELECT 'Hyd', '20160115', 1, 'A';
INSERT INTO #table SELECT 'Vjd', '20160116', 2, 'B';
INSERT INTO #table SELECT 'Viz', '20160115', 3, 'C';
INSERT INTO #table SELECT 'Hyd', '20160315', 4, 'A';
INSERT INTO #table SELECT 'Vjd', '20160315', 5, 'B';
INSERT INTO #table SELECT 'Viz', '20160315', 6, 'C';
INSERT INTO #table SELECT 'Hyd', '20160315', 4, 'A';
INSERT INTO #table SELECT 'Vjd', '20160515', 5, 'B';
INSERT INTO #table SELECT 'Viz', '20160515', 6, 'C';
SELECT
loc,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 1 AND sts = 'A' THEN 1 END) AS Jan_A,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 1 AND sts = 'B' THEN 1 END) AS Jan_B,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 1 AND sts = 'C' THEN 1 END) AS Jan_C,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 3 AND sts = 'A' THEN 1 END) AS Mar_A,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 3 AND sts = 'B' THEN 1 END) AS Mar_B,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 3 AND sts = 'C' THEN 1 END) AS Mar_C,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 5 AND sts = 'A' THEN 1 END) AS May_A,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 5 AND sts = 'B' THEN 1 END) AS May_B,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 5 AND sts = 'C' THEN 1 END) AS May_C
FROM
#table
GROUP BY
loc;
Results:
loc Jan_A Jan_B Jan_C Mar_A Mar_B Mar_C May_A May_B May_C
Hyd 1 0 0 2 0 0 0 0 0
Viz 0 0 1 0 0 1 0 0 1
Vjd 0 1 0 0 1 0 0 1 0

SQL Query to make dynamic columns

Hi I have a query that gets the total count of incidents per month and year
I wanted a result that shows all distinct years in columns
Could you please advise how to make this query dynamic?
Expected Result:
Month 2013 2014 2015
January 8 0 12
February 9 6 10
March 12 1 9
April 10 13 27
May 9 22 15
June 27 4 20
July 15 12 22
August 20 2 2
September 22 5 10
October 10 8 12
November 0 7 0
December 0 15 0
Query
select DATENAME(MONTH,DateOpened) as Month,
sum(case when year(DateOpened) = '2015' then 1 else 0 end) as [2015],
sum(case when year(DateOpened) = '2014' then 1 else 0 end) as [2014]
from Incidents
group by DATENAME(MONTH,DateOpened), MONTH(DateOpened)
order by MONTH(DateOpened)
Thanks for your help!
You can use the PIVOT table operator instead, something like this:
SELECT *
FROM
(
SELECT
DATENAME(MONTH,DateOpened) as Month,
DATENAME(Year,DateOpened) AS Year,
DateOpened
FROM Incidents
) AS t
PIVOT
(
COUNT(DateOpened)
FOR Year IN([2013], [2014], [2015])
) AS p;
SQL Fiddle Demo
If you don't need to write the list of years and do it dynamically for any year, you have to use dynamic SQL to run the query dynamically, like this:
DECLARE #cols AS NVARCHAR(MAX);
DECLARE #query AS NVARCHAR(MAX);
SELECT #cols = STUFF((SELECT distinct ',' +
QUOTENAME(DATENAME(Year,DateOpened))
from Incidents AS t
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '');
SELECT #query = 'SELECT * , '+ #cols + '
FROM
(
select
DATENAME(MONTH,DateOpened) as Month,
DATENAME(Year,DateOpened) AS Year,
DateOpened
FROM Incidents
) AS t
PIVOT
(
COUNT(DateOpened)
FOR Year IN(' + #cols + ')' +
') p';
execute(#query);
SQL Fiddle Demo
This will give you something like this:
| Month | 2014 | 2015 | 2014 | 2015 |
|-----------|------|------|------|------|
| April | 0 | 3 | 0 | 3 |
| August | 1 | 1 | 1 | 1 |
| December | 1 | 0 | 1 | 0 |
| February | 0 | 1 | 0 | 1 |
| July | 1 | 1 | 1 | 1 |
| September | 1 | 0 | 1 | 0 |
Note that: Both the queries won't list any month that has no dates on the original table. If you want to list any month that is not listed on the table with counts 0, you have to modify the anchor query so that it lists all the months even if not listed:
SELECT
m.Name as Month,
i.Year,
i.DateOpened
FROM
(
VALUES ('Janurary'), ('February'), ('March'),
('April'), ('May'), ('June'),
('July'), ('August'), ('September'),
('October'), ('November'), ('December')
) AS m(Name)
LEFT JOIN
(
SELECT
DATENAME(MONTH,DateOpened) as Month,
DATENAME(Year,DateOpened) AS Year,
DateOpened
FROM Incidents
) AS i ON i.Month = m.Name
and replace it in the dynamic query.
Updated SQL Fiddle
This will give you the missing months with zeros results:
| Month | 2014 | 2015 | 2014 | 2015 |
|-----------|------|------|------|------|
| Janurary | 0 | 0 | 0 | 0 | <<
| February | 0 | 1 | 0 | 1 |
| March | 0 | 0 | 0 | 0 | <<
| April | 0 | 3 | 0 | 3 |
| May | 0 | 0 | 0 | 0 | <<
| June | 0 | 0 | 0 | 0 | <<
| July | 1 | 1 | 1 | 1 |
| August | 1 | 1 | 1 | 1 |
| September | 1 | 0 | 1 | 0 |
| October | 0 | 0 | 0 | 0 | <<
| November | 0 | 0 | 0 | 0 | <<
| December | 1 | 0 | 1 | 0 |
A simple create table something like this..
CREATE TABLE #tmpIncidents
(
IncidentName NVARCHAR(50)
, DateOpened DATETIME
)
INSERT INTO #tmpIncidents (IncidentName,DateOpened) VALUES
('Test1',GETDATE()),
('Test1',DATEADD(YEAR,-1,GETDATE())),
('Test1',DATEADD(YEAR,-2,GETDATE())),
('Test1',DATEADD(YEAR,2,GETDATE())),
('Test1',DATEADD(YEAR,1,GETDATE())),
('Test1',DATEADD(YEAR,3,GETDATE()))
Then a build up query for multiple years
DECLARE #columnVar NVARCHAR(4000)
SELECT #columnVar =
(SELECT DISTINCT
'[' + CONVERT(NVARCHAR(150),DATEPART(YEAR,DateOpened)) + '],' AS [text()]
FROM #tmpIncidents
FOR XML PATH('')
)
SET #columnVar = (SELECT LEFT(#columnVar,LEN(#columnVar)-1))
--SELECT #columnVar --so you can see how it looks..
Then execute your query.
EXEC ('
SELECT
pv.*
FROM
(
SELECT DATENAME(MONTH,DateOpened) AS [DateName], DATEPART(YEAR,DateOpened) AS [YEAR], IncidentName FROM #tmpIncidents
) src
PIVOT
(
COUNT(IncidentName)
FOR [YEAR] IN (' + #columnVar + ')
) pv;
')

Pivot values of field based on another field in TSQL / Microsoft SQL Server

Alright here is a table I have with example data. There is a unique key for itemCode and dateType, so no itemCode can have repeating dateType, and vice versa. Also dateStatus is a bit datatype.
+----+----------+----------+------------+-----------+--------------+
| id | itemCode | dateType | dateStatus | dateDue | dateComplete |
+----+----------+----------+------------+-----------+--------------+
| 1 | 501 | aa | 1 | 1/1/2014 | 5/1/2014 |
| 2 | 501 | bb | 0 | 1/2/2014 | |
| 3 | 501 | cc | 0 | 1/3/2014 | |
| 4 | 602 | aa | 0 | 1/4/2014 | |
| 5 | 602 | bb | 1 | 1/5/2014 | 5/5/2014 |
| 6 | 602 | cc | 0 | 1/6/2014 | |
| 7 | 602 | dd | 0 | 1/7/2014 | |
| 8 | 703 | bb | 0 | 1/8/2014 | |
| 9 | 703 | cc | 0 | 1/9/2014 | |
| 10 | 703 | dd | 1 | 1/10/2014 | 5/10/2014 |
+----+----------+----------+------------+-----------+--------------+
I'm trying to pivot the dateType against the itemCode and have the value be either dateComplete or dateDue, depending on if the dateStatus is 0 or 1.
+----------+---------------+---------------+---------------+----------------+
| itemCode | aa | bb | cc | dd |
+----------+---------------+---------------+---------------+----------------+
| 501 | fin: 5/1/2014 | due: 1/2/2014 | due: 1/3/2014 | |
| 602 | due: 1/4/2014 | fin: 5/5/2014 | due: 1/6/2014 | due: 1/7/2014 |
| 703 | | due: 1/8/2014 | due: 1/9/2014 | fin: 5/10/2014 |
+----------+---------------+---------------+---------------+----------------+
I can do this easily in Access Query CrossTab and the SQL results in
TRANSFORM First(Switch([dateStatus]=0,"due: " & [dateDue],
[dateStatus]=1,"fin: " & [dateComplete])) AS Expr1
SELECT Table1.itemCode
FROM Table1
GROUP BY Table1.itemCode
PIVOT Table1.dateType In ("aa","bb","cc","dd");
But I cannot figure out how to build this query in TSQL / Microsoft SQL server management studio. I can get some basic pivots done, but aggregate functions seem to not pull values, so I'm thinking the query may be a combination of pivots, joins and case statements. Or maybe I'm over complicating things.
Any code drop for reference would be very helpful. Thanks.
Try this out. It's a little simpler than the accepted answer and it's a little more scalable like lets say another dateType is added. All you would have to do is add value in the pivot instead of a whole new case statement.
DECLARE #pivotCols VARCHAR(MAX);
SELECT #pivotCols = COALESCE(#pivotCols + ',','') + QUOTENAME(dateType)
FROM #yourTable
GROUP BY dateType
EXEC
(
'SELECT *
FROM
(
SELECT itemCode,
CASE
WHEN dateStatus = 1
THEN ''fin: '' + CAST(dateComplete AS CHAR(10))
ELSE ''due: '' + CAST(dateDue AS CHAR(10))
END dateDue,
dateType
FROM #yourTable
) A
PIVOT
(
MAX(dateDue) FOR dateType IN (' + #pivotCols + ')
) pvt'
)
One option is to use conditional aggregation to pivot your results, but you also have to use case to pick the correct date:
select itemcode,
max(case when dateType = 'aa' and dateStatus = 0
then 'due:' + cast(dateDue as varchar(20))
when dateType = 'aa' and dateStatus = 1
then 'fin:' + cast(dateComplete as varchar(20))
end) as aa,
max(case when dateType = 'bb' and dateStatus = 0
then 'due:' + cast(dateDue as varchar(20))
when dateType = 'bb' and dateStatus = 1
then 'fin:' + cast(dateComplete as varchar(20))
end) as bb,
max(case when dateType = 'cc' and dateStatus = 0
then 'due:' + cast(dateDue as varchar(20))
when dateType = 'cc' and dateStatus = 1
then 'fin:' + cast(dateComplete as varchar(20))
end) as cc,
max(case when dateType = 'dd' and dateStatus = 0
then 'due:' + cast(dateDue as varchar(20))
when dateType = 'dd' and dateStatus = 1
then 'fin:' + cast(dateComplete as varchar(20))
end) as dd
from Table1
group by itemCode
SQL Fiddle Demo
Another option is to use the pivot command, but you'll still need to use a case statement in your subquery to create the correct values with dates.