How to join 5 tables in SQL Server? [closed] - sql

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'

Related

SQL count function for specific value [closed]

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

SQL Error: ambiguous column nameor syntax error [closed]

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).

How to join tables on calculation - Oracle [closed]

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;

SQL Server : select on Multiple table with join not completing [closed]

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 8 years ago.
Improve this question
I have this query which should give this kind of output -
NAME DATE_JOINED TOTAL_GROUPS_FORMED LOANS_ADVANCES TOTAL_ISSUED_AMOUNT TOTAL_LOAN_AMT_REPAID TOTAL_ACCOUNT TOTAL_CLIENTS_RECRUITED TOTAL_AMT_DEPOSITS TOTAL_AMT_WITHDRAWALS NUMBER_DROPOUTS REPORT_DATE
This is my query.
SELECT
D.NAME,
0.0 AS DATE_JOINED,
0 AS TOTAL_GROUPS_FORMED,
COUNT(LOAN_NUMBER) AS LOANS_ADVANCES,
SUM(ISSUED_AMOUNT) AS TOTAL_ISSUED_AMOUNT,
0.0 AS TOTAL_LOAN_AMT_REPAID,
COUNT(A.ACCOUNT_NUMBER) AS TOTAL_ACCOUNT,
COUNT(C.CUSTOMER_CODE) AS TOTAL_CLIENTS_RECRUITED,
0.0 AS TOTAL_AMT_DEPOSITS,
0.0 AS TOTAL_AMT_WITHDRAWALS,
COUNT(CH.ACCOUNT_NUMBER) AS NUMBER_DROPOUTS,
D.REPORT_DATE FROM SALES_OFFICER D
LEFT JOIN LOANS L ON D.SALES_OFFICER_CODE=L.OFFICER_CODE
LEFT JOIN OPEN_ACCOUNT A ON D.SALES_OFFICER_CODE=A.OFFICER_CODE
LEFT JOIN CUSTOMERS C ON D.SALES_OFFICER_CODE=C.OFFICER_CODE
LEFT JOIN CLOSED_ACCOUNT CH ON D.SALES_OFFICER_CODE=CH.OFFICER_CODE
GROUP BY D.NAME,D.REPORT_DATE
This query is not completing probably because all the tables have a lot of data.
Is there another way to have the same output where one Sales Officer's information can be viewed from multiple tables and aggregated together as a single record, grouped by the reporting date.
You are getting a cartesian product for each sales officer -- the number of loans TIMES the number of open accounts TIME the number of customers TIMES the number of closed accounts.
You need to summarize each dimension prior to doing the join. Here is a start:
from sales_offices so left join
(select l.OFFICER_CODE, COUNT(*) AS LOANS_ADVANCES,
SUM(ISSUED_AMOUNT) AS TOTAL_ISSUED_AMOUNT
from loans l
group by l.OFFICER_CODE
)
on so.SALES_OFFICER_CODE = l.OFFICER_CODE
This is just a guess, because you don't use table aliases on all the columns.

Why does this not pull any data? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
The following snippet is a join on multiple tables. I need to display all order number, customer name, product name, price and quantity ordered from anyone from Australia. I'm getting table headings but no rows. Is there something wrong with this?
SELECT
"order".orderno AS ord,
customer.cname,
product.prodname,
customer.country_code,
orderdetl.price,
orderdetl.qty,
country.country_code
FROM
prema01.country,
prema01.customer,
prema01."order",
prema01.orderdetl,
prema01.product
WHERE
customer.country_code = 'AUS'
I've changed the code, verified there is data in the tables and it still comes out blank.
I am completely stumped.
SELECT O.ORDERNO, C.CNAME, PN.PRODNAME, ODT.PRICE, ODT.QTY, ODT.QTY * PN.PRODSELL AS TOTAL
FROM prema01.ORDER O, prema01.CUSTOMER C, prema01.ORDERDETL ODT, prema01.PRODUCT PN, prema01.COUNTRY CT
WHERE CT.COUNTRY_NAME = 'Australia'
AND C.COUNTRY_CODE = CT.COUNTRY_CODE
AND C.CUSTNO = O.CUSTNO
AND O.ORDERNO = ODT.ORDERNO
AND PN.PRODNO = ODT.PRODNO AND O.ORDERNO <= 60
ORDER BY TOTAL DESC;
forgot to add the change. the tables have data in them, i've physically verified that.
For starters, you need some join conditions e.g.
Select
r.country_code,
c.cname
From
prema01.country r
inner join
prema01.customer c
on r.country_code = c.country_code
You need to build up the relationships with the other tables in a similar way.
Also, are you sure your tables have any data in them. Does
Select
Count(*)
From
prema01.country
return anything? How about
Select
Count(*)
From
prema01.customer
Where
country_code = 'AUS'
?
Fixed, I was looking for 'Australia' while I have 'AUSTRALIA' in my country table. thanks for the help