Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi...

I have one Table name canteen contain the information lunch ,breakfast cost...

SQL
EMPID   Name  Checktime   Category     Emp_Contrii  Comp_Contri   TOTAL
------      ---------  -----   -------- -----------      --------   -----------  ----------
BIN0112     Rajendran V     11/9/2010 07:54   BREAKFAST   10          20          30  
BIN0112      Rajendran V    12/4/2010 07:54   BREAKFAST   10          20          30  
BIN0113      Anand J        6/29/2010 07:49   BREAKFAST   10          20          30  
BIN0113      Anand J        6/30/2010 02:48   LUNCH       27          33          60  
BIN0114      xyz J          6/30/2010 07:48   BREAKFAST   10          20          30  
BIN0113      Anand J        7/1/2010 07:36    BREAKFAST   10          20          30  
BIN0117      Arvind         7/2/2010 07:49    BREAKFAST   10          20          30  
BIN0113      Anand J        7/6/2010 07:39    BREAKFAST   10          20          30  
BIN0119      Anna J         7/8/2010 02:27    LUNCH       27          33          60  
BIN0113      Anand J        7/9/2010 07:45    BREAKFAST   10          20          30  
BIN0118      cmf J          7/10/2010 07:45   BREAKFAST   10          20          30  
BIN0110      Anand J        7/12/2010 07:41   BREAKFAST   10          20          30  
BIN0113      Anand J        7/13/2010 02:45   LUNCH       27          33          60  

And i have one more table canteenTotal conatiin the information of all the employee of one month..

SQL
EMPID   Name          GRAND Emp_Contrii GRAND Comp_Contri  GRANDTOTAL
------ ------------   -----------------  -----------------  ----------- 
BIN0111 a
BIN0112 b
BIN0113 c
BIN0114 d
BIN0115 e
BIN0116 f
BIN0117 g
BIN0118 h
BIN0119 i
BIN0120 j
BIN0121 k
BIN0122 l
BIN0123 m


here name and EMPID are present ...
now i want to write a query to fill the sum of Emp_Contrii in GRAND Emp_Contrii same for other two column between the two dates where both the tables EMPID should be same. for example 1/02/2013 to 30/02/2013.
Posted
Updated 13-Aug-13 0:52am
v4

check below query

SQL
SELECT EMPID, Name, sum(Emp_Contrii), sum(Comp_Contri), sum(Emp_Contrii) + sum(Comp_Contri) as TOTAL
FROM canteen
WHERE  Checktime between '1-Jan-2013' and '30-Jan-2013'
GROUP BY EMPID, Name
 
Share this answer
 
Comments
Nawab Ahmad 13-Aug-13 7:12am    
Hello,
thanks for your reply...
I want to generate the report of all the employee whose EMPID is present in the canteenTotal..

suppose i have 4 employee
EMPID
----
1
2
3
4
so when i run the query it should effect all the employees and will comw like..

EMPID Name GRAND Emp_Contrii GRAND Comp_Contri GRANDTOTAL
------ ------------ ----------------- ----------------- -----------
BIN0111 1 1234 1548 10047
BIN0112 2
BIN0113 3
BIN0114 4

plz help....
The below query will give you monthly reports:

SQL
SELECT CONVERT( VARCHAR(7),Checktime,121)'Year-Month',
        EMPID,
        Name,
        SUM(Emp_Contrii) Total_Emp_Contrii,
        SUM(Comp_Contri) Total_Comp_Contri,
        SUM(TOTAL) TOTAL
FROM CANTEEN
GROUP BY CONVERT( VARCHAR(7),Checktime,121), EMPID, Name
 
Share this answer
 
You can use this SQL to insert the select result into another table:

SQL
Insert into canteenTotal (EMPID, Name, GRAND_Emp_Contrii, GRAND_Comp_Contri, GRANDTOTAL)
Select EMPID, Name, sum(Emp_Contrii), sum(Comp_Contri), sum(TOTAL)
From canteen
Where Checktime between '1-Jan-2013' and '30-Jan-2013'
Group by EMPID, Name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900