Solved

SQL Syntax mulitple count columns

Posted on 2012-03-13
2
307 Views
Last Modified: 2012-03-13
I am trying to create a query that will list each item and then the number of times this item was sold with two yearly count columns. The first column needs to be the year 2011 and the second column needs to be 2012. How do I restructure this query to give the correct yearly totals. This script is close but for each line it repeats the same data 3 times.


select y1.itemnmbr,y1.ITEMDESC,y1.NumberPerYear as '2011',y2.NumberPerYear as '2012'
from SOP30300 sl
inner join SOP30200 sh on sl.SOPNUMBE=sh.SOPNUMBE and sl.SOPTYPE=sh.SOPTYPE
inner join
(
      select itemnmbr,itemdesc,COUNT(itemnmbr) as NumberPerYear
      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)=2011
      group by ITEMNMBR,ITEMDESC
) y1 on sl.itemnmbr= y1.itemnmbr
inner join
(
      select itemnmbr,COUNT(itemnmbr) as NumberPerYear
      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)=2012
      group by ITEMNMBR,ITEMDESC
) y2 on sl.itemnmbr= y2.itemnmbr
where custnmbr='195' and sh.SOPTYPE=3
0
Comment
Question by:rwheeler23
2 Comments
 
LVL 25

Accepted Solution

by:
jogos earned 500 total points
Comment Utility
You can do that with only joining the table once

      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

Open in new window


Good reflex to doubt a sollution that has a repeating pattern.
0
 

Author Closing Comment

by:rwheeler23
Comment Utility
Excellent solution and advice. Thank you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

8 Experts available now in Live!

Get 1:1 Help Now