Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Complicated Query

Author  Topic 

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2007-06-06 : 15:08:53
Hi Guys,

Here is my Question:

I have 2 tables


Create table #UserLeave (UserID int, LeaveDate datetime, PortionOfDay decimal(8,2), ReasonID int)
Create table #UserLeaveReasons (ReasonID int, [ReasonText] varchar(100), [Group] varchar(100))

Insert Into #UserLeaveReasons values (1, 'Vacation', 'Vacation')
Insert Into #UserLeaveReasons values (2, 'Sick', 'Sick')
Insert Into #UserLeaveReasons values (3, 'Maternity', 'Other')
Insert Into #UserLeaveReasons values (4, 'Jury Duty', 'Other')

Insert Into #UserLeave values (123, '06/06/2007', 0.5, 2)
Insert Into #UserLeave values (123, '06/08/2007', 1.0, 1)
Insert Into #UserLeave values (123, '06/13/2007', 0.5, 1)
Insert Into #UserLeave values (123, '06/14/2007', 1.0, 2)
Insert Into #UserLeave values (123, '06/15/2007', 1.0, 3)
Insert Into #UserLeave values (123, '06/26/2007', 1.0, 4)

Insert Into #UserLeave values (123, '06/06/2006', 1.0, 3)
Insert Into #UserLeave values (123, '06/08/2006', 1.0, 1)
Insert Into #UserLeave values (123, '06/13/2006', 1.0, 1)
Insert Into #UserLeave values (123, '06/14/2006', 0.5, 3)
Insert Into #UserLeave values (123, '06/15/2006', 0.5, 3)
Insert Into #UserLeave values (123, '06/26/2006', 0.5, 4)

Insert Into #UserLeave values (222, '06/06/2007', 0.5, 2)
Insert Into #UserLeave values (222, '06/08/2007', 1.0, 1)
Insert Into #UserLeave values (222, '06/13/2007', 0.5, 1)
Insert Into #UserLeave values (222, '06/14/2007', 1.0, 2)
Insert Into #UserLeave values (222, '06/15/2007', 1.0, 3)
Insert Into #UserLeave values (222, '06/26/2007', 1.0, 4)

Insert Into #UserLeave values (222, '06/06/2006', 1.0, 2)
Insert Into #UserLeave values (222, '06/08/2006', 1.0, 1)
Insert Into #UserLeave values (222, '06/13/2006', 1.0, 1)
Insert Into #UserLeave values (222, '06/14/2006', 0.5, 2)
Insert Into #UserLeave values (222, '06/15/2006', 0.5, 3)
Insert Into #UserLeave values (222, '06/26/2006', 0.5, 4)

-- I want to query by UserID
-- Considering all groups of #UserLeaveReasons, I need to display the total # of leave for each year in a seperate column.
-- Assume only the year 2006 & 2007 data is present.
-- I need the results set when queried as follows (queried for the userid = 123)
-- Also I need it in the same order as given below ( I can put a Column to do "Order By " in #UserLeaveReasons

Leave Type 2006 2007
--------- ---- ----
Vacation 2.0 1.5
Sick 0.0 1.5
Other 2.5 2.0


I tried Case - When, Group By, Sum() .... nothing worked


Srinika

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2007-06-06 : 15:41:59
OK Guys,

I got closer



Select [Group],
Sum(Case When LeaveDate between '2006-01-01' and '2006-12-31' then PortionOfDay else 0 end ) as [2006],
Sum(Case When LeaveDate between '2007-01-01' and '2007-12-31' then PortionOfDay else 0 end ) as [2007],
Sum(Case When LeaveDate between '2008-01-01' and '2008-12-31' then PortionOfDay else 0 end ) as [2008]
from #UserLeave ul
left Join #UserLeaveReasons ulr On ulr.[ReasonID] = ul.[ReasonID]
where UserID = 123
Group By [Group]



But still, if all the data in the ReasonID Column of #UserLeaveReasons, I need the other 2 types with 0.00 as their sum for the corresponding year.

How can I get that ?

Srinika
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-06-06 : 16:14:43
You have it backwards -- select FROM the userleaveReasons table and then left join TO the UserLeave table:



Select ulr.[Group],
Sum(Case When ul.LeaveDate between '2006-01-01' and '2006-12-31' then ul.PortionOfDay else 0 end ) as [2006],
Sum(Case When ul.LeaveDate between '2007-01-01' and '2007-12-31' then ul.PortionOfDay else 0 end ) as [2007],
Sum(Case When ul.LeaveDate between '2008-01-01' and '2008-12-31' then ul.PortionOfDay else 0 end ) as [2008]
from #UserLeaveReasons ulr
left Join #UserLeave ul On ulr.[ReasonID] = ul.[ReasonID] and ul.UserID = 123
Group By ulr.[Group]



Also, always prefix all of your columns with a table name or alias, that makes your SQL easier to read and work with; without the schema, you cannot tell which table Group comes from, for example, without a prefix.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2007-06-07 : 10:13:04
Thanks Jeff,

Yes I used the Join in the opposite way. That I corrected.
Could you please tell me the different in results due to use of "UserID = 123"
-- in the Join's ON condition
&
-- in the Where Clause

each way gives different results.




Srinika
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-06-07 : 10:19:49
see:

http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/60205.aspx

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -