Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Adding Zero Expand / Collapse
Author
Message
Posted Friday, October 11, 2013 1:51 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, November 8, 2013 9:24 AM
Points: 16, Visits: 23
Hi Dwain,

I did have a go at your attempt, however if I want to be able to use this on a monthly basis i would have to do the manual insert statement every time??

James
Post #1503905
Posted Friday, October 11, 2013 2:10 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:18 AM
Points: 3,417, Visits: 5,332
james.ingamells (10/11/2013)
Hi Dwain,

I did have a go at your attempt, however if I want to be able to use this on a monthly basis i would have to do the manual insert statement every time??

James


Not sure what you mean by the manual insert as mine doesn't do any inserts.

I took your original code and embedded it in the first CTE. You should uncomment that and use it instead of the SELECT/UNION ALL SELECT query inside that CTE (you said that's what your query produced).

The rest should stand pretty much on its own.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1503911
Posted Friday, October 11, 2013 2:31 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, November 8, 2013 9:24 AM
Points: 16, Visits: 23
HI Dwain,

Sorry to be a pain, but i am struggling to make this work and i know its probably quite simple. Here is what i have changed and the results attached it produces:

WITH StartingPoint AS
(
SELECT
count([Unit]) as Actual,
unit,
[1st_of_month],
last_of_month
FROM [lchs_live].[dbo].[Full_Referrals_Dataset_live]
where RfR1 =
'18 month project'
group by unit,
[1st_of_month],
[last_of_month]
--union all

--SELECT Actual=6,unit='NW',[1st_of_month]='2013-08-01',last_of_month='2013-08-31'
--UNION ALL SELECT 4,'SE','2013-08-01','2013-08-31'
--UNION ALL SELECT 5,'SW','2013-08-01','2013-08-31'
),
Units (unit) AS
(
SELECT 'NW' UNION ALL SELECT 'SE' UNION ALL SELECT 'SW' UNION ALL SELECT 'NE'
)
SELECT Actual=ISNULL(Actual, 0), b.unit, a.[1st_of_month], a.last_of_month
FROM
(
SELECT DISTINCT [1st_of_month], last_of_month
FROM StartingPoint a
) a
CROSS JOIN Units b
LEFT JOIN StartingPoint c ON b.unit = c.unit
Post #1503913
Posted Friday, October 11, 2013 3:32 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:18 AM
Points: 3,417, Visits: 5,332
That looks like my query but I don't see any results attached.

BTW. There is the possibility that mine might not return what you expect if your base query returns more projects than in your sample data.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1503926
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse