I have two tables Employee and Attendance, now i want date wise record both absent and present employee.
Like:--
Table1: Employee (EmpId,Name)
Table2: Attendance (Empid, CheckInOutDateTime, Status (like --I/O))
(Record will not available when Employee is Absent in Attendance Table but need record)
Now I want output:
**EmpID Name Date Status**
1 Emp1 06/11/2013 Persant
2 Emp2 06/11/2013 Absant
3 Emp3 06/11/2013 Persant
1 Emp1 06/12/2013 Persant
2 Emp2 06/12/2013 absant
3 Emp3 06/12/2013 absant
Try like this...
Select Empid,EmpName,Date1,(
cASE WHEN EXISTS(
SELECT EMPID FROM Atten AT WHERE T.EMPID=AT.EMPID AND T.date1=AT.Chechout
) then 'Present' Else 'Absent' End )as Status
FROM
(
Select Empid,EmpName,Cast(Chechout as DATE)AS DATE1 from Emp a,(Select Distinct Chechout from Atten) b
) T
in ms access-:
Select Empid,EmpName,Date1,(iif((SELECT Count(Empid)FROM Atten AT WHERE T.EMPID=AT.EMPID AND T.date1=DateVAlue(AT.Chechout))>0,'Absent','Present')) as Status
FROM(
Select Empid,EmpName,DateVAlue(Chechout) AS DATE1 from Emp a,(Select Distinct DateValue(Chechout) as Chechout from Atten) b
)T
SQL Fiddle Demo
The SQL feature you are looking for is called an outer join. You may also want to read up on the other SQL features.
If for whatever reason you cannot use an outer join, you can use a subselect and count the records in the attendance table because you are only interested in a boolean value, either there or not there for a given day (count > 0, employee was present, count = 0, employee was absent).
I could probably write a fancy query to get the results you want, but it would also require a 3rd table with every possible date.
I think it would be easier to have a scheduled task that added the absent records at the end of each day. Or you can make everyone absent ahead of time, then change the record to present as the employee shows up.
You can try this:
SELECT A.EmpId,A.Name,B.CheckInOutDateTime,status = CASE WHEN B.STATUS = 'IN' THEN 'PRESENT'
ELSE 'ABSENT' END from Table1 A join Table2 B ON A.Empid=B.empid
Here is the pseudo code:
for each date as varDate
query = "SELECT COUNT(*) AS cnt FROM Employee te LEFT JOIN Attendance ta ON te.Empid = ta.Empid WHERE ta.CheckInOutDateTime = " + varDate.ToString() + " GROUP BY te.Empid";
While displaying the result of above query, you can do this:
if Convert.ToInt32(dataRow("cnt")) > 0
status = "Present";
else
status = "Absent";
Related
Suppose I have a table with two columns:
TABLE A
-------
ProjectID NUMBER
STATUS VARCHAR2(6) // either 'CLOSED' or 'NEW'
There could be maximum two entries for a ProjectID with the two possible values of STATUS and the combination (ProjectID, STATUS) is unique.
I need to select only those ProjectID's that have status 'NEW'. Also, if for a projectID, there are two entries with different statuses (NEW and CLOSED), I don't want it in the output.
I tried using group by, then ordering the resultset descending (so as to get 'NEW' row for a project ID first) and then taking the first row in LINQ, similar to this:
var query = (from a in context.A.Where(o => o.STATUS == 'NEW')
group a by a.ProjectID into groups
select groups.OrderByDescending(o => o.ProjectID)
.ThenBy(o => o.STATUS)
.FirstOrDefault());
Butt it's resulting into an "APPLY" clouse in the query which is resulting into an error. Apparantly, Oracle 10g doesn't support it.
Any help is appreciated.
Something like this, perhaps?
SQL> with test (projectid, status) as
2 (select 1, 'new' from dual union -- should be returned
3 select 2, 'new' from dual union
4 select 2, 'closed' from dual union
5 select 3, 'closed' from dual union
6 select 4, 'new' from dual -- should be returned
7 )
8 select projectid
9 from test
10 group by projectid
11 having min(status) = max(status)
12 and min(status) = 'new';
PROJECTID
----------
1
4
SQL>
Proper tu use having count(distinct STATUS=1) :
create table tableA( ProjectID int, STATUS varchar2(10) );
insert all
into tableA values(1 ,'NEW')
into tableA values(1 ,'CHANGED')
into tableA values(2 ,'NEW')
into tableA values(3 ,'CHANGED')
select * from dual;
/
select * from
(
select ProjectID, max(STATUS) STATUS
from tableA
group by ProjectID
having count(distinct STATUS)=1
)
where STATUS = 'NEW';
I believe I have accomplished what you want, using a subquery in LINQ.
var query = (from a in context.A
where (from b in context.A
where b.ProjectID == a.ProjectID
select new { a.ProjectID, a.STATUS }).Distinct().Count() == 0
&& a.STATUS == "NEW"
select a.ProjectID).ToList();
Essentially, the outer query just makes sure that each a record has a NEW status, and the inner query makes sure that there are no two distinct records with the given ProjectID, because if there are, one is CLOSED. I avoided using a GROUP BY since you said your database does not support LINQ's way of doing it.
I hope I understood your problem correctly, and I hope this helps!
I have written the below query to list all the rows in table [HRSurvey]
which empid is only present in empsurveyselection. Both table have empid columns.
But i am not able to get the proper results because there
might be multiple empid for same surveyid in empsurveyselection table.
But this query works fine when there is only one empid for each surveyid in empsurveyselection table.
Could you please rewrite the query to list the all the rows in the table HRSurvey where the empid are there in empsurveyselection ?
SELECT hrs.*
FROM [HRSurvey] hrs
Left Join [HRSurveyEmployee] hse
ON hse.EmpID = hrs.EmpID
LEFT Join Surveys s
ON s.surveyid = hse.surveyid
WHERE hrs.empid IN (
SELECT empid FROM [HRSurveyEmployee] where surveyid = s.surveyid
) and sempid
in ( select DISTINCT empid FROM empsurveyselection WHERE deptid=9 and surveyid = s.surveyid)
You can write as:
SELECT hrs.col1, -- Worst Practice to use * in production code to pull data
hrs.col12 -- Use explicit column names instead
FROM [HRSurvey] hrs
INNER JOIN [HRSurveyEmployee] hse ON hse.EmpID = hrs.EmpID
INNER JOIN [Surveys] s ON s.surveyid = hse.surveyid
INNER JOIN [empsurveyselection]ess ON ess.deptid=9
AND ess.surveyid = s.surveyid AND hrs.sempid = ess.empid
Please try with the below query....
select a.*
from HRSurvey a
where empid exists (select 1
from empsurveyselection b
where a.Empid = b.Empid)
Thanks for the help! Actually issue was not with the query. Issue was with the data and i fixed it.
I have 7 tables and each table will contain an entry for a particular product.I want to check whether all 7 tables contains entry for a particular ID(eg: 4562). ie, data exists or not.I am using SQL server 2008.Please help me to write a query to check the status.
Try the following command (example for 3 tables T1,T2,T3). It returns 1 if ID = 4562 exists in ALL tables and 0 if at least one table miss this ID.
SELECT
CASE WHEN
(
EXISTS(SELECT ID FROM T1 WHERE ID=4562)
AND EXISTS(SELECT ID FROM T2 WHERE ID=4562)
AND EXISTS(SELECT ID FROM T3 WHERE ID=4562)
)
THEN 1
ELSE 0
END AS [ID_Exists_in_all_tables]
SQLFiddle demo
If you do a basic join rather than left join, the product will only appear if it's in all of the tables.
select * from tab1
join tab2 on tab2.id = tab1.id
join tab3 on tab3.id = tab1.id
join tab4 on tab4.id = tab1.id
join tab5 on tab5.id = tab1.id
Where tab1.id = 1234
etc etc
In my database i have column name department and employed(return type is bit - 0 or 1), i would like to count the number of employee employed in each department (there are 5 departments) for this year and rank the department who has the highest employee. I need help combining SQL query.
This is how i can calculate 0'1 and 1's SELECT COUNT(CASE Authorised WHEN 1 THEN 1 ELSE NULL END) FROM TableName;
and this is how i can select date for this year: `
SELECT distinct *
From TableName
WHERE Time BETWEEN CONVERT(Date,#Time) AND CONVERT(Date, #Time2)
How do i combine these query?
You need to use this query, the query returns all departments name and the total of employees in it.
SELECT
DEPARTMENTNAME, COUNT(*) AS TOTAL
FROM
TABLENAME
WHERE
TIME BETWEEN
CONVERT(Date,#Time)
AND
CONVERT(Date, #Time2)
GROUP BY DEPARTMENTNAME
ORDER BY COUNT(*) DESC
This is an example of how your sql should be.
declare #WhatEverYourTableNameIs Table
(
Department varchar(3),
employed bit,
employeddate date
)
Insert into #WhatEverYourTableNameIs
Values
('dem',1,'2014-1-1'),
('dem',1,'2014-2-1'),
('dem',0,'2014-2-1'),
('eem',1,'2013-2-1'),
('dem',1,'2013-2-1'),
('eem',1,'2014-2-1')
Select Department, Count(employed) AS Cnt
From #WhatEverYourTableNameIs
Where employeddate >'2013-12-31' and employed = 1
Group by Department
order by Cnt desc
I have 2 tables :
Teachers :
teacher_id
teacher_name
=============
Timetable:
tt_id
tt_teacher_id
tt_term_id
tt_day_id
tt_hour_id
=====================================
I want 2 type SQL select query :
1)(this query for select teachers and set into combobox) select teachers_name that not exists in records with special parameters in timetable
tt_term_id=parameter1
tt_day_id =parameter2
tt_hour_id=parameter3
teacher names comes from teachers table based on above conditions in timetable table .
2) select teachers_name that not exists in records with special parameters in timetable
tt_term_id=parameter1
tt_day_id =parameter2
tt_hour_id=parameter3
tt_teacher_id !=parameter4
teacher names comes from teachers table based on above conditions in timetable table .
Thanks a lot
You can join the tables based on the teacher id and use whatever conditions you want.
SELECT DISTINCT teacher_id, teacher_name
FROM Teachers
INNER JOIN Timetable
ON teacher_id = tt_teacher_id
WHERE tt_term_id = parameter1
AND tt_day_id = parameter2
AND tt_hour_id = parameter3
ORDER BY teacher_name
The other one you will be able to come up with yourself :-)
If you want teachers who do not have any timetable records then you can do:
SELECT DISTINCT teacher_id, teacher_name
FROM Teachers
LEFT JOIN Timetable
ON teacher_id = tt_teacher_id
WHERE tt_id is NULL
ORDER BY teacher_name