473,327 Members | 1,976 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,327 software developers and data experts.

Joining of 2 SQL queries

I have managed to construct 2 separate SQL queries that basically gives me what I want. However; having them as a single query would be a great advantage as I could do an "ORDER BY" which would sort both results. As my SQL skills are getting a little rusty (to be moderate) I have been failing to combine them to 1 query.

My tables:
1. hprequests(requestid{PK}, username{FK}, flagtype, reqstatus, workstatus, requesttitle, statuschangedby)
2. hpentries(entryid{PK}, username{FK}, requestid{FK}, entry, regdate)
3. hpusers(username{PK}, firstname, lastname, etc)

My first query
Expand|Select|Wrap|Line Numbers
  1. SELECT hprequests.requestid, hprequests.workstatus, hprequests.username, hprequests.requesttitle, hprequests.reqstatus, entries.regdate 
  2. FROM hprequests 
  3. INNER JOIN (SELECT MAX(regdate) AS latest, requestid FROM hpentries GROUP BY requestid) maxresults 
  4. ON hprequests.requestid = maxresults.requestid 
  5. INNER JOIN hpentries entries 
  6. ON maxresults.latest = entries.regdate 
  7. WHERE LOWER(hprequests.requesttitle) LIKE LOWER('%test%') AND hprequests.reqstatus LIKE '%' 
This one will give me all the requests where the requesttitle contains 'test'; which in my case is requestid's: 1, 4, 24, 25, 27 and 31.

My second query
Expand|Select|Wrap|Line Numbers
  1. SELECT hprequests.requestid, hprequests.workstatus, hprequests.username, hprequests.requesttitle, hprequests.reqstatus, hprequests.statuschangedby, singleentry.entry, singleentry.latest 
  2. FROM hprequests 
  3. INNER JOIN (SELECT requestid, MAX(regdate) as latest, MIN(entry) as entry FROM hpentries WHERE LOWER(entry) LIKE LOWER('%test%') GROUP BY requestid) singleentry 
  4. ON hprequests.requestid = singleentry.requestid 
  5. WHERE hprequests.reqstatus LIKE '%' 
This one will give me all the requests which has an entry that contains 'test'; which in my case is requestid's: 1, 4, 5, 24, 25, 26, 27 and 30.

Trying to combine them I ended up with the following query:

Expand|Select|Wrap|Line Numbers
  1. SELECT hprequests.requestid, hprequests.username, hprequests.requesttitle, hprequests.reqstatus, hprequests.workstatus, singleentry.latest, singleentry.entry
  2. FROM hprequests
  3. INNER JOIN (SELECT requestid, requesttitle FROM hprequests WHERE LOWER(requesttitle) LIKE LOWER('%test%')) extratitles 
  4. ON hprequests.requestid = extratitles.requestid
  5. FULL OUTER JOIN (SELECT requestid, MAX(regdate) as latest, MIN(entry) as entry 
  6. FROM hpentries WHERE LOWER(entry) LIKE LOWER('%test%') GROUP BY requestid) singleentry 
  7. ON hprequests.requestid = singleentry.requestid
  8. ORDER BY requestid
This one actually sort of gives me what I want, except for some of the fields containing "null" for the requestid's 5, 26, 30 (missing from my first query) and 31 (missing from my second query). I suppose I have to do this with inner joins somehow, and loose the full outer one.

So, to sum it up: What I basically want to find is all requests where either the title or one of the entries contains a string ('test' in this case). It would be nice if it doesn't select any duplicates, but I suppose I could sort out duplicates code wise.

Hope this was somewhat understandable :)

-Johnb
Jul 29 '08 #1
2 7213
r035198x
13,262 8TB
If the queries return similar columns you could use UNION to combine their results.
Jul 29 '08 #2
Hey thanks!

UNION did the trick indeed!

I'll paste my final query in case someone else should be having a similar problem:

Expand|Select|Wrap|Line Numbers
  1. SELECT hprequests.requestid, hprequests.workstatus, hprequests.username, hprequests.requesttitle, hprequests.reqstatus, entries.regdate 
  2. FROM hprequests 
  3. INNER JOIN (SELECT MAX(regdate) AS latest, requestid FROM hpentries GROUP BY requestid) maxresults 
  4. ON hprequests.requestid = maxresults.requestid 
  5. INNER JOIN hpentries entries 
  6. ON maxresults.latest = entries.regdate 
  7. WHERE LOWER(hprequests.requesttitle) LIKE LOWER('%test%') AND hprequests.reqstatus LIKE '%'
  8. UNION
  9. SELECT hprequests.requestid, hprequests.workstatus, hprequests.username, hprequests.requesttitle, hprequests.reqstatus, maxresults.regdate
  10. FROM hprequests 
  11. INNER JOIN (SELECT requestid, MAX(regdate) as regdate, MIN(entry) as entry FROM hpentries WHERE LOWER(entry) LIKE LOWER ('%test%') GROUP BY requestid) maxresults
  12. ON hprequests.requestid = maxresults.requestid 
  13. WHERE hprequests.reqstatus LIKE '%'
  14. ORDER BY requestid 
It gives me some duplicates, but I'll sort that out in my code.
Thanks again for the input!
Jul 30 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Matt O'Donnell | last post by:
Does anyone know how I can 'join' the results of one SQL query to the bottom of another? Eg. I have two queries: 1. SELECT Name, Surname FROM People WHERE Surname = Smith NAME ...
4
by: Job Lot | last post by:
Is there anyway of Joining two or more DataTable with similar structure? I have three DataTables with following structures Data, AmountB/F, Repayments, InterestCharged and AmountC/F i want...
0
by: Gianfranco | last post by:
Hi I got a problem with 2 tables. I have a table, say A, with x records, coming from a make table query and a table, say B, with y records, coming from another make table query. I need to join...
0
by: Norma | last post by:
I have 2 queries that pull attendance records depending on the type of attendance occurance, or Both queries are drawn off 1 table where the field will depict whether it is a punctuality or...
0
by: dkintheuk | last post by:
Hi all, I'm trying to output a result set from some data as follows: I have raw data with the following columns Date, Country, System, ID,
10
by: Captain Nemo | last post by:
Hi I'm working on an ASP project where the clients want to be able to effectively perform SELECT queries joining tables from two different databases (located on the same SQL-Server). Does...
12
by: veaux | last post by:
Question about joins in queries. I have 2 tables with a field called "ID". Table 1 Rec1 = Jan12FredFlintstone Rec2 = Feb01WilmaRubble Table 2 Rec1 = Jan12BarneyRubble Rec2 = Mar03SamSlate
4
by: jason.teen | last post by:
Hi, when i am joining on a Column of Text Type with one of Memo type the resulting entry has funny chinese characters! Has anyone else encountered this before? Is there a cure?? Cheers.
3
by: nickvans | last post by:
Hello everyone, I am new to SQL Server, and am having trouble joining several SELECT statements together. I have three SQL Select statements that do separate count operations to get the status...
1
by: bhavinnaik | last post by:
Hi I am new to the IT enviro...although i've used QSL query for a while now but on simple or single queries. Here is the problem... I've got two queries looking at a set of tables to extract the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.