| Author |
Topic |
|
aikman
Starting Member
22 Posts |
Posted - 2008-08-12 : 12:42:13
|
| hi,I would like to do a select query that show X bookings last week in the last year of my database. I have come so far that i get the last week of the database. but i need last week in december. Can some one help me to modify query?Declare @Date DatetimeSelect @Date=MAX(DateAdd(d,DateDiff(d,0,Time),0))from dbo.bookingSelect Convert(VarChar(10),Time,120), Count(x)from dbo.bookingWhere [Time]>=DateAdd(Week,-1,@Date)Group By Convert(VarChar(10),Time,120)ThanksHåkan |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-12 : 12:50:30
|
| [code]Declare @Date DatetimeSelect @Date=DATEADD(d,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))Select Convert(VarChar(10),Time,120), Count(x)from dbo.bookingWhere [Time]>=DATEADD(d,-7,@Date)Group By Convert(VarChar(10),Time,120)[/code] |
 |
|
|
aikman
Starting Member
22 Posts |
Posted - 2008-08-12 : 13:16:53
|
| this is an old database, so cant use getdate. is 2005 and 2006 to april. need t get 2005-12-26 to 2005-12-31 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-12 : 13:28:07
|
quote: Originally posted by aikman this is an old database, so cant use getdate. is 2005 and 2006 to april. need t get 2005-12-26 to 2005-12-31
then replace it by your date valueSET DATEFORMAT dmyDeclare @Date DatetimeSelect @Date=31/12/2005'--DATEADD(d,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))Select Convert(VarChar(10),Time,120), Count(x)from dbo.bookingWhere [Time]>=DATEADD(d,-6,@Date)Group By Convert(VarChar(10),Time,120) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-12 : 13:47:39
|
Try using CURRENT_TIMESTAMP instead if GETDATE() E 12°55'05.25"N 56°04'39.16" |
 |
|
|
aikman
Starting Member
22 Posts |
Posted - 2008-08-12 : 14:01:17
|
| i still get all dates in results:2006.03.272005.12.302006.03.072006.04.242006.04.142006.04.042006.04.082006.03.112006.04.182006.03.192006.04.282006.03.212006.03.152005.12.282006.03.012006.03.132006.04.302006.03.292006.03.092006.03.252006.03.312006.03.052006.04.222006.03.032006.04.102006.04.262006.04.022006.04.122006.04.062006.04.162006.04.202006.03.172005.12.262006.03.23 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-12 : 14:08:46
|
How are the date values stored in database?Are they VARCHAR or DATETIME? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-12 : 14:09:24
|
| Is Time a datetime field? |
 |
|
|
aikman
Starting Member
22 Posts |
Posted - 2008-08-12 : 14:11:10
|
| it´s datetime |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-12 : 14:11:51
|
Strange, because . (dot) is not the normal date separator in Sweden. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-12 : 14:16:35
|
Run this and post back the resultUSE BookingGOSELECT DATA_TYPEFROM INFORMATION_SCHEMA.COLUMNSWHERE COLUMN_NAME = 'Time' AND TABLE_NAME = 'Booking' E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-12 : 14:21:49
|
| Are you using MS SQL Server? Having seen such a dateformat before. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-12 : 14:24:24
|
[code]DECLARE @Year SMALLINT, @dtFrom DATETIME, @dtTo DATETIME, @dtLast DATETIMESET @Year = 2005SELECT @dtLast = DATEADD(YEAR, @Year - 1899, '18991231'), @dtFrom = DATEADD(DAY, DATEDIFF(DAY, '19000101', @dtLast) / 7 * 7, '19000101'), @dtTo = CASE WHEN DATEDIFF(DAY, @dtFrom, @dtLast) < 8 THEN DATEADD(DAY, 1, @dtLast) ELSE DATEADD(DAY, 7, @dtFrom) ENDSELECT *FROM dbo.BookingWHERE [Time] >= @dtFrom AND [Time] < @dtTo[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
aikman
Starting Member
22 Posts |
Posted - 2008-08-12 : 14:31:11
|
| result is datetime, yes it´s win sql 2005 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-12 : 14:34:35
|
quote: Originally posted by aikman result is datetime, yes it´s win sql 2005
then post query you used. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-12 : 14:36:47
|
quote: Originally posted by Peso Try using CURRENT_TIMESTAMP instead if GETDATE() E 12°55'05.25"N 56°04'39.16"
Peso,Is there much difference in performance between CURRENT_TIMESTAMP & GETDATE()? |
 |
|
|
aikman
Starting Member
22 Posts |
Posted - 2008-08-12 : 14:37:58
|
| i use the one peso write above, SELECT DATA_TYPEFROM INFORMATION_SCHEMA.COLUMNSWHERE COLUMN_NAME = 'Time' AND TABLE_NAME = 'Booking'and get the results datetime |
 |
|
|
aikman
Starting Member
22 Posts |
Posted - 2008-08-12 : 14:44:21
|
this is working but i get some info i don´t need and it dont calculate how many bookid/day:bookid product salesperson customer time170 9 22 320 2005-12-26 00:00:00.000226 7 18 324 2005-12-26 00:00:00.000239 1 20 784 2005-12-26 00:00:00.000514 1 2 52 2005-12-26 00:00:00.000need it to look something like this:date bookings2005-12-26 or 2005.12.26 (doesn´t matter) 232005-12-27 352005-12-28 95quote: Originally posted by Peso
DECLARE @Year SMALLINT, @dtFrom DATETIME, @dtTo DATETIME, @dtLast DATETIMESET @Year = 2005SELECT @dtLast = DATEADD(YEAR, @Year - 1899, '18991231'), @dtFrom = DATEADD(DAY, DATEDIFF(DAY, '19000101', @dtLast) / 7 * 7, '19000101'), @dtTo = CASE WHEN DATEDIFF(DAY, @dtFrom, @dtLast) < 8 THEN DATEADD(DAY, 1, @dtLast) ELSE DATEADD(DAY, 7, @dtFrom) ENDSELECT *FROM dbo.BookingWHERE [Time] >= @dtFrom AND [Time] < @dtTo E 12°55'05.25"N 56°04'39.16"
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-12 : 15:55:26
|
[code]SELECT DATEADD(DAY, DATEDIFF(DAY, '19000101', Time), '19000101') AS Time, COUNT(*)FROM dbo.BookingWHERE [Time] >= @dtFrom AND [Time] < @dtToGROUP BY DATEADD(DAY, DATEDIFF(DAY, '19000101', Time), '19000101')[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|