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 |
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-11-11 : 09:43:51
|
| Hi GuysI am using the following query to gather the number of sales made for the last 5 weeks:SELECT DATEPART(wk,ri.DateEntered) AS 'Week Number' ,COUNT(ri.receiptid) FROM [dbo].[voiditem] vi (nolock) INNER JOIN [dbo].[receiptitem] AS ri (nolock) ON vi.receiptitemid = ri.receiptitemidINNER JOIN [dbo].[voidreason] AS vr (nolock) ON vi.enteredbyreasonid = vr.voidreasonidWHERE ri.dateentered >= DATEADD(week, -5, GETDATE()) GROUP BY DATEPART(wk,ri.DateEntered)ORDER BY DATEPART(wk,ri.DateEntered)These gives me results similar to the following:41 3934242 8403143 7024744 5683845 3906446 11718Is it possible to change the query so that it still displays the last 5 weeks worth of data but the following way:1 393422 840313 702474 568385 390646 11718Thanks |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-11 : 09:58:36
|
[code]SELECT 6 + DATEDIFF(WEEK, GETDATE(), ri.DateEntered) AS [Week Number], COUNT(ri.receiptid)FROM dbo.voiditem AS vi (nolock) INNER JOIN dbo.receiptitem AS ri (nolock) ON ri.receiptitemid = vi.receiptitemidINNER JOIN dbo.voidreason AS vr (nolock) ON vr.voidreasonid = vi.enteredbyreasonidWHERE ri.dateentered >= DATEADD(week, -5, GETDATE()) AND ri.dateentered < DATEADD(day, 1, GETDATE()) GROUP BY 6 + DATEDIFF(WEEK, GETDATE(), ri.DateEntered)ORDER BY 6 + DATEDIFF(WEEK, GETDATE(), ri.DateEntered)[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-11-11 : 10:27:08
|
| Hey thats kool.Can you explain what you did in the syntax?Thanks |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-11 : 10:38:09
|
Syntax is ok.Otherwise read Books Online about the functions I used. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|
|
|