I need help with some basic sql...
here is the problem:
In first table I have
ID (primary key)
data column (varchar)
which contains products, in other I have
ID (primary)
second ID (foreign key) //references to ID from first table
price (decimal 8,2)
What I need is to SUM price from second table that corresponds products from first table. also it should be saved as view.
any help?
JOIN the two tables, with GROUP BY and an aggregate function SUM like this:
CREATE VIEW DataPrices
AS
SELECT
p.id,
p.data,
SUM(t.price)
FROM products p
INNER JOIN secondtable t ON p.ID = t.ForeignKeyToTable1
GROUP BY p.Id, p.data;
select table1.id, table1.data, sum(table2.price) as `total`
from table1 inner join table2
on table1.id = table2.foreignkeyId
group by table1.id, table1.data
Related
I have two tables
2)- First Table where I store all product details like name, price, weight, etc.
1)- Second Table where I store updated price with product_id
I want to fetch an updated price with product_id from the second table but if the product_id is not available on the second table then the price will come from first table.
Left join and coalesce
select t1.pid,name, coalesce(t2.price,t1.price), weight
from t1
left join t2 on t1.pid = t2.pid;
Two tables are given. In one table we have following columns
productid
product_title
priority
categoryid
and in second table we have
id
productid
color
selling_price
stock
How to write a SQL query to select all product from table having more than 2 colors?
One way would be to use a sub-query to select product id from the stock table where product has multiple colors.
SELECT PRODUCTID FROM TABLE2 GROUP BY PRODUCTID HAVING COUNT(COLOR)>2
Use it in the main query
SELECT T1.PRODUCTID,T1.PRODUCT_TITLE FROM TABLE1 T1 WHERE PRODUCTID IN
(SELECT PRODUCTID FROM TABLE2 GROUP BY PRODUCTID HAVING COUNT(COLOR)>2)
I'm assuming the Database is not DB2 in which case this query won't work.
SELECT T1.productid, T1.product_title,COUNT(T2.COLOR) counter
FROM Table1 T1 INNER JOIN table2 T2 s ON T1.productid = T2.productid
GROUP BY T1.productid, T1.product_title
HAVING COUNT(T2.COLOR) > 2;
I have two tables customers and orders, below is the structure.
Table - contacts
id
Table - orders
id
contact_id
How can I select all from contacts table but only select the latest record from the orders table?
SELECT contacts.*,
Max(orders.id)
FROM contacts
LEFT JOIN orders
ON contacts.id = orders.contact_id
GROUP BY contacts.id;
But I always gets NULL if I use LEFT JOIN, it only have value if I use INNER JOIN.
select the latest record in orders and group it first
select contacts.*, orders.id
from contacts
left join (select max(id) as id, contact_id
from orders
group by contact_id) orders
on contacts.id = orders.contact_id
You can try to use UNION like
select * from orders order by id desc limit 1
UNION
select * from contacts
In order to aggregate max value with all columns from contacts table, add all columns from contacts table after group by function
I trust the answer provided by Alex should work well. The following query shall list all records from contacts and the last id from orders table.
SELECT
c.*,
(SELECT Max(o.id) FROM orders o
INNER JOIN contacts c1 ON o.id=c1.id
)as last_order_id
FROM contacts c
I have two tables 1.material_line_item and 2.item_master there is a foreign key relation between two tables (item_master_id present in material_line_item) and there is a column called item_code in item_master. So I want a Join Query to display the item_code with my current query.
I would approach this by joining the item_master table to a subquery of material_line_item which calculates the aggregates you want for each item master id value. I am selecting all columns available though you are free to choose whichever columns you want.
SELECT t1.*, t2.*
FROM item_master t1
INNER JOIN
(
SELECT item_master_id,
SUM(received_quantity) AS Total_Received_Qty,
SUM(ordered_quantity) AS Total_Ordered_Qty
FROM material_line_item
GROUP BY item_master_id
) t2
ON t1.id = t2.item_master_id
you can simply join the two tables like
select item.item_master_id, master.item_id, Sum(received_quantity),
sum(ordered_quantity) from material_line_item item
left join item_master master on item.item_master_id = master.id
group by item.item_master_id, master.item_id
Try this,
select a.item_master_id,a.Total_received_qty,a.Total_ordered_Qty,b.item_code from (select item_master_id,
sum(received_quantity) Total_received_qty,
sum(ordered_quantity) Total_ordered_Qty
from material_line_item
group by item_master_id) a, item_master b where a.item_master_id =b.item_master_id
Hope it will help.
I am struggling with a MySQL query which I cant get to work as I want.
In table1 I have co_id, name, code, product, logindate.
in table2 I have pr_id, productname, productno, price.
I want to count and group the PRODUCT from table1, so I can see how many that have picked for example product 1,2,3 etc.
But when I list the result on the page I will need productname, and productno for each id number in the GROUP search. table1.product is joined with table2.pr_id
This is what I have so far, but I think I am missing something with INNER JOIN or similar, right?
SELECT
codes.pickedgift,
products.productno,
products.productname,
COUNT(codes.pickedgift) as num
FROM
codes,
products
GROUP BY codes.pickedgift
ORDER BY codes.pickedgift
you missing the join condition, when you join 2 tables you should link primary key in table1 to its foreign key in another table, so your query can be:
SELECT
codes.pickedgift,
products.productno,
products.productname,
COUNT(codes.pickedgift) as num
FROM
codes INNER JOIN products ON codes.product = products.pr_id
GROUP BY codes.pickedgift
ORDER BY codes.pickedgift
You should use a sub-select for this query.
-- assuming I have your table structure correct.
SELECT p.productno, p.productname, num
FROM (SELECT codes.pickedgift, COUNT(codes.pickedgift) as num
FROM codes
GROUP BY codes.pickedgift) g
JOIN products p ON p.id = g.pickedgift
ORDER BY g.pickedgift
The other thing you have to make sure of is if you're using a group-by, the fields in your select must either be the fields in the group by, or aggregates. MySQL let's you include columns that are not part of the group-by / aggregate, it becomes ambiguous as to which value productno and productname should be represented, which is why I opted for a sub-select instead.