Solved

SQL Query Syntax Cross Join

Posted on 2012-03-14
9
360 Views
Last Modified: 2012-06-27
With previous help from the experts I have this working query. It simply counts the number of orders per year for a customer. Now I have been ask to add a second set of columns that will be the identical set of data only for a second customer. They are looking to see a side by side comparison of two customers for the same period of time. I believe a cross join is going to be in order because customer A may or may not order the same items as customer B and vice versa. So the new query will also have to have the custnmbr field as part of the group. How do you restructure this query to list

Item Numb, Desc, Cust Count #1 1stYear, Cust Count #2 1st Year,Cust Count #2 2nd Year,Cust Count #2 2nd Year



      select itemnmbr,itemdesc
      , sum(case when YEAR(sh.docdate)=2011 then 1 else 0 end ) as NumberPerYear2011
      , sum(case when YEAR(sh.docdate)=2012 then 1 else 0 end ) as NumberPerYear2012
      from SOP30300 sl
      inner join SOP30200 sh on sl.SOPNUMBE=sh.SOPNUMBE and sl.SOPTYPE=sh.SOPTYPE
      where custnmbr='195' and sh.SOPTYPE=3 and YEAR(sh.docdate) in (2011,2012)
      group by ITEMNMBR,ITEMDESC
                                            1:
0
Comment
Question by:rwheeler23
  • 5
  • 3
9 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 37722921
You can try like this.

;with Customer1 as (
    select itemnmbr,itemdesc
      , sum(case when YEAR(sh.docdate)=2011 then 1 else 0 end ) as NumberPerYear2011
      , sum(case when YEAR(sh.docdate)=2012 then 1 else 0 end ) as NumberPerYear2012
      from SOP30300 sl
      inner join SOP30200 sh on sl.SOPNUMBE=sh.SOPNUMBE and sl.SOPTYPE=sh.SOPTYPE
      where custnmbr='195' and sh.SOPTYPE=3 and YEAR(sh.docdate) in (2011,2012)
      group by ITEMNMBR,ITEMDESC),

Customer2 as (
    select itemnmbr,itemdesc
      , sum(case when YEAR(sh.docdate)=2011 then 1 else 0 end ) as NumberPerYear2011
      , sum(case when YEAR(sh.docdate)=2012 then 1 else 0 end ) as NumberPerYear2012
      from SOP30300 sl
      inner join SOP30200 sh on sl.SOPNUMBE=sh.SOPNUMBE and sl.SOPTYPE=sh.SOPTYPE
      where custnmbr='196' and sh.SOPTYPE=3 and YEAR(sh.docdate) in (2011,2012)
      group by ITEMNMBR,ITEMDESC)
select *
  from Customer1 c1
  full join Customer1 c2
    on c1.itemnmbr = c2.itemnmbr and c1.itemdesc = c2.itemdesc
0
 

Author Comment

by:rwheeler23
ID: 37723116
What does the

;with   do? Where can I find documentation on this structure?

This works. Thanks.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 37725141
One note just for general info:

YEAR(sh.docdate) in (2011,2012)

should be replaced with:

sh.docdate >= '20110101' AND sh.docdate < '20130101'

The second style allows an index with docdate to be used, whereas the first style does not.

When you use a function on any column, SQL can no longer use an index to search for the value.
0
 

Author Comment

by:rwheeler23
ID: 37725244
Thanks for the tip!!!
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:rwheeler23
ID: 37725275
I just realized those years may not always be consectutive. That is why I used the 'IN()'
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 37725469
For years 2010 and 2010:

((sh.docdate >= '20100101' AND sh.docdate < '20110101') OR
 (sh.docdate >= '20120101' AND sh.docdate < '20130101'))
0
 

Author Comment

by:rwheeler23
ID: 37725998
That is true. Good point. The time portion does not cause a problem with this? I remember writing some code and I set the begin date to the same date as the end date and it would not return any results. It was only when there was at least a one day difference would it return anything.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 37726315
Good point about the time portion -- that can be a real pain when dealing with datetime.

It does not cause an issue with the specific style I used --
because note that I am using < the NEXT day, rather than <= the ending day.

That is, instead of this:

sh.docdate BETWEEN '20110101' AND '20111231 23:59:59.997' --every day and time in 2011

I use this:

sh.docdate >= '20110101' AND sh.docdate < '20120101' --every day and time in 2011

That way it continues to work properly even if the datatype of docdate is changed to datetime2 (or smalldatetime).
0
 

Author Closing Comment

by:rwheeler23
ID: 37731401
Top notch answer. Thank you.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now