SQL查询...多个最大值选择。需要帮助

发布于 2024-08-10 01:29:16 字数 1020 浏览 3 评论 0原文

商业世界 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

随梦而飞# 2024-08-17 01:29:16

您可以更改 WHERE 语句来查找每个日志的最后日期:

AND ljd.receipt_date = (
    SELECT max(subljd.receipt_date) 
    from lib_journal_details subljd
    where subljd.journelname = ljd.journelname)

确保为子查询中的表指定与主查询中的表不同的别名。

You could change the WHERE statement to look up the last date for each journal:

AND ljd.receipt_date = (
    SELECT max(subljd.receipt_date) 
    from lib_journal_details subljd
    where subljd.journelname = ljd.journelname)

Make sure to give the table in the subquery a different alias from the table in the main query.

笔芯 2024-08-17 01:29:16

如果您需要最大起始日期,您应该使用分组依据。
应该看起来像这样:

SELECT 
   ljm.journelname
   , ljm.subscription_id
   , ljm.frequency
   , ljm.publisher
   , ljm.price
   , **MAX(ljd.receipt_date)**
FROM 
   lib_journals_master ljm
   , lib_subscriptionhistory lsh
   , lib_journal_details ljd
WHERE 
   ljd.journal_id=ljm.id 
GROUP BY 
   ljm.journelname
   , ljm.subscription_id
   , ljm.frequency
   , ljm.publisher
   , ljm.price

You should use Group By if you need the Max from date.
Should look something like this:

SELECT 
   ljm.journelname
   , ljm.subscription_id
   , ljm.frequency
   , ljm.publisher
   , ljm.price
   , **MAX(ljd.receipt_date)**
FROM 
   lib_journals_master ljm
   , lib_subscriptionhistory lsh
   , lib_journal_details ljd
WHERE 
   ljd.journal_id=ljm.id 
GROUP BY 
   ljm.journelname
   , ljm.subscription_id
   , ljm.frequency
   , ljm.publisher
   , ljm.price
無心 2024-08-17 01:29:16

像这样的东西应该对你有用。

SELECT ljm.journelname
       , ljm.subscription_id
       , ljm.frequency
       , ljm.publisher
       , ljm.price
       ,md.max_receipt_date
FROM lib_journals_master ljm
    , (    SELECT journal_id
            , max(receipt_date) as max_receipt_date
           FROM lib_journal_details 
           GROUP BY journal_id) md
WHERE ljm.id = md.journal_id
/

请注意,我已从 FROM 子句中删除了对查询没有任何贡献的表。如果 yopu 为了我们的利益简化了您的场景,您可能需要更换它们。

Something like this should work for you.

SELECT ljm.journelname
       , ljm.subscription_id
       , ljm.frequency
       , ljm.publisher
       , ljm.price
       ,md.max_receipt_date
FROM lib_journals_master ljm
    , (    SELECT journal_id
            , max(receipt_date) as max_receipt_date
           FROM lib_journal_details 
           GROUP BY journal_id) md
WHERE ljm.id = md.journal_id
/

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.

_蜘蛛 2024-08-17 01:29:16

将其分为两个查询,一个查询将获取期刊名称和最新日期,

declare table @table (journalName as varchar,saleDate as datetime)

insert into @table
select journalName,max(saleDate) from JournalTable group by journalName

从表中选择您需要的所有字段并将 @table 与它们连接。加入期刊名称。

Separate this into two queries one will get journal name and latest date

declare table @table (journalName as varchar,saleDate as datetime)

insert into @table
select journalName,max(saleDate) from JournalTable group by journalName

select all fields you need from your table and join @table with them. join on journalName.

_蜘蛛 2024-08-17 01:29:16

听起来像是小组中的佼佼者。您可以在 SQL Server 中使用 CTE


;WITH journeldata AS
(
SELECT 
     ljm.journelname
    ,ljm.subscription_id
    ,ljm.frequency
    ,ljm.publisher
    ,ljm.price
    ,ljd.receipt_date
    ,ROW_NUMBER() OVER (PARTITION BY ljm.journelname ORDER BY ljd.receipt_date DESC) AS RowNumber
FROM 
     lib_journals_master ljm
    ,lib_subscriptionhistory lsh
    ,lib_journal_details ljd 
WHERE 
    ljd.journal_id=ljm.id
    AND ljm.subscription_id = ljm.subscription_id
)
SELECT
     journelname
    ,subscription_id
    ,frequency
    ,publisher
    ,price
    ,receipt_date
FROM journeldata
WHERE RowNumber = 1 

Sounds like top of group. You can use a CTE in SQL Server:


;WITH journeldata AS
(
SELECT 
     ljm.journelname
    ,ljm.subscription_id
    ,ljm.frequency
    ,ljm.publisher
    ,ljm.price
    ,ljd.receipt_date
    ,ROW_NUMBER() OVER (PARTITION BY ljm.journelname ORDER BY ljd.receipt_date DESC) AS RowNumber
FROM 
     lib_journals_master ljm
    ,lib_subscriptionhistory lsh
    ,lib_journal_details ljd 
WHERE 
    ljd.journal_id=ljm.id
    AND ljm.subscription_id = ljm.subscription_id
)
SELECT
     journelname
    ,subscription_id
    ,frequency
    ,publisher
    ,price
    ,receipt_date
FROM journeldata
WHERE RowNumber = 1 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文