| Author |
Topic |
|
dishaniti
Starting Member
3 Posts |
Posted - 2008-11-21 : 15:21:41
|
| Hello All In following I am displaying total no of records from various table in 1 row for the month of October(10). Now I want to display the record of year 2008 monthwise means 1 record for each month. How Can I do this?*----------------------------------select (select count(*) from table1where year(uploaDdate) = 2008 and month(uploaddate) = 10 and source = 'H' ) as table1,(select count(*) from table2where year(uploaDdate) = 2008 and month(uploaddate) = 10 and source = 'H' ) as table2 ,(select count(*) from table3where year(uploaDdate) = 2008 and month(uploaddate) = 10 and source = 'H' ) as table3 ,(select count(*) from table4where year(uploaDdate) = 2008 and month(uploaddate) = 10 and source = 'H') as table4*---------------------------------- |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-11-21 : 15:37:41
|
select year(uploaDdate),month(uploaDdate),count(*) from( select * from Table1 where source = 'H' union all select * from Table1 where source = 'H' union all ...........)tgroup by year(uploaDdate),month(uploaDdate) |
 |
|
|
dishaniti
Starting Member
3 Posts |
Posted - 2008-11-21 : 15:43:53
|
| have you tested this solution, record will not come in following formatMOnth - Table1 - table2 - table3----------------------------------September - 12 - 34 -22October - 2 - 4 -5November - 1 - 4 -8 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-21 : 18:50:06
|
[code]SELECT theMonthName, SUM(table1) AS table1, SUM(table1) AS table2, SUM(table1) AS table3, SUM(table1) AS table4FROM ( SELECT DATEPART(MONTH, uploadDate) AS theMonthPart, DATENAME(MONTH, uploadDate) AS theMonthName, COUNT(*) AS table1, 0 AS table2, 0 AS table3, 0 AS table4 FROM Table1 WHERE uploadDate >= '20080101' AND uploadDate < '20090101' AND source = 'H' GROUP BY DATEPART(MONTH, uploadDate), DATENAME(MONTH, uploadDate) UNION ALL SELECT DATEPART(MONTH, uploadDate) AS theMonthPart, DATENAME(MONTH, uploadDate) AS theMonthName, 0 AS table1, COUNT(*) AS table2, 0 AS table3, 0 AS table4 FROM Table2 WHERE uploadDate >= '20080101' AND uploadDate < '20090101' AND source = 'H' GROUP BY DATEPART(MONTH, uploadDate), DATENAME(MONTH, uploadDate) UNION ALL SELECT DATEPART(MONTH, uploadDate) AS theMonthPart, DATENAME(MONTH, uploadDate) AS theMonthName, 0 AS table1, 0 AS table2, COUNT(*) AS table3, 0 AS table4 FROM Table3 WHERE uploadDate >= '20080101' AND uploadDate < '20090101' AND source = 'H' GROUP BY DATEPART(MONTH, uploadDate), DATENAME(MONTH, uploadDate) UNION ALL SELECT DATEPART(MONTH, uploadDate) AS theMonthPart, DATENAME(MONTH, uploadDate) AS theMonthName, 0 AS table1, 0 AS table2, 0 AS table3, COUNT(*) AS table4 FROM Table4 WHERE uploadDate >= '20080101' AND uploadDate < '20090101' AND source = 'H' GROUP BY DATEPART(MONTH, uploadDate), DATENAME(MONTH, uploadDate) ) AS dGROUP BY theMonthPart, theMonthNameORDER BY theMonthPart[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
dishaniti
Starting Member
3 Posts |
Posted - 2008-11-24 : 12:30:53
|
| Hello PesoThank you very much, it works very charmingly. The only thing I want to know If I run this query for 2 year span, it club the record, I want to display by year wise monthly record.Is this possible? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-24 : 12:35:58
|
| [code]SELECT theYearPart, theMonthName, SUM(table1) AS table1, SUM(table1) AS table2, SUM(table1) AS table3, SUM(table1) AS table4FROM ( SELECT DATEPART(MONTH, uploadDate) AS theMonthPart, DATENAME(MONTH, uploadDate) AS theMonthName, DATEPART(YEAR, uploadDate) AS theYearPart, COUNT(*) AS table1, 0 AS table2, 0 AS table3, 0 AS table4 FROM Table1 WHERE uploadDate >= '20080101' AND uploadDate < '20090101' AND source = 'H' GROUP BY DATEPART(MONTH, uploadDate), DATENAME(MONTH, uploadDate), DATEPART(YEAR, uploadDate) UNION ALL SELECT DATEPART(MONTH, uploadDate) AS theMonthPart, DATENAME(MONTH, uploadDate) AS theMonthName, DATEPART(YEAR, uploadDate) AS theYearPart, 0 AS table1, COUNT(*) AS table2, 0 AS table3, 0 AS table4 FROM Table2 WHERE uploadDate >= '20080101' AND uploadDate < '20090101' AND source = 'H' GROUP BY DATEPART(MONTH, uploadDate), DATENAME(MONTH, uploadDate), DATEPART(YEAR, uploadDate) UNION ALL SELECT DATEPART(MONTH, uploadDate) AS theMonthPart, DATENAME(MONTH, uploadDate) AS theMonthName, DATEPART(YEAR, uploadDate) AS theYearPart, 0 AS table1, 0 AS table2, COUNT(*) AS table3, 0 AS table4 FROM Table3 WHERE uploadDate >= '20080101' AND uploadDate < '20090101' AND source = 'H' GROUP BY DATEPART(MONTH, uploadDate), DATENAME(MONTH, uploadDate), DATEPART(YEAR, uploadDate) UNION ALL SELECT DATEPART(MONTH, uploadDate) AS theMonthPart, DATENAME(MONTH, uploadDate) AS theMonthName, DATEPART(YEAR, uploadDate) AS theYearPart, 0 AS table1, 0 AS table2, 0 AS table3, COUNT(*) AS table4 FROM Table4 WHERE uploadDate >= '20080101' AND uploadDate < '20090101' AND source = 'H' GROUP BY DATEPART(MONTH, uploadDate), DATENAME(MONTH, uploadDate), DATEPART(YEAR, uploadDate) ) AS dGROUP BY theYearPart,theMonthPart, theMonthNameORDER BY the YearPart,theMonthPart[/code] |
 |
|
|
|
|
|