| Author |
Topic |
|
zeyi
Starting Member
5 Posts |
Posted - 2009-01-12 : 07:25:13
|
| I have the table1 with every selling record, as:customer_name, timestamp.how can i get the table of yearly report including every month count?customer_name, buying_time_200701, buying_time_200702.....sum_2007 |
|
|
karthik_padbanaban
Constraint Violating Yak Guru
263 Posts |
Posted - 2009-01-12 : 07:30:43
|
| Need more explanations regarding your table and your expected report. |
 |
|
|
zeyi
Starting Member
5 Posts |
Posted - 2009-01-12 : 08:04:07
|
quote: Originally posted by karthik_padbanaban Need more explanations regarding your table and your expected report.
my table includes two columns, one is username, another is buying_time. ragarding to selling records, username could be show many times with different buying_time. the report i want is the table including username, count of buying time in particular month, say January,2008, Feb,2008....in 12 different columns. then sum the number of buying time in this whole year as a colunm as well.Is that clear? really appreciate if help!! |
 |
|
|
zeyi
Starting Member
5 Posts |
Posted - 2009-01-12 : 08:10:05
|
| like table1 which i haveusername timestampAA 20080109BB 20080307CC 20080711AA 20080111AA 20080222CC 20080714BB 20081225the table i expect likeusername count_01 count_02 count_03...count_07 count_12 sum_2008AA 2 1 0 0 0 3BB 0 0 1 0 1 2CC 0 0 0 2 0 2Considering the records in table1 is millions, i need the effective query. thanks a lot.waiting online! |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-01-12 : 08:23:12
|
| see Cross Tab Reporting in Books Online[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-12 : 08:54:42
|
quote: Originally posted by zeyi like table1 which i haveusername timestampAA 20080109BB 20080307CC 20080711AA 20080111AA 20080222CC 20080714BB 20081225the table i expect likeusername count_01 count_02 count_03...count_07 count_12 sum_2008AA 2 1 0 0 0 3BB 0 0 1 0 1 2CC 0 0 0 2 0 2Considering the records in table1 is millions, i need the effective query. thanks a lot.waiting online!
something likeSELECT username,SUM(CASE WHEN MONTH(timestamp)=1 AND YEAR(timestamp)=2008 THEN 1 ELSE 0 END) AS Jan2008,SUM(CASE WHEN MONTH(timestamp)=2 AND YEAR(timestamp)=2008 THEN 1 ELSE 0 END) AS Feb2008,SUM(CASE WHEN MONTH(timestamp)=3 AND YEAR(timestamp)=2008 THEN 1 ELSE 0 END) AS Mar2008,SUM(CASE WHEN MONTH(timestamp)=4 AND YEAR(timestamp)=2008 THEN 1 ELSE 0 END) AS Apr2008,SUM(CASE WHEN MONTH(timestamp)=5 AND YEAR(timestamp)=2008 THEN 1 ELSE 0 END) AS May2008,....FROM table1GROUP BY username |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-12 : 08:56:57
|
| or Search for Pivot queries in here. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
zeyi
Starting Member
5 Posts |
Posted - 2009-01-12 : 12:53:53
|
quote: Originally posted by visakh16
quote: Originally posted by zeyi like table1 which i haveusername timestampAA 20080109BB 20080307CC 20080711AA 20080111AA 20080222CC 20080714BB 20081225the table i expect likeusername count_01 count_02 count_03...count_07 count_12 sum_2008AA 2 1 0 0 0 3BB 0 0 1 0 1 2CC 0 0 0 2 0 2Considering the records in table1 is millions, i need the effective query. thanks a lot.waiting online!
something likeSELECT username,SUM(CASE WHEN MONTH(timestamp)=1 AND YEAR(timestamp)=2008 THEN 1 ELSE 0 END) AS Jan2008,SUM(CASE WHEN MONTH(timestamp)=2 AND YEAR(timestamp)=2008 THEN 1 ELSE 0 END) AS Feb2008,SUM(CASE WHEN MONTH(timestamp)=3 AND YEAR(timestamp)=2008 THEN 1 ELSE 0 END) AS Mar2008,SUM(CASE WHEN MONTH(timestamp)=4 AND YEAR(timestamp)=2008 THEN 1 ELSE 0 END) AS Apr2008,SUM(CASE WHEN MONTH(timestamp)=5 AND YEAR(timestamp)=2008 THEN 1 ELSE 0 END) AS May2008,....FROM table1GROUP BY username
hi, I don't think this query considering the usename issue. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-12 : 12:56:44
|
| what username issue? |
 |
|
|
|