x

Query to do an additional Count

Me again....

I'm writing a query which will total the COUNT returned in the subquery alias b. But I need to add another subquery which will COUNT based on lets say an alias c.

Here is what I have:

SELECT b.Employee , b.[Time In], b.[Time Out], COUNT(b.empnid) AS Orders
FROM (
         SELECT E.FIRSTNAME + ' ' + E.LASTNAME  AS Employee
                 , right('0' + ltrim(right(convert(varchar,  MIN(cast(LogInTime as time)), 100), 7)), 7)  AS [Time In]
                 , right('0' + ltrim(right(convert(varchar,  MAX(cast(LogOutTime as time)), 100), 7)), 7)  AS [Time Out]
                 , o.TicketNumber , o.ToCusNid , e.EmpNid 
         FROM DBO.SESSIONS AS S
              JOIN DBO.EMPLOYEES AS E ON E.EMPNID = S.EMPNID
              JOIN Orders AS O ON o.SlsEmpNid = e.EmpNid 
         WHERE CONVERT(DATE, S.LOGINTIME, 102) = '2011-03-08'  
                 AND CONVERT(date, o.OrderedDate, 102) = '2011-03-08'
         GROUP BY E.FirstName, e.LastName , o.TicketNumber , o.ToCusNid, e.EmpNid 
       ) b
GROUP BY b.Employee, b.[Time In], b.[Time Out]  

I would like to add as a subquery alias C

SELECT * FROM CustomerVisits 
WHERE CONVERT(DATE, VisitTime, 102) = '2011-03-08' 

And have a COUNT(c.cusnid) or something like that to give me an additional column count on the main select. I was trying to join alias C on employeeid but this was not working. the ORDERS count would increase and become incorrect.

Any suggestions how I can add an additional count to a subquery or how I should go about writing this?
more ▼

asked Mar 09, 2011 at 01:29 PM in Default

Noonies gravatar image

Noonies
1.2k 60 64 65

(comments are locked)
10|1200 characters needed characters left

2 answers: sort oldest

I stepped away from this and came back with a cleared head. I was able to add the alias C and join on empnid and visitedbynid and pull count through the alias subquery for c. This is taken care of. Thanks again.

SELECT 
  b.Employee , b.[Time In], b.[Time Out], COUNT(*) AS Orders, c.NONSERVICE 
FROM (
            SELECT E.FIRSTNAME + ' ' + E.LASTNAME  AS Employee
                        , right('0' + ltrim(right(convert(varchar,  MIN(cast(LogInTime as time)), 100), 7)), 7)  AS [Time In]
                        , right('0' + ltrim(right(convert(varchar,  MAX(cast(LogOutTime as time)), 100), 7)), 7)  AS [Time Out]
                        , o.TicketNumber , o.ToCusNid , e.EmpNid 
            FROM DBO.SESSIONS AS S
                    JOIN DBO.EMPLOYEES AS E ON E.EMPNID = S.EMPNID
                    JOIN Orders AS O ON o.SlsEmpNid = e.EmpNid 
            WHERE CONVERT(DATE, S.LOGINTIME, 102) = '2011-03-08'  
                        AND CONVERT(date, o.OrderedDate, 102) = '2011-03-08'
                        AND e.RecName like '(SR%'
            GROUP BY E.FirstName, e.LastName , o.TicketNumber , o.ToCusNid, e.EmpNid 
        ) b       
 JOIN ( 
         SELECT visitedbynid, COUNT(*) AS NONSERVICE FROM CustomerVisits 
         WHERE CONVERT(DATE, VisitTime, 102) = '2011-03-08' 
         GROUP BY VisitedByNid
       ) c ON c.VisitedByNid = b.EmpNid 
GROUP BY b.Employee, b.[Time In], b.[Time Out], c.NONSERVICE
more ▼

answered Mar 09, 2011 at 03:08 PM

Noonies gravatar image

Noonies
1.2k 60 64 65

(comments are locked)
10|1200 characters needed characters left

does this give you what you need:

SELECT 
  b.Employee , b.[Time In], b.[Time Out], COUNT(b.empnid) AS Orders,
  (SELECT COUNT(c.cusnid) FROM CustomerVisits 
     WHERE CONVERT(DATE, VisitTime, 102) = '2011-03-08') as AdditionalCount
FROM (
            SELECT E.FIRSTNAME + ' ' + E.LASTNAME  AS Employee
                        , right('0' + ltrim(right(convert(varchar,  MIN(cast(LogInTime as time)), 100), 7)), 7)  AS [Time In]
                        , right('0' + ltrim(right(convert(varchar,  MAX(cast(LogOutTime as time)), 100), 7)), 7)  AS [Time Out]
                        , o.TicketNumber , o.ToCusNid , e.EmpNid 
            FROM DBO.SESSIONS AS S
                    JOIN DBO.EMPLOYEES AS E ON E.EMPNID = S.EMPNID
                    JOIN Orders AS O ON o.SlsEmpNid = e.EmpNid 
            WHERE CONVERT(DATE, S.LOGINTIME, 102) = '2011-03-08'  
                        AND CONVERT(date, o.OrderedDate, 102) = '2011-03-08'
            GROUP BY E.FirstName, e.LastName , o.TicketNumber , o.ToCusNid, e.EmpNid 
        ) b
GROUP BY b.Employee, b.[Time In], b.[Time Out]
more ▼

answered Mar 09, 2011 at 01:55 PM

Kev Riley gravatar image

Kev Riley ♦♦
53.8k 47 49 76

What I would need is the count to be broken down by empnid or group by empnid.

So the COUNT(b.empnid) counts the rows in the b alias subquery based on a per employee basis. I would need the query you had added to do say a COUNT(cusnid) based on the employee also.

So for example I need a return set to show:

EMPLOYEE IN OUT ORDERS NON Andy 08:52AM 05:02PM 21 5 Angelo 06:12AM 08:17PM 19 6 Bob 08:39AM 05:30PM 24 10

The count for non I cannot pass through. Thanks for looking at this. I REALLY APPRECIATE IT!
Mar 09, 2011 at 02:09 PM Noonies
Ahh I had no idea that empnid had a relationship to that table
Mar 10, 2011 at 03:28 AM Kev Riley ♦♦
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x46

asked: Mar 09, 2011 at 01:29 PM

Seen: 1326 times

Last Updated: Mar 09, 2011 at 01:29 PM