|
|
Question : Last full week in sql
|
|
|
|
I am having a problem with selecting the last full week , the code below shows every thing after midnight Sunday to before midnight the next Sunday. I just want to see from 08.00 Monday morning to 07.00 am the next monday morming.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
|
DECLARE @TodayDayOfWeek INT
DECLARE @EndOfPrevWeek DateTime
DECLARE @StartOfPrevWeek DateTime
SET @TodayDayOfWeek = datepart(dw, GetDate())
SET @EndOfPrevWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())
SET @StartOfPrevWeek = DATEADD(dd, -(@TodayDayOfWeek+6), GetDate())
SELECT TOP (100) PERCENT dbo.CreateOrder.n_OrderOriginallyPlacedTime, dbo.Plu.Plu, dbo.Plu.ItemName, dbo.Plu.Price, dbo.t_OrderLines.n_Amount, dbo.t_OrderLines.n_OrderNum,
dbo.t_OrderLines.n_Type, dbo.t_OrderLines.n_MenuId, dbo.t_OrderPayment.n_Order, dbo.t_OrderPayment.n_PayAmount
FROM dbo.t_OrderPayment FULL OUTER JOIN
dbo.CreateOrder ON dbo.t_OrderPayment.nStoreID = dbo.CreateOrder.nStoreID AND
dbo.t_OrderPayment.n_Order = dbo.CreateOrder.n_OrderNum FULL OUTER JOIN
dbo.t_OrderLines ON dbo.CreateOrder.n_OrderNum = dbo.t_OrderLines.n_OrderNum FULL OUTER JOIN
dbo.Plu ON dbo.t_OrderLines.n_MenuId = dbo.Plu.n_MenuId
WHERE (dbo.t_OrderLines.nStoreID = '528') AND (dbo.CreateOrder.nStoreID = '528') AND (dbo.CreateOrder.n_StatusFlags <> '6') AND
(dbo.t_OrderLines.n_Type <> '26') AND (dbo.t_OrderLines.n_Type <> '18') AND(dbo.CreateOrder.n_OrderOriginallyPlacedTime BETWEEN
CONVERT(VARCHAR, @StartOfPrevWeek,7)
AND
CONVERT(VARCHAR, @EndOfPrevWeek+1,7))
AND (dbo.t_OrderLines.n_MenuId <> '2719')
ORDER BY dbo.CreateOrder.n_OrderOriginallyPlacedTime
|
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
|
DECLARE @TodayDayOfWeek INT
DECLARE @EndOfPrevWeek DateTime
DECLARE @StartOfPrevWeek DateTime
SET @TodayDayOfWeek = datepart(dw, GetDate())
SET @EndOfPrevWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())
SET @StartOfPrevWeek = DATEADD(dd, -(@TodayDayOfWeek+6), GetDate())
SELECT TOP (100) PERCENT dbo.CreateOrder.n_OrderOriginallyPlacedTime, dbo.Plu.Plu, dbo.Plu.ItemName, dbo.Plu.Price, dbo.t_OrderLines.n_Amount, dbo.t_OrderLines.n_OrderNum,
dbo.t_OrderLines.n_Type, dbo.t_OrderLines.n_MenuId, dbo.t_OrderPayment.n_Order, dbo.t_OrderPayment.n_PayAmount
FROM dbo.t_OrderPayment FULL OUTER JOIN
dbo.CreateOrder ON dbo.t_OrderPayment.nStoreID = dbo.CreateOrder.nStoreID AND
dbo.t_OrderPayment.n_Order = dbo.CreateOrder.n_OrderNum FULL OUTER JOIN
dbo.t_OrderLines ON dbo.CreateOrder.n_OrderNum = dbo.t_OrderLines.n_OrderNum FULL OUTER JOIN
dbo.Plu ON dbo.t_OrderLines.n_MenuId = dbo.Plu.n_MenuId
WHERE (dbo.t_OrderLines.nStoreID = '528') AND (dbo.CreateOrder.nStoreID = '528') AND (dbo.CreateOrder.n_StatusFlags <> '6') AND
(dbo.t_OrderLines.n_Type <> '26') AND (dbo.t_OrderLines.n_Type <> '18') AND(dbo.CreateOrder.n_OrderOriginallyPlacedTime BETWEEN
CONVERT(VARCHAR, @StartOfPrevWeek,7)
AND
CONVERT(VARCHAR, @EndOfPrevWeek+1,7))
AND (dbo.t_OrderLines.n_MenuId <> '2719')
ORDER BY dbo.CreateOrder.n_OrderOriginallyPlacedTime
|
|
|
|
|
Answer : Last full week in sql
|
|
|
|
Try to use the attached code. It only works fine if you run only on Mondays.
DECLARE @TodayDayOfWeek INT DECLARE @EndOfPrevWeek DateTime DECLARE @StartOfPrevWeek DateTime SET @TodayDayOfWeek = datepart(dw, GetDate()) SET @EndOfPrevWeek = CONVERT(VARCHAR, DATEADD(dd, -(@TodayDayOfWeek-2), GetDate()), 112) + ' 07:00' SET @StartOfPrevWeek = CONVERT(VARCHAR, DATEADD(dd, -(@TodayDayOfWeek+5), GetDate()), 112) + ' 08:00' SELECT TOP (100) PERCENT dbo.CreateOrder.n_OrderOriginallyPlacedTime, dbo.Plu.Plu, dbo.Plu.ItemName, dbo.Plu.Price, dbo.t_OrderLines.n_Amount, dbo.t_OrderLines.n_OrderNum, dbo.t_OrderLines.n_Type, dbo.t_OrderLines.n_MenuId, dbo.t_OrderPayment.n_Order, dbo.t_OrderPayment.n_PayAmount FROM dbo.t_OrderPayment FULL OUTER JOIN dbo.CreateOrder ON dbo.t_OrderPayment.nStoreID = dbo.CreateOrder.nStoreID AND dbo.t_OrderPayment.n_Order = dbo.CreateOrder.n_OrderNum FULL OUTER JOIN dbo.t_OrderLines ON dbo.CreateOrder.n_OrderNum = dbo.t_OrderLines.n_OrderNum FULL OUTER JOIN dbo.Plu ON dbo.t_OrderLines.n_MenuId = dbo.Plu.n_MenuId WHERE (dbo.t_OrderLines.nStoreID = '528') AND (dbo.CreateOrder.nStoreID = '528') AND (dbo.CreateOrder.n_StatusFlags <> '6') AND (dbo.t_OrderLines.n_Type <> '26') AND (dbo.t_OrderLines.n_Type <> '18') AND(dbo.CreateOrder.n_OrderOriginallyPlacedTime BETWEEN @StartOfPrevWeek AND @EndOfPrevWeek AND (dbo.t_OrderLines.n_MenuId <> '2719') ORDER BY dbo.CreateOrder.n_OrderOriginallyPlacedTime
|
|
|
|
|