GROUP BY from two tables

I have  a table "hours" with some date-stamped records, and want sums from each day

SELECT CAST(DateStamp AS DATE) as DateField, SUM(Lengde) as SumField
FROM hours
GROUP BY CAST(DateStamp AS DATE)
ORDER BY CAST(Dato AS DATE)

Open in new window


Running this I get:
DateField	SumField
2015-04-11	90
2015-04-14	123
2015-04-15	30

Open in new window


But since I want the sum of hours from last 14 days, including a 0 on days with no records I've tried to UNION then following:
DECLARE @dt DATE, @dtEnd DATE
set @dt = dateadd(day,-13,getdate())
set @dtEnd = dateadd(day, 14, @dt)
select dateadd(day, number, @dt) AS Dato, '0' AS SumField
from 
    (select distinct number from master.dbo.spt_values
     where name is null
    ) n
where dateadd(day, number, @dt) < @dtEnd

Open in new window


I then get:
DateField	SumField
2015-04-03	0
2015-04-04	0
2015-04-05	0
2015-04-06	0
2015-04-07	0
2015-04-08	0
2015-04-09	0
2015-04-10	0
2015-04-11	0
2015-04-11	90
2015-04-12	0
2015-04-13	0
2015-04-14	123
2015-04-14	0
2015-04-15	0
2015-04-15	30
2015-04-16	0

Open in new window


How can I GROUP/SUM the result BY the date?
fmsolAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sarabhaiCommented:
DECLARE @dt DATE, @dtEnd DATE
set @dt = dateadd(day,-13,getdate())
set @dtEnd = dateadd(day, 14, @dt)

SELECT Dato , SUM(SumField) AS SumField FROM
(
select dateadd(day, number, @dt) AS Dato, '0' AS SumField
from
    (select distinct number from master.dbo.spt_values
     where name is null
    ) n
where dateadd(day, number, @dt) < @dtEnd
) A Group by Dato
0
fmsolAuthor Commented:
Not sure if I understand this.
What about the hours table which needs to be UNIONed?

SELECT CAST(DateStamp AS DATE) as DateField, SUM(Lengde) as SumField
FROM hours
GROUP BY CAST(DateStamp AS DATE)
ORDER BY CAST(Dato AS DATE)

Open in new window

0
fmsolAuthor Commented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fmsolAuthor Commented:
Used a calendar table (which is a really smart idea in many situations)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.