473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Selection problem

759 512MB
Hello !

I have 2 tables:
tblHouses (ID_House)
tblBulbs (ID_Bulb, ID_House, HasLight)
In tblBulbs the "ID_House" field is a look up on tblHouses and the "HasLight" field is of type Yes/No.

Now:
1) If each bulb in a house has light (HasLight=Yes), is OK.
2) If each bulb in a house hasn't light (HasLight=No), is, again, OK.
3) If some bulbs in a house have light and some bulbs in the same house haven't light, is NOT OK.

The question is:
How can I find the houses in situation 3) ?

I am looking for a smart solution, because I already have a solution "in force" :
In a query I have counted the bulbs from each house, in other query I have counted the bulbs with light for each house and, finaly, in the third query I do the comparation between the first and second query.

Thank you !
Jun 22 '13 #1

✓ answered by ADezii

I can get this to work with a Calculated Field calling a Public Function, but I'm sure the SQL Guys will have a better approach. I'll post what I have for reference anyway.
  1. tblBulbs:
    Expand|Select|Wrap|Line Numbers
    1. ID_House     ID_Bulb       HasLight
    2. House 1          1           Yes
    3. House 2          1           No
    4. House 3          1           Yes
    5. House 3          2           No
    6. House 3          3           Yes
    7. House 4          1           Yes
    8. House 4          2           Yes
    9. House 4          3           Yes
    10. House 4          4           Yes
    11. House 4          5           Yes
    12. House 4          6           Yes
    13.  
  2. SQL:
    Expand|Select|Wrap|Line Numbers
    1. SELECT DISTINCT tblBulbs.ID_House, fCalcNotOK([ID_House]) AS OK_Or_NOT
    2. FROM tblBulbs;
    3.  
  3. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcNotOK(strHouseID As String) As String
    2. If DCount("*", "tblBulbs", "[ID_House] = '" & strHouseID & "' and [HasLight]=False") > 0 And _
    3.    DCount("*", "tblBulbs", "[ID_House] = '" & strHouseID & "' and [HasLight]=True") Then
    4.      fCalcNotOK = "Not OK"
    5. Else
    6.      fCalcNotOK = "OK"
    7. End If
    8. End Function
  4. Results:
    Expand|Select|Wrap|Line Numbers
    1. ID_House    OK_Or_NOT
    2. House 1        OK
    3. House 2        OK
    4. House 3        Not OK
    5. House 4        OK
    6.  

5 1250
ADezii
8,834 Expert 8TB
I can get this to work with a Calculated Field calling a Public Function, but I'm sure the SQL Guys will have a better approach. I'll post what I have for reference anyway.
  1. tblBulbs:
    Expand|Select|Wrap|Line Numbers
    1. ID_House     ID_Bulb       HasLight
    2. House 1          1           Yes
    3. House 2          1           No
    4. House 3          1           Yes
    5. House 3          2           No
    6. House 3          3           Yes
    7. House 4          1           Yes
    8. House 4          2           Yes
    9. House 4          3           Yes
    10. House 4          4           Yes
    11. House 4          5           Yes
    12. House 4          6           Yes
    13.  
  2. SQL:
    Expand|Select|Wrap|Line Numbers
    1. SELECT DISTINCT tblBulbs.ID_House, fCalcNotOK([ID_House]) AS OK_Or_NOT
    2. FROM tblBulbs;
    3.  
  3. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcNotOK(strHouseID As String) As String
    2. If DCount("*", "tblBulbs", "[ID_House] = '" & strHouseID & "' and [HasLight]=False") > 0 And _
    3.    DCount("*", "tblBulbs", "[ID_House] = '" & strHouseID & "' and [HasLight]=True") Then
    4.      fCalcNotOK = "Not OK"
    5. Else
    6.      fCalcNotOK = "OK"
    7. End If
    8. End Function
  4. Results:
    Expand|Select|Wrap|Line Numbers
    1. ID_House    OK_Or_NOT
    2. House 1        OK
    3. House 2        OK
    4. House 3        Not OK
    5. House 4        OK
    6.  
Jun 22 '13 #2
Mihail
759 512MB
Thank you, ADezii !
Good enough for me but I don't wish to close (yet) this thread by selecting your answer as the best answer.
That because I wonder too what solution can find the "SQL Guys".
Jun 22 '13 #3
ADezii
8,834 Expert 8TB
I would wait also, since I do believe that there is a purely more efficient SQL approach. I'll attempt one later but SQL is definitely not my 'Cup-of-Tea' (LOL).
Jun 22 '13 #4
Rabbit
12,516 Expert Mod 8TB
I would have done similar to @Mihail's 3 query approach. I don't know if this would be more efficient but you could also do something along the lines of this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.    ID_House,
  3.    IIf( 
  4.       COUNT(*) = SUM(IIf(HasLight = True, 1, 0))
  5.          OR
  6.       COUNT(*) = SUM(IIf(HasLight = False, 1, 0))
  7.    , 'OK', 'Not OK') AS OK_Or_Not
  8.  
  9. FROM tblBulbs
  10.  
  11. GROUP BY ID_House
Jun 22 '13 #5
Mihail
759 512MB
Thank you, guys !
Seems that are not other solutions.

I have implemented ADezii's solution (in fact, idea) because I understand better [even if he has multiple houses for one bulb :) . Energy economy.].

My skill in SQL is almost null. So I must say thank you to Rabbit, but I'm afraid that I'll not be able to manage the SQL if I'll have some changes in code in the future.

I have select the best answer only from this view point, because really I don't know which one is better.

Thank you again !
Jun 27 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Irmen de Jong | last post by:
Hi, I'm having trouble with the code below. It's just a regular Tk text widget in which you can type and select text as expected, however the call to tkFileDialog.askopenfilename() seems to screw...
2
by: Mark Szlazak | last post by:
The following code fails in Firefox to get at selected text in the right-side textarea. Any help would be appreciated. <html> <head> <script> var agt = navigator.userAgent.toLowerCase();...
0
by: Robin Tucker | last post by:
Apologies for the report, but this problem is doing my head in! : I have a list box (just happens to be owner draw). When I select multiple items using the CTRL key, the items I have selected...
0
by: JS0001 | last post by:
I have inherited an Access database that was written in VBA and am still an amateur when it comes to VBA. I am tasked with computing a selection criteria based on a number of values that have to be...
10
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
0
by: felix.karpel | last post by:
I have a ListView with multi select I have 2 events: SelectedIndexChanged and ItemSelectionChanged If user selects 200 items, these events will be called for 200 times How can I get only 1 event...
1
by: shrutid27 | last post by:
Hi... I am just trying to use FCK Editor. I am having problem with link selection on double click for firefox. Means if I select the link by double clicking on it and then swith to source mode,...
0
by: =?Utf-8?B?SmFj?= | last post by:
hi, I would like to create a Excel macro that can help me to create charts. First, I created the Excel macro as below:- Sub Macro1() Selection.CurrentRegion.Select...
6
by: BabyLucifer666 | last post by:
hello, What I'm trying to do is create a list of classes based on what Area and Station they are in. I have the following tables set up already: tblClasses (Fields: "ClassID", "ClassName",...
1
by: sleepyhedgehog | last post by:
Hi! After making some minor changes to the database/form view, the filter by selection function had stopped working for most fields in the main form. Previously I could filter by almost any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.