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 |
|
PBoy
Starting Member
22 Posts |
Posted - 2010-06-21 : 18:34:28
|
| Hi All,Just wonder if anyone can give me a idea how I may do this?What Im looking to do is to show how long transaction have been our system for example we have transaction listed below (small sample)Trans No. Trans Date400024366 2009-12-23 00:00:00.000400056932 2009-12-31 00:00:00.000400027707 2009-12-31 00:00:00.000400020381 2010-01-11 00:00:00.000400020417 2010-01-11 00:00:00.000400020418 2010-01-11 00:00:00.000400020419 2010-01-11 00:00:00.000400021367 2010-01-14 00:00:00.000400021367 2010-01-14 00:00:00.000400035002 2010-01-21 00:00:00.000400035004 2010-01-21 00:00:00.000400055159 2010-01-25 00:00:00.000400055160 2010-01-25 00:00:00.000400031684 2010-05-29 00:00:00.000what I would like to do is a report that shows the transactions group (counted by DISTINCT trans numbers) by how long they have been in the system i.e. 10 Days, 20 Days, 30 days etcso something like this ------------------------------------------| 10 Days | 20 Days | 30 Days | 30+ Days |------------------------------------------| 3 | 18 | 10 | 1 |------------------------------------------Is this possible? Any help would be greatCheersPatrick |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2010-06-21 : 18:51:43
|
| [CODE]select sum(case when d.Duration <= 10 then 1 else 0 end) [10 Days], sum(case when d.Duration <= 20 then 1 else 0 end) [20 Days], sum(case when d.Duration <= 30 then 1 else 0 end) [30 Days], sum(case when d.Duration > 30 then 1 else 0 end) [30+ Days]from ( select t.TransNo, DateDiff(Day, t.TransDate, GetDate()) Duration ( select distinct TransNo, TransDate from Transactions ) t ) d[/CODE]=======================================A couple of months in the laboratory can save a couple of hours in the library. -Frank H. Westheimer, chemistry professor (1912-2007) |
 |
|
|
PBoy
Starting Member
22 Posts |
Posted - 2010-06-21 : 19:01:42
|
| HI Bustaz,Thanks for that looks like it would do the trick I have just tried it and its give me the error belowMsg 102, Level 15, State 1, Line 10Incorrect syntax near '('.Msg 102, Level 15, State 1, Line 13Incorrect syntax near 't'.CheersPatrick |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-06-21 : 23:27:25
|
It is a typo error.Just put from keyword marked in red.select sum(case when d.Duration <= 10 then 1 else 0 end) [10 Days], sum(case when d.Duration <= 20 then 1 else 0 end) [20 Days], sum(case when d.Duration <= 30 then 1 else 0 end) [30 Days], sum(case when d.Duration > 30 then 1 else 0 end) [30+ Days]from ( select t.TransNo, DateDiff(Day, t.TransDate, GetDate()) Duration from ( select distinct TransNo, TransDate from Transactions ) t ) d Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2010-06-22 : 19:39:32
|
| Thanks for the correction, Idera.PBoy, if you supply scripts to create the table and data, you'll stand a better chance of getting a response and, clearly, less screw-ups from me.=======================================A couple of months in the laboratory can save a couple of hours in the library. -Frank H. Westheimer, chemistry professor (1912-2007) |
 |
|
|
PBoy
Starting Member
22 Posts |
Posted - 2010-06-23 : 12:17:19
|
| Thanks Bustaz and Idera that worked a treat :) however see if I want to have a another column in front of it that is not getting sum for example ---------------------------------------------------------------| Type | 10 Days | 20 Days | 30 Days | 30+ Days |---------------------------------------------------------------| Supplier invoice | 3 | 18 | 10 | 1 |---------------------------------------------------------------where in the script do I put this column name in?CheersPatrick |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-06-23 : 13:48:42
|
Just add a group by clause shown belowselect Type, sum(case when d.Duration <= 10 then 1 else 0 end) [10 Days], sum(case when d.Duration <= 20 then 1 else 0 end) [20 Days], sum(case when d.Duration <= 30 then 1 else 0 end) [30 Days], sum(case when d.Duration > 30 then 1 else 0 end) [30+ Days]from ( select t.TransNo, DateDiff(Day, t.TransDate, GetDate()) Duration,Type from ( select distinct TransNo, TransDate,Type from Transactions ) t ) dGroup by Type Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
|
|
|
|
|