Solved

sql query in Access 2010

Posted on 2016-09-20
9
36 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 13

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 13

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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 13

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now