473,387 Members | 3,820 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,387 software developers and data experts.

Complex(?) conditions query

176 100+
Hello all.

I have a Jet SQL question:

I'm trying to write a query based on these 2 tables:

table1:
A: B:
1 1
2 1
3 1
4 1
5 1
1 2
2 2
3 2
4 2
5 2

table2:
C: D:
1 1
2 1
3 1
4 1
5 1
1 2
2 2
3 2
4 2
5 2

Now the query needs to show all the combinations of all the fields:

Expand|Select|Wrap|Line Numbers
  1. SELECT table1.A, table1.B, table2.C, table2.D
  2. FROM table1, table2;
but without the rows where A = C and without the rows where both of these conditions occur on the same row: A = C + 1, B<>D.

Please help me to combine this query without using UNION, if possible

Thank you, I appreciate your time.
Apr 30 '11 #1

✓ answered by ADezii

Try:
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.A, Table2.C, Table1.B, Table2.D
  2. FROM Table1, Table2
  3. WHERE (((Table1.A)<>[Table2].[C] And (Table1.A)<>[Table2].[C]+1) AND ((Table1.B)=[Table2].[D]));
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. A    C    B    D
  2. 5    1    1    1
  3. 4    1    1    1
  4. 3    1    1    1
  5. 5    2    1    1
  6. 4    2    1    1
  7. 1    2    1    1
  8. 5    3    1    1
  9. 2    3    1    1
  10. 1    3    1    1
  11. 3    4    1    1
  12. 2    4    1    1
  13. 1    4    1    1
  14. 4    5    1    1
  15. 3    5    1    1
  16. 2    5    1    1
  17. 1    5    1    1
  18. 5    1    2    2
  19. 4    1    2    2
  20. 3    1    2    2
  21. 5    2    2    2
  22. 4    2    2    2
  23. 1    2    2    2
  24. 5    3    2    2
  25. 2    3    2    2
  26. 1    3    2    2
  27. 3    4    2    2
  28. 2    4    2    2
  29. 1    4    2    2
  30. 4    5    2    2
  31. 3    5    2    2
  32. 2    5    2    2
  33. 1    5    2    2
  34.  

4 1801
ADezii
8,834 Expert 8TB
Try:
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.A, Table2.C, Table1.B, Table2.D
  2. FROM Table1, Table2
  3. WHERE (((Table1.A)<>[Table2].[C] And (Table1.A)<>[Table2].[C]+1) AND ((Table1.B)=[Table2].[D]));
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. A    C    B    D
  2. 5    1    1    1
  3. 4    1    1    1
  4. 3    1    1    1
  5. 5    2    1    1
  6. 4    2    1    1
  7. 1    2    1    1
  8. 5    3    1    1
  9. 2    3    1    1
  10. 1    3    1    1
  11. 3    4    1    1
  12. 2    4    1    1
  13. 1    4    1    1
  14. 4    5    1    1
  15. 3    5    1    1
  16. 2    5    1    1
  17. 1    5    1    1
  18. 5    1    2    2
  19. 4    1    2    2
  20. 3    1    2    2
  21. 5    2    2    2
  22. 4    2    2    2
  23. 1    2    2    2
  24. 5    3    2    2
  25. 2    3    2    2
  26. 1    3    2    2
  27. 3    4    2    2
  28. 2    4    2    2
  29. 1    4    2    2
  30. 4    5    2    2
  31. 3    5    2    2
  32. 2    5    2    2
  33. 1    5    2    2
  34.  
May 1 '11 #2
Michael R
176 100+
Works well, thanks!
I guess you can't build everything with Access query builder.
May 2 '11 #3
ADezii
8,834 Expert 8TB
Actually, this Query you can (both Tables Shown but not Joined):
Expand|Select|Wrap|Line Numbers
  1. Field: A                                            B             C         D
  2. -----------------------------------------------------------------------------------
  3. Table: Table 1                                      Table1        Table2    Table2
  4. -----------------------------------------------------------------------------------
  5. Criteria: <>[Table2].[C] And <>[Table2].[C]+1       [Table2].[D]
  6. -----------------------------------------------------------------------------------
  7.  
May 2 '11 #4
Rabbit
12,516 Expert Mod 8TB
It's just easier to show SQL rather than showing the query designer.
May 2 '11 #5

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

Similar topics

1
by: DotNetJunkies User | last post by:
I am trying to build a xPath Query to use on SelectNodes function in VB.net The data I want to search is by MemberCity = O'Fallon My xPath Query is xPathQuery = "Matched" The problem is...
4
by: Akinia | last post by:
Hi every body I've a little problem with my query, I can't figure it out. I've three tables: Table A (EmployeNr, Date, Code) Table B (EmployeNr, Date, Code) Which is an historic of Table A...
9
by: MLH | last post by:
I have a table (tblCorrespondence) holding records with fields like , , , , , , , etc... About a dozen 's are defined and I often use queries to extract records of a given . That's pretty easy....
15
by: stockblaster | last post by:
Hello I am using stored procedure with sql 2005 (with Visual studio 2005) I have two tables .. TABLE1 And TABLE2 >From TABLE1 i need to retrive the OrderID's of the 4 most top rows. so i...
9
by: jardar.maatje | last post by:
I am logging scientific data. For this a normal relationship database is not idéal but I am going to try it. I have basically three tables like: datarecord: * idx - integer * time - datetime...
2
by: kalindi05 | last post by:
Hello everyone, Please help me revise this query to make it work. I don't know much about update query so I will need some help with it. Thanks, update encounter set mergedpayment =...
2
by: kalindi05 | last post by:
Hello everyone, Please help me revise this query to make it work. I don't know much about update query so I will need some help with it. Thanks, update encounter set mergedpayment =...
3
by: Robert | last post by:
I need to set up a query that will pick out records for the current winter season. I.e., each season runs from October 1 until March 31. The catch is, the year can't be hard coded. So, if the...
13
by: Slower Than You | last post by:
Well, I think it's complex anyway -- you might not :) TableDef: CREATE TABLE CustTransactions ( TransactionKey int IDENTITY(1,1) NOT NULL, CustomerID int, AmountSpent float, CustSelected bit...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.