In redshift for a provided dataset for a restaurant
Every Dish_id is being assigned to a category
So based on the distinct orders which are being palced
I need to find out for a provided primary dish what all other items went along with it
primary item (every distinct item of that restaurant will act as a primary dish once)
Currently i am able to do it for a single dish_id and getting its contribution
Select category_name,count(category_name) from (
Select order_id,dish_id,dish_name,category_id,category_name from abc
where order_id in (Select distinct order_id from abc where dish_name='Paneer_pizza' and restaurant_id=1)
group by order_id,dish_id,dish_name,category_id,category_name
order by category_name
)
group by category_name
Question's
How can i print Panner_pizza in the outer query along with category_name and count?
How can i pass all the dish_name present in that restaurant in inner query
and get the contribution for all the dishes along with count for all the categories?
Hard to tell exactly what you want...you might need two queries. But I think an inner query like this might help. This will filter the results to just the primary dish and restaurant that you are interested in and add the primary dish to each record in the order.
select a.order_id, primary_dish, dish_id, dish_name, category_id, category_name
from abc a
inner join (
Select distinct order_id, dish_name as primary_dish
from abc
where dish_name='Paneer_pizza' and restaurant_id=1
) b on b.order_id = a.order_id
where primary_dish != dish_name --optionally exclude the primary dish record
So count of other dish categories for the primary dish would be:
with dishes as (
select a.order_id, primary_dish, dish_id, dish_name, category_id, category_name
from abc a
inner join (
Select distinct order_id, dish_name as primary_dish
from abc
where dish_name='Paneer_pizza' and restaurant_id=1
) b on b.order_id = a.order_id
where primary_dish != dish_name --optionally exclude the primary dish record
)
select primary_dish, category_name, count(*) from dishes
group by primary_dish, category_name
I think this might be what you actually want. This gives all the dishes in the restaurant treating each as a primary dish and then gives dishes ordered with the primary, their category and the total number of orders placed that included the secondary dish:
with dishes as (
select a.order_id, primary_dish, dish_id, dish_name, category_id, category_name
from abc a
inner join (
Select distinct order_id, dish_name as primary_dish
from abc
where restaurant_id=1
) b on b.order_id = a.order_id
where primary_dish != dish_name --optionally exclude the primary dish record
)
select primary_dish, dish_name, category_name, count(*) from dishes
group by primary_dish, dish_name, category_name
Related
Can you help me to find the proper MySQL query to get the most numbers of vendors per a given location and list them all by name and name of their shop:
1 - The query must find out which location has the highest number of vendors then list them by name with the name of the shop they work in.
I have the following tables:
CITIES
(
ID "unique",
NAME
)
SHOPS
(
ID "unique",
NAME,
CITY_ID ( foreign key of CITIES TABLE ID)
)
VENDORS
(
ID "unique",
NAME,
SHOP_ID ( foreign key of SHOPS TABLE ID)
)
Example with dummy data
CITIES : NY, SF
SHOPS: Boom - NY, Flash - NY, Sofast - SF
Vendors:
Mark : Boom,
John : Boom,
Carlos : Sofast,
Alex : Sofast,
David : Flash,
James: Flash
The NY has the highest number of vendors so it should list
Mark : Boom, John : Boom, David : Flash, James: Flash
Check if this works -
Select vendors.name, shops.name
from
cities inner join shops on cities.id= shops.city_id
inner join vendors on shops.id = vendors.shop_id
where cities.id = (select id from (select cities.id, count(1) from
cities inner join shops on cities.id= shops.city_id
inner join vendors on shops.id = vendors.shop_id
group by cities.id order by 2 desc) limit 1)
If you are running MySQL 8.0, you could approach this with window functions:
select *
from (
select x.*, rank() over(order by cnt) rn
from (
select v.*, count(*) over(partition by c.id) cnt
from cities c
inner join shops s on s.city_id = c.id
inner join vendors v on v.shop_id = s.id
) t
) t
where rn = 1
The most inner query joins the three tables, and counts how many vendors each city has. The next level ranks records by descending count. Finally, the last level filters on top-ranked rows.
I was trying to access list of foods with which has restaurant_id = 2 and if the food_id in carts table it should print cart_id and if the food_id in favorites table it should print favorite_id otherwise it will return null. But the problem i'm facing here is food_id i'm getting from that specific restaurant but while checking the carts and favorites id it fails to check for the user_id.
food_id is in the carts table but not for that user but it shows that cart_id. How do i make the query correct?
Foods table: id, restaurant_id
Carts Table: id, user_id, food_id
Favorites Table: id, user_id, food_id
SQL QUERY:
SELECT foods.id, carts.id as cart_id , favorites.id as favorite_id
FROM foods
LEFT JOIN carts
ON carts.food_id= foods.id
LEFT JOIN favorites
ON favorites.food_id= foods.id
WHERE foods.restaurant_id = 2
AND (carts.user_id = 1 OR favorites.user_id = 1)
you need to use something like this:
SELECT foods.id, carts.id as cart_id, favorites.id as favorite_id
FROM foods
left join carts on carts.food_id=foods.id and carts.user_id = 1
LEFT JOIN favorites on favorites.food_id=foods.id and favorites.user_id = 1
WHERE foods.restaurant_id = 2
note that you need to filter both tables (cards/favorites) by user id
Lets say I have this tables:
country:
country_id,
country_name;
currency:
currency_id,
currency_name;
country_currency:
country_id,
currency_id;
I would like to select and show all countries having two or more currencies. How should my query look like?
Join all the tables with the common columns, then group the result by country_id, and count the number of rows (or currency) per group as shown below.
SELECT A.COUNTRY_ID, COUNT(*) NUMBER_OF_CURRENCY
FROM COUNTRY A
INNER JOIN COUNTRY_CURRENCY B ON A.COUNTRY_ID = B.COUNTRY_ID
INNER JOIN CURRENCY C ON B.CURRENCY_ID=C.CURRENCY_ID
GROUP BY A.COUNTRY_ID
HAVING NUMBER_OF_CURRENCY > 1 ;
A better query would be:
SELECT COUNTRY_NAME
FROM COUNTRY
WHERE COUNTRY_ID IN (
SELECT COUNTRY_ID
FROM COUNTRY_CURRENCY
GROUP BY COUNTRY_ID
HAVING COUNT(*) > 1
);
We have the following tables:
Customer:
customer_id (pk)
customer_name
customer_dob
adress
gender
Album:
Album_id (PK)
album_name
singer_id (FK)
Order_Details:
order_id (Pk)
quantity purchased
customer_id (fk)
Order_Basket:
Order_id (fk)
album_id (fk)
I want to create an SQL query statement where I want to list out customers that have purchased 4 or more albums by the same singer. I'm very new to this and I would like to seek input from the much more professional individuals here.
try this:
select customer.customer_id, album.singer_id , count(*)
from customer ,order_details, order_basket, album
where customer.customer_id = order_details.customer_id
and order_details.order_id = order_basket.order_id
and order_basket.album_id = album.Album_id
group by customer.customer_id, album.singer_id
having count(*) > 3
WITH PurchaseSummary AS (
SELECT d.customer_id, a.singer_id, count(a.album_id) AS album_count
FROM Order_Details d
LEFT JOIN Order_Basket b ON on b.Order_id = d.Order_id
LEFT JOIN Album a ON a.Album_id = b.album_id
GROUP BY d.customer_id, a.singer_id)
SELECT ps.customer_id, ps.singer_id, ps.album_count
FROM PurchaseSummary ps
LEFT JOIN Customer c ON c.customer_id = ps.customer_id
WHERE ps.album_count >= 4
ORDER BY d.customer_id
Use a subquery to look at all of your orders. Because an order may have more than one album, tack on the order_basket table. Now you have all orders with albums. Tack on the Album table and you have the artist name for each of the orders. Because you also have the customer ID, you can group the customer ID and singer ID with a count of the albums. The result will be like:
Customer|Artist|Num Albums
1 A 1
1 B 4
2 A 1
2 B 6
2 C 5
Then, you select your customer ID and artist ID from that table with your condition they have to have bought >=4 albums from that artist. Lastly, join the Customer table on to get further details about that customer. If you have an Artist table too, you can join that on the Artist ID to get things like name, etc.
I have two tables, one is Customer Detail and the other is Order Detail. Each time the Customer will order something it will store it in database. How can I find which customer orders and how many times they order and combine the customer name and orders?
CUSTOMER DETAIL:
ID NAME AGE COUNTRY
1 AAA 22 US
2 BBB 26 UK
3 CCC 25 TN
ORDER DETAIL:
ID ITEMS PRICE QUANTITY
1 APPLE 5
1 ORANGE 6
1 MANGO 4
Customer AAA has many orders.
EXPECTED RESULT:
CUSTOMERDETAIL ORDERDETAIL
AAA (HOW MANY TIMES HE ORDER AND ORDER DETAILS)
Create Table Customer (CustomerId int identity(1,1),Name varchar(20))
Create Table OrderDetail(OrderDetailId int identity(1,1), CustomerId int, Item varchar(20), Quantity int)
Insert into Customer values('AAA')
Insert into Customer values('BBB')
Insert into Customer values('CCC')
Insert into OrderDetail values(1,'Apple',10)
Insert into OrderDetail values(1,'Banana',10)
Insert into OrderDetail values(1,'Mango',10)
select y.total, x.Item,x.quantity from
(
select c.customerId as customerId, d.Item, d.quantity from customer c inner join orderdetail d
on c.customerId = d.customerId
)x
inner join
(
select customerid, COUNT(customerId)as total from orderdetail group by customerId
)y
on x.customerId = y.customerId
Suppose you have the schema like this:
Customer table (customer)
ID (Customer Id) int primary key
Name varchar(100)
Order Table (orders)
ID (Order ID) int
cusotmer_id (reference id of customer table primary key-ID) int
quality int
price float
First Count of Order against eash customer:
select count(o.customer_id) as total_order, c.* from customer c left join orders o on c.ID = o.customer_id
Orders listing against a single customer:
select c.*, o.* from customer c inner join orders o on c.ID = o.customer_id where c.ID = 1
I hope this will help you.
The following query gives what i expect.
SELECT name,items,price FROM customerdetail INNER JOIN orderdetail ON customerdetail .sno=orderdetail .id WHERE id=1 ;