Solved

SQL Query Syntax Cross Join

Posted on 2012-03-14
9
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 41

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:Scott Pletcher
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:rwheeler23
ID: 37725244
Thanks for the tip!!!
0
 

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:Scott Pletcher
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:
Scott Pletcher 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

627 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