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

Full Outer Join Expand / Collapse
Author
Message
Posted Friday, November 8, 2013 10:43 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 3:28 PM
Points: 214, Visits: 626
The Input data looks like that :
Date Productions
11/1/2013 500
11/30/2013 100
10/1/2013 10
10/20/2013 50
11/1/2012 1
11/20/2012 5


Current Month Previous Month Previous Year
500 10 1
100 50 5



I am trying to use Full outer join but some how its chaninging the grain .
Any ideas.


CREATE TABLE [dbo].[D](
[ProdDate] [smalldatetime] NULL,
[Productions] [varchar](20) NULL
) ON [PRIMARY]

Post #1512764
Posted Friday, November 8, 2013 11:06 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: Yesterday @ 11:22 PM
Points: 3,325, Visits: 7,174
Why are you storing productions as varchar? Do you expect non numeric values?
How do you compare results for previous month and previous year?
You should review your requirements for the report, it makes no sense if the amount of rows for current month, previous month and previous year don't match.



Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1512771
Posted Saturday, November 9, 2013 9:23 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Yesterday @ 1:06 PM
Points: 605, Visits: 2,107
not sure from that explanation what exactly you want.

But a note on outer joins that they will be overridden if you filter on an "outer" table in the where clause. So for a full outer join, you either want all your filtering in the ON, or you want to define the outer join in a derived table/cte, then select from it with a where clause.
Post #1512916
Posted Sunday, November 10, 2013 6:02 PM


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: Yesterday @ 6:03 PM
Points: 3,609, Visits: 5,221
I am not sure if this is what you're trying to do or not but I don't think you need a FULL JOIN for this:

CREATE TABLE #D(
[ProdDate] [smalldatetime] NULL,
[Productions] [varchar](20) NULL
);

INSERT INTO #D
SELECT '11/1/2013','500'
UNION ALL SELECT '11/20/2013','100' -- Changed so day matches others
UNION ALL SELECT '10/1/2013','10'
UNION ALL SELECT '10/20/2013','50'
UNION ALL SELECT '11/1/2012','1'
UNION ALL SELECT '11/20/2012','5';

SELECT
CurrentMonth=SUM(
CASE WHEN MONTH(ProdDate) = MONTH(GETDATE()) AND
YEAR(ProdDate) = YEAR(GETDATE())
THEN CAST(Productions AS INT) END)
,PriorMonth=SUM(
CASE WHEN MONTH(ProdDate) = MONTH(GETDATE())-1 AND
YEAR(ProdDate) = YEAR(GETDATE())
THEN CAST(Productions AS INT) END)
,PriorYear=SUM(
CASE WHEN MONTH(ProdDate) = MONTH(GETDATE()) AND
YEAR(ProdDate) = YEAR(GETDATE())-1
THEN CAST(Productions AS INT) END)
FROM #D
GROUP BY DAY(ProdDate)

GO
DROP TABLE #D;


Note that I changed one row of your sample data (see comment).



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 #1512992
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse