Tom Moreau
3/24/2007 9:05:00 PM
Try:
SELECT sum(t1qty * t2.fatgrams) as fat, sum(t1.qty * t2.calories) as cals,
datepart (wk, [date]) as week
FROM
table1 t1
JOIN
table2 t2 ON t2.categoryid = t1.categoryid
WHERE
t1.userid = 1
AND t1.[date] >= '20070101' and t1.[date] < getdate()
GROUP BY
datepart (wk, [date])
--
Tom
----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
.
"MikeB" <m@nospam.com> wrote in message
news:uG4laJjbHHA.3408@TK2MSFTNGP03.phx.gbl...
Actually, after looking at this a little closer, I don't think it will work
for me since this can go over a year so I would cause it to restart. Is
there sometype of datediff funcion where I can pass it the date and todays
date and get the difference in weeks and then group that way?
"Tom Moreau" <tom@dont.spam.me.cips.ca> wrote in message
news:ukGUrfhbHHA.3420@TK2MSFTNGP05.phx.gbl...
> Try:
>
> SELECT sum(t1qty * t2.fatgrams) as fat, sum(t1.qty * t2.calories) as cals,
> datepart (wk, [date]) as week
> FROM
> table1 t1
> JOIN
> table2 t2 ON t2.categoryid = t1.categoryid
> WHERE
> t1.userid = 1
> GROUP BY
> datepart (wk, [date])
>
> --
> Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> .
> "MikeB" <m@nospam.com> wrote in message
> news:%233zxyVhbHHA.2448@TK2MSFTNGP02.phx.gbl...
> Hello All, I am new to sql and have a questions. I have the following
> table and need to group it by Week. Is there anyway to do this in the SQL
> and if so can someone please help me out?
>
> Table 1
> id PK int
> userid FK int
> CategoryID FK to table 2 int
> qty int
> date smalldatetime
>
> Example data
>
> 1 1 1 2 03/23/2007
> 2 1 2 2 03/24/2007
> 3 1 1 2 03/28/2007
> 4 1 2 2 03/28/2007
>
> Table 2
> id PK int
> categoryid FK int
> Item
> FatGrams int
> Calories int
>
> Example Data
> 1 1 pop/soda 5 100
> 2 2 candy 10 200
>
> Now for data, what I need is the total fatgrams and calories grouped by
> week
>
> SELECT sum(t1qty * t2.fatgrams) as fat, sum(t1.qty * t2.calories), week ?
> FROM table1 t1, table2 t2
> WHERE t2.categoryid = t1.categoryid
> AND t1.userid = 1
> GROUP BY week ?
>
>
>