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 |
andros30
Yak Posting Veteran
80 Posts |
Posted - 2009-03-05 : 15:50:42
|
I need help getting some information from a table. Here is my existing table:DETAIL TABLEPatsNumber|PatsName|Status|TxDate|CntrDate|CntrAmt3837744873|Mel Abny|Approve|01/05/2009|01/04/2009|42098736485202|Trey White|Pending|(blank)|01/04/2009|(blank)The differenct Statuses are: Approved, Denied, Pending, Recall, N/AI need to return a SUMMARY TABLE that will sum up the above table based on the Statuses and then if the other columns have pertinent data. If there is a valid date then count it otherwise do not.Here is what i have but it doesn't work.select sum(case when TxDate > ' ' then 1 else 0 end) as 'TxStarted',sum(case when CntrDate > ' ' then 1 else 0 end) as 'Contracts',sum(case when CntrAmt > 0 then 1 else 0 end) as 'Contract Value',sum(case when StartTxDate = ' ' then 1 else 0 end) as 'No TxStarted',sum(case when CntrDate <> ' ' then 1 else 0 end) as 'Contracts',sum(case when CntrAmt > 0 then 1 else 0 end) as 'Contract Value'from #tmpDetailswhere Status = 'Approved' |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-03-05 : 17:39:40
|
Looks kinda ok....Maybe you want to group by status and add it in your select..I have just created some sample data and ran this query below...and got your desired output...You can try this..declare @t table (Stat char(30),TxDate char(30),CntrDate char(30),CntrAmt char(30)) insert @tselect 'Approved','01/05/2009','01/04/2009','4209' union allselect 'Approved','01/05/2009','01/04/2009','' union allselect 'Pending','','01/04/2009','' union allselect 'Pending','01/05/2009','01/04/2009','' select Stat, sum(case when TxDate > '' then 1 else 0 end) as 'TxStarted',sum(case when CntrDate > '' then 1 else 0 end) as 'Contracts',sum(case when CntrAmt > 0 then 1 else 0 end) as 'Contract Value',sum(case when TxDate = '' then 1 else 0 end) as 'No TxStarted'from @tgroup by StatResultStat TxStarted Contracts Contract Value No TxStarted------------------------------ ----------- ----------- -------------- ------------Approved 2 2 1 0Pending 1 2 0 1 |
|
|
andros30
Yak Posting Veteran
80 Posts |
Posted - 2009-03-06 : 09:48:46
|
I may have more than 600 records returned. Am I correct to understand that I have to insert each record the way you suggest above? |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-03-06 : 10:06:48
|
No..the result set of the SELECT statement will be directly inserted into your tablefor ex:INSERT INTO urtable (Status,TxStarted,Contracts,Contract Value,No TxStarted)SELECT Stat, sum(case when TxDate > '' then 1 else 0 end) as 'TxStarted',sum(case when CntrDate > '' then 1 else 0 end) as 'Contracts',sum(case when CntrAmt > 0 then 1 else 0 end) as 'Contract Value',sum(case when TxDate = '' then 1 else 0 end) as 'No TxStarted'from @tgroup by Stat |
|
|
|
|
|
|
|