al4629740
asked on
Query the data and format the results differently
I am querying the data in tblOrgMonthlyReport with the following query
My Output looks like this:
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
select AgencyID, Month, Finished from tblOrgMonthlyReport order by Agencyid, Month
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
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
What is the content of the monthly columns in your output since they don't seem to match your data provided?
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
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')
ASKER
I am nervous to use this code as it looks like it is modifying my DB...
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.
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.
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
results
Getting away from what I was looking for.
(No column name)
[1-Jan], [2-Feb], [5-May], [6-Jun], [7-Jul], [8-Aug]
Getting away from what I was looking for.