Solved

SQL Query Syntax Cross Join

Posted on 2012-03-14
9
361 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

20 Experts available now in Live!

Get 1:1 Help Now