如何使用MySQL从下表中获取"max"日期?
date_time
-----------------------------
Wednesday, 21 July 2010 20:41:51
Tuesday, 19 October 2010 16:7:41
Tuesday, 29 November 2010 16:7:41
Run Code Online (Sandbox Code Playgroud)
我想从表中检索最大日期.
我有一个包含以下结构的行/数据的表
-----------
| SURVEY_ID |
------------
| 1 |
| 2 |
| 2 |
| 3 |
| 4 |
-----------
Run Code Online (Sandbox Code Playgroud)
我想在同一查询中获取不同的 ID 和最大 ID。我试过
select distinct(survey_id) as survey_id , max(survey_id) as current
from survey_main
group by survey_id
Run Code Online (Sandbox Code Playgroud)
这似乎没有返回正确的结果。我缺少什么?
编辑:所需结果
----------------------
| 与众不同| 最大|
----------------------
| 1 | 4 |
| 2 | 4 |
| 3 | 4 |
| 4 | 4 |
----------------------
我正在使用LINQ to Entities来查询以下MS-SQL表:
RateID RateTypeID Amount EffectiveDate IsRetired
------ ---------- ------ ------------- ---------
1 1 0.565 1/1/2013 0:00 FALSE
2 2 6.000 1/1/2013 0:00 FALSE
3 3 9.000 1/1/2013 0:00 FALSE
4 4 12.000 1/1/2013 0:00 FALSE
5 5 1.500 1/1/2013 0:00 FALSE
6 1 0.550 7/1/2012 0:00 FALSE
7 1 0.495 1/1/2012 0:00 FALSE
8 3 8.000 1/1/2011 0:00 FALSE
9 3 11.000 1/1/2015 0:00 FALSE
10 5 2.000 7/1/2013 0:00 TRUE
Run Code Online (Sandbox Code Playgroud)
我想构建一个函数,它将使用LINQ返回一个IQueryable(Of Rate)我可以加入其他查询的函数,以便有效地确定应用于每个项目的当前速率RateTypeID. IsRetired …
我在MySQL服务器上试过这个查询(5.1.41)......
SELECT max(volume), dateofclose, symbol, volume, close, market FROM daily group by market
Run Code Online (Sandbox Code Playgroud)
我得到了这个结果:
max(volume) dateofclose symbol volume close market
287031500 2010-07-20 AA.P 500 66.41 AMEX
242233000 2010-07-20 AACC 16200 3.98 NASDAQ
1073538000 2010-07-20 A 4361000 27.52 NYSE
2147483647 2010-07-20 AAAE.OB 400 0.01 OTCBB
437462400 2010-07-20 AAB.TO 31400 0.37 TSX
61106320 2010-07-20 AA.V 0 0.24 TSXV
Run Code Online (Sandbox Code Playgroud)
如您所见,最大音量与音量列的"实际"值非常不同?!?
volume列定义为int(11),我在这个表中有200万行,但它离MyISAM存储的最大值很远,所以我不相信这是问题所在!?同样奇怪的是数据从同一天开始显示(dateofclose).如果我使用WHERE子句强制特定日期,则相同的符号会出现不同的max(volume)结果.这很奇怪......
需要一些帮助!
更新:
这是我编辑的"工作"请求:
SELECT a.* FROM daily a
INNER JOIN (
SELECT market, MAX(volume) AS max_volume
FROM daily
WHERE dateofclose = '20101108' …Run Code Online (Sandbox Code Playgroud) 我有一张桌子:发票
inv_id cus_id due_amt paid total_due
1 71 300 0 300
2 71 200 0 500
3 71 NULL 125 375
4 72 50 0 50
5 72 150 0 200
Run Code Online (Sandbox Code Playgroud)
我想要结果
cus_id total_due
71 375
72 200
Run Code Online (Sandbox Code Playgroud)
这是我想total_due的unique customer或以其他方式可以说我需要latest invoice的细节unique customer。
我试过的:
SELECT cus_id, total_due FROM invoice GROUP BY cus_id ORDER BY inv_id DESC
但这并没有给出所需的结果。
请有人可以帮助我..
我在oracle 11g中有一个像这样的表:
id date
--- ---
1 1-jun
1 2-jun
1 3-jun
2 1-jul
2 2-jul
2 3-jul
Run Code Online (Sandbox Code Playgroud)
我正在尝试提取与每个id对应的最新记录.我试过group by,max但是我无法让它工作.我想要的是:
id date
--- ---
1 3-jun
2 3-jul
Run Code Online (Sandbox Code Playgroud)