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


Need the Recordset with high performance


Need the Recordset with high performance

Author
Message
lokesha.b
lokesha.b
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 32
DECLARE @TblData TABLE( OrganizationName VARCHAR(100), DeptName VARCHAR(50), Total int)

INSERT INTO @TblData VALUES( 'AAACompanyLtd', 'HumanResourse', 122)
INSERT INTO @TblData VALUES( 'AAACompanyLtd', 'Finance', 83)
INSERT INTO @TblData VALUES( 'AAACompanyLtd', 'HelpDesk', 63)
INSERT INTO @TblData VALUES( 'AAACompanyLtd', 'ITDevision', 563)
INSERT INTO @TblData VALUES( 'XYZprivateLtd', 'HumanResourse', 12)
INSERT INTO @TblData VALUES( 'XYZprivateLtd', 'Finance', 31)
INSERT INTO @TblData VALUES( 'XYZprivateLtd', 'HelpDesk', 42)
INSERT INTO @TblData VALUES( 'TeamOutsouce', 'Finance', 21)
INSERT INTO @TblData VALUES( 'TeamOutsouce', 'ITDevision', 11)

select * from @TblData

I need the record set in MS SQL as below. Please help. I am using MS SQL server 2008

--------------------------------------------------------------------------------------
SlNo# OrganizationName HumanResourse Finance HelpDesk ITDevision GrandTotal
--------------------------------------------------------------------------------------
1 AAACompanyLtd 122 83 63 563 831
2 XYZprivate Ltd 12 31 42 - 85
3 TeamOutsouce - 21 - 11 32
-------------------------------------------------------------------------------------
GrandTotal 134 135 105 574 948
-------------------------------------------------------------------------------------

Thanks and Regards,
Lokesh GB
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16125 Visits: 19534
-- standard crosstab query
SELECT [SlNo#] = ROW_NUMBER() OVER(ORDER BY GrandTotal DESC),
OrganizationName, HumanResourse, Finance, HelpDesk, ITDevision, GrandTotal
FROM (
SELECT
OrganizationName,
HumanResourse = SUM(CASE WHEN DeptName = 'HumanResourse' THEN Total ELSE 0 END),
Finance = SUM(CASE WHEN DeptName = 'Finance' THEN Total ELSE 0 END),
HelpDesk = SUM(CASE WHEN DeptName = 'HelpDesk' THEN Total ELSE 0 END),
ITDevision = SUM(CASE WHEN DeptName = 'ITDevision' THEN Total ELSE 0 END),
GrandTotal = SUM(Total)
FROM @TblData
GROUP BY OrganizationName
) d



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Neeraj Dwivedi
Neeraj Dwivedi
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1099 Visits: 1334
He can use roll up to get the bottom total too like below but I am not able to sort it right as he want based on Grandtotal.

SELECT [SlNo#] = ROW_NUMBER() OVER(ORDER BY OrganizationName DESC), 
OrganizationName, HumanResourse, Finance, HelpDesk, ITDevision, GrandTotal
FROM (
SELECT top 100 percent
CASE WHEN (GROUPING(OrganizationName) = 1) THEN ' GrandTotal'
ELSE ISNULL(OrganizationName, 'UNKNOWN') end as OrganizationName ,
--OrganizationName,
HumanResourse = SUM(CASE WHEN DeptName = 'HumanResourse' THEN Total ELSE 0 END),
Finance = SUM(CASE WHEN DeptName = 'Finance' THEN Total ELSE 0 END),
HelpDesk = SUM(CASE WHEN DeptName = 'HelpDesk' THEN Total ELSE 0 END),
ITDevision = SUM(CASE WHEN DeptName = 'ITDevision' THEN Total ELSE 0 END),
GrandTotal = SUM(Total)
FROM TblData
GROUP BY OrganizationName WITH ROLLUP
--COMPUTE SUM(Total)

) d


lokesha.b
lokesha.b
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 32
Thanks Guys It helped me lot.

Also I tried it to sortby Organizations since GrandTotal needs to appear on bottom

SELECT [SlNo#] = ROW_NUMBER() OVER(ORDER BY CASE WHEN ORGANIZATIONNAME = 'GrandTotal' THEN 'ZZZZZZ' ELSE ORGANIZATIONNAME END Asc), 
ORGANIZATIONNAME, HUMANRESOURSE, FINANCE, HELPDESK, ITDEVISION, GRANDTOTAL
FROM (
SELECT top 100 percent
CASE WHEN (GROUPING(OrganizationName) = 1) THEN 'GrandTotal'
ELSE ISNULL(OrganizationName, 'UNKNOWN') end as OrganizationName ,
--OrganizationName,
HumanResourse = SUM(CASE WHEN DeptName = 'HumanResourse' THEN Total ELSE 0 END),
Finance = SUM(CASE WHEN DeptName = 'Finance' THEN Total ELSE 0 END),
HelpDesk = SUM(CASE WHEN DeptName = 'HelpDesk' THEN Total ELSE 0 END),
ITDevision = SUM(CASE WHEN DeptName = 'ITDevision' THEN Total ELSE 0 END),
GrandTotal = SUM(Total)
FROM @TblData
GROUP BY OrganizationName WITH ROLLUP
) d


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