SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help with a query for report


Need help with a query for report

Author
Message
Renuka__
Renuka__
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2330 Visits: 1379
Hi,

We have a table in the following format:
ServerName ServerFunction BackupReqCode
Server1 IS 0
Server2 Ops 0
Server3 Ops 2
Server4 Ops 4
Server5 IS 1
Server6 IS 5
Server7 Ops 0
Server8 IS 2
Server9 Ops 3
Server10 Ops 3

The requirement is to generate a report in the following format:

BackupReqCode CountOfISServers CountOfOpsServers
0 1 2
1 1 0
2 1 1
3 0 2
4 0 1
5 1 0

With the very limited TSQL knowledge I have, I am able to get either first two columns(BackupCode, CountofISServers) or the first and third (BackupCode and CountOfOpsServers) but not the all the three columns in one result set. We dont have any codes for serverfunction so only way to differentiate is 'WHERE ServerFunction Like '<>'. This is for a report, if it is not possible from TSQL, I am hoping to get any work around from SSRS (like joining two datasets etc.,).

Any help or suggestion would be greatly appreciated.

Thank you
Renuka__

Renuka__
anthony.green
anthony.green
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63074 Visits: 8598
This work for you?


DECLARE @Temp TABLE (ServerName VARCHAR(10), ServerFunction VARCHAR(3), BackupReqCode TINYINT)
INSERT INTO @Temp VALUES
('Server1','IS',0),
('Server2','Ops',0),
('Server3','Ops',2),
('Server4','Ops',4),
('Server5','IS',1),
('Server6','IS',5),
('Server7','Ops',0),
('Server8','IS',2),
('Server9','Ops',3),
('Server10','Ops',3)

SELECT
BackupReqCode,
ISNULL(SUM(CASE WHEN ServerFunction = 'IS' THEN 1 END),0) AS 'IS',
ISNULL(SUM(CASE WHEN ServerFunction = 'OPS' THEN 1 END),0) AS 'OPS'
FROM
@Temp
GROUP BY
BackupReqCode





How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Renuka__
Renuka__
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2330 Visits: 1379
Hi Anthony....

Works as exactly how I wanted.
Many thanks... :-)

Renuka__

Renuka__
writearjun53
writearjun53
Mr or Mrs. 500
Mr or Mrs. 500 (550 reputation)Mr or Mrs. 500 (550 reputation)Mr or Mrs. 500 (550 reputation)Mr or Mrs. 500 (550 reputation)Mr or Mrs. 500 (550 reputation)Mr or Mrs. 500 (550 reputation)Mr or Mrs. 500 (550 reputation)Mr or Mrs. 500 (550 reputation)

Group: General Forum Members
Points: 550 Visits: 227
This query gives output as you required please try

SELECT temp.BackupReqCode,max(case WHEN temp.ServerFunction='IS' THEN temp.CountOfOpsServer ELSE 0 END ) as 'CountOfISServer',
max(case WHEN temp.ServerFunction='Ops' THEN temp.Countofserver ELSE 0 END ) as 'CountOfOpsServer' from
(SELECT BackupReqCode,ServerFunction,count(ServerName)as 'Countofserver'
from TabName
group by BackupReqCode,ServerFunction) temp
group by temp.BackupReqCode


Regards,
Arjun Singh Shekhawat
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search