SQL查询...多个最大值选择。需要帮助
商业世界 1256987 每月 10 2009-10-28
商业世界 1256987 每月 10 2009-09-23
商业世界 1256987 每月 10 2009-08-18
Linux 4 U 456734 每月 25 2009-12-24
Linux 4 U 456734 每月 25 2009-1 1 -11
Linux 4 U 456734 每月 25 2009-10-28
我通过查询得到这个结果:
SELECT DISTINCT ljm.journelname,ljm. subscription_id,
ljm.frequency,ljm.publisher, ljm.price, ljd.receipt_date
FROM lib_journals_master ljm,
lib_subscriptionhistory
lsh,lib_journal_details ljd
WHERE ljd.journal_id=ljm.id
ORDER BY ljm.publisher
我需要的是每个期刊的最新日期?
我尝试了这个查询:
SELECT DISTINCT ljm.journelname, ljm.subscription_id,
ljm.frequency, ljm.publisher, ljm.price,ljd.receipt_date
FROM lib_journals_master ljm,
lib_subscriptionhistory lsh,
lib_journal_details ljd
WHERE ljd.journal_id=ljm.id
AND ljd.receipt_date = (
SELECT max(ljd.receipt_date)
from lib_journal_details ljd)
但它给了我整个列的最大值。我需要的结果将有两个日期(每本杂志的最大值),但这个查询只给我一个?
Business World 1256987 monthly 10 2009-10-28
Business World 1256987 monthly 10 2009-09-23
Business World 1256987 monthly 10 2009-08-18
Linux 4 U 456734 monthly 25 2009-12-24
Linux 4 U 456734 monthly 25 2009-11-11
Linux 4 U 456734 monthly 25 2009-10-28
I get this result with the query:
SELECT DISTINCT ljm.journelname,ljm. subscription_id,
ljm.frequency,ljm.publisher, ljm.price, ljd.receipt_date
FROM lib_journals_master ljm,
lib_subscriptionhistory
lsh,lib_journal_details ljd
WHERE ljd.journal_id=ljm.id
ORDER BY ljm.publisher
What I need is the latest date in each journal?
I tried this query:
SELECT DISTINCT ljm.journelname, ljm.subscription_id,
ljm.frequency, ljm.publisher, ljm.price,ljd.receipt_date
FROM lib_journals_master ljm,
lib_subscriptionhistory lsh,
lib_journal_details ljd
WHERE ljd.journal_id=ljm.id
AND ljd.receipt_date = (
SELECT max(ljd.receipt_date)
from lib_journal_details ljd)
But it gives me the maximum from the entire column. My needed result will have two dates (maximum of each magazine), but this query gives me only one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以更改 WHERE 语句来查找每个日志的最后日期:
确保为子查询中的表指定与主查询中的表不同的别名。
You could change the WHERE statement to look up the last date for each journal:
Make sure to give the table in the subquery a different alias from the table in the main query.
如果您需要最大起始日期,您应该使用分组依据。
应该看起来像这样:
You should use Group By if you need the Max from date.
Should look something like this:
像这样的东西应该对你有用。
请注意,我已从 FROM 子句中删除了对查询没有任何贡献的表。如果 yopu 为了我们的利益简化了您的场景,您可能需要更换它们。
Something like this should work for you.
Note that I have removed the tables from the FROM clause which don't contribute anything to the query. You may need to replace them if yopu simplified your scenario for our benefit.
将其分为两个查询,一个查询将获取期刊名称和最新日期,
从表中选择您需要的所有字段并将 @table 与它们连接。加入期刊名称。
Separate this into two queries one will get journal name and latest date
select all fields you need from your table and join @table with them. join on journalName.
听起来像是小组中的佼佼者。您可以在 SQL Server 中使用 CTE:
Sounds like top of group. You can use a CTE in SQL Server: