472,362 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Query Help.

AJ
Hi all,

I have this monster query (at least i think it is).

SELECT
c.ID, c.Company_Name, p.[level], 1 As QueryNbr
FROM
(Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID)
LEFT JOIN
Package AS p ON s.Package_ID = p.ID
WHERE
c.Category = 'EXH'
AND (s.ID = (SELECT Max(ID) FROM Sale WHERE Company_ID = c.ID) Or
IsNull(s.ID))
AND EXISTS(SELECT Company_ID FROM Event_Company_Link WHERE Event_ID = 20 AND
Company_ID = c.ID)
AND (INT(Start_Date) <= 38911) AND (INT(End_Date) >= 38911)
AND Company_Name LIKE "% Inc% "
ORDER BY p.[level] DESC , c.Company_Name, c.ID
UNION
SELECT
c.ID, c.Company_Name, p.[level], 2 As QueryNbr
FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID)
LEFT JOIN Package AS p ON s.Package_ID = p.ID
WHERE c.Category = 'EXH'
AND (s.ID = (SELECT Max(ID) FROM Sale WHERE Company_ID = c.ID) Or
IsNull(s.ID))
AND EXISTS(SELECT Company_ID FROM Event_Company_Link WHERE Event_ID = 20
AND Company_ID = c.ID)
AND Company_Name LIKE "%Inc%"
ORDER BY c.Company_Name, c.ID

My issue is, i need it to return distinct companies only; In this case c.ID
is the column i require to be unique.

The UNION clause normally filters out duplicates, but because there is an
extra column needed ('QueryNbr') to indicate which query the results were
retrieve in (1 or 2) this filtering isn't having an effect.

The UNION appears to look for duplicates on a row by row basic rather than a
single column;

Has anyone got any ideas on how to get around this issue?

I am suffering with Access in this problem!!!

Cheers,
Adam
Jul 14 '06 #1
2 1580
Why do you have to know which query the results come from? Is that germaine
to your application? If so, which query should you choose if both queries
have the same result?

If you have to know that result, do this:

If query 1 wins (i.e., the results should say query 1 if found in both)
1. Create a temp table
2. Put results of query one in the table
3. Join query two to the results table and only find records that do not
appear in the temp table, insert those records
4. Query the temp table
5. After you have results, destroy the temp table

If query 2 wins, reverse the order in filling the temp table so query one
does not enter data entered by query 2.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
"AJ" wrote:
Hi all,

I have this monster query (at least i think it is).

SELECT
c.ID, c.Company_Name, p.[level], 1 As QueryNbr
FROM
(Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID)
LEFT JOIN
Package AS p ON s.Package_ID = p.ID
WHERE
c.Category = 'EXH'
AND (s.ID = (SELECT Max(ID) FROM Sale WHERE Company_ID = c.ID) Or
IsNull(s.ID))
AND EXISTS(SELECT Company_ID FROM Event_Company_Link WHERE Event_ID = 20 AND
Company_ID = c.ID)
AND (INT(Start_Date) <= 38911) AND (INT(End_Date) >= 38911)
AND Company_Name LIKE "% Inc% "
ORDER BY p.[level] DESC , c.Company_Name, c.ID
UNION
SELECT
c.ID, c.Company_Name, p.[level], 2 As QueryNbr
FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID)
LEFT JOIN Package AS p ON s.Package_ID = p.ID
WHERE c.Category = 'EXH'
AND (s.ID = (SELECT Max(ID) FROM Sale WHERE Company_ID = c.ID) Or
IsNull(s.ID))
AND EXISTS(SELECT Company_ID FROM Event_Company_Link WHERE Event_ID = 20
AND Company_ID = c.ID)
AND Company_Name LIKE "%Inc%"
ORDER BY c.Company_Name, c.ID

My issue is, i need it to return distinct companies only; In this case c.ID
is the column i require to be unique.

The UNION clause normally filters out duplicates, but because there is an
extra column needed ('QueryNbr') to indicate which query the results were
retrieve in (1 or 2) this filtering isn't having an effect.

The UNION appears to look for duplicates on a row by row basic rather than a
single column;

Has anyone got any ideas on how to get around this issue?

I am suffering with Access in this problem!!!

Cheers,
Adam
Jul 14 '06 #2
AJ wrote:
Hi all,

I have this monster query (at least i think it is).

SELECT
c.ID, c.Company_Name, p.[level], 1 As QueryNbr
FROM
(Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID)
LEFT JOIN
Package AS p ON s.Package_ID = p.ID
WHERE
c.Category = 'EXH'
AND (s.ID = (SELECT Max(ID) FROM Sale WHERE Company_ID = c.ID) Or
IsNull(s.ID))
AND EXISTS(SELECT Company_ID FROM Event_Company_Link WHERE Event_ID =
20 AND Company_ID = c.ID)
AND (INT(Start_Date) <= 38911) AND (INT(End_Date) >= 38911)
AND Company_Name LIKE "% Inc% "
ORDER BY p.[level] DESC , c.Company_Name, c.ID
UNION
SELECT
c.ID, c.Company_Name, p.[level], 2 As QueryNbr
FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID)
LEFT JOIN Package AS p ON s.Package_ID = p.ID
WHERE c.Category = 'EXH'
AND (s.ID = (SELECT Max(ID) FROM Sale WHERE Company_ID = c.ID) Or
IsNull(s.ID))
AND EXISTS(SELECT Company_ID FROM Event_Company_Link WHERE
Event_ID = 20 AND Company_ID = c.ID)
AND Company_Name LIKE "%Inc%"
ORDER BY c.Company_Name, c.ID

My issue is, i need it to return distinct companies only; In this
case c.ID is the column i require to be unique.

The UNION clause normally filters out duplicates, but because there
is an extra column needed ('QueryNbr') to indicate which query the
results were retrieve in (1 or 2) this filtering isn't having an
effect.

The UNION appears to look for duplicates on a row by row basic rather
than a single column;

Has anyone got any ideas on how to get around this issue?

I am suffering with Access in this problem!!!
Create a saved query with this sql. For the sake of this example, call it
qUnionQuery.
Then create a new query that uses qUnionQuery in its FROM clause and groups
by the id field. You will need to provide aggregate functions for all the
other fields in the query (max or min will usually work)

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 14 '06 #3

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

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
7
by: Simon Bailey | last post by:
How do you created a query in VB? I have a button on a form that signifies a certain computer in a computer suite. On clicking on this button i would like to create a query searching for all...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
36
by: Liam.M | last post by:
hey guys, I have one last problem to fix, and then my database is essentially done...I would therefore very much appreciate any assistance anyone would be able to provide me with. Currently I...
5
by: elitecodex | last post by:
Hey everyone. I have this query select * from `TableName` where `SomeIDField` 0 I can open a mysql command prompt and execute this command with no issues. However, Im trying to issue the...
10
by: aaronrm | last post by:
I have a real simple cross-tab query that I am trying to sum on as the action but I am getting the "data type mismatch criteria expression" error. About three queries up the food chain from this...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
4
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.