SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Ultimate Newb here!
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rnelsch
Starting Member

USA
31 Posts

Posted - 01/18/2013 :  16:03:03  Show Profile  Reply with Quote
Gosh I feel like such a newb, but here is what I have so far:

Select Count(Distinct Job.JobID) as JobsRun,
Count (If (Job.SaleIsSold = 'True')) as JobsSold,
From Job
Where Job.PrimaryEmp = '294' and Job.SchedDate between '12/27/12' and '1/16/13'

I need to return two columns basically, 1 that shows the total of Jobs Run (JobID) and one that shows how many sold (SaleIsSold = True)

This is from a SQL Server 2008 server. Not sure what other information is needed.

Ryan A Nelsch

James K
Flowing Fount of Yak Knowledge

3649 Posts

Posted - 01/18/2013 :  16:34:36  Show Profile  Reply with Quote
The syntax would be like shown below.
SELECT COUNT(DISTINCT JOB.JobID) AS JobsRun,
       COUNT (DISTINCT CASE WHEN Job.SaleIsSold = 'True' THEN JOB.JobId END) as JobsSold
FROM   JOB
WHERE  JOB.PrimaryEmp = '294'
       AND JOB.SchedDate BETWEEN '12/27/12' AND '1/16/13'
Couple of things to note:

1. I added a distinct clause in the second count as well - not sure if your business logic calls for that or not.

2. Is SaleIsSold a char or varchar type of column? If it is, you are ok, but if it is a bit column, instead of comparing to 'True' you should compare to 1.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/19/2013 :  02:21:21  Show Profile  Reply with Quote
i would have written date range as below

JOB.SchedDate >= '20121227' 
AND JOB.SchedDate <= '20130116'


if SchedDate contains time part it shoulde be


JOB.SchedDate >= '20121227' 
AND JOB.SchedDate < '20130117'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/19/2013 :  02:23:23  Show Profile  Reply with Quote
also see

http://visakhm.blogspot.in/2012/12/different-ways-to-implement-date-range.html

http://visakhm.blogspot.in/2012/07/generate-datetime-values-from-integers.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

subashseo
Starting Member

Barbados
4 Posts

Posted - 01/19/2013 :  07:16:08  Show Profile  Reply with Quote
unspammed
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000