Query the data and format the results differently

I am querying the data in tblOrgMonthlyReport with the following query

select AgencyID, Month, Finished from tblOrgMonthlyReport order by Agencyid, Month

Open in new window


My Output looks like this:

AgencyID	Month	Finished
2	2-February	0
2	7-July    	0
4	1-January 	0
4	7-July    	0
7	1-January 	0
74	1-January 	0
74	2-February	NULL
74	5-May     	0
74	6-June    	0
74	7-July    	1
74	8-August  	0

Open in new window



I would like to output my results to re-organize the months into columns and each row is the agency.  The Finished column would be the content of the reorganized table.  Is this even possible from the attached table.  For example

        1-January   2-February ......
2          1                    1
4           1                    0
7            1                    0
74           1                    1
tblOrgMonthlyReport.xlsx
al4629740Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
What is the content of the monthly columns in your output since they don't seem to match your data provided?
0
Brian CroweDatabase AdministratorCommented:
Here is a PIVOT example that might work depending on your logic for the aggregate columns:

CREATE TABLE #tblOrgMonthlyReport
(
	MonthlyID	INT,
	AgencyID	INT,
	[Month]		VARCHAR(10),
	Fiscal		INT,
	Finished	BIT DEFAULT(0)
	-- Lot of extra columns we don't need for this example
);

INSERT INTO #tblOrgMonthlyReport (MonthlyID, AgencyID, [Month], Fiscal, Finished)
VALUES (3, 74, '2-Feb', 2015, NULL),
	(4, 2, '2-Feb', 2015, 0),
	(5, 74, '7-Jul', 2015, 1),
	(6, 4, '7-Jul', 2015, 0),
	(7, 74, '6-Jun', 2015, 0),
	(8, 4, '1-Jan', 2015, 0),
	(9, 7, '1-Jan', 2015, 0),
	(10, 74, '1-Jan', 2015, 0),
	(11, 74, '5-May', 2015, 0),
	(12, 2, '7-Jul', 2015, 0),
	(13, 74, '8-Aug', 2015, 0);

SELECT AgencyID, [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 AgencyID, [Month], ISNULL(Finished, 0) AS Finished
	FROM #tblOrgMonthlyReport
	WHERE Fiscal = 2015
) AS Src
PIVOT
(
	COUNT(Finished)
	FOR [Month] IN ([1-Jan], [2-Feb], [3-Mar], [4-Apr], [5-May], [6-Jun], [7-Jul], [8-Aug], [9-Sep], [10-Oct], [11-Nov], [12-Dec])
) AS Pvt
ORDER BY Pvt.AgencyID

Open in new window

0
MlandaTCommented:
I agree with Brian Crowe, however, the limitation here is that in order for the code to work against any data from your table... you have to convert it into a dynamic SQL like so:

DROP TABLE #tblOrgMonthlyReport
GO
CREATE TABLE #tblOrgMonthlyReport
(
	MonthlyID	INT,
	AgencyID	INT,
	[Month]		VARCHAR(10),
	Fiscal		INT,
	Finished	BIT DEFAULT(0)
	-- Lot of extra columns we don't need for this example
);

INSERT INTO #tblOrgMonthlyReport (MonthlyID, AgencyID, [Month], Fiscal, Finished)
VALUES (3, 74, '2-Feb', 2015, NULL),
	(4, 2, '2-Feb', 2015, 0),
	(5, 74, '7-Jul', 2015, 1),
	(6, 4, '7-Jul', 2015, 0),
	(7, 74, '6-Jun', 2015, 0),
	(8, 4, '1-Jan', 2015, 0),
	(9, 7, '1-Jan', 2015, 0),
	(10, 74, '1-Jan', 2015, 0),
	(11, 74, '5-May', 2015, 0),
	(12, 2, '7-Jul', 2015, 0),
	(13, 74, '8-Aug', 2015, 0);

declare @flds varchar(max) = null
select @flds = isnull(@flds + ', ', '') + quotename([month]) 
from (
	select distinct [Month] 
	from #tblOrgMonthlyReport
) as Months 
order by [Month] --the order by here helps to order the data, but note that since Month above is a string, this is a character sort not a date sort

select @flds --just so you see the output here

exec('SELECT AgencyID, ' + @flds + '
FROM
(
	SELECT AgencyID, [Month], ISNULL(Finished, 0) AS Finished
	FROM #tblOrgMonthlyReport
	WHERE Fiscal = 2015 --these filters can be defined programmatically or you can create @variables and concatenate here
) AS Src
PIVOT
(
	COUNT(Finished)
	FOR [Month] IN (' + @flds + ')
) AS Pvt
ORDER BY Pvt.AgencyID')

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

al4629740Author Commented:
I am nervous to use this code as it looks like it is modifying my DB...
0
MlandaTCommented:
It's only creating temporary tables (and removing them, and SQL Server will also clear the temporary tables). You will notice that the table being used there is called "#tblOrgMonthlyReport"... the # tells you it's a temporary table.

I guess you have two choices:
1 - Create an empty database and run the code in there
2 - OR Follow the concepts provided and apply in your own structures

Thing is... Queries like this are a bit tricky to pull out of a hat - we need to create a working space that we can test the query before posting. We also don't want to have loads of dummy databases and tables left over when we assist with responding to questions. So temporary tables benefit everyone.
0
al4629740Author Commented:
The results are what I am looking for however after testing I get results that are inaccurate

AgencyID	1-Jan	2-Feb	5-May	6-Jun	7-Jul	8-Aug
2	0	1	0	0	1	0
4	1	0	0	0	1	0
7	1	0	0	0	0	0
74	1	1	1	1	1	1

Open in new window

0
MlandaTCommented:
Try changing COUNT(Finished) to SUM(Finished) in
PIVOT
(
	COUNT(Finished)
	FOR [Month] IN (' + @flds + ')
) AS Pvt

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
al4629740Author Commented:
results

(No column name)
[1-Jan], [2-Feb], [5-May], [6-Jun], [7-Jul], [8-Aug]

Open in new window


Getting away from what I was looking for.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.