Solved

sql query in Access 2010

Posted on 2016-09-20
9
45 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

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.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server views 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 Access…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

785 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