Wrox Programmer Forums
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 17th, 2007, 07:52 PM
Authorized User
 
Join Date: Dec 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to devendar
Default 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
 
Old July 18th, 2007, 01:50 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old July 18th, 2007, 01:26 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old July 18th, 2007, 08:52 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

To make matters worse, the UDF doesn't do the required SUM ;)

--Jeff Moden
 
Old July 19th, 2007, 12:51 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nice, Peso q'ry is performance wise much better.

Bijgupt





Similar Threads
Thread Thread Starter Forum Replies Last Post
Almost crazy trevo4f BOOK: Beginning ASP 3.0 2 May 15th, 2008 11:22 AM
[Discussion] - Crazy or Odd .NET Hacks dparsons General .NET 1 August 31st, 2007 08:28 PM
Crazy scrollbar issue Teessider_2000 CSS Cascading Style Sheets 3 August 21st, 2006 07:41 AM
session.use_trans_sid is driving me crazy!! Snib Pro PHP 6 July 24th, 2004 04:51 AM
Crazy Thing happening Kenny Alligood Access VBA 10 February 17th, 2004 12:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.