[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 122
  • Last Modified:

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
0
al4629740
Asked:
al4629740
  • 3
  • 3
  • 2
1 Solution
 
Brian CroweCommented:
What is the content of the monthly columns in your output since they don't seem to match your data provided?
0
 
Brian CroweCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now