Getting MIN Price in Subquery in SQL Server (using DISTINCT)? - sql

I am trying to get a Minimun price from a car in a table i have.. I am using DISTINCT
SELECT DISTINCT
datepart(year,[Registration]) AS YearRegistered,
MIN(SalePrice), Model, Make
FROM [VehicleSales]
But its not working, for example
without distinct returns many car makes and models so i use distinct so i get unique cars that are the same make and model and year....
I wish to incorporate a "Startign from price ..." hence the SalePrice can also be different for same model and make ... so i want to do a MIN..
But i am bit confused, the above is working working...
Any ideas?

You need to add a GROUP BY clause and get rid of the DISTINCT:
SELECT
datepart(year,[Registration]) AS YearRegistered,
MIN(SalePrice), Model, Make
FROM
[VehicleSales]
GROUP BY
datepart(year,[Registration]), Model, Make

SELECT DATEPART(year,[Registration]) AS YearRegistered, Model, Make, MIN(SalePrice)
FROM [VehicleSales]
GROUP BY
DATEPART(year,[Registration]) AS YearRegistered, Model, Make

Related

SQL - I cant think of any way to do this, i have done all the research?

This is my cars table which has 1000 rows. Each of make or manufacturer has different model and same model can have different price.
I have to output min price among all of the model of each manufacturer and i cant think of any way to do this.
Nearest to what i got is,
SELECT make, model, min(price) FROM car
GROUP BY model, make
ORDER BY make;
which outputs,527 rows
But i want min price among all of the models of each make.
HELP!!
In Postgres, I recommend distinct on for this purpose:
select distinct on (make) c.*
from cars c
order by make, price asc;
From your question it seems that you want minimum price of each make.
The following query can help in my opinion.
SELECT make,MIN(price) from car
group by make
order by make;
Assuming you need "the lowest priced Model for each Make", you can easily get the lowest price per Make according to the answers given, but that will not give you the Model name!
I'd suggest using a window function to rank the data, partitioning by Make, and ordering by Price ascending. Then simply select all rows ranked 1.
There may be a possibility that two Models within a Make may be equally low priced. In that case, you'd have two rows returned for that Make. If that's a possibility and also a problem, you'd have to engage in further processing to decide how to break the tie or consolidate the row into one (for example, by concatenating the Model names).
Use rank() window function inside a CTE to filter the minimum prices:
with cte as (
select *,
rank() over (partition by model order by price) rn
from car
)
select id, make, model, price
from cte
where rn = 1
order by make;
This will return ties in minimum price.
If you don't need ties replace rank() with row_number().
I assumed that a model name cannot be used by 2 makers. If this is not the case then change to this:
rank() over (partition by make, model order by price) rn
I think you are looking for a simple left join.
select t1.make, t1.model, t2.min_price
from car t1 left join (select make, min(price) min_price from car group by make) t2 on
t1.make=t2.make

Find all rented cards using EXCEPT or NOT EXISTS

My Database is like this
CUSTOMER (NAME,VAT,PHONE)
CAR(PLATENR,COLOUR)
RENTS(VAT,PLATENR, RENTDATE)
What I want to find is all the black cars that were rented by all customers (all VAT Numbers).
I want to use nested subqueries and EXCEPT or NOT EXISTS.
I already have this query using COUNT that works.
SELECT rents.PlateNr
FROM Rents,Car
WHERE Car.colour='black' AND car.PlateNr=rents.PlateNr
GROUP BY rents.PlateNr
HAVING COUNT(DISTINCT VAT) = (SELECT COUNT(*) FROM Customer);
I am trying to use this guide http://www.inf.usi.ch/faculty/soule/teaching/2016-fall/db/division.pdf to get my result but I don't get how the query is implemented in my case.
I was looking an answer similar to the link that I provided above regarding division.
After some search and try the final division query using except is this:
SELECT PlateNr
FROM Car
WHERE colour='black'
EXCEPT
SELECT PlateNr
FROM (SELECT car.PlateNr, rents.VAT
FROM car,rents
EXCEPT
SELECT PlateNr,VAT
FROM rents)
Have tried it in POSTGRE that supports EXCEPT and everything works like a charm.

Need query to figure out when car model name shared by two makes

I've got a simple table with fields make and model.
I'm trying to figure out a query that will tell me when I have a model that is shared by more than 1 make. That is, if ford and gm both made a car model named "rocket", I want to know that.
I'm using Sql Server but I don't think that really matters.
I made a wild guess of course not right.
select make,
model
group by make,
model
where count(MODEL) > 2
You are close, this will get you the make and model for each model that has more than one make if the table t consists of unique pairs of make and model.
select make, model
from t
where model in (
select model
from t
group by model
having count(*) > 1
)
If make and model are not unique pairs for the table, you could use count(distinct make) instead:
select make, model
from t
where model in (
select model
from t
group by model
having count(distinct make) > 1
)
You could also use an exists() query instead of performing aggregation like so:
select make, model
from t
where exists (
select 1
from t as i
where i.model=t.model
and i.make<>t.make
)

Displaying count of duplicates and removing duplicates at the same time?

Apologies if the title doesn't make full sense, I'll try to explain as best I can.
I have a table containing information about vehicles, there are many duplicates and around 5000 rows overall. Here's a snippet as an example:
As you can see the model '159 TI TBI' repeats twice, this essentially means there are two of these cars stored in London.
I am looking for something like below, where there is a count of how many times a particular vehicle in a particular location repeats, as well as removing duplicates so each vehicle only appears once for each location.
I am able to do a fairly simple select command for a particular vehicle and location, such as
SELECT COUNT(model), model, loc_name, vehicle_type
FROM vehicles
WHERE loc_name='London' AND model='159 TI TBI'
GROUP BY model, loc_name, vehicle_type
The issue is that I'd be repeating this command for every combination of a vehicle model in a particular location, it's not very efficient.
Hopefully this makes some sense, I haven't had a huge amount of experience with SQL so apologies if anything is badly wrong. Thanks.
This query will give you the required results
SELECT COUNT(model) cnt, model, loc_name, vehicle_type
FROM vehicles
GROUP BY model, loc_name, vehicle_type
Your question is a bit unclear. But let me try. It seems you think to get the count for each group, you would have to re-query with for each vehicle in the where clause. However, aggregation will allow you to get the count across all the vehicles. If you are just looking for the model, location, type uniquely and the count of occurrences, you have the right query, just remove your where clause and the power of SQL will take care of it for you.
SELECT COUNT(*) as quantity, model, loc_name, vehicle_type
FROM vehicles
GROUP BY model, loc_name, vehicle_type
if you want only the rows with more then one occurrence you can use having for filter th aggregated result
SELECT COUNT(*) as quantity, model, loc_name, vehicle_type
FROM vehicles
GROUP BY model, loc_name, vehicle_type
having count(*) > 1

SQL: ORDER BY with 2 criteria not working

In my work I have 2 tables:
Airports(serial_number, name, maker, model_name (which is a foreign key to the next table), op_start_date, op_end date
Models (name, number_rows, is_narrow, is_wide)
With them I have to make a table with the information for each airplane of their name, maker, model, and if they are wide-body or narrow-body. Adiotionally, I am supposed to order the list by their maker and their model.
The code I tried is below
SELECT maker, Airplanes.name, model_name, is_narrow, is_wide
FROM Airplanes
JOIN Models ON model_name=Models.name
ORDER BY maker, model_name;
However the table obtained was this one:
Link to the image:
As you can see, it's ordered by maker, but not model. Can anyone help me with this problem?
This could occur if you had invisible characters in the maker.
Try running:
SELECT a.maker, count(*)
FROM Airplanes a
GROUP BY a.maker
ORDER BY a.maker;
See if the "same" value for maker turns up more than once.