Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql query in Access 2010

Posted on 2016-09-20
9
Medium Priority
?
71 Views
Last Modified: 2016-09-21
Hello! I have what I hope is a simple enough how-to question for writing a query. I have Table1 with ColumnA, ColumnB, ColumnC, ColumnD, ColumnE, ColumnF, ColumnG, ColumnH, ColumnI, ColumnJ, and ColumnK. All of these columns are either a value 1 or 0 in each record. For ColumnA thru ColumnH, I want to query records that have either 1 or 0 in each of those columns in any combination and for ColumnI thru ColumnK to all equal 0. Can anyone help me figure out the way to write that query in the SQL format?
0
Comment
Question by:mrosier
  • 4
  • 3
  • 2
9 Comments
 
LVL 19

Expert Comment

by:John Tsioumpris
ID: 41807468
I hope this screenshot will help to figure out...Query
0
 

Author Comment

by:mrosier
ID: 41807476
Thanks John, but I really only understand these queries when using the SQL editor in Access. Can you convert that to SQL for me and paste that?
0
 
LVL 19

Expert Comment

by:John Tsioumpris
ID: 41807496
SELECT 1 AS ColumnA, 1 AS ColumnB, 1 AS ColumnC, 0 AS ColumnJ, 0 AS ColumnL
WHERE (((1)=1) AND ((0)=0)) OR (((1)=1) AND ((0)=0)) OR (((1)=1));

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:mrosier
ID: 41807613
Hi John, that query appears to be using the column names as aliases. I assume it would be ColumnA as 1 (for example), as opposed to 1 As ColumnA. So I can take all the columns that can have either 1 or 0 as values and give them all the same alias?
0
 
LVL 19

Expert Comment

by:John Tsioumpris
ID: 41807635
I think i misread a bit your question ...but the logic is the same
Just select your columns and follow the logic
 ((ColumnA =1 OR ColumnA =0) OR (ColumnB=1 OR ColumnB=0) OR ...(ColumnH=1OR ColumnH=0)) and (ColumnI =0 AND ColumnJ=0 AND ColumnK=0)

Open in new window

You can also use this :
(ColumnA IN(0,1) OR ColumnB IN(0,1) OR...ColumnH IN(0,1))  AND (ColumnI=0 AND ColumnJ=0 AND ColumnK=0))

Open in new window

If you want something more precise then i need a small amount of data along with a desired output
0
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 2000 total points
ID: 41807769
Maybe I'm misunderstanding the question, but if any column from A to H can have 0 or 1 as value and you don't care about the combination for these columns, the query should be simply
Select * from Table1 where ColumnI = 0 and ColumnJ = 0 and ColumnK = 0

Open in new window

0
 

Author Comment

by:mrosier
ID: 41808706
Thanks Ferruccio, that's what I am trying to do! For some reason I overcomplicated it when I kept trying to visualize the query. But let me ask you this in follow-up, and this might complicate it to the degree I visualized. For A to H,  if I want to query records where any two of those columns can equal 1 and the rest of those must equal 0, is there a way it can be done?
0
 
LVL 23

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 2000 total points
ID: 41808782
Having the last three columns = 0?
The fastest way by head could be to query records where the sum of the first 8 columns is >= 2 and the last 3 are 0. This means that at least 2 columns have value 1.

So, on the fly
Select * from Table1 where (ColumnA+ColumnB+ColumnC+ColumnD+ColumnE+ColumnF+ColumnG+ColumnH) >= 2 and (ColumnI = 0 and ColumnJ = 0 and ColumnK = 0)

Open in new window

If you meant just 2 columns = 1 and the others all 0 just use Where (columnA+...) = 2
0
 

Author Comment

by:mrosier
ID: 41808786
wow, I didn't even consider that, thanks Ferruccio!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question