mysql subquery - results do not fulfill both - mysql

I have the following SQL query for MySQL:
SELECT SQL_CALC_FOUND_ROWS objects.objects_no
FROM objects
LEFT JOIN finds ON (objects.objects_no = finds.objects_no)
LEFT JOIN ceramics ON (objects.objects_no = ceramics.objects_no)
WHERE 1=1
and (objects.objects_no) in (select DISTINCT objects_no from objects_materials where thesaurus_term_id in (18658))
and (objects.objects_no) in (select DISTINCT objects_no from objects_objects where thesaurus_term_id in (24193))
GROUP BY objects.objects_no
ORDER BY objects.objects_no
Instead of getting results that match both subqueries, I also get results that match one or the other. Does anyone have an idea why that is?
Thanks, Sandro

Try parenthesizing the conditions.
WHERE (
(1=1)
and ((objects.objects_no) in (select DISTINCT objects_no from objects_materials where thesaurus_term_id in (18658)))
and ((objects.objects_no) in (select DISTINCT objects_no from objects_objects where thesaurus_term_id in (24193)))
)

Thanks for all your help. It actually does work just fine. There was a problem within the data inside the thesaurus.
Sorry!!!

Related

Nesting COUNT in statement with JOIN

Really trying to figure out, why SQL query doesnt go through. I assume the structure is a bit wrong, but cant figure out where exactly. The references to tables are all correct.
SELECT tap_questionnaires.id,
tap_questionnaires.NAME,
tap_questionnaires.active,
tap_useranswers_ip.questionnaire_id,
Count(tap_useranswers_ip.ip)
FROM tap_questionnaires
LEFT JOIN tap_useranswers_ip
ON tap_questionnaires.id = tap_useranswers_ip.questionnaire_id
WHERE author_email = admin#admin.com
If you use count you need to use group by for the other columns in your select clause.
SELECT TAP_questionnaires.id, TAP_questionnaires.name, TAP_questionnaires.active, TAP_useranswers_ip.questionnaire_id, COUNT(TAP_useranswers_ip.ip) FROM TAP_questionnaires LEFT JOIN TAP_useranswers_ip on TAP_questionnaires.id=TAP_useranswers_ip.questionnaire_id WHERE author_email="admin#admin.com"
group by TAP_questionnaires.id, TAP_questionnaires.active
I think TAP_questionnaires.name it's not necessary because I suppose it depends on TAP_questionnaires.id. TAP_useranswers_ip.questionnaire_id is the same value as TAP_questionnaires.id
Hope that helps!
I think this version is clearer:
SELECT q.id, q.name, q.active, COUNT(a.ip)
FROM TAP_questionnaires q LEFT JOIN
TAP_useranswers_ip a
ON on q.id = a.questionnaire_id
WHERE author_email = 'admin#admin.com'
GROUP BY q.id, q.name, q.active;
Notes:
You need a GROUP BY.
You need single quotes around the string constant.
Table aliases make the query easier to write and to read.
There is no reason to include a.questionnaire_id. You already have q.id.

Calculate between two statements

I got two statements and I want to calculate their values. Both values have to be calculated 5 - 5 = ( I want to see the answer 0 )
SELECT COUNT(*) AS 'Aantal stoelen geboekt'
FROM Boekingsregel, Vlucht
WHERE Boekingsregel.Vlucht_Vlucht_Id = Vlucht.Vlucht_Id
AND Vlucht_Datum = '2017-04-10';
SELECT min(Vliegtuig_Aantal_Stoelen) AS 'Max aantal stoelen'
FROM Vliegtuig;
While I do not see any relation of the two queries you also should not use an alias with spaces. Make your alias as short as possible but can define what value it holds, but since this is your query you know better than I do.
As for your problem you can combine the two queries into one something like this:
SELECT BR.`Aantal stoelen geboekt` - VT.`Max aantal stoelen` AS TheResult
FROM (SELECT COUNT(*) AS `Aantal stoelen geboekt`
FROM Boekingsregel, Vlucht
WHERE Boekingsregel.Vlucht_Vlucht_Id = Vlucht.Vlucht_Id
AND Vlucht_Datum = '2017-04-10') BR,
(SELECT min(Vliegtuig_Aantal_Stoelen) AS `Max aantal stoelen` FROM Vliegtuig) VT;
NOTE: Not tested I just type it here.
If this is not what you are looking for then maybe you should explain your requirements in greater detail so dhat everyone can understand and will be able to help you.
First, learn to use proper JOIN syntax.
Second, combine these in the FROM clause.
SELECT COUNT(*) AS AantalStoelenGeboekt, vt.MaxAantalStoelen
FROM Boekingsregel b JOIN
Vlucht v
ON b.Vlucht_Vlucht_Id = v.Vlucht_Id CROSS JOIN
(SELECT min(Vliegtuig_Aantal_Stoelen) AS MaxAantalStoelen
FROM Vliegtuig
) vt
WHERE v.Vlucht_Datum = '2017-04-10';
Note: MySQL allows this syntax. Perhaps a cleaner way is to use an aggregation function on MaxAantalStoelen:
SELECT COUNT(*) AS AantalStoelenGeboekt,
MAX(vt.MaxAantalStoelen) as MaxAantalStoelen
FROM Boekingsregel b JOIN
Vlucht v
ON b.Vlucht_Vlucht_Id = v.Vlucht_Id CROSS JOIN
(SELECT min(Vliegtuig_Aantal_Stoelen) AS MaxAantalStoelen
FROM Vliegtuig
) vt
WHERE v.Vlucht_Datum = '2017-04-10';

Nested Select Statement Query with error "Incorrect syntax near the keyword 'GROUP' "

I have been through a few other posts relating to my error, but none of the solutions seem to work. I'm fairly new to SQL so sorry if its something really simple. I have two tables
Movie Inventory - which has columns movie_title, onhand_qty, and replacement_price
NotFlix - which has subscriber_name, queue_nbr, and movie_title
I am trying to join the two tables to output the total replacement price cost per customer, but when I do it gives me the error titled above. Here is my code, thanks in advance for any help!
SELECT subscriber_name, SUM (replacement_price) as replacement
FROM
(SELECT NotFlix.subscriber_name, NotFlix.movie_title, NotFlix.queue_nbr, MovieInventory.replacement_price
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
)
GROUP BY subscriber_name;
You are missing an alias:
SELECT AliasNameHere.subscriber_name, SUM (AliasNameHere.replacement_price) as replacement
FROM
(SELECT NotFlix.subscriber_name as subscriber_name, NotFlix.movie_title, NotFlix.queue_nbr, MovieInventory.replacement_price as replacement_price
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
) AliasNameHere
GROUP BY subscriber_name;
I Just don't get why are you doing a temporary table in FROM clause, you could just do a basic INNER JOIN here and potientialy avoid problem with alias name :
SELECT NotFlix.subscriber_name, SUM (MovieInventory.replacement_price) as replacement
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
GROUP BY subscriber_name;

Use NEWID() without losing distinct?

I am trying to create a new data extract from a (badly designed) sql database. The customer requires that I add a distinctidentifier which I am attempting to do using the NEWID() function. Unfortunately this leads to multiple duplicate records being returned.
After a bit of research I have found that the NEWID() function does indeed 'undo' the use of the distinct keyword, but I cannot work out why or how to overcome this.
An example of the query I am trying to write is as follows:
select distinct
NEWID() as UUID
,Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)
If I miss out the NEWID() line in the select block, I get 569 records returned, which is correct, but if I include that line then I get in excess of 30,000 which are all duplicates of the original 569 but with different IDs. Can anyone suggest a way around this problem?
Thanks in advance
Use a sub query would be the easiest way to do it.
SELECT NEWID() as UUID
, * -- this is everything from below
FROM (
select distinct
Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)) as mySub
select NEWID() as UUID
,ISRN
,Internal_Patient_No
,Date_of_Birth
,histo_report
,Investigation_Result_Date
from (
select distinct
,Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)) t
You can use a sub-query to get around the issue, something like.....
SELECT NEWID() as UUID
,*
FROM (
select distinct
Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report
on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)
) t

Using an "AS" clause in a sub-query than contains a "WHERE" clause

I want to store result in the variable generated by using "AS" clause in the MS Access,
and use this result in the sub-query with WHERE clause.
I tried this:
SELECT en_date AS date_en, (select sum(amount)
from main where
CrDb='Cr'
and
en_date=date_en) AS CR_AMT
FROM main
GROUP BY en_date;
I'm fairly certain you can't use an alias (the AS destination) in the same SELECT you defined it in.
I can't tell quite what you're trying to do, but it kind of looks like you want to be joining the table to itself.
SELECT
en_date,
SUM(amount)
FROM
main a
INNER JOIN
(
SELECT
en_date AS date_en,
CrDb
FROM
main
WHERE
CrDb='Cr'
)b
ON
a.en_date = b.date_en
AND a.CrDb = b.CrDb
GROUP BY
en_date
select m.en_date date_en, sum(m.amount)
from main m
where CrDb = 'Cr'
group by m.en_date
In other words, I dont think you even need a sub-query to get the results you are looking for.