MySQL Want to SUM and Minus from two SQL statements - mysql

I want to SUM the first Statement(A) and Second Statement(B) Then I want to get the result of A-B. After that I get the output GROUP BY who
SELECT `who`, mpoints-spoints AS netpoints
FROM
(
SELECT `who`,`points`, SUM(points) AS mpoints FROM `game` WHERE (`datetime` BETWEEN '2015-12-1' AND '2015-12-31') AND (`cal`='1') AND (`result`='1' OR `result`='2') GROUP BY `who`
) t1,
(
SELECT `who`,`points`, SUM(points) AS spoints FROM `game` WHERE (`datetime` BETWEEN '2015-12-1' AND '2015-12-31') AND (`cal`='1') AND (`result`='3' OR `result`='4') GROUP BY `who`
) t2
ORDER BY netpoints DESC
GROUP BY `who`
I want the output like
whlie($rs=mysql_fetch_assoc($query)){
No. Who. Points
$i++ $rs['who'] $rs['netpoints']
}

The query could look like the following, but I have not tested it.
SELECT `who`, mpoints-spoints AS netpoints
FROM
(
SELECT `who`,`points`, SUM(points) AS mpoints, 0 as spoints FROM `game` WHERE (`datetime` BETWEEN '2015-12-1' AND '2015-12-31') AND (`cal`='1') AND (`result`='1' OR `result`='2') GROUP BY `who`
UNION ALL
SELECT `who`,`points`, 0 as mpoints, SUM(points) AS spoints FROM `game` WHERE (`datetime` BETWEEN '2015-12-1' AND '2015-12-31') AND (`cal`='1') AND (`result`='3' OR `result`='4') GROUP BY `who`
) A
GROUP BY `who`
ORDER BY netpoints;

Related

Use join for three tables

I have three tables and I would like sum over values by month from three tables.
The structure of database:
1)costs_1
-id
-total
-updated_at(timestamp)
2)costs_2
-id
-total_1
-updated_at_1(timestamp)
3)costs_3
-id
-total_2
-updated_at_2(timestamp)
Structure of result table: 1)total; 2)total_1; 3)total_2; 4)month
I have code that work for two tables, but don't know how to adapte it
SELECT t1.y year,
t1.m month,
COALESCE(t1.total, 0),
COALESCE(t2.total, 0)
FROM
(
SELECT YEAR(date_month) y,
MONTH(date_month) m,
SUM(total) total
FROM market_costs
GROUP BY YEAR(date_month), MONTH(date_month)
) t1
LEFT JOIN
(
SELECT YEAR(date_month) y,
MONTH(date_month) m,
SUM(total) total
FROM general_costs
GROUP BY YEAR(date_month), MONTH(date_month)
) t2 ON t1.y = t2.y and t1.m = t2.m
UNION
SELECT t2.y year,
t2.m month,
COALESCE(t1.total, 0),
COALESCE(t2.total, 0)
FROM
(
SELECT YEAR(date_month) y,
MONTH(date_month) m,
SUM(total) total
FROM market_costs
GROUP BY YEAR(date_month), MONTH(date_month)
) t1
RIGHT JOIN
(
SELECT YEAR(date_month) y,
MONTH(date_month) m,
SUM(total) total
FROM general_costs
GROUP BY YEAR(date_month), MONTH(date_month)
) t2 ON t1.y = t2.y and t1.m = t2.m
Combine the 3 tables using UNION ALL then group the data:
SELECT
YEAR(t1.date_month) y
, MONTH(t1.date_month) m
, SUM(t1.total) total
, SUM(t1.total_1) total_1
, SUM(t1.total_2) total_2
FROM (
SELECT
date_month
, total
, NULL as total_1
, NULL as total_2
FROM costs
UNION ALL
SELECT
date_month
, NULL as total
, total_1
, NULL as total_2
FROM costs_1
UNION ALL
SELECT
date_month
, NULL as total
, NULL as total_1
, total_2
FROM costs_2
) t1
GROUP BY
YEAR(t1.date_month)
, MONTH(t1.date_month)
added
SELECT
YEAR(t1.date_month) y
, MONTH(t1.date_month) m
, SUM(t1.total) total
, SUM(t1.total_1) total_1
, SUM(t1.total_2) total_2
FROM (
SELECT
date_month
, total
, NULL as total_1
, NULL as total_2
FROM costs
UNION ALL
SELECT
date_month
, NULL as total
, total as total_1 -- changed
, NULL as total_2
FROM costs_1
UNION ALL
SELECT
date_month
, NULL as total
, NULL as total_1
, total as total_2 -- changed
FROM costs_2
) t1
GROUP BY
YEAR(t1.date_month)
, MONTH(t1.date_month)

Why is MySQL cumulative sum producing wrong results

I need to find the cumulative sum for the following data:
Following query:
SELECT created, COUNT( * )
FROM `transactions`
GROUP BY created
Gives me:
created COUNT( * )
2015-8-09 1
2015-8-15 1
2015-8-16 2
2015-8-17 1
2015-8-23 1
I tried to do the cumulative sum like:
SELECT t1.created, COUNT( * ) , SUM( t2.totalcount ) AS sum
FROM transactions t1
INNER JOIN (
SELECT id, created c, COUNT( * ) AS totalcount
FROM transactions
GROUP BY created
ORDER BY created
)t2 ON t1.id >= t2.id
GROUP BY t1.created
ORDER BY t1.created
but the results it gives arent as expected:
created COUNT( * ) sum
2015-8-09 5 6
2015-8-15 3 4
2015-8-16 6 8
2015-8-17 1 1
2015-8-23 4 5
How do i produce the following result:
created COUNT( * ) sum
2015-8-09 1 1
2015-8-15 1 2
2015-8-16 2 4
2015-8-17 1 5
2015-8-23 1 6
select tmp.*, #sum := #sum + cnt as cum_sum
from
(
SELECT created, COUNT( * ) as cnt
FROM `transactions`
GROUP BY created
ORDER BY created
) tmp
cross join (select #sum := 0) s
Your inner query is selecting id without grouping on it. Let's rework it in terms of the date.
SELECT t1.created, COUNT( * ) AS daycount, SUM( t2.totalcount ) AS sum
FROM transactions t1
INNER JOIN ( SELECT created, COUNT( * ) AS totalcount
FROM transactions
GROUP BY created
) t2 ON t1.created >= t2.created
GROUP BY t1.created
ORDER BY t1.created;
Or you might want to put the totalcount inline:
SELECT t1.created, COUNT(*) AS daycount
, ( SELECT COUNT(*) FROM transactions t2
WHERE t2.created <= t1.created ) AS totalcount
FROM transactions t1
GROUP BY created
ORDER BY CREATED;

How to merge the result of this 2 queries in mysql server

I am actually stuck in merging the result of this two queries:
first query:
SELECT c.code, c.name, pc.sku, pc.cat_code, pp.title
FROM `cat_parent` cp, cat c, prod_cat pc, products pp
WHERE c.code = cp.cat_code
AND cp.cat_code = pc.cat_code
AND pp.sku = pc.sku
AND cp.parent_code = 01110
AND hide =0
The result I get is:
Second query:
SELECT `sku` , `update_date` , `description` , count( * ) AS total_sold
FROM `orderline`
WHERE `update_date` >= ( DATE_ADD(CURDATE( ) , INTERVAL -14 DAY ) )
AND `update_date` <= ( DATE_ADD(CURDATE( ) , INTERVAL -7 DAY ) )
GROUP BY left( sku, 7 )
ORDER BY total_sold DESC
The result:
The question I want to ask that how can I get the result by filtering the sku available in both tables.
Just bit confused on that part....any ideas will be appreciated.
This is only part of the data. there is heaps of data. Yes, I want to merge the both tables and want to find the common sku available in both tables.
My expected result will be sku, title, total sold.
Thanks, anyway I managed to get around to get the result.
My final query:
SELECT * FROM (
SELECT sku , update_date , description FROM orderline WHERE
update_date >= '2012-03-06' AND update_date <= '2012-03-07' )g
JOIN (
SELECT c.code, c.name, pc.sku, pc.cat_code FROM cat_parent cp, cat
c, prod_cat pc, products pp WHERE c.code = cp.cat_code AND cp.cat_code
= pc.cat_code AND pp.sku = pc.sku AND cp.parent_code =01110 AND hide =0 )p ON left( g.sku, 7 ) = left( p.sku, 7 )
Something like this -
SELECT
`c`.`code`, `c`.`name`, `pc`.`sku`, `pc`.`cat_code`, `pp.title`,
`ol`.`sku`, `ol`.`update_date`, `ol`.`description`, COUNT(*) AS `total_sold`
FROM `cat_parent` `cp`
INNER JOIN `cat` `c`
ON `c`.`code` = `cp`.`cat_code`
INNER JOIN `prod_cat` `pc`
ON `cp`.`cat_code` = `pc`.`cat_code`
INNER JOIN `products` `pp`
ON `pp`.`sku` = `pc`.`sku`
INNER JOIN `orderline` `ol`
ON LEFT(`pc`.`sku`, 7) = LEFT(`ol`.`sku`, 7)
WHERE `cp`.`parent_code` = 01110
AND `hide` = 0
AND `ol`.`update_date` >= ( DATE_ADD(CURDATE( ) , INTERVAL -14 DAY ) )
AND `ol`.`update_date` <= ( DATE_ADD(CURDATE( ) , INTERVAL -7 DAY ) )
GROUP BY left( `ol`.`sku`, 7 )
ORDER BY `total_sold` DESC

Subquery issue in mysql

I am trying to fetch the top 3 scores of unique players in the last n months. While I try to execute the query I am getting an error:
Unknown column 'f.SC_Date' in 'where clause'
Below is my query
SELECT * from scores f
WHERE
SC_Id IN (
SELECT SC_Id FROM (
SELECT SC_Id from scores where DATE_FORMAT( SC_Date, "%Y%m" ) = DATE_FORMAT( f.SC_Date, "%Y%m" ) AND US_Id != 0 ORDER BY SC_Score DESC
LIMIT 3
) AS u )ORDER BY DATE_FORMAT( SC_Date, "%Y%m" ) DESC, SC_Score DESC
You can't use f.SC_Date in the inner query like that - remember the bit in brackets will be run first, and then the bit outside will be run on the result.
You'd need to declare scores f inside that select statement for this to work.
I expect you don't need it at all though - something like:
SELECT * from scores f
WHERE
SC_Id IN (
SELECT SC_Id FROM (
SELECT SC_Id from scores where DATE_FORMAT( SC_Date, "%Y%m" ) = DATE_FORMAT( SC_Date, "%Y%m" ) AND US_Id != 0 ORDER BY SC_Score DESC
LIMIT 3
) AS u )ORDER BY DATE_FORMAT( SC_Date, "%Y%m" ) DESC, SC_Score DESC
Should work, unless I'm mistaken as to your intentions here.

MAX (SUM(votes)) | get only winners

I want to get only winners in mysql table.
SELECT mayor_id, local_unit_id, Value FROM
(SELECT mayor_id, local_unit_id, SUM( `votes` ) AS 'Value'
FROM mayorresults
GROUP BY `mayor_id`) AS t1
ORDER BY `t1`.`local_unit_id` ASC
Idea is to Sum votes first then get only largest number, in this case the winner.
With this query I can get all, but not just the winners.
I want MAX(SUM(votes)) to get, but It doesn't work like this.
EDIT: I want to get winners for each localunit
eg.
local_unit_id mayor_id votes
1 25 8562
2 534 18562
Update, after the explanations:
SELECT grp.local_unit_id, grp.mayor_id, grp.Value
FROM
( SELECT local_unit_id, mayor_id, SUM( votes ) AS Value
FROM mayorresults
GROUP BY local_unit_id, mayor_id
) AS grp
JOIN
( SELECT local_unit_id, MAX(Value) AS Value
FROM
( SELECT local_unit_id, mayor_id, SUM( votes ) AS Value
FROM mayorresults
GROUP BY local_unit_id, mayor_id
) AS grp
GROUP BY local_unit_id
) AS grp2
ON grp2.local_unit_id = grp.local_unit_id
AND grp2.Value = grp.Value
ORDER BY local_unit ASC
Have you tried:
SELECT mayor_id, local_unit_id, MAX(Value) FROM
(SELECT mayor_id, local_unit_id, SUM( `votes` ) AS 'Value'
FROM mayorresults
GROUP BY `mayor_id`) AS t1
ORDER BY `t1`.`local_unit_id` ASC
You can't have the max value of a sum. You can have the max sum of a subquery.