June 3, 2004 at 8:38 am
Hi guys (and gals),
This is my first post, and the reason I have joined your community.
I have a query table with the following info in it:-
and what I would like to do is return:-
At the moment I am using something like:-
Select AgentID, Sum(Case When EventID='A' Then 1 Else 0 End) as A, {repeat for every bloody letter of the alphabet} Group By AgentID, Order By AgentID
Now this works fine, but I can't believe that there is not a simpler way of doing this, i.e. something like: Select AgentID, CountOfGroupingOfColumn(EventID) - If you get the picture....
In any case my 26 Select clause works.... it just seems a bit rubbish thats all....
Comments greatly appreciated.
Paul.
June 3, 2004 at 9:55 am
Paul,
I've been writing T-SQL for about a 2yrs now and I've never seen such a function like that. If you find one let me know because I make full use of the CASE statement all the time, and things can get kinda hairy.
Brad
June 4, 2004 at 2:46 am
This is a classic Crosstab query.
Search on this forum for "Crosstab" and you will come up with some tips, tricks and procs.
eg.
http://www.sqlservercentral.com/scripts/contributions/936.asp
/rokmooose
You must unlearn what You have learnt
June 4, 2004 at 3:26 am
Ok... I can see that working (although all it does is create the same query I would have created anyway....)
But how does that translate to a single ADO type Query from ASP; ie.
MyRst.Open "Select * from...."
Regards,
Paul.
June 4, 2004 at 3:43 am
Also....
I need it to return events that are not present;
AgentID A B C .... Z
with zero's in the empty columns....
I am now assuming I need a while / wend type thing? - I can program this server-side in ASP, but I was hoping for a more 'elegant' solution...
June 4, 2004 at 4:33 am
Yup crosstabs is not a forte in SQL :-(, it could be easier to manage in code...
What You could do is write a method (in VB/C#... ) that takes a recordset:
Rowtext, Coltext, Value
2001 A 2
2002 A 1
2001 B 1
2002 B 0
.
.
2001 E 1
2002 E 1
2001 F 0
2001 F 0
2002 F 1
2001 G 0
2002 G 1
.
.
And transforms this to a crosstab:
public static DataTable Crosstab( DataTable tableToCrosstab ){...}
This You could reuse for crosstab purposes,
The SQL would be responsible for just retrieving the data in the specified format: Rowtext, Coltext, Value.
( I know I did something similar once for crosstabing in an app,
SQL for dataretrieval, Method for crosstab formatting in app 
/rockmoose
You must unlearn what You have learnt
June 4, 2004 at 5:08 am
If the purpose for producing the cross-tab is to generate a report then most reporting tools (SQL RS or Crystal) will produce the cross-tab for you. YOu can even pull the data into Access via a linked table in Access and it will easily cross-tab the data.
It all depends on what your need is...
June 4, 2004 at 8:55 am
You can use dynamic sql
---------------------------
DECLARE
@CHAR VARCHAR(50),
@TCHAR VARCHAR(50),
@CHARLEN INT,
@BEGIN INT,
@END INT,
@Eventlist varchar(100),
@eventcount int,
@sql VARCHAR(5000),
@MYSQL VARCHAR(200)
SET @sql=''
set @eventlist=''
SELECT @EventLIST=@EventLIST+ISNULL(S.eventid,'')+',' FROM
(SELECT DISTINCT eventid FROM agentevents ) S
SET @MYSQL='SUM(CASE WHEN eventid = ''XYZ'' THEN 1 ELSE 0 END)'
--print @mysql
SET @CHARLEN = LEN(@eventlist)
SET @BEGIN = 1
SET @END = CHARINDEX(',',@eventlist)
WHILE @END < @CHARLEN
BEGIN
IF @END = 0
BEGIN
SET @CHAR = RTRIM(@eventlist)
END
SET @END = CHARINDEX(',',@eventlist, @BEGIN)
IF @END = 0
BEGIN
SET @CHAR = SUBSTRING(@eventlist,@BEGIN,@CHARLEN - @BEGIN + 1)
SET @sql =@SQL+ ','+REPLACE(@MYSQL,'XYZ',@CHAR) + 'AS ' + @CHAR
BREAK
END
SET @CHAR = SUBSTRING(@eventlist,@BEGIN,@END - @BEGIN)
SET @BEGIN = @END + 1
SET @sql =@SQL+ ','+REPLACE(@MYSQL,'XYZ',@CHAR) + 'AS ' + @CHAR
END
SET @sql =RIGHT(@SQL,LEN(@SQL)-1)
SELECT @sql='Select Agentid ,'+@SQL+' FROM AgentEvents
GROUP BY Agentid'
exec(@sql)
----------------------------
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply