SQL query to select produts using join on specific problem - mysql

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;

Related

Easy query on 2 tables in SQL

I have a table, TABLE1, with two columns: SSN and Date.
I have another table, TABLE2, with SSN, name and Surname.
I want to view the Name and the Surname of the person who is associated to the SSN that appears the highest number of times in the first table (the one with SSN and Date)
Which query should I write?
The fastest way is probably to aggregation before joining:
select *
from (select t1.ssn
from table1 t1
group by t1.ssn
order by count(*) desc
limit 1
) t1 left join
table2 t2
using (ssn)
Try this.
SELECT t1.ssn, t2.name, t2.surname, count(*) as vol
FROM table1 t1
LEFT JOIN table2 t1 on t1.ssn = t2.ssn
GROUP BY 1
ORDER BY count(*) DESC
LIMIT 1
Use a subquery in the WHERE clause which returns the SSN that appears the most in table1:
SELECT *
FROM table2
WHERE SSN = (SELECT SSN FROM table1 GROUP BY SSN ORDER BY COUNT(*) DESC LIMIT 1)
With an index on SSN (which I assume exists) this is the fastest way to get the result.

Joining 2 tables and adding price to give total revenue in MySQL

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

mysql error: use result of innerjoin into new query

I have 2 sql string I want to combine:
The first table has all data of a product
The second table (inner join) has all data of products that has a group price.
But I get different result
test search 'sku'
SELECT sku
FROM `c3eiwitlive`.`catalog_product_entity`
WHERE (
`entity_id` LIKE '%17086%'
)
Direct sql on the group_price table
result 269 records
SELECT SKU, value
FROM `c3eiwitlive`.`catalog_product_entity_group_price` AS T1
INNER JOIN
`c3eiwitlive`.`catalog_product_entity` AS T2
ON T1.`entity_id` = T2.`entity_id`;
Combined sql returns less records
result 234 records
SELECT name
FROM `c3eiwitlive`.`catalog_product_flat_1` AS T1
WHERE T1.`sku`
IN (
SELECT sku
FROM `c3eiwitlive`.`catalog_product_entity_group_price` AS T1
INNER JOIN `c3eiwitlive`.`catalog_product_entity` AS T2 ON T1.`entity_id` = T2.`entity_id`
)
It look to me like there are some sku entries missing from the 'catalog_product_flat_1' table.
Why not invert the query to identify the missing rows?
SELECT sku
FROM `c3eiwitlive`.`catalog_product_entity_group_price` AS T1
INNER JOIN `c3eiwitlive`.`catalog_product_entity` AS T2 ON T1.`entity_id` = T2.`entity_id`
WHERE sku
NOT IN (
SELECT sku from `c3eiwitlive`.`catalog_product_flat_1`
)

sql sum for two tables

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

Mysql can't join two tables columns into one show

I want to show two columns summarize data.
table1 - count all fields that the id same as the id on the show_users table.
table2 - sum all values that the id same as the id on the show_users table.
This is my query:
SELECT show_users.id, COUNT(`table1`.id) as sum_fields , SUM(`table2`.count) as count_all
FROM `show_users`
LEFT JOIN `table1` ON `show_users`.id = `table1`.id
LEFT JOIN `table2` ON `show_users`.id = `table2`.id
GROUP by show_users.id
ORDER BY sum_fields DESC
The table2 results are fine, but the table1 count isn't correct values...
Why is that?
SELECT show_users.id, COUNT(DISTINCT `table1`.id) as sum_fields , SUM(`table2`.count) as count_all