Solved

sql query in Access 2010

Posted on 2016-09-20
9
58 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 17

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 17

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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 17

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 22

Accepted Solution

by:
Ferruccio Accalai earned 500 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 22

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 500 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

739 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