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 !
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. - tblBulbs:
-
ID_House ID_Bulb HasLight
-
House 1 1 Yes
-
House 2 1 No
-
House 3 1 Yes
-
House 3 2 No
-
House 3 3 Yes
-
House 4 1 Yes
-
House 4 2 Yes
-
House 4 3 Yes
-
House 4 4 Yes
-
House 4 5 Yes
-
House 4 6 Yes
-
- SQL:
- SELECT DISTINCT tblBulbs.ID_House, fCalcNotOK([ID_House]) AS OK_Or_NOT
-
FROM tblBulbs;
-
- Function Definition:
- Public Function fCalcNotOK(strHouseID As String) As String
-
If DCount("*", "tblBulbs", "[ID_House] = '" & strHouseID & "' and [HasLight]=False") > 0 And _
-
DCount("*", "tblBulbs", "[ID_House] = '" & strHouseID & "' and [HasLight]=True") Then
-
fCalcNotOK = "Not OK"
-
Else
-
fCalcNotOK = "OK"
-
End If
-
End Function
- Results:
-
ID_House OK_Or_NOT
-
House 1 OK
-
House 2 OK
-
House 3 Not OK
-
House 4 OK
-
5 1250
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. - tblBulbs:
-
ID_House ID_Bulb HasLight
-
House 1 1 Yes
-
House 2 1 No
-
House 3 1 Yes
-
House 3 2 No
-
House 3 3 Yes
-
House 4 1 Yes
-
House 4 2 Yes
-
House 4 3 Yes
-
House 4 4 Yes
-
House 4 5 Yes
-
House 4 6 Yes
-
- SQL:
- SELECT DISTINCT tblBulbs.ID_House, fCalcNotOK([ID_House]) AS OK_Or_NOT
-
FROM tblBulbs;
-
- Function Definition:
- Public Function fCalcNotOK(strHouseID As String) As String
-
If DCount("*", "tblBulbs", "[ID_House] = '" & strHouseID & "' and [HasLight]=False") > 0 And _
-
DCount("*", "tblBulbs", "[ID_House] = '" & strHouseID & "' and [HasLight]=True") Then
-
fCalcNotOK = "Not OK"
-
Else
-
fCalcNotOK = "OK"
-
End If
-
End Function
- Results:
-
ID_House OK_Or_NOT
-
House 1 OK
-
House 2 OK
-
House 3 Not OK
-
House 4 OK
-
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".
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).
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: - SELECT
-
ID_House,
-
IIf(
-
COUNT(*) = SUM(IIf(HasLight = True, 1, 0))
-
OR
-
COUNT(*) = SUM(IIf(HasLight = False, 1, 0))
-
, 'OK', 'Not OK') AS OK_Or_Not
-
-
FROM tblBulbs
-
-
GROUP BY ID_House
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 !
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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();...
|
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...
|
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...
|
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:...
|
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...
|
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,...
|
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...
|
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",...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |