If i'm understanding your question correctly you can do this at the beginning of your code
set datefirst 2
That will make Tuesday the first day of your week. This will also make Monday the last date of your week.
Question
I have a table with two columns.
pydt Decimal(13,0) and amt Decimal(18,4)
payDt column contains datetime in UNIX time stamp and amt contains amount. Now i need to make a report week wise.For this i use bellow command.
Select X.PMonth,X.PWeek,SUM(X.Payment) as Payment from
(
select
MONTH(dateadd(s,CAST(LEFT(pydt, LEN(pydt) - 3) AS DECIMAL(10,0)),'01/01/1970')) as PMonth,
DATEPART(WK,dateadd(s,CAST(LEFT(pydt, LEN(pydt) - 3) AS DECIMAL(10,0)),'01/01/1970')) as PWeek,
amt as Payment
from tblpayment
)X
group by
X.PMonth,X.PWeek
order by X.PMonth,X.PWeek
And it works. Now i need to specify week start and week and for report. Means i need a sql query that automatically sum data based on given week start and week end days.
Mon to Sun,Tue to Mon,........... and Sun to Mon
Can any one tell me how can i do that.
DECLARE @TESTTABLE TABLE(pydt DECIMAL(13,0),amt DECIMAL(18,4))
INSERT INTO @TESTTABLE
SELECT 1371633942104, 500.000 UNION ALL
SELECT 1371536351014, 200.000 UNION ALL
SELECT 1371508936993, 800.000 UNION ALL
SELECT 1371421684905, 800.000 UNION ALL
SELECT 1371279960886, 500.000 UNION ALL
SELECT 1371250768515, 900.000 UNION ALL
SELECT 1371115833490, 200.000 UNION ALL
SELECT 1371052445207, 100.000 UNION ALL
SELECT 1370935778269, 100.000 UNION ALL
SELECT 1370874220086, 200.000 UNION ALL
SELECT 1370790602634, 100.000 UNION ALL
SELECT 1370679256936, 300.000 UNION ALL
SELECT 1370644477888, 100.000 UNION ALL
SELECT 1370524059728, 500.000 UNION ALL
SELECT 1370419275541, 100.000 UNION ALL
SELECT 1370383832831, 900.000 UNION ALL
SELECT 1370299308283, 100.000 UNION ALL
SELECT 1370211913979, 200.000 UNION ALL
SELECT 1370066810107, 100.000 UNION ALL
SELECT 1370039853256, 200.000 UNION ALL
SELECT 1369888990306, 200.000 UNION ALL
SELECT 1369864999229, 1000.000 UNION ALL
SELECT 1369863700412, 200.000 UNION ALL
SELECT 1369863257127, 300.000 UNION ALL
SELECT 1369863225725, 200.000 UNION ALL
SELECT 1369860049462, 100.000 UNION ALL
SELECT 1369859952006, -47.640 UNION ALL
SELECT 1369858718486, 100.000 UNION ALL
SELECT 1369835887771, 100.000 UNION ALL
SELECT 1369815058443, 100.000 UNION ALL
SELECT 1369781776403, 900.000
Select min(X.pydt) as [Start From],max(X.pydt) as [End To],MAX(X.PMonth) AS PMonth,X.PWeek,SUM(X.Payment) as Payment from
(
select
dateadd(s,CAST(LEFT(pydt, LEN(pydt) - 3) AS DECIMAL(10,0)),'01/01/1970') as pydt,
MONTH(dateadd(s,CAST(LEFT(pydt, LEN(pydt) - 3) AS DECIMAL(10,0)),'01/01/1970')) as PMonth,
DATEPART(WK,dateadd(s,CAST(LEFT(pydt, LEN(pydt) - 3) AS DECIMAL(10,0)),'01/01/1970')) as PWeek,
amt as Payment
from @TESTTABLE
)X
group by
X.PWeek
order by PMonth,X.PWeek
Start From End To PMonth PWeek Payment
2013-05-28 22:56:16 2013-06-01 06:06:50 6 22 3452.3600
2013-06-02 22:25:13 2013-06-08 08:14:16 6 23 2200.0000
2013-06-09 15:10:02 2013-06-15 07:06:00 6 24 2100.0000
2013-06-16 22:28:04 2013-06-19 09:25:42 6 25 2300.0000
Here result show grouped data from Sunday to Saturday. Now i need group by data from specific day to day.
From 2013-06-02 22:25:13(Sunday) To 2013-06-08 08:14:16(Saturday)
From 2013-06-04 22:25:13(Tuesday) To 2013-06-08 08:14:16(Monday)
I think every thing is clear.
Solution
If i'm understanding your question correctly you can do this at the beginning of your code
set datefirst 2
That will make Tuesday the first day of your week. This will also make Monday the last date of your week.