Multiple COUNT functions using one SELECT

Posted on 2010-01-08
Last Modified: 2013-11-05
Does anyone know how to get two COUNT functions working on one SELECT statement?  I need to be able to count the total number of items a location has and the total times those items have been used in one query so I can chart the results through SSRS using a bar chart.  The chart can only accept one dataset, so the counts have to exist in the same query.

I've tried putting counts on selects using inner joins, and as subqueries of the initial select statement but haven't had any luck yet.

Question by:scross1276
    LVL 10

    Expert Comment

    by:Rakesh Jaimini
    you can use UNION or JOINS to get the required results

    if you provide some sample data with required output then query can b formed
    LVL 11

    Expert Comment

    by:Goodangel Matope
    It will be easier if we know what the table structures are like. Please put even a simplified structure showing the structure of the tables.

    Author Comment

    Ugh, as luck would have it, you guys are willing to help and now I have to run to a 3.5 hour meeting.  I'll post the info you're asking for when I get out of the meeting this afternoon.  I'm VERY sorry for the delay and GREATLY appreciate your quick response!!  The other information will be posted asap.
    LVL 41

    Accepted Solution

    if you're in SQL 2008, you can take advantage of the OVER clause. Check the query I've posted below (please note that I'm assuming your column names)
    More info on the OVER clause here:

    select 	distinct item
    	count(item) over (partition by locationcolumn) countlocation,
    	count(item) over (partition by usedcolumn) countused
    from yourtable

    Open in new window

    LVL 5

    Expert Comment

    Based on your original description, here is a direction you could go...

       (select ItemName,count(*) as TotalCount from SomeTable where <predicate for total> group by ItemName) TotalItems
       inner join (select ItemName,count(*)as UsedCount from SomeTable where <predicate for used> group by ItemName) UsedItems
          on UsedItems.ItemName = TotalItems.ItemName

    Open in new window


    Author Closing Comment

    I have never heard of the OVER function before.  It worked perfectly!  Thanks so much!!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    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

    Hi, I have heard from my friends that it’s not possible to create Label Printing report using SSRS. I am amazed after hearing this words not possible in SSRS. I googled lot and found that it is possible to some of people know about the Report Bui…
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    732 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

    17 Experts available now in Live!

    Get 1:1 Help Now