Solved

sql query in Access 2010

Posted on 2016-09-20
9
64 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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 23

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 23

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
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 …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

630 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