SQL query …multiple max value selection. Help need

2019-06-11 14:26发布

问题:

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?

回答1:

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


回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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