Microsoft Access SQL Homework Issue - sql

For each maker that produces printers, find the number of printer models produced by this maker. Output a table consisting of two columns: (maker, number of models). Sort the result by numbers of models in ascending order.
What I have tried so far:
SELECT maker, Count(*) AS [Number of Models]
FROM (SELECT DISTINCT model
FROM Product
WHERE type='printer')
It doesn't work, prompts and asks me to enter a maker instead of inserting makers from my list.

You can simply use this query :
SELECT maker, Count(distinct model) AS [Number of Models]
FROM Product WHERE type='printer'
group by maker
order by Count(distinct model)

Related

How to GROUP BY a column/s in data studio

I have a sales table with the purchase history of multiple customers. Needless to say a single customer can appear multiple times in the table. I need to group by the customers and do a count of the industries each customer works in and visualize it in a table in data studio. I need to do all of this in data studio itself.
In big query the syntax would look something like this:
SELECT Industry, count(industry) AS industry_count
FROM (SELECT
CustomerID,
Industry
FROM `project1.pr.df_full`
WHERE segment = 'Lost'
GROUP BY CustomerID, Industry)
GROUP BY Industry
ORDER BY industry_count DESC
How can I achieve the same thing in data studio? The WHERE clause doesn't have to be there because I have a segment filter on the page I'm trying to do this on
As I said in the comment, I reproduced your query and it worked fine.
Here you can see a guide about how to connect BigQuery to DataStudio
Please, notice that DataStudio have some limitations in the query syntax:
If you need further information, please let me know
You could query the raw data, and make calculations on Data Studio side. Be sure to use the field you need to group by as dimension.
SELECT
CustomerID,
Industry,
segment
FROM `project1.pr.df_full`
Then in a Data Studio table use "Industry" field as dimension, and "CustomerId" field as metric, using Count as the aggregation for the metric. As you also have a "segment" field in your data source, filtering by this field will not be a problem.
I hope this help!
I'm wondering why you don't write the query as:
SELECT CustomerID, COUNT(DISTINCT Industry) as industry_count
FROM `project1.pr.df_full`
WHERE segment = 'Lost'
GROUP BY CustomerID
ORDER BY industry_count DESC;

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

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.

Need SQL query to group together but sort overall

I have a table with Display_UPC, Brand, Item_Description, and other fields. There are several items with the same Display_UPC (all items belonging to the same display), and some displays have multiple brands.
I'm trying to print out a page that shows all of the Display contents (all Display_UPC together) with the various item descriptions, but sorted by Brand (so it starts with the "A" brands at the top of the page, then "B" brands, etc...).
Problem is, if I try:
SELECT DISTINCT *
FROM tbl_All_Displays
ORDER BY Brand, Display_UPC
some of the displays (the ones containing multiple brands) are missing some items because they are different brands. I can get rid of "Brand" in the ORDER BY and it returns complete displays together but they are not sorted by Brand (obviously).
I'm guessing maybe a GROUP BY is needed here but I can't get one to work. If I try something like:
SELECT DISTINCT *
FROM tbl_All_Displays
GROUP BY Display_UPC
ORDER BY Brand, Display_UPC
I get the error:
Column 'tbl_All_Displays.Item_Description' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
But I need Item_Description (and the other various fields) to be displayed on the page. They just aren't important in the ordering/grouping.
Sample Data:
Sample Expected Result:
So, basically, it doesn't matter which brand in a display the query uses in the sort. If a display contains a brand then it's okay for it to belong to that brand "group" if that makes sense. Is this possible?
Note: I deleted and reposted a previos question because it got messy with edits.
Edit: Here is sqlfiddle with sample table data - http://sqlfiddle.com/#!6/5069c
You want to sort first by the min(Brand) of each Display_UPC?
Then you need to sort by a "Group Min" first (fiddle:
SELECT *
FROM Table1
ORDER BY
min(Brand) over (partition by Display_UPC),
Display_UPC,
Brand
dnoeth did a great solution. But i want show the one I was working with.
SQL FIDDLE DEMO
with
minBrand as (
SELECT Display_UPC, MIN(BRAND) Brand
from Table1
GROUP BY Display_UPC
)
select m.Brand MainBrand, t.*
from minBrand m
inner join Table1 t
on m.Display_UPC = t.Display_UPC
order by m.Brand, t.Display_UPC, t.Brand, t.Item_Description