probably a simple query for someone to answer but I'm new at this and a bit stuck!
Trying to map from one table to another and sum together numbers in a column from Table 1. For example:
Table 1:
Item_ID Price
I0001 3.50
I0002 2.50
Table 2:
Item_ID Date_sold
I0001 10/11/14
I0002 12/11/14
What I want to do is tell MySQL that where 'Date_sold' is 'not null' in Table 2, to identify 'Item_id', match this back to table 1, read the 'Price' column in that row, and then add the results together for total revenue.
Any help appreciated!
I ll try something like that :
SELECT t1.Item_ID, SUM(Price) AS Total
FROM Table1 t1
INNER JOIN Table 2 t2
ON t1.Item_ID = t2.Item_ID
WHERE t2.Sold_date IS NOT NULL
GROUP BY Item_ID;
You will get the grand total by item if you add the group by statement.
If you only want the grand total :
SELECT SUM(Price) AS GrandTotal
FROM Table1 t1
INNER JOIN Table 2 t2
ON t1.Item_ID = t2.Item_ID
WHERE t2.Sold_date IS NOT NULL;
You could join both tales on the item_id, and then group by to sum the price:
SELECT date_sold, SUM(price)
FROM table_2
JOIN table_1 on table_2.item_id = table_1.item_id
WHERE date_sold IS NOT NULL
GROUP BY date_sold
select sum(table1.price) as revenue from table1 join table2 on table1.item_id=table2.item_id where table2.data_sold is not null
Related
I was wondering if there is a quick way to do the following. As I understand it, an INNER JOIN on a field present in two tables returns information from the two tables having the field and its values in common. However, what I'm trying to do is to return the table rows that do not meet the INNER JOIN criteria. So, for example if I had Table 1 as
Product Price
Greens $2
Beans $1
Potatoes $3
Tomatoes $4
and table 2 as
Product Quantity
Potatoes 45
Tomatoes 100
Chickens 27
Turkeys 33
what I'm looking to output is
Product Price Quantity
Greens $2 NULL
Beans $1 NULL
Chickens NULL 27
Turkeys NULL 33
Is this at all possible?
Thanks!
You'd have to use two outer joins, one for products not having a corresponding price (price IS NULL) and one for products not having a corresponding quantity.
I used two separate queries (one for each join).
Next, used UNION to combine the results.
SELECT table1.Product, table1.Price, table2.Quantity
FROM table1
LEFT JOIN table2 ON table1.Product = table2.Product
WHERE table2.Quantity IS NULL
ORDER BY table1.Price DESC
UNION
SELECT table2.Product, table1.Price, table2.Quantity
FROM table1 RIGHT JOIN table2 ON table1.Product = table2.Product
WHERE table1.Price IS NULL
ORDER BY table2.Quantity
You can use NOT EXISTS for each of the tables and combine the results with UNION ALL:
select t1.product, t1.price, null as quantity from table1 as t1
where not exists (select 1 from table2 where product = t1.product)
union all
select t2.product, null as price, t2.quantity from table2 as t2
where not exists (select 1 from table1 where product = t2.product)
You can achieve it using the UNION and MINUS operations.
(SELECT * FROM t1 UNION SELECT * FROM t2) MINUS SELECT * FROM t1 INNER JOIN t2 ON t1.Product = t2.Product
I'm using PDO to handle my database and I have one table:
Table:
id sku category
1 XYZ Ballerinas
2 Ballerinas
3 Ballerinas
4 ABC Ballerinas
As you can see I have 4 rows that are in the same category Ballerinas, but two of them don't have an sku. How can I return those rows using a PDO query only if all Ballerinas have an sku?
Thank you! :D
One option to find matching categories is to aggregate by category and then assert the total count matches the count of sku values which are not empty string. If these two counts match, then retain that category.
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT category
FROM yourTable
GROUP BY category
HAVING COUNT(*) = COUNT(CASE WHEN sku <> '' THEN 1 END)
) t2
ON t1.category = t2.category;
Demo
Or you can do it without a join:
SELECT *
FROM Table1 t1
WHERE NOT EXISTS (SELECT 1 FROM Table1 t2 WHERE t1.category = t2.category AND t2.sku IS NULL);
If you consider empty values ('') also as "not being a value" then the condition in the exists clause should be
where t2.category=t1.category and not t2.SKU>''
I would like to display all orders which have more than 1 item but to display only 2 rows if they have more than 2 items
If you have any ideas i will appreciate it, thank you.
You already have a row number column for each id, which greatly simplifies the query. In this case, we can arrive at your expected output by just joining the ORDERS table to a subquery which identifies id having 2 or more records associated with them.
SELECT t1.*
FROM ORDERS t1
INNER JOIN
(
SELECT id
FROM ORDERS
GROUP BY id
HAVING COUNT(*) >= 2
) t2
ON t1.id = t2.id
WHERE t1.rown_num <= 2
try this simple solution
SELECT * FROM orders od WHERE od.id IN( SELECT id FROM orders o GROUP BY o.id HAVING o.id HAVING COUNT(o.id)>1)
try this:
the first join will give you the rows you want and the second join will add the rown_num field
select a.id,
a.item_num,
b.rown_num
from
(select id,
item_num
from orders
where rown_num in (1,2)
group by id,item_num
having count(id)>=2)a
inner join (select *
from orders )b on a.id=b.id
and a.item_num=b.item_num
I have 2 tables one with inventory and other with prices list on different dates. I need to update table 1 with price on a particular date which may not be available in table 2 so i need to lookback on last available price. How can I do this. Following are my tables:
Table1
SrNo Commodity Date Price
1 Car 20-Aug-2015 <115>
2 Cycle 20-Aug-2015 <78>
Table2
SrNo Commodity Price Date
1 Car 100 1-Jan-2015
2 Car 120 1-Jun-2015
3 Car 115 20-Aug-2015
4 Cycle 80 10-May-2015
5 Cycle 78 10-Jun-2015
I tried using an inner join but I could get it for Car since it has an entry on 20-Aug-2015. I want cycle to be shown as 78 as it was the last available price.
Can someone suggest me how to do this.
Thanks,
Swati
Next code will work on T-SQL - try so
update t1 set
t1.Price = t2.Price
from Table1 as t1
outer apply (
select top 1
t2.Price
from Table2 as t2
where t2.SrNo = t1.SrNo
order by t2.Date desc
) t2
Try this:
UPDATE a
SET a.Price = b.Price
FROM Table1 a
INNER JOIN Table2 b ON a.Commodity = b.Commodity
WHERE b.[Date] = (SELECT MAX([Date])
FROM Table2 c
WHERE b.Commodity = c.Commodity
AND c.[Date] <= a.[Date]
GROUP BY c.Commodity)
For MySql
UPDATE Table1
JOIN ( SELECT Commodity,Price
FROM Table2 JOIN (SELECT Table2.Commodity,MAX(DATE) As LastDate
FROM Table2
GROUP BY Commodity ) AS Tmp1
ON Table2.Date = Tmp1.LastDate
) AS Tmp2
ON Tmp2.Commodity = Table1.Commodity
SET Table1 .Price = Tmp2.Price
MAX(DATE) is calculated in inner query Tmp1 to get last availaible price of commodity
SQLFiddle Demo
I need to be able to find when each customer placed their second order and I can't think of how to do it!
Basic table structure:
Order ID Customer ID Order Date
1 123 2014-09-12
2 456 2014-10-22
3 456 2014-11-01 <-- THIS IS CUST 456's 2ND ORDER
4 789 2014-11-01
5 123 2014-11-09 <-- THIS IS CUST 123's 2ND ORDER
6 225 2014-11-11
How do I get the second orders out of the table using mysql?
The real table has over 200K of orders and over 70K customers, each ranging from only having placed one order to having placed 20+ orders since 2010.
If you just want the 2nd order date then a self join to exclude the first order, and a MIN to get the remaining earliest order:-
SELECT t1.CustomerId, MIN(t2.OrderDate)
FROM
( SELECT CustomerId, MIN(OrderDate) AS OrderDate
FROM some_table
GROUP BY CustomerId
) t1
INNER JOIN some_table t2
ON t1.CustomerId = t2.CustomerId
AND t1.OrderDate < t2.OrderDate
GROUP BY t1.CustomerId
If you need other details (such as the order id) then you would need to use this as a sub query and join it back against the main table.
EDIT - Might be possible to simplify this as follows:-
SELECT t1.CustomerId, MIN(t2.OrderDate)
FROM some_table t1
INNER JOIN some_table t2
ON t1.CustomerId = t2.CustomerId
AND t1.OrderDate < t2.OrderDate
GROUP BY t1.CustomerId
This is joining the table against itself based on the customer id, but also that the order date is larger on the 2nd join of the table.
This is likely to generate a massive amount of data while doing the calculation.
I have a feeling I have missed something with this though.