sql server ce - Help me with this SQL Query -
i've got sql server ce 3.5 table (transactions) following schema:
- id
- transaction_date
- category
- description
- amount
query:
select transaction_date, sum(amount) transactions group transaction_date;
i'm trying sum(amount) , group transaction_date can total amount each day want values days there no transactions record day no transactions have $0.00 amount.
thanks help!
you need calendar table select on dates. alternatively, if have numbers table, turn calendar table. basically, it's table every date in it. it's easy enough build , generate data , comes in handy these situations. use:
select c.calendar_date, sum(t.amount) calendar c left outer join transactions t on t.transaction_date = c.calendar_date group c.calendar_date order c.calendar_date
a few things keep in mind:
if you're sending front-end or reporting engine should send dates have (your original query) , have front end fill in $0.00 days if that's possible.
also, i've assumed here date exact date value no time component (hence "=" in join). calendar table include "start_time" , "end_time" can use between working dates include time portion. saves having strip off time portions , potentially ruining index usage. calculate start , end points of day when use it, since it's prefilled work table it's easier imo include start_time , end_time.
Comments
Post a Comment