I believe I already provided the answer to this in my first post. Here is
example with all details:
CREATE TABLE Teams (
team VARCHAR(10),
transaction_date DATETIME,
amount INT,
PRIMARY KEY (team, transaction_date))
INSERT INTO Teams VALUES ('Blue', '20080301', 100)
INSERT INTO Teams VALUES ('Blue', '20080302', 200)
INSERT INTO Teams VALUES ('Green', '20080301', 500)
INSERT INTO Teams VALUES ('Green', '20080302', 400)
-- Summary amounts by team
SELECT team, SUM(amount) AS total
FROM Teams
GROUP BY team
-- Summary amounts by team for the month
SELECT team, SUM(amount) AS total
FROM Teams
WHERE transaction_date >= '20080301'
AND transaction_date < '20080401'
GROUP BY team
-- Summary amounts by year/month
SELECT CONVERT(VARCHAR(7), transaction_date, 126) AS year_month,
team, SUM(amount) AS total
FROM Teams
GROUP BY CONVERT(VARCHAR(7), transaction_date, 126), team
-- Latest team amount
SELECT team, amount
FROM Teams AS T1
WHERE T1.transaction_date =
(SELECT MAX(T2.transaction_date)
FROM Teams AS T2
WHERE T2.team = T1.team)
HTH,
Plamen Ratchev
http://www.SQLStudio.com
mahmad - 11 Mar 2008 17:05 GMT
Hi Plamen,
I cant hardcode those dates and amounts as the amounts will be based on the
amounts field.
I just want to add the amount from yesterday and the day before and the day
before for the current month and get a total.
thanks
> I believe I already provided the answer to this in my first post. Here is
> example with all details:
[quoted text clipped - 40 lines]
> Plamen Ratchev
> http://www.SQLStudio.com
Plamen Ratchev - 11 Mar 2008 17:37 GMT
I am not sure where you see any hardcoding. Take this query that I posted:
SELECT CONVERT(VARCHAR(7), transaction_date, 126) AS year_month,
team, SUM(amount) AS total
FROM Teams
GROUP BY CONVERT(VARCHAR(7), transaction_date, 126), team
It seems to me it does exactly what you are describing.
To get a better answer it is best to post your table structure (CREATE TABLE
statements), sample data, and desired results.
HTH,
Plamen Ratchev
http://www.SQLStudio.com
mahmad - 12 Mar 2008 17:32 GMT
Hi,
Sorry my fault i had a closer look at your code and yes it is what i am
looking for.
Thanks for your help
Mo
> I believe I already provided the answer to this in my first post. Here is
> example with all details:
[quoted text clipped - 40 lines]
> Plamen Ratchev
> http://www.SQLStudio.com