p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > SQL Server > SQL Server 2005 > SQL Server 2005
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 17th, 2007, 08:52 PM
Authorized User
Points: 54, Level: 1
Points: 54, Level: 1 Points: 54, Level: 1 Points: 54, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2006
Location: Texas, TX, USA.
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old July 18th, 2007, 02:50 AM
Friend of Wrox
Points: 242, Level: 4
Points: 242, Level: 4 Points: 242, Level: 4 Points: 242, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2006
Location: Delhi, Delhi, India.
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old July 18th, 2007, 02:26 PM
Friend of Wrox
Points: 751, Level: 10
Points: 751, Level: 10 Points: 751, Level: 10 Points: 751, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old July 18th, 2007, 09:52 PM
Friend of Wrox
Points: 1,412, Level: 15
Points: 1,412, Level: 15 Points: 1,412, Level: 15 Points: 1,412, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2006
Location: , MI, USA.
Posts: 454
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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

--Jeff Moden
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old July 19th, 2007, 01:51 AM
Friend of Wrox
Points: 242, Level: 4
Points: 242, Level: 4 Points: 242, Level: 4 Points: 242, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2006
Location: Delhi, Delhi, India.
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Bijgupt
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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



All times are GMT -4. The time now is 12:43 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc