|
|
 |
| SQL Server 2005 General discussion of SQL Server *2005* version only. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2005 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

July 17th, 2007, 08:52 PM
|
|
Authorized User
|
|
Join Date: Dec 2006
Location: Texas, TX, USA.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
SQL Crazy Query...
CREATE TABLE EMPDev
(eno int,
ENAME varchar(20),
DName varchar(20),
Hours Decimal(10,2),
WorkType varchar(20));
INSERT INTO EMPDev Values(1,'xyz','Computers',14.12,'HOME');
INSERT INTO EMPDev Values(2,'xyz','Computers',15.20,'OFFICE')
INSERT INTO EMPDev Values(3,'xyz','Finance',16.30,'HOME')
INSERT INTO EMPDev Values(4,'xyz','Computers',14.10,'OFFICE')
INSERT INTO EMPDev Values(5,'xyz2','Computers',7.30,'HOME')
INSERT INTO EMPDev Values(6,'xyz2','Computers',16.45,'OFFICE')
INSERT INTO EMPDev Values(7,'xyz3','Computers',2.20,'HOME')
INSERT INTO EMPDev Values(8,'xyz4','Computers',14.10,'OFFICE')
I Want output like
ENAME DNAME HOME_Hours OFFICE_Hours
xyz Computers 14.12 29.50
xyz Finance 16.30 0
xyz2 Computers 7.30 16.45
xyz3 Computers 2.20 0
xyz4 Computers 0 14.20
Please help me.
Thanks in advance
|

July 18th, 2007, 02:50 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Location: Delhi, Delhi, India.
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
CREATE FUNCTION dbo.GetHours
(
@eno int,
@WorkType nvarchar(20)
)
RETURNS decimal
AS
BEGIN
Declare @w_hours decimal
set @w_hours = 0
if(@WorkType = ( Select WorkType From EMPDev Where eno = @eno))
Begin
Select @w_hours = [Hours] From EMPDev Where eno = @eno
End
RETURN @w_hours
END
-----------------------------------
Select EName,DName, dbo.GetHours(eno,'HOME') As HOME_Hours , dbo.GetHours(eno,'OFFICE') As OFFICE_Hours
From EMPDev
Bijgupt
|

July 18th, 2007, 02:26 PM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Why a UDF?
SELECT ENAME,
DName,
SUM(CASE WHEN WorkType = 'HOME' THEN Hours ELSE 0 END) AS Home_Hours,
SUM(CASE WHEN WorkType = 'OFFICE' THEN Hours ELSE 0 END) AS Office_Hours
FROM Table1
GROUP BY ENAME,
DName
ORDER BY ENAME,
DName
|

July 18th, 2007, 09:52 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Location: , MI, USA.
Posts: 454
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
To make matters worse, the UDF doesn't do the required SUM ;)
--Jeff Moden
|

July 19th, 2007, 01:51 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Location: Delhi, Delhi, India.
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Nice, Peso q'ry is performance wise much better.
Bijgupt
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |