Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2010
    Posts
    1

    Unanswered: sql select sum query--urgent help.

    Ive two tables as:

    declare @t1 table(userid1 varchar(20),workhours1 int, calendarday1 datetime)
    insert into @t1 values('abc1', 3, '7/1/2010')
    insert into @t1 values('abc1', 5, '7/2/2010')
    insert into @t1 values('abc1', 7, '7/3/2010')
    declare @t2 table(userid2 varchar(20),workhours2 int, calendarday2 datetime)
    insert into @t2 values('abc1', 8, '7/1/2010')
    insert into @t2 values('abc1', 4, '7/4/2010')
    insert into @t2 values('abc1', 6, '7/5/2010')

    and I want the below result..sum the workhours when calendarday1=calendarday2; and also display workhours for calendarday1 & calendarday2..

    Userid Workhour calendarday
    abc1 11 07/01/2010
    abc1 5 07/02/2010
    abc1 7 07/03/2010
    abc1 4 07/04/2010
    abc1 6 07/05/2010

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Code:
    SELECT c1 AS Userid, SUM(
    v66) AS Workhour, z2 AS 
    calendarday FROM (SELECT 
    userid1 AS c1, workhours1 
    AS v66, calendarday1 AS z2 
    FROM @t1 UNION ALL SELECT 
    * FROM @t2) AS z GROUP BY 
    c1, z2 ORDER BY 1, 3
    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •