Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have following sort of records returned from a query.

Name ===== CanAccess
--------------------------------
Payroll ===== 0
Payroll ===== 1
Employees ===== 0
Employees ===== 1
Inventory ===== 0
Finance ===== 0


I want to select unique records from same Name if "CanAccess" is true for any of them.

I want output like-
Name ===== CanAccess
--------------------------------
Payroll ===== 1
Employees ===== 1
Inventory ===== 0
Finance ===== 0


What I have tried:

I tried different joins, but no luck.
Posted
Updated 18-Mar-18 3:06am
v3
Comments
#realJSOP 18-Mar-18 9:36am    
You're looking at it backwards. You're not excluding rows that meet certain conditions, you're SELECTING rows that meet certain conditions. That's why the command is called SELECT.
[no name] 18-Mar-18 10:25am    
"I want to select unique records from same Name ___if "CanAccess" is true___ for any of them."

Does this not conflict with your sample Output where
Inventory ===== 0
Finance ===== 0

?
#realJSOP 18-Mar-18 19:30pm    
No. It's does not conflict with my solution. Based on the sample data you provided, aggregation is not necessary. A simple select statement will suffice. Of course, there may be something you're not telling us, but we can't just divine that by praying to a large rock in the back yard to seek guidance.

1 solution

SQL
select name from [mytablenmame] where CanAccess=1


[EDIT]
Given the OP's sample data, where there are only TWO records for a given name, and EACH of them having a different value for CanAccess, aggregation is not required to get the results he claims to be after. Stop 1-voting my answer.
[/EDIT]
 
Share this answer
 
v2
Comments
Krunal Rohit 18-Mar-18 8:43am    
No mate, please see the updated question.
#realJSOP 18-Mar-18 9:27am    
How does my solution not give him the results he wants. Without seeing his data and understanding WHY he has two records for the same name with different values for CanAccess, we cannot POSSIBLY give him the query he needs. Based on his illustrated data, and the text of his question, there is no need for anything in the query that would demand a clause that would establish "unique" records. Voting my answer a 1 was uncalled for.
Maciej Los 19-Mar-18 8:19am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900