How to join tables on calculation - Oracle [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 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;

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 5 tables in SQL Server? [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 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'

How to get Total number of Rows from a query with also all column [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 simple query, I want to retrieve all the data that select statement is selecting now (rightly), plus I want to get total rows number using the same query how can I do this. How do I add total rows query with below query?
SELECT
tblTaxingSchemeDetails.TaxSchemeDetailsId,
tblTaxingScheme.TaxSchemeId,
tblTaxingScheme.TaxSchemeName,
tblTaxingSchemeDetails.TaxType,
TaxName,
tblTaxingSchemeDetails.TaxRate
From tblTaxingScheme INNER JOIN tblTaxingSchemeDetails
On tblTaxingScheme.TaxSchemeId = tblTaxingSchemeDetails.TaxSchemeId
INNER JOIN tblTaxType
on tblTaxingSchemeDetails.TaxType = tblTaxType.TaxTypeID
where tblTaxingScheme.TaxSchemeId =5#TaxSchemeId
Last item of this result has a row number that equals the number of records.
SELECT
ROW_NUMBER() over(tblTaxingSchemeDetails.TaxSchemeDetailsId)as SeqNo,
tblTaxingSchemeDetails.TaxSchemeDetailsId,
tblTaxingScheme.TaxSchemeId,
tblTaxingScheme.TaxSchemeName,
tblTaxingSchemeDetails.TaxType,
TaxName,
tblTaxingSchemeDetails.TaxRate
From tblTaxingScheme INNER JOIN tblTaxingSchemeDetails
On tblTaxingScheme.TaxSchemeId = tblTaxingSchemeDetails.TaxSchemeId
INNER JOIN tblTaxType
on tblTaxingSchemeDetails.TaxType = tblTaxType.TaxTypeID
where tblTaxingScheme.TaxSchemeId =5#TaxSchemeId
SELECT * COUNT(*) OVER() AS [Total_Rows]
FROM table 1 innerjoin table 2
You can get the desired result through this Query... Also would remind this query would be take more resource while compare to the normal query...
SELECT
COUNT(tblTaxingSchemeDetails.TaxSchemeDetailsId) over(PARTITION BY tblTaxingScheme.TaxSchemeId) as h,
tblTaxingSchemeDetails.TaxSchemeDetailsId,
tblTaxingScheme.TaxSchemeId,
tblTaxingScheme.TaxSchemeName,
tblTaxingSchemeDetails.TaxType,
TaxName,
tblTaxingSchemeDetails.TaxRate
From tblTaxingScheme INNER JOIN tblTaxingSchemeDetails
On tblTaxingScheme.TaxSchemeId = tblTaxingSchemeDetails.TaxSchemeId
INNER JOIN tblTaxType
on tblTaxingSchemeDetails.TaxType = tblTaxType.TaxTypeID
where tblTaxingScheme.TaxSchemeId =5#TaxSchemeId

Does anyone know why I am not getting any results with this query? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I will just go straight to the problem.
This query is supposed to a list of records where PFlag is set to 'No'
However, when I run it, I get blank results.
If I remove the WHERE clause (where pFlag='No'), I get results.
So, the WHERE clause is presenting a problem.
Any idea what is wrong?
Here is the code I am currently running.
SELECT DISTINCT t.username,
lg.Email,
lg.fullname,
c.CourseName,
l.location,
d.trainingDates,
d.trainingTime,
t.processedFlag,
i.instructorName
FROM tblTrainings t
INNER JOIN tblCourses c on t.courseId = c.courseId
INNER JOIN tblLocations l on t.locationId = l.LocationId
INNER JOIN tblTrainingDates d on t.dateid=d.dateid
INNER JOIN tblCourseInstructor ic on c.courseId = ic.CourseId
INNER JOIN tblInstructors i on ic.instructorId = i.instructorId
INNER JOIN tblLogin lg on t.username = lg.username
WHERE t.PFlag = 'No'
ORDER BY lg.fullname DESC
It seems simple enough.
Thanks a lot in advance
PFlag
0x59006500
0x59006500
0x59006500
try rewriting the WHERE statement like this:
WHERE t.PFlag like '%No%'
hope it helps.
Check your table columns definitions.
if the type of field pFlag is CHAR with particular length, e.g. CHAR[4], then any string in that column will be filled up to 4 chars.
In that case the real data stored in your table will be No[space][space].
If this is the case, convert your column type to VARCHAR, and do an update to that column with trim().
Try running it like #user2065377 said :
WHERE t.PFlag like '%No%'
Also :
Run the query without the where clause and add this column : SELECT ...., CAST(t.PFlag AS VARBINARY(4)) and see the distinct values.
maybe you have whitespace (invisible) chars
also paste the result for :
select distinct ( t.PFlag) from .....
edit
SELECT CONVERT(VARCHAR(100), 0x59006500) yields Y
Where is the Yes word ?
even so , it's not an ordinary Y :
SELECT CASE WHEN CONVERT(nVARCHAR(100), 0x59006500)='Y' THEN 1 ELSE 0 END ( yields 0)