Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql data sorted by calendar date

SQL Data sorted weekly only for the calendar days..Ex Feb 1, 2013 is a Friday, in need to pull in the data for Feb 1 & 2. RIght now it pulls the data for the entire week of 1-27-2013 to 2-2-2013. My results put in the total billed for the week end dates. EX( 1-5-13, 1-12-13) For the end of January and the beginning of Feburary I need the results to look like 1-27-13 to 1-31-13 = "total" with the week ending date to be the 31st. For Febuary 2-1-13 to 2-2-13 = "total"

I have been able to do it by individual days, but thats not how "Accounting" wants it.

Thanks in advance. Here is my code:

SELECT
    DATEADD(week, DATEdiff(WK, 0, bd.[Service Date 1]) ,5) AS month, 
    bd.Chart, 
    bd.[Transaction Code], 
    SUM(bd.Units) AS [Total Billed]

FROM dbo.[Billing Detail] AS bd 

INNER JOIN dbo.Patient AS p ON bd.Chart = p.[Chart Number]
WHERE

    (bd.[Transaction Code] = 'H2016') 
    AND (bd.[Service Date 1] >= '01/01/2013') 
    AND (bd.[Service Date 1] < '12/31/2013')

GROUP BY 
    DATEADD(week, DATEdiff(WK, 0, bd.[Service Date 1]) ,5), 
    bd.Chart, 
    bd.[Transaction Code]

ORDER BY bd.Chart
like image 690
user2011628 Avatar asked Jun 30 '26 20:06

user2011628


1 Answers

Just add to your select list (and group by list) two fields:

...
MONTH([Service Date 1]),
DATEPART(week,[Service Date 1])
...

and that should do it

like image 92
Chains Avatar answered Jul 02 '26 10:07

Chains



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!