SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Full Outer Join


Full Outer Join

Author
Message
sharonsql2013
sharonsql2013
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1296 Visits: 1265
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]
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42424 Visits: 19838
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.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Nevyn
Nevyn
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3554 Visits: 3149
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.
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18047 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search