| Author |
Topic |
|
mukhan85
Starting Member
46 Posts |
Posted - 2008-09-09 : 11:19:40
|
| Hi, I have the following table:Name Date ColorApple 4/5/05 RedOrange 3/4/08 Yellow...I used the query:that generated the following result set:Name Number MonthApple 45 1Orange 33 1Peach 222 2I am trying to get the same result set, but I need one more column that would display the total number of fruits for the given month.Name Number Month TotalApple 45 1 78Orange 33 1 78 Peach 22 2 22...I tried to put a subquery inside my query but, it gave an error that more than one result was returned.Thank you.Thank you. |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-09-09 : 11:26:47
|
| Didnt see a question in your post. Post sample data, table structure, desired output and problems you are facing. Help us help you. |
 |
|
|
mukhan85
Starting Member
46 Posts |
Posted - 2008-09-09 : 11:34:53
|
quote: Originally posted by sakets_2000 Didnt see a question in your post. Post sample data, table structure, desired output and problems you are facing. Help us help you.
Hi,well I have the following results from my query:Name Number MonthApple 45 1Orange 33 1Peach 22 2I am trying to get the same result set, but I need one more column that would display the total number of fruits for the given month.For example, for Month 1 there were 45 apples and 33 Oranges. So, the column Total should show 45+33 = 78 for all fruits for Month 1 and so on.Name Number Month TotalApple 45 1 78Orange 33 1 78Peach 22 2 22Thank you. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-09 : 11:35:37
|
What is Number value? Is it number of records for that name?What is Month value? Is it the month number of the date column?What is Total value? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-09-09 : 11:42:59
|
quote: Originally posted by mukhan85
quote: Originally posted by sakets_2000 Didnt see a question in your post. Post sample data, table structure, desired output and problems you are facing. Help us help you.
Hi,well I have the following results from my query:Name Number MonthApple 45 1Orange 33 1Peach 22 2I am trying to get the same result set, but I need one more column that would display the total number of fruits for the given month.For example, for Month 1 there were 45 apples and 33 Oranges. So, the column Total should show 45+33 = 78 for all fruits for Month 1 and so on.Name Number Month TotalApple 45 1 78Orange 33 1 78Peach 22 2 22Thank you.
select * from yourtable join (select month,sum(number) as total from yourtable group by month)zon z.[month]=test.month |
 |
|
|
mukhan85
Starting Member
46 Posts |
Posted - 2008-09-09 : 11:43:17
|
quote: Originally posted by Peso What is Number value? Is it number of records for that name?What is Month value? Is it the month number of the date column?What is Total value? E 12°55'05.63"N 56°04'39.26"
Well the actual table looks like this:Name Date_Sold AddressApple 4/5/08 ...Apple 4/3/05 ...So, I am trying to calculate how many apples where sold every month.So the Number column displays number of apples sold in the indicated month in the Month column. and Total shows the total number of items sold for the given month (Includes all fruits: apple, orange...)Thank you. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-09-09 : 11:43:52
|
| select * from yourtable join (select month,sum(number) as total from yourtable group by month)zon z.[month]=yourtable.month |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-09 : 11:48:59
|
Is month calculated out of date column? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
mukhan85
Starting Member
46 Posts |
Posted - 2008-09-09 : 11:52:15
|
quote: Originally posted by Peso Is month calculated out of date column? E 12°55'05.63"N 56°04'39.26"
Yep |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-09 : 12:01:37
|
[code]SELECT DISTINCT Name, COUNT(*) OVER (PARTITION BY DATEPART(MONTH, Date)) AS Number DATEPART(MONTH, Date) AS [Month], COUNT(*) OVER (PARTITION BY Name) AS TotalFROM Table1ORDER BY Name, DATEPART(MONTH, Date)[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|