Avatar of Chris Michalczuk
Chris MichalczukFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

How do I extract the TOP 1 from a list

How do I show only the last issuenumber for a sequencenumber in a list - this is my query but I get the results as show below
I only want the results to show the 2 lines marked with *********** below

SELECT
      [ACCOUNTID]
      ,[SEQUENCENUMBER]
      ,[ISSUENUMBER]
   
  FROM temp1
  where accountid='A6UJ9A000AAQ'
  order by  [SEQUENCENUMBER],ISSUENUMBER desc

  select  SEQUENCENUMBER, issuenumber FROM temp1
  where accountid='A6UJ9A000AAQ'
  group by SEQUENCENUMBER,issuenumber

  order by  [SEQUENCENUMBER] desc ,issuenumber desc



ACCOUNTID      SEQUENCENUMBER      ISSUENUMBER
A6UJ9A000AAQ      4264      14  *********************
A6UJ9A000AAQ      4264      13
A6UJ9A000AAQ      4264      12
A6UJ9A000AAQ      4264      11
A6UJ9A000AAQ      4264      10
A6UJ9A000AAQ      4264      9
A6UJ9A000AAQ      4264      8
A6UJ9A000AAQ      21600      1 ********************
A6UJ9A000AAQ      21600      0
Microsoft SQL Server

Avatar of undefined
Last Comment
awking00
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

SELECT 
       [ACCOUNTID]
       ,[SEQUENCENUMBER]
       ,MAX([ISSUENUMBER])
FROM temp1
where accountid='A6UJ9A000AAQ'
group by  [ACCOUNTID], [SEQUENCENUMBER]
order by  [SEQUENCENUMBER],ISSUENUMBER desc

Open in new window

Avatar of Chris Michalczuk

ASKER

Hi Vitor sorry but this returns all the records - I only want the first one for each sequencenumber so I am expecting to return 2 records only
ACCOUNTID      SEQUENCENUMBER      ISSUENUMBER
A6UJ9A000AAQ      4264      14  *********************
A6UJ9A000AAQ      21600      1 ********************

I am still returning


ACCOUNTID      SEQUENCENUMBER      ISSUENUMBER
A6UJ9A000AAQ      4264      14  *********************
A6UJ9A000AAQ      4264      13
A6UJ9A000AAQ      4264      12
A6UJ9A000AAQ      4264      11
A6UJ9A000AAQ      4264      10
A6UJ9A000AAQ      4264      9
A6UJ9A000AAQ      4264      8
A6UJ9A000AAQ      21600      1 ********************
A6UJ9A000AAQ      21600      0
It shouldn't. Can you post a print screen with my query and the results?
Avatar of Chris Michalczuk

ASKER

was missing the brackets!!!!  order by  [SEQUENCENUMBER],MAX([ISSUENUMBER]) desc
 works now thanks


SELECT
       [ACCOUNTID]
       ,[SEQUENCENUMBER]
       ,MAX([ISSUENUMBER])
FROM [dbo].[VW_CPM_LICENSEMASTER]
where accountid='A6UJ9A000AAQ'
group by  [ACCOUNTID], [SEQUENCENUMBER]
order by  [SEQUENCENUMBER],MAX([ISSUENUMBER]) desc
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of awking00
awking00
Flag of United States of America image

select accountid, sequencenumber, issuenumber from
(select accountid, sequencenumber, issuenumber,
 row_number() over (partition by accountid, sequencenumber order by issuenumber desc) rn
 from temp1) x
where rn = 1;
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo