I am doing a distinct to filter by 2 columns, but I need it to bring me all the columns of the query, in this case it only brings me "idMes" and "idAnio", I need it to show me the other columns as well.
How could I do it?
this is my sentence:
SELECT DISTINCT e.idMes, e.idAnio FROM expensas as e INNER JOIN anios as a on e.idAnio = a.idAnio INNER JOIN meses as m on e.idMes = m.idMes;
Select * gives you all columns
Related
I am trying to join two tables from a database; energyImport and sunAlt.
energyImport has three columns: timestamp_id, energy, duration.
sunAlt has two columns: timestamp_id, altitude
I am doing an inner join on these two tables using the SQL:
SELECT *
FROM energyImport
INNER JOIN sunAz ON sunAz.timestamp_id = energyImport.timestamp_id;
The output from this is:
timestamp_id,duration,energy,timestamp_id,altitude
1601769600,1800,81310,1601769600,0.0
1601771400,1800,78915,1601771400,0.0
1601773200,1800,78305,1601773200,0.0
The problem is that the timestamp_id column is repeated. How can I join these columns and only include the first timestamp_id?
Replace the * with
energyImport.timestamp_id,energyImport.duration, energyImport.energy,
sunAz.altitude
Either you specify the columns that you want in the results:
SELECT e.timestamp_id, e.duration, e.energy, s.altitude
FROM energyImport e INNER JOIN sunAz s
ON s.timestamp_id = e.timestamp_id;
Or, use NATURAL instead of INNER join, so that the join is based on the columns(s) with the same names of the 2 tables (if this fits your requirement), because NATURAL join returns only 1 of each pair of these columns:
SELECT *
FROM energyImport NATURAL JOIN sunAz;
See the demo.
i have following sql in java project:
select distinct * from drivers inner join licenses on drivers.user_id=licenses.issuer_id
inner join users on drivers.user_id=users.id
where (licenses.state='ISSUED' or drivers.status='WAITING')
and users.is_deleted=false
And result i database looks like this:
And i would like to get only one result instead of two duplicated results.
How can i do that?
Solution 1 - That's Because one of data has duplicate value write distinct keyword with only column you want like this
Select distinct id, distinct creation_date, distinct modification_date from
YourTable
Solution 2 - apply distinct only on ID and once you get id you can get all data using in query
select * from yourtable where id in (select distinct id from drivers inner join
licenses
on drivers.user_id=licenses.issuer_id
inner join users on drivers.user_id=users.id
where (licenses.state='ISSUED' or drivers.status='WAITING')
and users.is_deleted=false )
Enum fields name on select, using COALESCE for fields which value is null.
usually you dont query distinct with * (all columns), because it means if one column has the same value but the rest isn't, it will be treated as a different rows. so you have to distinct only the column you want to, then get the data
I suspect that you want left joins like this:
select *
from users u left join
drivers d
on d.user_id = u.id and d.status = 'WAITING' left join
licenses l
on d.user_id = l.issuer_id and l.state = 'ISSUED'
where u.is_deleted = false and
(d.user_id is not null or l.issuer_id is not null);
I have two tables. One table has floor number(tb_FloorNumber.FloorNumber. records :For example 1 to 15) and another table which has Floor number and User_Id column(tb_Emp_Master.FloorNumber, tb_Emp_Master.User_Id). I want to bring all the records from tb_FloorNumber and only the records from tb_Emp_Master with the condition (User_Id = "fat35108").
I know I can do this with two queries like this :
Query 1:
SELECT DISTINCT tb_Emp_Master.FloorNumber
FROM tb_Emp_Master
WHERE (((tb_Emp_Master.User_Id)="fat35108"));
Query2:
SELECT DISTINCT tb_FloorNumber.FloorNumber, Query1.FloorNumber
FROM tb_FloorNumber LEFT JOIN Query1 ON tb_FloorNumber.FloorNumber = Query1.FloorNumber;
But I want to write this query with sing query instead of using Query1 inside the Query 2
I have tried like this:
SELECT DISTINCT tb_FloorNumber.FloorNumber, tb_Emp_Master.FloorNumber
FROM tb_FloorNumber LEFT JOIN tb_Emp_Master ON tb_FloorNumber.FloorNumber = tb_Emp_Master.FloorNumber
WHERE (((tb_Emp_Master.User_Id)="fat35108"));
But it brings only one record (For instance 8)
Please help me how to write this
If you set the condition:
tb_Emp_Master.User_Id = "fat35108"
in the WHERE clause, then you actually get an INNER JOIN instead of a LEFT JOIN because you filter only the matched rows from tb_Emp_Master.
Use tb_Emp_Master in the LEFT JOIN instead of Query1 and set the condition in the ON clause:
SELECT DISTINCT
tb_FloorNumber.FloorNumber,
tb_Emp_Master.FloorNumber
FROM tb_FloorNumber LEFT JOIN tb_Emp_Master
ON tb_FloorNumber.FloorNumber = tb_Emp_Master.FloorNumber AND tb_Emp_Master.User_Id = "fat35108";
I don't know why you need DISTINCT so I use it too.
I have two columns i need to show all the records in column [PR] and some columns from column [EM]. The below SQL statement does not return all the records from column [PR].
SELECT
[PR].[WBS1], [EM].[FirstName], [EM].[LastName], [EM].[EMail]
FROM
[VisionDemo].[dbo].[PR]
JOIN
[VisionDemo].[dbo].[EM] ON [VisionDemo].[dbo].[PR].[Principal] = [VisionDemo].[dbo].[EM].[Employee]
How do I do this ?
Use a LEFT JOIN:
SELECT [PR].[WBS1],[EM].[FirstName],[EM].[LastName], [EM].[EMail]
FROM [VisionDemo].[dbo].[PR]
LEFT JOIN [VisionDemo].[dbo].[EM]
ON [VisionDemo].[dbo].[PR].[Principal] = [VisionDemo].[dbo].[EM].[Employee]
Use LEFT JOIN. This is fundamental stuff, go check out the FAQ
SELECT [PR].[WBS1],[EM].[FirstName],[EM].[LastName], [EM].[EMail]
FROM [VisionDemo].[dbo].[PR]
LEFT JOIN [VisionDemo].[dbo].[EM] --Use a left join
ON [VisionDemo].[dbo].[PR].[Principal] = [VisionDemo].[dbo].[EM].[Employee]
I have a SQL Query that joins 3 tables and pulls 3 total columns out of them. I was try to figure out how to Query that Query to get distinct records from only one of the columns. Here is what I have so far
Select Distinct Make.NAME
From
(
Select MakeModel.MAKE_ID, Make.NAME, Vehicle.MODEL_YR
From NagsInfo.dbo.Make
INNER JOIN NagsInfo.dbo.MakeModel
on Make.MAKE_ID = MakeModel.MAKE_ID
INNER JOIN NagsInfo.dbo.Vehicle
on MakeModel.MAKE_MODEL_ID = Vehicle.MAKE_MODEL_ID
Where Vehicle.MODEL_YR = #YEAR
)
I keep getting multiple different syntax errors, I believe the most recent one telling me that the parentheses were incorrect, but everywhere I looked they are required for Sub queries.
Why use a subquery at all? Why not just do:
SELECT DISTINCT M.NAME
FROM NagsInfo.dbo.Make M
JOIN NagsInfo.dbo.MakeModel MM ON M.MAKE_ID = MM.MAKE_ID
JOIN NagsInfo.dbo.Vehicle V ON MM.MAKE_MODEL_ID = V.MAKE_MODEL_ID
WHERE V.MODEL_YR = #YEAR