简体   繁体   中英

MySQL Select COUNT(*) and Row with Max Date

I am trying to select the COUNT(*) of deals grouped by seller along with their most recent product and the date that it was added. However, for some reason it seems to keep ordering the deals by creation date, ascending even when I try subqueries to prevent that. Here is an example table:

------------------------------------------------
| ID | Provider |      URL        | CreateDate |
------------------------------------------------
| 1  | Prov1    | http://ex.com/1 | 2015-05-10 |
| 2  | Prov1    | http://ex.com/2 | 2015-06-10 |
| 3  | Prov1    | http://ex.com/3 | 2015-07-10 |
| 4  | Prov2    | http://ex.com/4 | 2015-05-10 |
| 5  | Prov2    | http://ex.com/5 | 2015-06-10 |
------------------------------------------------

I am looking to return the following:

-----------------------------------------------------------
| ID | COUNT(*) | Provider |      URL        | CreateDate |
-----------------------------------------------------------
| 3  |    3     | Prov1    | http://ex.com/3 | 2015-07-10 |
| 5  |    2     | Prov2    | http://ex.com/5 | 2015-06-10 |
-----------------------------------------------------------

My current query is:

SELECT ID,COUNT(*),Provider,CreateDate,URL
FROM (SELECT * FROM products ORDER BY CreateDate DESC)
GROUP BY Provider;

But this doesn't seem to work. Does anyone have a suggestion?

EDIT Thank you all for the great answers. What is extremely strange to me is that while they seem to work in a SQL fiddle, they fail on my database server. For example using the following on the server provides the following:

mysql> INSERT INTO products
-> (`ID`, `Provider`, `URL`, `CreateDate`)
-> VALUES
-> (1, 'Prov1', 'http://ex.com/1','2015-05-10'),
-> (2, 'Prov1', 'http://ex.com/2','2015-06-10'),
-> (3, 'Prov1', 'http://ex.com/3','2015-07-10'),
-> (4, 'Prov2', 'http://ex.com/4','2015-05-10'),
-> (5, 'Prov2', 'http://ex.com/5','2015-06-10')
-> ;
Query OK, 5 rows affected (0.06 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> SELECT ID,COUNT(*),Provider,CreateDate,URL
    -> FROM (SELECT * FROM products ORDER BY CreateDate DESC) t1
    -> GROUP BY Provider;
+------+----------+----------+---------------------+-----------------+
| ID   | COUNT(*) | Provider | CreateDate          | URL             |
+------+----------+----------+---------------------+-----------------+
|    1 |        3 | Prov1    | 2015-05-10 00:00:00 | http://ex.com/1 |
|    4 |        2 | Prov2    | 2015-05-10 00:00:00 | http://ex.com/4 |
+------+----------+----------+---------------------+-----------------+

While running the same thing on SQL fiddle works as expected. Further the comment regarding the JOIN should work, but I am having the same issue with it returning unexpected results. My version of MySQL is 5.5.37-MariaDB-34.0. Any ideas on this?

SELECT p1.*, COUNT(*)
FROM products p1
JOIN
(
    select provider, max(CreateDate) as m_date
    from products 
    group by provider
) p2 on p1.provider = p2.provider and p1.CreateDate = p2.m_date

You need specify the field name where you want mysql return count content:

select count(*) as nro from table;

in your case:

SELECT ID,COUNT(*) as nro,Provider,CreateDate,URL
FROM (SELECT * FROM products ORDER BY CreateDate DESC)
GROUP BY Provider;

but for all solution you need create an alias for this part of query "SELECT * FROM products ORDER BY CreateDate DESC"

CREATE VIEW p AS (SELECT * FROM products ORDER BY CreateDate DESC);

and the second query:

  SELECT ID,COUNT(*) as nro,Provider,CreateDate,URL
  FROM (p)
  GROUP BY Provider;

must be work.

I did it in SQL Server but the query in mysql works.

Create TABLE #Products(
Id int,
Provider varchar(50),
URL Varchar(300),
Createdate varchar(50)
)


INSERT INTO #Products

SELECT 1,'Prov1','http://ex.com/1 ','2015-05-10'  UNION ALL
SELECT 2,'Prov1','http://ex.com/2 ','2015-06-10'  UNION ALL
SELECT 3,'Prov1','http://ex.com/3 ','2015-07-10'  UNION ALL
SELECT 4,'Prov2','http://ex.com/4 ','2015-05-10'  UNION ALL
SELECT 5,'Prov2','http://ex.com/5 ','2015-06-10' 




SELECT p1.id, p2.Provider,p2.m_date,p1.url, COUNT(*)
FROM #products p1
JOIN
(
    select  provider, max(CreateDate) as m_date
    from #products 
    group by provider
) p2 on p1.provider = p2.provider and p1.CreateDate = p2.m_date
GROUP BY p1.id, p2.Provider,p2.m_date,p1.url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM