I have this table:
id item_name_1 quantity 1 item_name_2 quantity_2 item_name_3 quantity_3
-----------------------------------------------------------------------------------
1 Apple 2 Pear 3 Orange 5
2 Pear 1 Apple 4
3 Orange 6
4 Apple 1 Pear 2 Orange 3
I want this result:
item total
--------------
Apple 7
Pear 6
Orange 14
I tried this:
SELECT
(SELECT item_name_1, SUM(quantity_1) AS count FROM table1
GROUP BY item_name_1) AS item,
(SELECT item_name_2, SUM(quantity_2) AS count FROM table1
GROUP BY item_name_2) AS item,
(SELECT item_name_3, SUM(quantity_3) AS count FROM table1
GROUP BY item_name_3) AS item,
SUM(count) AS total
FROM table1
GROUP BY item;
Error Code: 1241. Operand should contain 1 column(s)
Any suggestions?
You're selecting two columns in your sub-queries when there can only be one column.
Something like this will get you what you are looking for:
SELECT item_name, SUM(quantity) AS quantity
FROM
(SELECT item_name_1 AS item_name, SUM(quantity_1) AS quantity
FROM table1
GROUP BY item_name_1
UNION ALL
SELECT item_name_2 AS item_name, SUM(quantity_2) AS quantity
FROM table1
GROUP BY item_name_2
UNION ALL
SELECT item_name_3 AS item_name, SUM(quantity_3) AS quantity
FROM table1
GROUP BY item_name_3) AS ABB1
GROUP BY item_name;
I would try it like this.
select t.item_name_1, s1+s2+s3
from
(
select * From
(
select sum(quantity_1) as s1, item_name_1
from
table1
group by item_name_1
) t1
full outer join
(
select sum(quantity_2) as s2, item_name_2
from
table1
group by item_name_2
) t2 on t1.item_name_1 = t2.item_name_2
full outer join
(
select sum(quantity_3) as s3, item_name_3
from
table1
group by item_name_3
) t3 on t2.item_name_2 = t3.item_name_3
) t
Related
Select idn, sum(tblppmp.total_item) as a_total
sum(tblRequest.Quantity) as b_total
sum(a_total- b_total) as itemsleft
FROM PPMP.dbo.tblppmp, ppmp.dbo.tblrequest
Group by idn
i have my problem how to sum individual items from table1 and table2 and the result will be subtracted to get the answer.
like this
table1
id item
1 2
2 3
3 4
table2
id item
1 1
2 2
3 3
the result i want is like this..
table 3
sum(table.item) - sum(table2.item)
table1.id 1 = 2
table2.id 1 = 1
so (2-1) = 1
id item_left
1 1
2 1
3 1
id item
You could caculate sum for each table first and then subtract them.
select idn,
a_total - ISNULL(r_total,0) as itemsleft
FROM
(
select idn, sum(p.total_item) as p_total
from PPMP.dbo.tblppmp p
group by idn
) p
LEFT JOIN
(
select idn, sum(r.Quantity) as r_total
from ppmp.dbo.tblrequest r
group by idn
) r on p.idn = r.idn
You can not use sum(a_total- b_total). You have to use sum(tblppmp.total_item) - sum(tblRequest.Quantity).
Select tblppmp.idn
, tblppmp.total_item as a_total
,tblRequest.Quantity as b_total
,tblppmp.total_item - tblRequest.Quantity as itemsleft
FROM dbo.tblppmp
INNER JOIN
(SELECT
tblrequest.idn
,sum(tblRequest.Quantity) AS Quantity
FROM tblrequest
WHERE tblrequest.dr_year = 2015
GROUP BY tblrequest.idn) tblrequest ON tblppmp.idn = tblrequest.idn
I have 2 tables with information: ID, persona_id, total_amount
The persona ID can repeat dozen of times. So i get all the one persons id total_amount with query:
select d.id as debt_id, p.name as persona, sum(d.total_amount) as total_amount
from debt d
join persons p on d.persona_id = p.id group by p.name
I want to get data from each table in one query and do aritmethic propertys with the total_amount column and return it as 1 tabel.
TABLE 1
id persons_id total_amount
1 2 50
2 3 100
3 2 200
4 5 300
5 1 500
TABLE 2
id persons_id total_amount
1 2 25
2 1 100
3 5 50
4 3 100
5 4 300
As a result i want to get the 2 tables comined with arithmetic operation (-, +, / , * ) of Total amount columns.Basicaly a change to get the ending result total amount in form i want for different cases.
What worked for me based on JohnHC answear was :
select c.id, c.persona_id, c.total_amount - d.total_amount as new_total
from ( select c.id , c.persona_id, sum(c.total_amount) as total_amount from credit c
join persons p on c.persona_id = p.id
group by p.name) c
inner join ( select d.id, d.persona_id, sum(d.total_amount) as total_amount from debt d
join persons p on d.persona_id = p.id
group by p.name) d
on c.persona_id = d.persona_id
group by c.id, c.persona_id
If you want the total, try:
select id, person_id, sum(total_amount)
from
(
select id, person_id, total_amount
from table1
union all
select id, person_id, total_amount
from table2
)
group by id, person_id
If you want to do other things, try:
select t1.id, t1.person_id, t1.total_amount [+ - / *] t2.total_amount as new_total
from table1 t1
inner join table2 t2
on t1.id = t2.person_id
group by t1.id, t1.person_id
I'm running a query that selects details of orders, and I want to see only the orders that have gone through multiple stages. My data looks like:
id | order_id | action
1 100 1
2 100 2
3 100 4
4 101 1
5 102 2
6 103 1
7 103 2
So that only the rows for order_id 100 and 103 will be selected. This needs to be nested in a larger query.
You can use a subquery to get the orders that had multiple stages:
SELECT order_id
FROM your_table
GROUP BY order_id
HAVING COUNT(*)>1
then you can join this result back to your table:
SELECT o.*
FROM yourtable AS o INNER JOIN (
SELECT order_id
FROM your_table
GROUP BY order_id
HAVING COUNT(*)>1
) dup ON o.order_id = dup.order_id
Use group by with count and having
select *,count(order_id) as total from table
group by order_id
having total > 1
you can try this query:
select * from your_table
where ( select count(*) from your_table internal_table
where your_table .order_id = internal_table.order_id
) > 1
I have a table in hive that looks something like this
cust_id prod_id timestamp
1 11 2011-01-01 03:30:23
2 22 2011-01-01 03:34:53
1 22 2011-01-01 04:21:03
2 33 2011-01-01 04:44:09
3 33 2011-01-01 04:54:49
so on and so forth.
For each record I want to check that how many unique products has this customer bought within the last 24 hrs excluding the current transaction. So the output should look something like this -
1 0
2 0
1 1
2 1
3 0
My hive query looks something like this
select * from(
select t1.cust_id, count(distinct t1.prod_id) as freq from temp_table t1
left outer join temp_table t2 on (t1.cust_id=t2.cust_id)
where t1.timestamp>=t2.timestamp
and unix_timestamp(t1.timestamp)-unix_timestamp(t2.timestamp) < 24*60*60
group by t1.cust_id
union all
select t.cust_id, 0 as freq from temp_table t2
)unioned;
Just get all the rows for last 24 hours do a group by on custid and count(distinct productid) -1 as the output. Overall query would look something like this.
select cust_id, COUNT(distinct prod_id) - 1 from table_name where
unix_timestamp(t1.timestamp)-unix_timestamp(t2.timestamp) < 24*60*60
GROUP BY cust_id
*I am subtracting 1 here to exclude the latest transactionid of the user. (hope this is what you meant)
You can join to a derived table that contains the distinct # of products purchased in the past 24 hours for each customer/timestamp pair.
select t1.cust_id, t1.prod_id, t1.timestamp, t2.count_distinct_prod_id - 1
from mytable t1
join (
select t2.cust_id, t2.timestamp, count(distinct t3.prod_id) count_distinct_prod_id
from mytable t2
join mytable t3 on t3.cust_id = t2.cust_id
where unix_timestamp(t2.timestamp) - unix_timestamp(t3.timestamp) < 24*60*60
group by t2.cust_id, t2.timestamp
) t2 on t1.cust_id = t2.cust_id and t1.timestamp = t2.timestamp
for example: I have 4 tables:
table 1 : item
item_id name quantity
==========================
1 a 10
2 b 15
table 2 : additional
add_id name quantity item_id
===================================
1 c 5 1
table 3 : item_out (this will subtract item quantity)
item_id quantity
==========================
1 10
2 5
table 4 : additional_out (this will subtract additional quantity)
additional_id quantity
==========================
1 5
My question is: what mysql-query should I give in order to get these result?
item_ id quantity additional_id additinal_quantity
=================================================
1 5 1 0
2 15
because I use mysql query code like:
SELECT item_id, SUM(quantity - IFNULL(number,0)), additional_id, SUM(additional_quantity - IFNULL(additional_number,0))
FROM(
SELECT a.item_id, a.quantity, b.additional_id, b.quantity AS additional_quantity, NULL AS number, NULL AS additional_number
FROM table_1 a JOIN table_2 b ON some condition
UNION
SELECT NULL AS item_id , NULL AS quantity,... a.quantity AS number, b.quantity AS additional_number
FROM table_3 a JOIN table_4 b ON condition
)AS T
and it ends with only 1 row because of SUM in the first selection.
Try this:
SELECT item_ id,
(item.quantity - IFNULL(item_out.quantity,0)) AS quantity,
additional_id,
(IFNULL(a.quantity,0) - IFNULL(ao.quantity,0)) AS additional_quantity
FROM item
LEFT JOIN item_out
ON item.item_id = item_out.item_id
LEFT JOIN additional a
ON item.item_id = a.item_id
LEFT JOIN additional_out ao
ON a.addid = ao.additional_id