Using Access 2003
Table1
Personid Date1
101 02-02-2008
102 02-02-2008
103 02-02-2008
101 03-02-2008
102 03-02-2008
103 03-02-2008
101 04-02-2008
102 04-02-2008
103 04-02-2008
So On…,
Table2
Personid Date1 Name Title
101 03-02-2008 Raja Accountant
102 04-02-2008 Ravi Supervisor
103 02-02-2008 Ram Manager
So on…,
I want to display all personid, date1 from table1 and name, title from table 2 where table2.date1 = table1.date
Expected Output:
Personid Date1 Name Title
101 02-02-2008
102 02-02-2008
103 02-02-2008 Ram Manager
101 03-02-2008 Raja Accountant
102 03-02-2008
103 03-02-2008
101 04-02-2008
102 04-02-2008 Ravi Supervisor
103 04-02-2008
So on…,
How to make a query for the above expected output.
Need Query Help
SELECT
Table1.personid,
Table1.date1,
Table2.name,
Table2.title
FROM Table1
LEFT JOIN Table2 ON Table1.date1 = Table2.date1
AND Table1.personid = Table2.personid;
Similar to mik's, but the AND at the end there is necessary, otherwise it applies the name and title to rows it doesn't belong in
SELECT Table1.personid, Table1.date1, Table2.name, Table2.title
FROM Table1 LEFT JOIN Table2 ON Table1.date1=Table2.date1;
This query should work
Related
I have three tables
Student
studenid stuname
101 john
102 aron
103 mary
104 lucy
Subject
studenid subjid subjname
101 1 maths
102 2 science
103 3 computer
104 4 english
Marks
subjid mark
1 50
2 40
3 55
4 60
1 40
2 55
3 60
I want output like this where studenid (sum of mark as total)
studenid stuname mark
101 john 90
102 aron 95
103 mary 115
104 lucy 60
Thank you in advance for yout help, i want output like this even join query or subquery which is best for timing
This just requires a straight left join across all tables, with an aggregation by student.
SELECT
st.studenid,
st.stuname,
COALESCE(SUM(m.mark), 0) AS mark
FROM Student st
LEFT JOIN Subject su
ON st.studenid = su.studenid
LEFT JOIN Marks m
ON su.subjid = m.subjid
GROUP BY
st.studenid,
st.stuname;
Demo
Note that if studenid be a primary key in the Student table, then strictly we would only need to aggregate by this column alone.
Master_table Table_A
ID Problem Date ID Problem Date
101 123 01-02-1993 101 123 01-02-1993
101 124 101 124
102 125 102 125 07-02-1994
103 126 08-22-1999 103 126 08-22-1999
103 131 103 131 08-09-1999
Table_B Table_C
ID Problem Date ID Problem Date
101 124 101 124
102 125 06-30-1994 102 125
103 126 08-22-1999 103 131 08-08-1999
103 131 08-09-1999 106 137 02-02-1987
106 137 01-02-1987 110 145 12-22-1995
I need to create a new table named ‘final_table’ such that all observations in Master_table have a date. Rule for achieving this: Date from Master_table will be used as the date when Date exists. Otherwise, the minimum Date from Table_A, Table_B and Table_C will be used. If Date is missing in all the tables then drop the observation.
Desired Output
ID Problem Date
101 123 01-02-1993
102 125 06-30-1994
103 126 08-22-1999
103 131 08-08-1999
What I've tried
SELECT ID, Problem, MIN(Date) as Date
FROM
( SELECT ID, Problem, Date
FROM Table_A
UNION ALL
SELECT ID, Problem, Date
FROM Table_B
UNION ALL
SELECT ID, Problem, Date
FROM Table_C
) as subQuery
Delete From table Where Date IS NULL
Seeme you need a union between the 3 tables this way
select id, problem, data
from Table_A
where date is not null
union
select id, problem, min(data)
from Table_B
group by id, problem
where date is not null
union
select id, problem, min(data)
from Table_B
group by id, problem
where date is not null
Student
_______________________
SudentId Name
101 Peter
103 John
112 James
116 Peter
117 Peter
SudentId_Subject
_______________________
SudentId Subject
101 Physics
103 Chemistry
112 Mathematics
116 Physics
117 Commerce
To find duplicate students, I tried the query
select * from Student group by Name having count(*)>1, got
SudentId Name
_______________________
101 Peter
116 Peter
117 Peter
Actually I need to filter students with the same subject. ie,
101 Peter
116 Peter
are duplicates (same subject) but not 117 Peter
How can I write query for this?
I may have over complicated things, but couldn't think of an easier way :
SELECT ss.student_id,ss.name
FROM student ss
JOIN studentid_subject tt
ON(ss.student_id = tt.student_id)
JOIN(SELECT t.name,s.department
FROM student t
JOIN studentid_subject s
ON(t.student_id = s.student_id)
GROUP BY t.name,s.department
HAVING COUNT(*) > 1) p
ON(ss.name = p.name and tt.department = p.department)
select * from student left join SudentId_Subject on Student.SudentId = SudentId_Subject.DepId where Student.SudentId=SudentId_Subject.DepId
Employee
Eid ename salary
1 charles 3000
2 kiran 2000
3 naveen 8000
4 manju 7000
Department
deptid dname
101 charles
102 kiran
103 naveen
104 manju
Register
deptid eid
101 1
102 2
103 3
104 4
write a query to print (employee) ename and (department)dname.
SELECT a.ename, b.depart, c.eid
FROM employee a
INNER JOIN department b on a.ename=b.dname
INNER JOIN register c on b.deptid=c.deptid
where a.ename=b.dname and b.deptid=c.deptid;
Using Access 2003
Table1
EmpID Name city
101 Raja Sydney
102 Ram Melbourne
103 Ravi Adelaide
101
102
103
So on…,
I want to update or select the same name, city for the blank Empid’s
Expected Output
EmpID Name city
101 Raja Sydney
102 Ram Melbourne
103 Ravi Adelaide
101 Raja Sydney
102 Ram Melbourne
103 Ravi Adelaide
So on...,
How do I make a query for this condition?
It is possible to use sub queries to update in Access:
UPDATE (
SELECT tblT.EmpID, tblT.EmpName, tblT.City
FROM tblT
WHERE tblT.EmpName Is Null) AS t
INNER JOIN (
SELECT tblT.EmpID, tblT.EmpName, tblT.City
FROM tblT
WHERE tblT.EmpName Is Not Null) AS x
ON t.EmpID = x.EmpID
SET t.EmpName = [x].[EmpName], t.City = [x].[City];
Be aware that the query design window can seriously mess up the layout and can stop sub queries from working by changing the parentheses () to square brackets [], however, it is safe enough to paste the SQL into SQL View, save and run.
You table as posted has no key and violates first normal form.
For example, if you table contained this data:
Table1
EmpID Name city
101 Raja Sydney
102 Ram Melbourne
103 Ravi Adelaide
101 Ajaa New York
102 Mar Des Moines
103 Ivar Dallas
101
102
103
...then which value would you expect this query to pick for 102: Melbourne or Des Moines?