Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rkalyani
Starting Member
30 Posts |
Posted - 2009-03-06 : 12:37:59
|
| I have two queriesselect count(ind_app_id)as totalSubmitted from tableA where SubmittedDate between '02/01/2009' and '03/01/2009'select count(ind_app_id)as totalSold from tableA where SoldDate between '02/01/2009' and '03/01/2009'My requirement is to show total submitted vs total sold for a given date range. I created both these queries. Is there a way to combine these.Thank youThank you,kal30 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-03-06 : 12:45:35
|
| [code]Select Count(Case when SubmittedDate between '02/01/2009' and '03/01/2009' then ind_app_id Else 0 End)as totalSubmitted,Count(Case when SoldDate between '02/01/2009' and '03/01/2009'then ind_app_id Else 0 End)as totalSoldfrom table A[/code] |
 |
|
|
tmitch
Yak Posting Veteran
60 Posts |
Posted - 2009-03-06 : 12:48:02
|
| SELECT (select count(ind_app_id) from tableA where SubmittedDate between '02/01/2009' and '03/01/2009') [totalSubmitted] , (select count(ind_app_id) from tableA where SoldDate between '02/01/2009' and '03/01/2009') [totalSold] hth,Tim---------------------Tim Mitchellwww.BucketOfBits.com |
 |
|
|
rkalyani
Starting Member
30 Posts |
Posted - 2009-03-06 : 13:25:18
|
| Tim, I used your query. I had to attach a where clause in the end and my dates were not real dates. They were varchar fields and so I had to convert it. So my final query looks like thisselect (select count(ind_app_id) from tableA where case when isdate(submittedDate) =1 then convert(datetime, submittedDate) end between '02/01/2009' and '03/01/2009')[totalSubmitted],(select count(ind_app_id) from tableA wherecase when isdate(SoldDate) =1 then convert(datetime, SoldDate) end between '02/01/2009' and '03/01/2009')[totalSold]from tableAwhere transaction_type_id=7 and ind_app_id <> 0But I get different answer from my original seperated queryTotal submitted should be 293. I get 1325Total Sold should be 112. I get 875Am I missing something while converting the varchar to dateThank youThank you,kal30 |
 |
|
|
tmitch
Yak Posting Veteran
60 Posts |
Posted - 2009-03-06 : 13:49:07
|
| You need to put your WHERE clause in each of the subqueries, not in the outer query.hth,Tim---------------------Tim Mitchellwww.BucketOfBits.com |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-06 : 23:36:38
|
| try this one if there are varchar fieldsslight modification to tmitch query,SELECT (select count(ind_app_id) from tableA where convert(datetime,SubmittedDate) between '02/01/2009' and '03/01/2009' and transaction_type_id=7 and ind_app_id <> 0) [totalSubmitted], (select count(ind_app_id) from tableA where convert(datetime,SoldDate) between '02/01/2009' and '03/01/2009' and transaction_type_id=7 and ind_app_id <> 0) [totalSold]try sodeep query as like thisSelect Count(Case when convert(datetime,SubmittedDate) between '02/01/2009' and '03/01/2009' then ind_app_id Else 0 End)as totalSubmitted,Count(Case when convert(datetime,SoldDate) between '02/01/2009' and '03/01/2009'then ind_app_id Else 0 End)as totalSoldfrom table Awhere transaction_type_id=7 and ind_app_id <> 0 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-07 : 13:27:35
|
quote: Originally posted by sodeep
Select Count(Case when SubmittedDate between '02/01/2009' and '03/01/2009' then ind_app_id Else Null End)as totalSubmitted,Count(Case when SoldDate between '02/01/2009' and '03/01/2009'then ind_app_id Else Null End)as totalSoldfrom table A
|
 |
|
|
|
|
|
|
|