Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Sum the counts from two different tables

Sum the counts from two different tables

Scheduled Pinned Locked Moved Database
databasesql-serversysadminregexhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Elizabeth Rani
    wrote on last edited by
    #1

    Hi All, Iam new to database. Here iam using sql server 2000. i want to sum the counts from two different tables. I need this to calculate the employees who have taken continuous leaves in a week/month. Example: I have three table such as Table1:tbl_attandence(it contains the employee id and the intimings and outtime) Fields:empcode,intime,outime Eg: EMP001,11/24/2011 09:30:45 AM,11/24/2011 04:30:15 PM Table2:tbl_leaveentry(it contains the leave dates taken by an employee) Fields:empcode,entrydate Eg: EMP001,11/25/2011 Table3:tbl_holidays(it contains the list of holidays such as sunday,saturday,chrismas,newyear....) Fields:holidays,days Eg: 11/25/2011,sunday I am attaching my code such that it return the sum but iam unable to return the employee name and id because it is showing the result as null as the holidays does not match please help me in completion of process select sum(counts) from( select a.empcode as empcode,b.name,count(*) as counts from tbl_leaveentry a,tbl_emp b where a.empcode=b.empcode and a.empcode='EMP001' and a.entrydate between '11/24/2011'; and '11/27/2011' group by a.empcode,b.name union select null,null,count(*) as counts from tbl_holidays where holidays between '11/24/2011' and '11/27/2011' ) as c having sum(counts)>=3 the above code returns correct sum but if i add name before the sum and execute the result is not been summed because holidays does not match with the leaves Please suggest that how can i do this task any help will be appreciated

    C S 2 Replies Last reply
    0
    • E Elizabeth Rani

      Hi All, Iam new to database. Here iam using sql server 2000. i want to sum the counts from two different tables. I need this to calculate the employees who have taken continuous leaves in a week/month. Example: I have three table such as Table1:tbl_attandence(it contains the employee id and the intimings and outtime) Fields:empcode,intime,outime Eg: EMP001,11/24/2011 09:30:45 AM,11/24/2011 04:30:15 PM Table2:tbl_leaveentry(it contains the leave dates taken by an employee) Fields:empcode,entrydate Eg: EMP001,11/25/2011 Table3:tbl_holidays(it contains the list of holidays such as sunday,saturday,chrismas,newyear....) Fields:holidays,days Eg: 11/25/2011,sunday I am attaching my code such that it return the sum but iam unable to return the employee name and id because it is showing the result as null as the holidays does not match please help me in completion of process select sum(counts) from( select a.empcode as empcode,b.name,count(*) as counts from tbl_leaveentry a,tbl_emp b where a.empcode=b.empcode and a.empcode='EMP001' and a.entrydate between '11/24/2011'; and '11/27/2011' group by a.empcode,b.name union select null,null,count(*) as counts from tbl_holidays where holidays between '11/24/2011' and '11/27/2011' ) as c having sum(counts)>=3 the above code returns correct sum but if i add name before the sum and execute the result is not been summed because holidays does not match with the leaves Please suggest that how can i do this task any help will be appreciated

      C Offline
      C Offline
      Corporal Agarn
      wrote on last edited by
      #2

      Try something like:

      select name, sum(counts)
      from(
      select a.empcode as empcode,b.name,count(*) as counts
      from tbl_leaveentry a
      INNER JOIN tbl_emp b
      ON a.empcode=b.empcode
      where a.empcode='EMP001'
      and a.entrydate between '11/24/2011'; and '11/27/2011'
      group by a.empcode, b.name

      union
      
      select null, null, count(\*) as counts 
      from tbl\_holidays
      where holidays between '11/24/2011' and '11/27/2011'
      

      ) as c
      GROUP BY name
      having sum(counts)>=3

      Note the GROUP BY and I have changed to an INNER JOIN. Hope this helps. Sorry, I do not have time to dive deeper into this. [edit]to spell note correctly[/edit]

      1 Reply Last reply
      0
      • E Elizabeth Rani

        Hi All, Iam new to database. Here iam using sql server 2000. i want to sum the counts from two different tables. I need this to calculate the employees who have taken continuous leaves in a week/month. Example: I have three table such as Table1:tbl_attandence(it contains the employee id and the intimings and outtime) Fields:empcode,intime,outime Eg: EMP001,11/24/2011 09:30:45 AM,11/24/2011 04:30:15 PM Table2:tbl_leaveentry(it contains the leave dates taken by an employee) Fields:empcode,entrydate Eg: EMP001,11/25/2011 Table3:tbl_holidays(it contains the list of holidays such as sunday,saturday,chrismas,newyear....) Fields:holidays,days Eg: 11/25/2011,sunday I am attaching my code such that it return the sum but iam unable to return the employee name and id because it is showing the result as null as the holidays does not match please help me in completion of process select sum(counts) from( select a.empcode as empcode,b.name,count(*) as counts from tbl_leaveentry a,tbl_emp b where a.empcode=b.empcode and a.empcode='EMP001' and a.entrydate between '11/24/2011'; and '11/27/2011' group by a.empcode,b.name union select null,null,count(*) as counts from tbl_holidays where holidays between '11/24/2011' and '11/27/2011' ) as c having sum(counts)>=3 the above code returns correct sum but if i add name before the sum and execute the result is not been summed because holidays does not match with the leaves Please suggest that how can i do this task any help will be appreciated

        S Offline
        S Offline
        SilimSayo
        wrote on last edited by
        #3

        Try this.

        DECLARE
        @holidayCount AS int,
        @startDate AS datetime,
        @endData AS datetime,
        @empCode AS VARCHAR(20)

        SET @startDate='11/24/2011 00:00:00.000'
        SET @endDate='11/27/2011 23:59:59.997'
        SET @empCode='EMP001'

        SET @holidayCount=(SELECT COUNT(*) FROM tbl_holidays WHERE
        holidays BETWEEN @startDate AND @endDate)

        --I am assuming that 'holidays' is a datetime field/column

        SELECT empCode, name, (Counts + @holidayCount) As holidayCounts
        FROM
        (
        SELECT a.empCode, b.name, (Count(*) as Counts
        FROM tbl_leaveentry a,tbl_emp b
        WHERE a.empCode=b.empCode AND
        a.empCode=@empCode AND
        a.entryDate @startDate and @endDate
        GROUP BY by a.empCode,b.name
        ) AS temp
        WHERE Counts + @holidayCount>3

        E 1 Reply Last reply
        0
        • S SilimSayo

          Try this.

          DECLARE
          @holidayCount AS int,
          @startDate AS datetime,
          @endData AS datetime,
          @empCode AS VARCHAR(20)

          SET @startDate='11/24/2011 00:00:00.000'
          SET @endDate='11/27/2011 23:59:59.997'
          SET @empCode='EMP001'

          SET @holidayCount=(SELECT COUNT(*) FROM tbl_holidays WHERE
          holidays BETWEEN @startDate AND @endDate)

          --I am assuming that 'holidays' is a datetime field/column

          SELECT empCode, name, (Counts + @holidayCount) As holidayCounts
          FROM
          (
          SELECT a.empCode, b.name, (Count(*) as Counts
          FROM tbl_leaveentry a,tbl_emp b
          WHERE a.empCode=b.empCode AND
          a.empCode=@empCode AND
          a.entryDate @startDate and @endDate
          GROUP BY by a.empCode,b.name
          ) AS temp
          WHERE Counts + @holidayCount>3

          E Offline
          E Offline
          Elizabeth Rani
          wrote on last edited by
          #4

          Hi SilimSayo, Thank you so much for the query it matches my result perfectly.

          S 1 Reply Last reply
          0
          • E Elizabeth Rani

            Hi SilimSayo, Thank you so much for the query it matches my result perfectly.

            S Offline
            S Offline
            SilimSayo
            wrote on last edited by
            #5

            No problem.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups