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
Related
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 three tables in SQL Server where I need to combine all matching rows from all tables into a fourth MergedTable that will contain all the columns from the three individual tables based on the U_ID column.
Is there a way of doing this via T-SQL in a stored procedure, or should I just create a loop function in C#?
Bottom line is this is going to be executed from a command from a website, so it needs to be something I can encapsulate into an MVC project or component.
Here is an example of the tables.
Table 1:
U_ID ClientNumber OrderDate Amount
---------------------------------------------
BB000Kw 1920384 5/14/2013 1093.39
AA000bM 3839484 12/8/2012 584.42
AA000gH 8294848 2/28/2014 4849.38
AA000md 3849484 4/31/2013 590.84
AA000mF 3998398 3/29/2013 448.82
AA000mG 9944848 11/28/2014 98.85
AA000mn 0292938 10/31/2012 300.48
Table 2:
U_ID Name Date
------------------------------------------
AA000bM "Krivis, Jeffrey" 7/1/2002
AA000bv "Saydah, Michael" 7/30/2002
AA000cA "Byrne, Richard" 4/21/2003
AA000dd "McNeil, Joseph" 6/10/2003
AA000dH "Greenberg, Arnold" 1/16/2003
AA000gH "Rich, Elwood" 7/5/2003
AA000id "O'Neill, Robert J." 11/20/2002
AA000jf "Patsey, Richard" 4/22/2003
AA000jr "Jones, Arthur" 7/1/2002
AA000jU "Toff, Ronald" 7/15/2002
AA000k4 "Anderson, Carl" 8/14/2002
BB000Kw "Wilson, Sam" 3/9/2003
Table 3:
U_ID Name
-----------------------------
AA000bM Acme Company
AA000jr Stockwell Industries
BB000ke Gensen Motors
BB999di Falstaff Cards
BB000dl B and R Printing
BB000Kw Go Golf Carts
AA000gH Rich's Sandwiches
Resulting merged table
U_ID ClientNumber OrderDate Amount CustomerName JoinDate CompanyName
-------------------------------------------------------------------------------------------------------
BB000Kw 1920384 5/14/2013 1093.39 "Wilson, Sam" 3/9/2003 Go Golf Carts
AA000bM 3839484 12/8/2012 584.42 "Krivis, Jeffrey" 7/1/2002 Acme Company
AA000gH 8294848 2/28/2014 4849.38 "Rich, Elwood" 7/5/2003 Rich's Sandwiches
Table 1 is the master table that the others are matched to. You can see from the result that there will be only a subset of all the tables based on those that are matched from Table 1.
I'll be using MVC with the Entity Framework 6 and Linq-to-Entities, but if a T-SQL script is more efficient, then I should probably use that instead.
Which is the better way to go to get this result?
If you want to create a new table you can use SELECT ... INTO ... FROM ... query. In your case it would look like this:
SELECT t1.U_ID, t1.ClientNumber, t1.OrderDate, t1.Amount,
t2.Name as CustomerName, t2.Date as JoinDate,
t3.Name as CompanyName
INTO dbo.ResultingMergedTable
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.U_ID = t2.U_ID
INNER JOIN Table3 t3 ON t1.U_ID = t3.U_ID
Keep in mind that if you are looking at really big data table this will take a lot of time to execute.
You can create a 4th table to do what you mentioned but if you are using sql you can create a view to do the same thing. A view is a virtual table. We use this when we partition data as well as make a detailed record like described above.
http://msdn.microsoft.com/en-us/library/ms187956.aspx
http://www.sqlinfo.net/sqlserver/sql_server_VIEWS_the_basics.php
CREATE VIEW DetailView AS
(
SELECT
-- table1
t1.U_ID,
t1.ClientNumber,
t1.OrderDate,
t1.Amount,
-- table2
t2.Name,
t2.Date as [JoinDate],
-- table3
t3.Name as [Company]
FROM
table1 t1
LEFT JOIN
table2 t2
ON t1.U_ID = t2.U_ID
LEFT JOIN
table3 t3
ON t1.U_ID = t3.U_ID
WHERE
t1.U_ID = t2.U_ID
and
t1.U_ID = t3.U_ID
)
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
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";
Hi all I am having my data table which is from my database is as follows
Name Total
XYZ 20
XYZ 20
ABC 20
Now I would like to have my data table as follows
Name Total
XYZ 40
ABC 20
I tried this linq from here Find duplicate and merge record into single datatable c# which works fine but as I am having my values from database I don't know the type of the variable so can some one help me and give me the solution in non-linq way
If you have two tables and you want to combine them all then the below is what you are after
SELECT bothTables.Name, SUM(total) FROM
(
SELECT Name, SUM(total) as total FROM Table_1 GROUP BY Name
UNION ALL
SELECT Name, SUM(total) as total FROM Table_2 GROUP BY Name
) AS bothTables
GROUP BY bothTables.Name
ORDER BY bothTables.Name desc
or if you want to do it using your Data Table (dt in this example)
var summedValues = from table in dt.AsEnumerable()
group table by table.Field<string>("Name")
into groupedTable
select new
{
Name = groupedTable.Key,
Total = groupedTable.Sum(x => x.Field<int>("Total"))
};
SQL version of the solution would be:
select Name, sum(Total) group by Name