sum of elements in mysql - mysql

We have a mysql table which has following schema
item varchar(20)
count int
We have a chart which classifies the items into categories (item1 and item2 belongs to cat1 and item3, item4 belongs to cat4, but that is not in a table and we cannot create a table.
I want the count of items present in the cat1 and cat2. Can this be done without joining with any table and with inner query ?

SELECT 'Cat1', sum(table1.count) FROM Table1 WHERE item IN ('Item1','Item2') UNION SELECT 'Cat2', sum(table1.count) FROM Table1 WHERE item IN ('Item3','Item4');

You can do this:
SELECT
c.categoryname,
SUM(IFNULL(i.`count`,0))
FROM
(
SELECT 'item1' itemname, 'cat1' CategoryName
UNION ALL
SELECT 'item2' , 'cat1'
UNION ALL
SELECT 'item3' , 'cat2'
UNION ALL
...
) c
LEFT JOIN items i ON c.itemname = i.item
GROUP BY c.categoryname
SQL Fiddle Demo

Related

How to retrieve all members from a transitive relationship through one SQL query [duplicate]

I have a table with following structure
Table name: matches
That basically stores which product is matching which product. I need to process this table
And store in a groups table like below.
Table Name: groups
group_ID stores the MIN Product_ID of the Product_IDS that form a group. To give an example let's say
If A is matching B and B is Matching C then three rows should go to group table in format (A, A), (A, B), (A, C)
I have tried looking into co-related subqueries and CTE, but not getting this to implement.
I need to do this all in SQL.
Thanks for the help .
Try this:
;WITH CTE
AS
(
SELECT DISTINCT
M1.Product_ID Group_ID,
M1.Product_ID
FROM matches M1
LEFT JOIN matches M2
ON M1.Product_Id = M2.matching_Product_Id
WHERE M2.matching_Product_Id IS NULL
UNION ALL
SELECT
C.Group_ID,
M.matching_Product_Id
FROM CTE C
JOIN matches M
ON C.Product_ID = M.Product_ID
)
SELECT * FROM CTE ORDER BY Group_ID
You can use OPTION(MAXRECURSION n) to control recursion depth.
SQL FIDDLE DEMO
Something like this (not tested)
with match_groups as (
select product_id,
matching_product_id,
product_id as group_id
from matches
where product_id not in (select matching_product_id from matches)
union all
select m.product_id, m.matching_product_id, p.group_id
from matches m
join match_groups p on m.product_id = p.matching_product_id
)
select group_id, product_id
from match_groups
order by group_id;
Sample of the Recursive Level:
DECLARE #VALUE_CODE AS VARCHAR(5);
--SET #VALUE_CODE = 'A' -- Specify a level
WITH ViewValue AS
(
SELECT ValueCode
, ValueDesc
, PrecedingValueCode
FROM ValuesTable
WHERE PrecedingValueCode IS NULL
UNION ALL
SELECT A.ValueCode
, A.ValueDesc
, A.PrecedingValueCode
FROM ValuesTable A
INNER JOIN ViewValue V ON
V.ValueCode = A.PrecedingValueCode
)
SELECT ValueCode, ValueDesc, PrecedingValueCode
FROM ViewValue
--WHERE PrecedingValueCode = #VALUE_CODE -- Specific level
--WHERE PrecedingValueCode IS NULL -- Root

MySQL error with union

What is wrong with the following sql query
select itemname from Item where itemid
in
((select ItemID
FROM Delivery NATURAL JOIN Supplier
WHERE SupplierName = 'Nepalese Corp.')
union
(select ItemID
FROM Sale NATURAL JOIN Department
WHERE DepartmentName = 'Navigation'))
I have seen another post on this site that recommends removing the inner parentheses on the two union sets and giving the first union set an alias. I tried it, and MYSQL shows an x at the line shown below, however, the query runs fine. My question is what happened?
select itemname from Item where itemid
in
(select ItemID as id
FROM Delivery NATURAL JOIN Supplier
WHERE SupplierName = 'Nepalese Corp.'
union
select ItemID . //shows an x at this line
FROM Sale NATURAL JOIN Department
WHERE DepartmentName = 'Navigation')
select itemname from Item
where itemid in
(
select ItemID
FROM Delivery
NATURAL JOIN Supplier WHERE SupplierName = 'Nepalese Corp.'
union
select ItemID
FROM Sale
NATURAL JOIN Department WHERE DepartmentName = 'Navigation'
)
You can try above code.
Simply remove unnecessary brackets will resolve you issue.
You can try join the inner result to main table
select itemname from Item as t1 join
(select distinct ItemID
FROM Delivery NATURAL JOIN Supplier
WHERE SupplierName = 'Nepalese Corp.'
union
select distinct ItemID
FROM Sale NATURAL JOIN Department
WHERE DepartmentName = 'Navigation') as t2 on t1.ItemID=t2.ItemID

Select Statement To Retrieve Same Column Twice

I Have 2 tables:
Table 1 named category_desription, it contains 2 fields: category_name and category_id.
Table 2 named category includes the category_id and parent_Category_id fields which are foreign keys from category_id in the first table.
I want to select 4 fields whereby the result will contain category_id and its name category_name and parent_category_id and its name as well which happens to be category_name as well
I couldn't figure out how to do it so far so I wish if someone assist me in this.
I hope this will solve your prblm
SELECT category_id,
(Select category_name from table1 where category_id= table2.category_id) as category_name ,
parent_category_id,
(Select category_name from table1 where category_id= table2.parent_category_id)
as parent_category_name from table2
with category_desc as
(select 1 as category_id, 'Category 1' as category_name
from dual
union all
select 2 as category_id, 'Category 2' as category_name from dual),
category as
(select 1 as category_id, 2 as parent_category_id from dual)
select c.category_id,
c_d.category_name,
c.parent_category_id,
pc_d.category_name parent_category_name
from category c
join category_desc c_d
on c_d.category_id = c.category_id
join category_desc pc_d
on pc_d.category_id = c.parent_category_id
use category_desc with different alias to find parent_category details
This is a simple name conflict issue in SQL, which can be solved easily by using alias
e.g.
Select t1.category_id, t1.category_name, t2.category_id, t2.category_name
from table1 t1 join table2 t2 on <some condition..>
You can also give different names to the result column
Select t1.category_id, t1.category_name as 'Parent Category Name',
t2.category_id, t2.category_name as 'Category Name'
from table1 t1 join table2 t2 on <some condition..>
I hope it solves your issue.

mysql rows where count(x) > 1 and count(y) > 0

I have a sales table containing invoice number, product code, product category, qty, etc.
Using this table how would you go about finding invoices that contain at least one product from say category A AND 1 product from category B ?
Okay, here's what I believe is what you'll want. Happy to see any simpler suggestions if anyone has them:
Select tableA.invoiceNumber
from
(select * from myTable
where productCategory = 'A') tableA
inner join
(select * from myTable
where productCategory = 'B') tableB
on tableA.invoiceNumber = tableB.invoiceNumber
Here's a SQLFiddle:
http://sqlfiddle.com/#!9/03ddd/3
It's basically joining your query onto itself where there's a condition for each table on category.
Try this :
select * from mytable
inner join(
select invoiceNumber
from mytable
where mytable.productcategory = 'A'
) as a using (invoiceNumber)
inner join(
select invoiceNumber
from mytable
where mytable.productcategory = 'B') as b
using (invoiceNumber)
group by invoicenumber

How to use select statement within select statements if condition

What I am trying to do:
I want to get the name of the product with it's id from orders table , but in my case I have stored products in 3 different tables.
i.e dish,drinks,others.
Problem:
So the problem is in orders table i have put a type(what kind of product it is) like "dirnks,dish,others".So how can i get the name of the product when product can be in any tables(dish,drinks,others)
What i want:
I don't whether it's possible or not but if anyone can tell what below query is there,how can i use select statement in IF statment
SELECT product_id,type,quantity ,
( IF(type='Drinks)
THEN
(SELECT drink_name FROM drinks WHERE drink_id=product_id)
ELSE IF (type='Dish)
THEN
(SELECT dish_name FROM dish WHERE dish_id=product_id)
)
as drink_name
FROM order_product
WHERE shop_id='JSMMSK730'
Is this thing possible or not?
Use a UNION, and add a new column to the subquery that indicates which table the rows came from. Then you can use this in the JOIN condition.
SELECT product_id, o.type, quantity, name FROM
order_product o JOIN
(
SELECT "dish" type, dish_id id, drink_name name from dish
UNION
SELECT "drinks" type, drink_id id, drink_name name from drinks
UNION
SELECT "others" type, other_id id, other_name name from others
) p
ON p.id = o.product_id and p.type = o.type
WHERE shop_id = 'JSMMSK730'
Try below Syntax:
SELECT * FROM
(
SELECT product_id, product_name from dish
UNION
SELECT product_id, product_name from drinks
UNION
SELECT product_id, product_name from others) as product, order
)
WHERE product.product_id = order.order_id