Heidestrand
Registered User.
- Local time
- Today, 06:54
- Joined
- Apr 21, 2015
- Messages
- 73
Hello community,
I have a problem / question regarding a SQL query and I hope you can help me. My initial situation:
I have 3 tables that I INNER JOIN together with a number. The number is the same in each table. By doing this I get a big table with 11 columns. The tables I use look like this:
table 1: tblDetector
DetectorIDRef | SAPNr | Detector
table 2: tblEKLand
EKLandIDRef| SAPNr | EKLand
table 3: tblSysSold
ID | SAPNr | SystemSold | CountryID | DetectorID
What I want is: I want to count all the SAPNr numbers when I apply certain criteria. An exmaple:
- How many SAPNr numbers do I have when tblDetector.Detector = X AND tblEKLand.EKLand = China AND tblSysSold.SystemSold = biplane?
For this I wrote a SQL query:
Now my problem occurs: my SQL query only works when all my criteria are selected. But what is if I only have 2 criteria selected? Let's say I only want to know how many systems were sold in China? Or only how many detectors of a special kind are sold overall?
Then my code gives me 0 because the other two criteria are missing.
Therefore my question is: Is there a possibility how I can alter my code that a NULL value is also possible? So that my code also works when I didn't select all criteria?
Hope you understand my problem and can help me.
Best regards,
Heide
I have a problem / question regarding a SQL query and I hope you can help me. My initial situation:
I have 3 tables that I INNER JOIN together with a number. The number is the same in each table. By doing this I get a big table with 11 columns. The tables I use look like this:
table 1: tblDetector
DetectorIDRef | SAPNr | Detector
table 2: tblEKLand
EKLandIDRef| SAPNr | EKLand
table 3: tblSysSold
ID | SAPNr | SystemSold | CountryID | DetectorID
What I want is: I want to count all the SAPNr numbers when I apply certain criteria. An exmaple:
- How many SAPNr numbers do I have when tblDetector.Detector = X AND tblEKLand.EKLand = China AND tblSysSold.SystemSold = biplane?
For this I wrote a SQL query:
Code:
SELECT Count(*) AS SAPNr " & _
"FROM (tblSysSold INNER JOIN tblEKLand ON tblSysSold.SAPNr = tblEKLand.SAPNr) INNER JOIN tblDetector ON tblEKLand.SAPNr = tblDetector.SAPNr " & _
"WHERE tblDetector.Detector ='" & strDetec & "' AND tblEKLand.EKLand ='" & strLand & "' AND tblSysSold.SystemSold ='" & strSys & "'"
Now my problem occurs: my SQL query only works when all my criteria are selected. But what is if I only have 2 criteria selected? Let's say I only want to know how many systems were sold in China? Or only how many detectors of a special kind are sold overall?
Then my code gives me 0 because the other two criteria are missing.
Therefore my question is: Is there a possibility how I can alter my code that a NULL value is also possible? So that my code also works when I didn't select all criteria?
Hope you understand my problem and can help me.
Best regards,
Heide