I have two tables called Job and Bid . customer can add job details and multiple suppliers can bid for the job.Once customer select one supplier from bids,that supplier id (sId) will update on Job table. i want to select job details with bid value.
This is what i tried
SELECT job.id,job.title,job.desc,job.mobile,job.address, job.city,job.updatedAt,job.status,bid.value,customer.cName,supplier.sName
FROM job
LEFT JOIN customer
ON customer.id = job.cId
LEFT JOIN supplier
ON supplier.id = job.sId
LEFT JOIN bid
ON bid.sId = job.sId`
but this query showing messy information with some duplicates
job Table
bid table
There are two suppliers (sId=2 and sId=1) has bid for job no 5 (i have marked on bid table).but customer has pick sId 2 for the job number 5.now i want to select from those tables without messy records.
I think that you are just missing a join condition on bid, that filters on the chosen supplier. The logic to allow jobs without a supplier yet while evicting jobs whose supplier was not picked by the customer is a bit tricky, and is implemented in the WHERE clause:
SELECT ...
FROM job
INNER JOIN customer
ON customer.id = job.cId
LEFT JOIN supplier
ON supplier.id = job.sId
LEFT JOIN bid
ON bid.sId = job.sId
AND bid.jId = job.id --> here
WHERE supplier.id IS NULL OR bid.id IS NOT NULL
in your select statment you tring to retrive data from bid table bid.value but you didn't join that table.
select j.id,
j.title,
j.desc,
j.mobile,
j.address,
j.city,
j.updatedAt,
j.status,
b.value,
c.cName,
s.sName
from job as j
inner join customer as c
on j.cId = c.id
inner join supplier as s
on j.sId = s.id
inner join bid as b
on j.id = b.jId;
Related
I have written the following two queries for the below requirement. Please let me know which method is correct or both methods are wrong? Thanks a lot
There were two tables -
'Orders' with - order_id(PK), item id, quantity, order_date [Transactional Table]
'Catalog' with-item id, product group, location [Dimension Table]
They asked to write a SQL code that will return the product groups of US that has no sale in any unit(i.e all the item id from an individual product group has no sale).
1st Method:
with cte as
(
select c.*,o.order_id,
case when o.order_id is not null then 1 else 0 end sale_ind
from Catalog c
left join Orders o
on c.item_id = o.item_id
and c.location = 'US'
)
select product_group
from cte
group by product_group having sum(sale_ind) = 0
2nd Method:
select c.*
from Catalog c
where c.location='US'
and item_id not in (
select item_id
from Orders)
They asked to write a SQL code that will return the product groups of US that has no sale in any unit(i.e all the item id from an individual product group has no sale).
I would tend to go with not exists for this:
select distinct c.product_group
from catalog c
where c.location = 'US' and
not exists (select 1
from orders o
where o.item_id = c.item_id
);
That said, both your queries look okay, but the first is correct. The second is returning all catalog records not all product_groups. As for the second, I would discourage you from ever using not in with a subquery. No rows are returned if item_id returned by the subquery is ever NULL.
SELECT DISTINCT c.product_group
FROM Catalog c
LEFT OUTER JOIN Orders o
on c.item_id = o.item_id
WHERE c.location='US'
AND o.item_id is null
Left join: because you want catalog records (left side) even if there are no order records (right side). The second part of the WHERE clause filters out instances where there are orders.
You can’t use an inner join as that would return only records where the Catalog record had corresponding orders, which is not what you want
I'm trying to make a request which results some SUM and COUNT. But some SUM rows are false because of duplicate inner join rows. I tried so many ideas but no one worked at the moment.
With INNER JOIN, when i have multiple rows on invoiceRow or invoicePayment, sum of invoice rows are false (ex : if I have 4 rows on invoiceRow, I will have Sum(invoice) multiplied by 4).
INVOICE
id
customer
repertory
subTotal
totalPaid
CUSTOMER
id
name
INVOICEROW
id
invoice
type
INVOICEPAYMENT
id
invoice
method
amount
If somebody can help me, thanks by advance.
I have tried some SUM(DISTINCT) but it doesn't really work as i want to.
sql results with inner join
SELECT DATE(i.endDate),
c.currency,
ir.type,
ip.method,
AVG(i.totalPaid),
COUNT(DISTINCT i.repertory),
COUNT(DISTINCT i.id),
SUM(i.subTotal),
SUM(i.totalPaid)
from invoice i
INNER JOIN invoiceRow ir ON ir.invoice=i.id
INNER JOIN invoicePayment ip ON ir.invoice=ip.invoice
INNER JOIN customer c ON c.id=i.customer
where i.customer=21 and i.totalPaid is not null and ip.method IN (3)
GROUP BY DATE(i.endDate);
results without inner join
SELECT DATE(i.endDate),
c.currency,
AVG(i.totalPaid),
COUNT(DISTINCT i.repertory),
COUNT(DISTINCT i.id),
SUM(i.subTotal),
SUM(i.totalPaid)
from invoice i
INNER JOIN invoiceRow ir ON ir.invoice=i.id
INNER JOIN invoicePayment ip ON ir.invoice=ip.invoice
INNER JOIN customer c ON c.id=i.customer
where i.customer=21 and i.totalPaid is not null
GROUP BY DATE(i.endDate);
Customer should be parent, so it can exists int the query with a join when you use Sum(). InvoiceRow and InvoicePayment however, don't seem to have any contribution to the result other than an existence check:
SELECT DATE(i.endDate),
c.currency,
AVG(i.totalPaid),
COUNT(i.repertory),
COUNT(i.id),
SUM(i.subTotal),
SUM(i.totalPaid)
from invoice i
INNER JOIN customer c ON c.id=i.customer
where i.customer=21 and i.totalPaid is not null AND
exists (select * from invoiceRow ir
INNER JOIN invoicePayment ip ON ir.invoice=ip.invoice
where ir.invoice=i.id)
GROUP BY DATE(i.endDate), c.Currency;
EDIT: As you said there were fields from other tables, here is how you should handle it:
SELECT DATE(i.endDate),
c.currency,
AVG(i.totalPaid),
COUNT(i.repertory),
COUNT(i.id),
SUM(i.subTotal),
SUM(i.totalPaid),
irp.Type, irp.Method
from invoice i
INNER JOIN customer c ON c.id=i.customer
inner join (select distinct i.id, ir.Type, ip.Method
from invoice inv
inner join invoiceRow ir
INNER JOIN invoicePayment ip ON ir.invoice=ip.invoice
where ir.invoice=inv.id)irp on irp.invoice = i.id
where i.customer=21 and i.totalPaid is not null
GROUP BY DATE(i.endDate), c.Currency, irp.Type, irp.Method;
In general it looks weird to have type and method on InvoiceRow and InvoicePayment, I would expect them to be on header.
I have some problem with the query issue when trying to sum up the quantity.
Table
This cart item table stored id_cart and id product
This order table stored id_cart and other id may be included such as supplier. This table is used to track order record and send notification to supplier.
Wrong result. Expected output = 1, 1, 1
SELECT id, id_product, SUM(qty)
from cart_item
left join Orderp using(id_cart)
group by id_product
http://sqlfiddle.com/#!9/07bf57/1
The issue caused by duplicate id_cart in order table as well. How can i handle this? Any solution to make it works? Thanks.
There is something wrong in your data, or in your data model
INSERT INTO OrderP(`id_order`,`id_cart`)VALUES(1, 1);
INSERT INTO OrderP(`id_order`,`id_cart`)VALUES(2, 1);
There are 2 rows for id_cart = 1, so the "natural join" will double every row when joining cart_item to orderp.
Using an inner join to a different column in orderp works better because now there is only one row in orederp for each cart_item.
SELECT id_product, sum(qty)
from cart_item ci
left join Orderp o on ci.id_cart = o.id_order
GROUP BY id_product
http://sqlfiddle.com/#!9/07bf57/13
Try the following query
SELECT
i.id_product,
p.name productname,
b.id_branch,
b.branchname,
SUM(i.qty)
from cart_item i
left join (SELECT DISTINCT id_cart,id_branch FROM Orderp) o on o.id_cart=i.id_cart
left join product p on i.id_product=p.id_product
left join catalog c on c.id_product=p.id_product and c.id_branch=o.id_branch
left join branch b on b.id_branch=o.id_branch
group by
i.id_product,
p.name,
b.id_branch,
b.branchname
The main problem in Orderp table because it containts two different orders for one cart (DISTINCT id_cart,id_branch helps here). And you need to use the second condition by id_branch for catalog (and c.id_branch=o.id_branch).
SQL Fiddle - http://sqlfiddle.com/#!9/f32d5f/16
And I think you can use everywhere INNER JOIN instead LEFT JOIN
SELECT
i.id_product,
p.name productname,
b.id_branch,
b.branchname,
SUM(i.qty)
from cart_item i
join (SELECT DISTINCT id_cart,id_branch FROM Orderp) o on o.id_cart=i.id_cart
join product p on i.id_product=p.id_product
join catalog c on c.id_product=p.id_product and c.id_branch=o.id_branch
join branch b on b.id_branch=o.id_branch
group by
i.id_product,
p.name,
b.id_branch,
b.branchname
I'm getting the error " Not a Unique table/alias 'Customer' " when I run my join statement. I want to display all the information that I have. I've researched the JOIN statements and I can't find what's wrong.
SELECT Customer.CustomerID, Customer.FirstName, Customer.LastName, Customer.StreetAddress,Customer.City,Customer.State,Customer.Zipcode, Customer.HomePhone,Customer.MobilePhone,Customer.OtherPhone, Pizza.PizzaID,
Pizza.PizzaName, Pizza.Description, Pizza.UnitPrice, OrderInformation.OrderID, OrderInformation.OrderDate, OrderItem.Quantity
FROM Customer
JOIN OrderInformation ON OrderInformation.OrderID = OrderItem.OrderID
JOIN Pizza ON Pizza.PizzaID = OrderItem.PizzaID
JOIN Customer ON Customer.CustomerID = OrderInformation.CustomerID;
SELECT
...
FROM Customer
...
JOIN Customer
You're selecting FROM Customer and then doing JOIN Customer, meaning you now have two instances of Customer. When you reference something like Customer.CustomerID, your query doesn't know which iteration of the Customer table you're referring to.
If you actually needed two references to the same table, you could give one an alias. However, being that you reference an OrderItem table, but never JOIN or select FROM it, I have a hunch that one of those Customer tables should be OrderItem. Perhaps like this...
SELECT ...
FROM OrderItem
JOIN OrderInformation ON OrderInformation.OrderID = OrderItem.OrderID
JOIN Pizza ON Pizza.PizzaID = OrderItem.PizzaID
JOIN Customer ON Customer.CustomerID = OrderInformation.CustomerID;
I would strongly suggest that you use table aliases. It would appear that the first reference to Customer should really be OrderItem:
SELECT c.CustomerID, c.FirstName, c.LastName,
c.StreetAddress, c.City, c.State, c.Zipcode,
c.HomePhone, c.MobilePhone, c.OtherPhone,
p.PizzaID, p.PizzaName, p.Description, p.UnitPrice,
oinf.OrderID, oinf.OrderDate, oi.Quantity
FROM OrderItem oi JOIN
OrderInformation oinf
ON oinf.OrderID = oi.OrderID JOIN
Pizza p
ON p.PizzaID = oi.PizzaID JOIN
Customer c
ON c.CustomerID = oi.CustomerID;
I'm trying understand how I can pull information from multiple tables at once in one query if that is possible.
I have 3 tables and I'm wondering if there is a way I can query all the product names for customers that live in california?
Table:
products
Fields:
productOid
productName
companyOid
Table:
customerData
Fields:
customerOid
firstName
lastName
state
Table:
orders
Fields:
orderNumber
customerOid
productOid
Would this fall under something like an INNER JOIN?
Also, I'm learning mySQL.
You will need to use inner joins for this.
SELECT DISTINCT p.productName
FROM orders o
INNER JOIN customerData c ON o.customerOid = c.customerOid
INNER JOIN products p ON o.productOid = p.productOid
WHERE c.state = 'CA';
I am using DISTINCT here because it's possible a customer would order the same product more than once (or multiple customers would order the same products) and I'm assuming you don't want duplicates.
I'm also making the assumption that your state is represented as a two character column.
Read more about joins
You could use one more join, but I would write it this way:
SELECT DISTINCT p.productName
FROM
orders o INNER JOIN products p
ON o.productOid = p.productOid
WHERE
o.customerOid IN (SELECT customerOid
FROM customerData
WHERE state = 'California')
It might be a little slover than a join, but it's more readable.
This shows products that CA customers have ordered:
SELECT p.productName
FROM orders o
INNER JOIN products p ON o.productOid = p.productOid
INNER JOIN customerData c ON o.customerOid = c.customerOid
WHERE c.state = 'CA'