| Author |
Topic |
|
aikman
Starting Member
22 Posts |
Posted - 2008-07-07 : 11:12:33
|
| Table:book_id product_id sale_id cust_id time1 8 13 895 2005-10-09 00:00:00.0002 2 5 40 2005-11-12 00:00:00.0003 5 22 364 2005-10-31 00:00:00.0004 2 2 5 2006-04-22 00:00:00.0005 10 15 429 2006-03-23 00:00:00.0006 6 18 209 2005-10-05 00:00:00.0007 6 15 476 2006-04-10 00:00:00.0008 7 7 233 2005-12-18 00:00:00.0009 10 38 79 2006-03-05 00:00:00.00010 7 10 982 2006-03-07 00:00:00.00011 10 22 500 2005-11-26 00:00:00.00012 4 24 712 2005-11-14 00:00:00.00013 6 38 96 2006-03-07 00:00:00.00014 8 14 890 2006-04-02 00:00:00.00015 6 2 914 2005-12-02 00:00:00.00016 5 22 122 2005-12-12 00:00:00.000I want a view that display X booking last week (yyyy:mm:dd). Every row is one booking. And I have to display it like this example:SELECT * from viewDate bookings2005-12-26 32005-12-27 22005-12-28 22005-12-29 32005-12-30 52005-12-31 2thanks for all help |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-07-07 : 11:21:28
|
| SELECT CONVERT(varchar(10),time,120) ,count(*)FROM yourTablegroup by CONVERT(varchar(10),time,120) Jim |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-07 : 11:22:52
|
" last week "Your current date is 2006 Jan ?2005-12-26 32005-12-27 2 How do you get the 3 bookings for 2005-12-26 ? I don't see such record in your sample data KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
aikman
Starting Member
22 Posts |
Posted - 2008-07-07 : 11:42:55
|
| the output is just an example on how its should look like, i need from the last week from table |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-07-07 : 11:56:20
|
| SELECT CONVERT(varchar(10),time,120) ,count(*)FROM yourTableWHERE [TIME] >= DATEADD(week,-1,GETDATE())GROUP BY CONVERT(varchar(10),time,120) will give you the last 7 daysJim |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-07 : 12:31:45
|
You don't have to convert the time to string at allselect [time], cnt = count(*)from table1where [time] >= dateadd(week, -1, getdate())group by [time] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-07-07 : 12:41:48
|
| But that will display the hh:mm:ss part of the field, which the op doesn't want. Jim |
 |
|
|
aikman
Starting Member
22 Posts |
Posted - 2008-07-07 : 13:41:59
|
| on jimf's i get 2 colum with name no colum name and no data.and with khtan i get 2 colum namedtime and cnt but no data. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-07 : 13:49:22
|
then i think you probably need this:-DECLARE @MaxDate datetimeSELECT @MaxDate=MAX(DATEADD(d,DATEDIFF(d,0,time),0))FROM TableSELECT CONVERT(varchar(10),time,120) ,count(*)FROM yourTableWHERE [TIME] >= DATEADD(week,-1,@MaxDate)GROUP BY CONVERT(varchar(10),time,120) |
 |
|
|
aikman
Starting Member
22 Posts |
Posted - 2008-07-07 : 14:46:12
|
| that works great thanks, but i need two more things, the colums are not named. should be named date and booking. and my output are not ordered by date |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-07 : 19:41:54
|
[code]declare @max_time datetimeselect @max_time = max([time])from table1select [time] as date, count(*) as bookingfrom table1where [time] >= dateadd(week, -1, @max_time)group by [time]order by [time][/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|