Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 formating the record

Author  Topic 

imranabdulaziz
Yak Posting Veteran

83 Posts

Posted - 2008-04-07 : 07:23:14
Hi all,
i am using sql server 2005.
let me explain the scenario.
I have to built a report where I have to find out no of call made by an executive to a perticular customer during a year

my table record look like

empid custid product date and other field
em111 iimran aaaa 01-04-2008 ........
em111 iimran bbbb 01-04-2008 ........
em111 iimran cccc 01-04-2008 ........
em111 xxxxxx aaaa 01-04-2008 ........
em111 xxxxxx 2222 01-04-2008 ........
em111 iimran aaaa 02-04-2008 ........
em111 iimran bbbb 02-04-2008 ........
em111 iimran cccc 02-04-2008 ........
em111 xxxxxx aaaa 02-04-2008 ........
em111 xxxxxx 2222 02-04-2008 ........

.
problem is it has to implement in this manner

Srno custname jan feb mar apr may june july aug sep oct nov dec
1 iimran 0 4 0 5 6 7 0 3 5 7 3 1

please suggest some idea.


Please Help

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-07 : 07:39:26
1. Search for PIVOT / CROSSTAB queries.
2. Use new PIVOT operator
3. Use old school syntax

SELECT custid, sum(case when datepart(month, productdate) = 1 then 1 else 0 end) AS January,
sum(case when datepart(month, productdate) = 2 then 1 else 0 end) AS February,
...,
FROM Table
GROUP BY custid



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -