Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Syntax mulitple count columns

Posted on 2012-03-13
2
Medium Priority
?
315 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
[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
2 Comments
 
LVL 25

Accepted Solution

by:
jogos earned 2000 total points
ID: 37717611
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
ID: 37717851
Excellent solution and advice. Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

722 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