Combine database tables and to form into one table - html

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 ;

Related

How do I display multiple columns from 3 Different tables in SQL?

I have 3 tables:
Reservation, Trip, Customer
I only need to display the trip name, trip type, customer first name, customer last name of the customers who have multiple reservations
Sort of like this
Reservation table
Reservation_ID ... ... .. Customer_Num
16001 101
16002 101
16003 102
16004 103
16005 103
Customer table
Customer_ID ... ... .. Customer_Num
30 101
31 102
32 103
Customer table's primary ID is customer_ID and has a column name of
Customer_Num. this column name is also in the reservation table.
Reservation table's primary id is Reservation_ID
I’ve tried:
SELECT Customer.First_Name, Customer.Last_Name, Trip.Trip_Name, Trip.Type, Reservation.Customer_Num COUNT(Reservation.Customer_Num
FROM Reservation, Customer, Trip
WHERE Reservation.Customer_Num = Customer.Customer_Num
HAVING COUNT(Reservation.Customer_Num) > 1
GROUP BY Customer.First_Name, Customer.Last_Name, Trip.Trip_Name, Trip.Type, Reservation.Customer_Num;
Try this, presuming that Trip table using Customer_num field as foreign key
select A.Customer_id, A.Name, A.Last_name, B.name, B.type from Customer as A
where Customer_id in
(select Customer_Num from Reservation group by Customer_Num having count(Customer_Num) > 1)
left join Trip as B on Trip.Customer_Num = Customer.Customer_Num
You can use EXISTS :
SELECT t.*, c.*
FROM reservation r INNER JOIN
customer c
ON c.Customer_Num = r.Customer_Num INNER JOIN
trip t
ON . . .
WHERE EXISTS (SELECT 1
FROM reservation r1
WHERE r1.customer_no = r.customer_no AND
r1.Reservation_ID <> r.Reservation_ID
);
You need to adjust the ON clause for Trip table as you didn't specified the table information.
Presumming in your TRIP table you have columns as trip name, trip type, You may try below query -
SELECT Customer.First_Name
,Customer.Last_Name
,Trip.Trip_Name
,Trip.Type
,Reservation.Customer_Num
,COUNT(Reservation.Customer_Num) Customer_Num_Cnt
FROM Reservation R
INNER JOIN Customer C ON Reservation.Customer_Num = Customer.Customer_Num
INNER JOIN Trip T ON R.TRIP_ID = T.TRIP_ID
GROUP BY Customer.First_Name
,Customer.Last_Name
,Trip.Trip_Name
,Trip.Type
,Reservation.Customer_Num
-- HAVING COUNT(Reservation.Customer_Num) > 1;
If this doesn't fulfil your requirements, Please share the complete structure of TRIP table and CUSTOMER table.
This displays the results making it so I can pick which names have more than one reservation. In the reservation table it lists the customer num who has more than one reservation but I'm not sure why it wasn't working.
SELECT CUSTOMER.FIRST_NAME, CUSTOMER.LAST_NAME, TRIP.TRIP_NAME, TRIP.TYPE, RESERVATION.CUSTOMER_NUM, COUNT(RESERVATION.CUSTOMER_NUM) CUSTOMER_NUM_CNT
FROM RESERVATION
INNER JOIN CUSTOMER ON CUSTOMER.CUSTOMER_NUM = RESERVATION.CUSTOMER_NUM
INNER JOIN TRIP ON RESERVATION.TRIP_ID = TRIP.TRIP_ID
GROUP BY CUSTOMER.FIRST_NAME, CUSTOMER.LAST_NAME, TRIP.TRIP_NAME, TRIP.TYPE, RESERVATION.CUSTOMER_NUM
commented out--HAVING COUNT(RESERVATION.CUSTOMER_NUM) > 1;

Duplicate record issue on query

This is my sample data
-- schema
CREATE TABLE Cart (
Id_cart INT NOT NULL,
Id_product VARCHAR(25)
);
CREATE TABLE Orders (
Id INT,
Id_cart INT NOT NULL,
Id_vendor INT NOT NULL,
status VARCHAR(25),
order_no VARCHAR(25)
);
-- data
INSERT INTO Cart
(Id_cart, Id_product)
VALUES
(1, 'abc002'),
(1, 'abc003')
;
INSERT INTO Orders
(Id, Id_cart,Id_vendor,status,order_no)
VALUES
(1, 1,1, 'pending','aaa001'),
(2, 1,2, 'pending','aaa002')
;
I use this query to show record.
Select c.id_cart,order_no,id_product from cart as c
left join (SELECT id_cart,status,order_no FROM orders) o using(id_cart)
The result i get
id_cart order_no id_product
1 aaa002 abc002
1 aaa001 abc002
1 aaa002 abc003
1 aaa001 abc003
The result i expected
id_cart order_no id_product
1 aaa001 abc002
1 aaa002 abc003
Anything wrong with my query? How can i eliminate the duplicate record?
Let's say i added a column vendor_id to the orders table. Each order_no belong to one vendor and an id_cart will belong to many supplier. When i am trying to display my product, i want to display order_no and my id_product.
E.g. I have a cart belong to two order
an order belong to vendor A (1) and consist of product A and B.
an order belong to vendor B (2) and consist of product C and D.
During display the output should be
order_no | product_id
order1 | A
order1 | B
order2 | C
order2 | D
Now my problem is each order_no will be loop for each product_id. How can i overcome this?
You want to show cart products along with their orders. Carts contain products from different vendors for which exist separate orders. So join the product table to the cart in order to know the vendor and only then join the order table.
select
id_cart,
o.order_no,
id_product
from cart c
join product p using (id_product)
join orders o using (id_cart, id_vendor);
If you also want cart products for which no order has been written yet, make the orders join an outer join.
UPDATE: As you are reporting an issue with "Unknown column 'id_vendor' in 'from clause'", here is the query with ON clauses instead:
select
c.id_cart,
o.order_no,
c.id_product
from cart c
join product p on p.id_product = c.id_product
join orders o on o.id_cart = c.id_cart and o.id_vendor = p.id_vendor;
Simple add GROUP BY to your sql Query
SELECT c.id_cart,order_no,id_product FROM cart AS c
left join (SELECT id_cart,status,order_no FROM orders) o using(id_cart) GROUP BY o.order_no
Example :- http://sqlfiddle.com/#!9/b6b118/6/0

MYSQLQuery- How to create a query where we have to list out customers who have bought a specific number of album by the same singer

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.

Can I get a sum of master and a sum of a detail in one sql statement?

I have a master / detail table arrangement (one to many) and I want a total of a master field and a total of a detail field.
Do I have to run 2 sql statments or can I combine them efficiently into one statement and still not get duplicate sum of the master value?
Sample:
orders:
customer_id int,
order_id int,
order_amount int
details:
customer_id int,
order_id int,
detail_amount int
SELECT sum(order_amount), sum(detail_amount) FROM orders m
JOIN details d on d.order_id = m.order_id
WHERE customer_id = 123;
the results will be incorrect when orders exist with multiple details.
I can:
SELECT sum(order_amount) FROM orders m
WHERE customer_id = 123;
and
SELECT sum(detail_amount) FROM details d
WHERE customer_id = 123;
can I combine them efficiently?
It is easy to do with a subquery.
SELECT sum(order_amount) as orderTotal, (SELECT sum(detail_amount) FROM details d
WHERE d.customer_id = m.customer_id) as detailTotal FROM orders m
WHERE customer_id = 123;

Inactive customers mysql query optimization

I have a customer table with million of records.
Customer
id | name | .....
I also have an orders table with
id | custID | orderDate | ....
I need to find all the people who have not placed an order for more than 30 days.It should also include people who have never placed the order
select name,customer.id from customer where id in
(select custID from orders where datediff(curdate(),orders.orderDate) > 30 )
union
select name,customer.id from customer left join orders on customer.id = orders.custID where orders.id is null
How can i optimize the query ?
Try
select name,t.id
from customer t where
not exists (
select 1
from orders where
custID=t.id
and
datediff(curdate(),orders.orderDate) <= 30 )
Try this one
Select Customer.Custid,
Customer.name
from Customer
left join orders on
customer.custid = orders.custid and
datediff(getdate(),orders.orderdate)>30)
where
orders.id is null