Group values from different columns and Count - mysql

I have this table:
UNIQUE_ID | WINNER_ID | FINALIST_ID
________1 | ________1 | __________2
________2 | ________1 | __________3
________3 | ________3 | __________1
________4 | ________1 | __________2
And I need a list of all players (Winners and Finalists) and a COUNT of how many times they have got 1st or 2nd place.
In this case it would be:
PLAYER_ID | WINNER_TIMES | FINALIST_TIMES
________1 | ___________3 | _____________1
________2 | ___________0 | _____________2
________3 | ___________1 | _____________1
A similar question was already asked here (LINK), but I didn't understand the answer.

select coalesce(winner_id, finalist_id) as PLAYER_ID
, count(winner_id) as WINNER_TIMES
, count(finalist_id) as FINALIST_TIMES
from (
select winner_id
, null as finalist_id
from YourTable
union all
select null
, finalist_id
from YourTable
) as SubQueryAlias
group by
coalesce(winner_id, finalist_id)
Live example at SQL Fiddle.

Try this ::
Select
user_id as user,
winner_temp.count(1) as winning_count
finalist_temp.count(1) as runner_up_count
from
user_table
left join
(Select winner_id, count(1) from table group by winner_id) as winner_temp on (user_table.user_id = winner_temp.winner_id)
left join
(Select finalist_id, count(1) from table group by finalist_id) as finalist_temp on
(user_table.user_id = finalist_temp.finalist_id)

Related

MySQL: Get most recent record satisfying certain conditions

I was inspired by this post. But what I'm going to solve is more complex.
In the table below we have three columns, id,rating,created, call it test_table,
+----+--------+----------------------+
| id | rating | created |
+----+--------+----------------------+
| 1 | NULL | 2011-12-14 09:25:21 |
| 1 | 2 | 2011-12-14 09:26:21 |
| 1 | 1 | 2011-12-14 09:27:21 |
| 2 | NULL | 2011-12-14 09:25:21 |
| 2 | 2 | 2011-12-14 09:26:21 |
| 2 | 3 | 2011-12-14 09:27:21 |
| 2 | NULL | 2011-12-14 09:28:21 |
| 3 | NULL | 2011-12-14 09:25:21 |
| 3 | NULL | 2011-12-14 09:26:21 |
| 3 | NULL | 2011-12-14 09:27:21 |
| 3 | NULL | 2011-12-14 09:28:21 |
+----+--------+----------------------+
I want to write a query which selects the most recent rating but not null for every id. If all of the ratings are null for a specific id, we select the most recent rating. The desired result is as follows:
+----+--------+----------------------+
| id | rating | created |
+----+--------+----------------------+
| 1 | 1 | 2011-12-14 09:27:21 |
| 2 | 3 | 2011-12-14 09:27:21 |
| 3 | NULL | 2011-12-14 09:28:21 |
+----+--------+----------------------+
The following gets the creation date:
select t.id,
coalesce(max(case when rating is not null then creation_date end),
creation_date
) as creation_date
from t
group by t.id;
You can then do this as:
select t.*
from t
where (id, creation_date) in (select t.id,
coalesce(max(case when rating is not null then creation_date end),
creation_date
) as creation_date
from t
group by t.id
);
One possible answer is this. Create a list of max(create) date per id and id having all NULL rating.
select t1.*
from myTable t1
join (
select id, max(created) as created
from myTable
where rating is not NULL
group by id
UNION ALL
select id, max(created) as created
from myTable t3
where rating is NULL
group by id
having count(*) = (select count(*) from myTable t4 where t4.id=t3.id)
) t2
where t1.id=t2.id
and t1.created=t2.created
order by t1.id;
select a.* from #test a join (select id, max(created) created
from #test
where rating is not null
group by id )b on a.id=b.id and a.created=b.created
union
select a.* from #test a join
(select id, max(created) created
from #test
where rating is null
and id not in
(select id from (select id, max(created) created
from #test
where rating is not null
group by id )d
group by id)
group by id )b on a.id=b.id and a.created=b.created
This query should work:
select a.id, a.rating, b.m from test_table a
join (
select id, max(created) as m from test_table
where rating is not null
group by id
) b on b.id = a.id and b.m = a.created
union
select a.id, a.rating, b.m from test_table a
join(
select id, max(created) as m from test_table a
where not exists
(select 1 from test_table b where a.id = b.id and b.rating is not null)
group by id
)b on b.id = a.id and b.m = a.created
You can get the created value in a correlated LIMIT 1 subquery:
select t.id, (
select created
from mytable t1
where t1.id = t.id
order by rating is null asc, created desc
limit 1
) as created
from (select distinct id from mytable) t
If you also need the rating column, you will need to join the result with the table again:
select t.*
from (
select t.id, (
select created
from mytable t1
where t1.id = t.id
order by rating is null asc, created desc
limit 1
) as created
from (select distinct id from mytable) t
) x
natural join mytable t
Demo: http://sqlfiddle.com/#!9/49e68c/8

Return rows that have one value but not another

For every same date (this is just one section of the table), I want to return the account numbers that made a purchase of A but DID NOT purchase B, and another query for vice-versa. So running the first query for A but no B should return 2 and 5. Running the vice-versa query for B but no A should give me 4. Thanks for the help. I'm assuming I would have to do a join of some sorts on the table but I'm stuck.
+----+----------------+---------------+----------+--+--+
| ID | Account Number | Purchase Type | Date | | |
+----+----------------+---------------+----------+--+--+
| 1 | 1 | A | 20140301 | | |
| 1 | 1 | A | 20140301 | | |
| 1 | 1 | B | 20140301 | | |
| 2 | 2 | A | 20140301 | | |
| 3 | 3 | A | 20140301 | | |
| 3 | 3 | B | 20140301 | | |
| 4 | 4 | B | 20140301 | | |
| 5 | 5 | A | 20140301 | | |
| 5 | 5 | A | 20140301 | | |
+----+----------------+---------------+----------+--+--+
Not sure if it is necessarily the best approach, but an inner select will work:
select distinct account_number
from purchases p
where purchase_type = "A" and account_number not in
(
select account_number
from purchases
where purchase_date = p.purchase_date and purchase_type = "B"
)
You first collect all ids that have purchase type "B" and then all ids with purchase type "A" that are not in the first collection.
(Assuming your table is purchases, ID is id int, Purchase Date is purchase_date char(1) and Date is purchase_date char(8), but you should be able to adapt the query to your actual columns.
Corresponding fiddle: http://sqlfiddle.com/#!9/edf73f/7/0)
One approach would be to use a full outer join where one or the other side is null; but mySQL doesn't support them. So to simulate: use a left join and then a union (or union all if you want to keep the fact that 1,1,A exists twice.) and we simply switch the criteria between the joins for the second SQL to union to handle both ways.
DEMO using SQL fiddle in comment: http://sqlfiddle.com/#!9/52c893/20/0
SELECT A.*
FROM purch A
LEFT JOIN purch B
on A.`Account Number` = B.`Account Number`
AND B.`Purchase Type` = 'B'
WHERE b.`Account Number` is null
AND A.`Purchase Type` = 'A'
UNION ALL
SELECT A.*
FROM purch A
LEFT JOIN purch B
on A.`Account Number` = B.`Account Number`
AND B.`Purchase Type` = 'A'
WHERE b.`Account Number` is null
AND A.`Purchase Type` = 'B'
You can use Exists on the same table:
select distinct AccountNumber , Date
from table1 outer_table
where PurchaseType = 'A' and not exists
(
select ID
from table1 inner_table
where
PurchaseType = 'B'
and inner_table.Date = outer_table.Date
and inner_table.AccountNumber = outer_table.AccountNumber
)
Fiddle: http://sqlfiddle.com/#!9/b84ecd/9
select id,sum(if(purchase_type='A',1,0)) as sumA,sum(if(purchase_type='B',1,0)) as sumB
from purchases
group by id
having sumA>0 and sumB=0
Fiddle: http://sqlfiddle.com/#!9/edf73f/16
and to get the two request in one:
select id,sum(if(purchase_type='A',1,0)) as sumA,sum(if(purchase_type='B',1,0)) as sumB
from purchases
group by id
having (sumA>0 and sumB=0) OR(sumA=0 and sumB>0 )
Fiddle: http://sqlfiddle.com/#!9/edf73f/18
You get a bit messed up here because MySQL's set arithmetic operations are incomplete. It has UNION and INTERSECT but not EXCEPT. If it had EXCEPT you could do
SELECT DISTINCT `Account Number` FROM purch WHERE `Purchase Type` = 'A'
EXCEPT /* MySQL fail! */
SELECT DISTINCT `Account Number` FROM purch WHERE `Purchase Type` = 'B'
and your problem would be solved.
So you can use the LEFT JOIN ... IS NULL query pattern. It's more verbose but works fine. (http://sqlfiddle.com/#!9/52c893/18/0)
SELECT suba.`Account Number`
FROM (
SELECT DISTINCT `Account Number`
FROM purch
WHERE `Purchase Type` = 'A'
) suba
LEFT JOIN (
SELECT DISTINCT `Account Number`
FROM purch
WHERE `Purchase Type` = 'B'
) subb ON suba.`Account Number` = subb.`Account Number`
WHERE subb.`Account Number` IS NULL
SELECT GROUP_CONCAT( DISTINCT A.account SEPARATOR ', ') AS "accounts"
FROM test A, test B
WHERE A.type='A'
AND A.id=B.id
AND A.date=B.date
AND A.date='2014-03-01'

Get second highest values from a table

I have a table like this:
+----+---------+------------+
| id | conn_id | read_date |
+----+---------+------------+
| 1 | 1 | 2010-02-21 |
| 2 | 1 | 2011-02-21 |
| 3 | 2 | 2011-02-21 |
| 4 | 2 | 2013-02-21 |
| 5 | 2 | 2014-02-21 |
+----+---------+------------+
I want the second highest read_date for particular 'conn_id's i.e. I want a group by on conn_id. Please help me figure this out.
Here's a solution for a particular conn_id :
select max (read_date) from my_table
where conn_id=1
and read_date<(
select max (read_date) from my_table
where conn_id=1
)
If you want to get it for all conn_id using group by, do this:
select t.conn_id, (select max(i.read_date) from my_table i
where i.conn_id=t.conn_id and i.read_date<max(t.read_date))
from my_table t group by conn_id;
Following answer should work in MSSQL :
select id,conn_id,read_date from (
select *,ROW_NUMBER() over(Partition by conn_id order by read_date desc) as RN
from my_table
)
where RN =2
There is an intresting article on use of rank functions in MySQL here : ROW_NUMBER() in MySQL
If your table design as ID - date matching (ie a big id always a big date), you can group by id, otherwise do the following:
$sql_max = '(select conn_id, max(read_date) max_date from tab group by 1) as tab_max';
$sql_max2 = "(select tab.conn_id,max(tab.read_date) max_date2 from tab, $sql_max
where tab.conn_id = tab_max.conn_id and tab.read_date < tab_max.max_date
group by 1) as tab_max2";
$sql = "select tab.* from tab, $sql_max2
where tab.conn_id = tab_max2.conn_id and tab.read_date = tab_max2.max_date2";

mysql subquery not producing all results

I have two tables: contacts and client_profiles. A contact has many client_profiles, where client_profiles has foreign key contact_id:
contacts:
mysql> SELECT id,first_name, last_name FROM contacts;
+----+-------------+-----------+
| id | first_name | last_name |
+----+-------------+-----------+
| 10 | THERESA | CAMPBELL |
| 11 | donato | vig |
| 12 | fdgfdgf | gfdgfd |
| 13 | some random | contact |
+----+-------------+-----------+
4 rows in set (0.00 sec)
client_profiles:
mysql> SELECT id, contact_id, created_at FROM client_profiles;
+----+------------+---------------------+
| id | contact_id | created_at |
+----+------------+---------------------+
| 6 | 10 | 2014-10-09 17:17:43 |
| 7 | 10 | 2014-10-10 11:38:01 |
| 8 | 10 | 2014-10-10 12:20:41 |
| 9 | 10 | 2014-10-10 12:24:19 |
| 11 | 12 | 2014-10-10 12:35:32 |
+----+------------+---------------------+
I want to get the latest client_profiles for each contact. That means There should be two results. I want to use subqueries to achieve this. This is the subquery I came up with:
SELECT `client_profiles`.*
FROM `client_profiles`
INNER JOIN `contacts`
ON `contacts`.`id` = `client_profiles`.`contact_id`
WHERE (client_profiles.id =
(SELECT `client_profiles`.`id` FROM `client_profiles` ORDER BY created_at desc LIMIT 1))
However, this is only returning one result. It should return client_profiles with id 9 and 11.
What is wrong with my subquery?
It looks like you were trying to filter twice on the client_profile table, once in the JOIN/ON clause and another time in the WHERE clause.
Moving everything in the where clause looks like this:
SELECT `cp`.*
FROM `contacts`
JOIN (
SELECT
`client_profiles`.`id`,
`client_profiles`.`contact_id`,
`client_profiles`.`created_at`
FROM `client_profiles`
ORDER BY created_at DESC
LIMIT 1
) cp ON `contacts`.`id` = `cp`.`contact_id`
Tell me what you think.
Should be something like maybe:
SELECT *
FROM `client_profiles`
INNER JOIN `contacts`
ON `contacts`.`id` = `client_profiles`.`contact_id`
GROUP BY `client_profiles`.`contact_id`
ORDER BY created_at desc;
http://sqlfiddle.com/#!2/a3f21b/9
You need to prequery the client profiles table grouped by each contact.. From that, re-join to the client to get the person, then again to the client profiles table based on same contact ID, but also matching the max date from the internal prequery using max( created_at )
SELECT
c.id,
c.first_name,
c.last_name,
IDByMaxDate.maxCreate,
cp.id as clientProfileID
from
( select contact_id,
MAX( created_at ) maxCreate
from
client_profiles
group by
contact_id ) IDByMaxDate
JOIN contacts c
ON IDByMaxDate.contact_id = c.id
JOIN client_profiles cp
ON IDByMaxDate.contact_id = cp.contact_id
AND IDByMaxDate.maxCreate = cp.created_at

SUM a pair of COUNTs from two tables based on a time variable

Been searching for an answer to this for the better part of an hour without much luck. I have two regional tables laid out with the same column names and I can put out a result list for either table based on the following query (swap Table2 for Table1):
SELECT Table1.YEAR, FORMAT(COUNT(Table1.id),0) AS Total
FROM Table1
WHERE Table1.variable='Y'
GROUP BY Table1.YEAR
Ideally I'd like to get a result that gives me a total sum of the counts by year, so instead of:
| REGION 1 | | REGION 2 |
| YEAR | Total | | YEAR | Total |
| 2010 | 5 | | 2010 | 1 |
| 2009 | 2 | | 2009 | 3 |
| | | | 2008 | 4 |
I'd have:
| MERGED |
| YEAR | Total |
| 2010 | 6 |
| 2009 | 5 |
| 2008 | 4 |
I've tried a variety of JOINs and other ideas but I think I'm caught up on the SUM and COUNT issue. Any help would be appreciated, thanks!
SELECT `YEAR`, FORMAT(SUM(`count`), 0) AS `Total`
FROM (
SELECT `Table1`.`YEAR`, COUNT(*) AS `count`
WHERE `Table1`.`variable` = 'Y'
GROUP BY `Table1`.`YEAR`
UNION ALL
SELECT `Table2`.`YEAR`, COUNT(*) AS `count`
WHERE `Table2`.`variable` = 'Y'
GROUP BY `Table2`.`YEAR`
) AS `union`
GROUP BY `YEAR`
You should use an UNION:
SELECT
t.YEAR,
COUNT(*) as TOTAL
FROM (
SELECT *
FROM Table1
UNION ALL
SELECT *
FROM Table2
) t
WHERE t.variable='Y'
GROUP BY t.YEAR;
Select year, sum(counts) from (
SELECT Table1.YEAR, FORMAT(COUNT(Table1.id),0) AS Total
FROM Table1
WHERE Table1.variable='Y'
GROUP BY Table1.YEAR
UNION ALL
SELECT Table2.YEAR, FORMAT(COUNT(Table2.id),0) AS Total
FROM Table2
WHERE Table2.variable='Y'
GROUP BY Table2.YEAR ) GROUP BY year
To improve upon Shehzad's answer:
SELECT YEAR, FORMAT(SUM(counts),0) AS total FROM (
SELECT Table1.YEAR, COUNT(Table1.id) AS counts
FROM Table1
WHERE Table1.variable='Y'
GROUP BY Table1.YEAR
UNION ALL
SELECT Table2.YEAR, COUNT(Table2.id) AS counts
FROM Table2
WHERE Table2.variable='Y'
GROUP BY Table2.YEAR ) AS newTable GROUP BY YEAR