Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 19 days ago.
Improve this question
SELECT nomDepar, ciudadSede, count(idempleado) as "Numero de Empleados"
from departamentos d, sedes s, empleados
where d.idSede = s.idSede
and s.ciudadSede in ('Madrid')
group by nomdepar, ciudadsede
I am trying to count all the employees in the departments located in Madrid. This is what I have so far but I am stuck here. It might be obvious but I only started learning SQL 2 weeks ago and this is stressing me out.
Just guessing, but it should be like here:
SELECT d.nomDepar, s.ciudadSede, count(e.idempleado) as "Numero_de_Empleados"
FROM departamentos d
INNER JOIN sedes s ON(s.idSede = d.idSede)
INNER JOIN empleados e ON(e.idSede = d.idSede) -- just guessing this join condition - you should put your own - there was none in your code
WHERE s.ciudadSede = 'Madrid'
GROUP BY d.nomDepar, s.ciudadSede
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I need to combine the results from two different CTEs into a single table. Hence, I have written the following SQL query wherein I attempted to create a left join on two different fields (patient_id, quarter in example below).
Unfortunately, I am getting a SQL compilation error (Unknown user defined function A.YR_QTR). But I have declared this variable in the code so am at a loss to why this error is cropping up. My code follows below:
WITH op_clms AS
(
SELECT
MC.UNIV_MBR_ID AS univ_mbr_id,
MC.YR_QTR AS YR_QTR,
COUNT DISTINCT MC.ENC_KEY AS op_clms
FROM
CRF.MED_CLAIMS AS MC
WHERE
MC.SERVICE BETWEEN '2016-01-01' AND '2020-12-31'
AND PLACE_OF_SERVICE ! = '23'
),
ed_clms AS
(
SELECT
MC.UNIV_MBR_ID AS univ_mbr_id,
MC.YR_QTR AS YR_QTR,
COUNT DISTINCT MC.ENC_KEY AS ed_clms
FROM
CRF.MED_CLAIMS AS MC
WHERE
MC.SERVICE BETWEEN '2016-01-01' AND '2020-12-31'
AND PLACE_OF_SERVICE = '23'
)
SELECT
a.UNIV_MBR_ID, a.YR_QTR
(IFNULL(op_clms, 0)) AS op_clms,
(IFNULL(ed_clms,0)) AS ed_clms,
1 as cohort
FROM
(SELECT UNIV_MBR_ID, YR_QTR
FROM op_clms
UNION
SELECT UNIV_MBR_ID, YR_QTR
FROM ed_clms) a
LEFT JOIN
op_clms ON a.UNIV_MBR_ID = op_clms.UNIV_MBR_ID
AND a.YR_QTR = op_clms.YR_QTR
LEFT JOIN
ed_clms ON a.UNIV_MBR_ID = ed_clms.UNIV_MBR_ID
AND a.YR_QTR = ed_clms.YR_QTR
Can somebody please guide me on what I am doing wrong? Each of the individual CTEs check out fine on their own. And I cant find anything else syntactically/logically wrong with my code.
It appears in my original code, I inadvertently omitted a comma after a.YR_QTR. Adding that as shown below, fixes the issue!
a.UNIV_MBR_ID, a.YR_QTR,
(IFNULL(op_clms, 0)) AS op_clms,
(IFNULL(ed_clms,0)) AS ed_clms,
1 as cohort
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Tried to change my code many times, need help with these errors, please!
Result: ambiguous column name: main.Guides.Guide_ID
SELECT *
FROM Guides
INNER JOIN Guides ON Guides_Countries.Guide_ID = Guides_Countries.Guide_ID
INNER JOIN Countries ON Countries.Country_ID = Guides_Countries.Country_ID
INNER JOIN Guides_Languages ON Guides.Guide_ID = Guides_Languages.Guide_ID
INNER JOIN Languages ON Languages.Language_ID = Guides_Languages.Language_ID
WHERE Countries.Name="Kazakhstan" AND (Languages.Name="German" OR Languages.Name="English") AND Guides.Guide_ID NOT IN
(SELECT Guide_ID
FROM GuidesUnavailableDates
INNER JOIN GuidesUnavailableDates ON GuidesUnavailableDates.UnDate_G_ID=Guides_UnDate.UnDate_G_ID
WHERE (Start_date<="21/06/2020" and End_date>="21/06/2020")
OR (Start_date<="30/06/2020" and End_date>="30/06/2020")
OR (Start_date>="21/06/2020" and End_date<="30/06/2020")
)
;
You have multiple table names appearing multiple times in your FROM clauses. That is causing your problem. I think you want:
SELECT *
FROM Guides g JOIN
Guides_Countries gc
ON gc.Guide_ID = g.Guide_ID JOIN
Countries c
ON c.Country_ID = gc.Country_ID JOIN
Guides_Languages gl
ON g.Guide_ID = gl.Guide_ID JOIN
Languages l
ON l.Language_ID = gl.Language_ID
WHERE c.Name = 'Kazakhstan' AND
l.Name IN ('German', 'English') AND
g.Guide_ID NOT IN (SELECT gud.Guide_ID
FROM GuidesUnavailableDates gud
WHERE gud.Start_date <= '2020-06-30' AND
gud.End_date >= '2020-06-21'
);
Notes:
Guide_Countries is not in your FROM list although Guides is there twice.
IN is much simpler than mutiple ORs.
All columns are qualified so it is clear what table they are coming from.
All tables have simple table aliases which are abbreviations for the table names.
There is no need for a JOIN in the subquery.
Use well formatted dates.
I am guessing that the weird date logic is to find an overlap with the time period mentioned so I simplified the logic. (You haven't explained the logic, so this is a guess and your original code doesn't make sense.)
I strongly recommend using NOT EXISTS with subqueries rather than NOT IN. However, I did not change the code here (mostly because I can't really tell what the subquery is supposed to be doing).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i am trying to join these two tables, but I can only do so if I change values of columns while joining like "substr(to_char(p.personnummer),3)" and "substr(k.ip_id,1,10)" . Can anyone tell me what is the order I am supposed to do it?
select x.ip_id, x.organisationsnummer
from (select
substr(to_char(p.personnummer),3) as ip_id_jnr,
substr(k.ip_id,1,10) as ipo_id
from customer k
inner join customer_VIEW p on ipo_id = ip_id_jnr) x
;
Your query doesn't show which table contains organisationsnummer; I used the k alias (as if it belongs to customer); fix it if necessary.
So, inline view:
select k.organisationsnummer,
k.ip_id
from customer k join customer_view p
on substr(to_char(p.personnummer),3) = substr(k.ip_id,1,10);
Now use it:
select x.ip_id,
x.organisationsnummer
from (select k.organisationsnummer,
k.ip_id
from customer k join customer_view p
on substr(to_char(p.personnummer),3) = substr(k.ip_id,1,10)
) x;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
SELECT temp.hhid, temp.country, temp.max_prod, temp.max_area, gen.price_seed, gen.qty_seed, gen.price
FROM (SELECT hhid, country, MAX(area) AS max_area, max(total_prod) AS max_prod FROM gen GROUP BY hhid, country) AS temp, gen
WHERE (((temp.hhid)=gen.hhid) And ((temp.country)=gen.country) And ((temp.max_prod)=gen.total_prod) And ((temp.max_area)=gen.area))
ORDER BY temp.hhid;
Why do some of the results were not seen?
I have atleast 100 hhid , each one has 3 area , 3 productions , quantity of seed , price etc ...
all of the hhid was shown in the ouput query except for 1 hhid ,
what might be wrong ?
The problem is that the maximum of prod and the maximum of area are rarely in the same row.
You should also learn to use explicit join syntax. A simple rule: never use a comma in the from clause.
This may be what you want:
SELECT temp.hhid, temp.country, temp.max_prod, temp.max_area, gen.price_seed,
gen.qty_seed, gen.price
FROM gen INNER JOIN
(SELECT hhid, country, MAX(area) AS max_area, max(total_prod) AS max_prod
FROM gen
GROUP BY hhid, country
) AS temp
ON temp.hhid = gen.hhid AND temp.country = gen.country
WHERE (temp.max_prod = gen.total_prod or temp.max_area = gen.area)
ORDER BY temp.hhid;
I left the WHERE clause in, because MS Access has strange restrictions on joins. Logically it should go in the ON clause, but I'm not 100% sure that Access accepts that syntax.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a minor problem with SQL Server, I have five tables:
Klientet (ID_Klienti (PK), Emri, Mbiemri, Adressa, Qyteti, Telefoni)
Punetoret (ID_Puntori (PK), ID_Lokacioni (foreign key), Emri, Mbiemri, Adressa, Qyteti, Telefoni)
Lokacioni (ID_Lokacioni (PK), Emri_Lokacionit)
Veturat (ID_Veturat (PK), Modeli, Viti, Ngjyrat)
Shitjet (ID_Shitja (PK), ID_Klienti (FK), ID_Puntori (FK), ID_Lokacioni(PK), ID_Vetura)
I want to inner join or (join) all five of them, and I get a result with 47 rows instead of number between 4 and 10. I've done it but it is missing something and I can't find what. I hope you guys can help me .
This is where I got so far
SELECT
Klientet.Emri, Klientet.Mbiemri,
Punetoret.Emri, Punetoret.Mbiemri,
Lokacioni.Emri_Lokacionit,
Shitjet.Data_shitjes
FROM
Klientet
INNER JOIN
Shitjet ON Shitjet.ID_Klienti = Klientet.ID_Klienti
INNER JOIN
Punetoret ON Punetoret.ID_Punetori = Shitjet.ID_Punetori
INNER JOIN
Lokacioni ON Lokacioni.ID_Lokacioni = Punetoret.ID_Lokacioni
INNER JOIN
Veturat ON Lokacioni.ID_Lokacioni = Veturat.ID_Lokacioni
WHERE
Emri_Lokacionit = 'Prishtine'
OR Emri_Lokacionit = 'Gjilan'
OR Emri_Lokacionit = 'Mitrovice'