473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

combine 2 queries

Hi,

I have 2 queries that I need to join. I have the following tables:
attendancelog :
headerid
reportmonth

attlogstuds:
headerid
sid

class:
sid
class
status
yearcode
logdate
cdate
mid
The result must return all the classes that appear in query2 but not
in query1.
I am not sure how to join the 2 queries.

Help will be appreciated :-)

Thanks

QUERY1

select sid from
attlogstuds studs
inner join
attendancelog attlog
on studs.headerid=attlog.headerid
where reportmonth=10

query2
-- students learning excl. studs left before 1th oct.

select class.SID from class

left outer JOIN ( select * from class where yearcode=26 and status=6

and ( logdate <'20041001' or CDate
< '20041001' )

) c6 ON c6.sid = class.sid and c6.mid=class.mid
and c6.cdate >= class.cdate

where class.yearcode=26 and class.status in (3,5) and
class.cdate<'20041101' and c6.sid is null
Jul 20 '05 #1
1 1294
[posted and mailed, please reply in news]

avital (av*********@walla.co.il) writes:
The result must return all the classes that appear in query2 but not
in query1.
I am not sure how to join the 2 queries.


Sounds like the EXCEPT operator would have come in hand, but that
operator is not in SQL Server.

It's always a good idea to include the following:

o CREATE TABLE statements for your tables.
o INSERT statements with sample data.
o The desired output given the sample.

This makes it possible to post a tested solution.

The below is an untested solution. I may have missed something but
it seems to me that a simple NOT EXISTS clause is what you need.

select class.SID
from class
left JOIN (select *
from class
where yearcode=26
and status=6
and ( logdate <'20041001' or
CDate < '20041001' )
) c6
ON c6.sid = class.sid
and c6.mid = class.mid
and c6.cdate >= class.cdate
where class.yearcode=26
and class.status in (3,5)
and class.cdate<'20041101'
and and c6.sid is null
and NOT EXISTS (select *
from attlogstuds studs
join attendancelog attlog
on studs.headerid=attlog.headerid
where studs.sid = class.sid)
--
Erland Sommarskog, SQL Swhere reportmonth=10erver MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Lutka | last post by:
I've been racking my brains all day over this. And I'm not the best at SQL either. I need a query that will produce the following results:...
5
by: Jamie Pittman via AccessMonster.com | last post by:
I have two tables with 5000 entries on them. One is based for regular time with several variables example (employee name, date,time in and out, code, customer, building) I have another table that...
4
by: Jason Gyetko | last post by:
Is there any way to combine these two queries into one? I have tables Item_Master & Kit_Master which are the source tables. Query 2 is using both Item_Master (table) & qryKit1 (query) &...
1
by: Will | last post by:
Hi all I have a table, where there's AutoID, Name of Person (looked up from tblPerson), Name of Dispute (looked up from tblDispute), Date, Details (Memo). What I have is another table that need...
4
by: PlayHard | last post by:
I want my output in two columns like this: C_INCIDENT_TYPE \ TOTAL COUNT How do I combine my two queries to get result. First Query SELECT C_INCIDENT_TYPE, COUNT (*)
46
by: dude88888 | last post by:
Hello I have one query where i get the total number of units for an entire fund say fund 89 has 1000 total units Then i need to find out how much of the total fund each user has say user...
4
by: | last post by:
I have query1 where I set a date range. Query2 is a crosstab query and is based on query1. I don't want the queries to be saved in the mdb but I do want them to be run in VBA using a querystring. If...
1
by: csolomon | last post by:
Hello: I am using two queries to get one result set. The issue is, I return no data when I combine them into one query. I have listed both queries, as well as the 3rd query that shows them...
2
by: bips2005 | last post by:
i have got two queries, $sql1 = "select cust_id from customer where comp_name = '$compname'"; $result = $DB->query($sql1); $result->fetchInto($row); $sql = "select expirydate from...
13
by: MNNovice | last post by:
I have three tables, tblAP, tblPayroll and tblAllocation. Each records three separate expenses. All three are related by FK ECHOID, each has common fields such as AccountNo, FundNo, GrantNo,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.