How to get TOP ID in query?


  • USE [iMIS_Prod]
    GO

    CREATE TABLE [dbo].[Orders](
        [ORDER_NUMBER] [numeric](15, 2) NOT NULL CONSTRAINT [DF_Orders_ORDER_NUMBER] DEFAULT ((0)),
        [ST_ID] [numeric](10) NOT NULL CONSTRAINT [DF_Orders_ST_ID] DEFAULT ((0)),
        [TOTAL_PAYMENTS] [money] NOT NULL CONSTRAINT [DF_Orders_TOTAL_PAYMENTS] DEFAULT ((0))
    CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
    (
        [ORDER_NUMBER] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

    GO

    SET ANSI_PADDING ON
    GO

    INSERT INTO [dbo].[Orders] (ORDER_NUMBER, ST_ID, TOTAL_PAYMENTS)
    VALUES
    (20650917.00,111156,220.50)
    ,(21888549.00,111156,220.50)
    ,(23475974.00,111156,245.00)
    ,(15438023.00,115240,97.00)
    ,(26580872.00,115240,166.00)
    ,(17064426.00,122994,117.50)
    ,(21887826.00,122994,121.00)
    ,(26467822.00,122994,130.00)
    ,(16240398.00,132943,117.50)
    ,(20445595.00,132943,172.50)
    ,(16694116.00,134513,245.00)
    ,(17437801.00,134513,220.50)
    ,(21784193.00,134513,247.50)

    Select TOP(1) SUM(TOTAL_PAYMENTS) as highest, ST_ID FROM ORDERS GROUP BY ST_ID

    How to get ST_ID (customer) who spend the most? My query I showed above does not get me correct data. Any suggestions? Thank you.

  • Add an ORDER BY clause. Data inside SQL Server isn't stored in any specific order, it's all a "heap". When you use a TOP 1, without an ORDER BY, the result you get back will simply be the first row in the dataset created; which could be anything. Adding an ORDER BY defines the order (surprise that!) the data is returned in, thus resulting in the TOP 1 being the first ordered row.

    Try yourself, and post back if you get stuck.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thom A - Tuesday, December 12, 2017 11:21 AM

    Add an ORDER BY clause. Data inside SQL Server isn't stored in any specific order, it's all a "heap". When you use a TOP 1, without an ORDER BY, the result you get back will simply be the first row in the dataset created; which could be anything. Adding an ORDER BY defines the order (surprise that!) the data is returned in, thus resulting in the TOP 1 being the first ordered row.

    Try yourself, and post back if you get stuck.

    Thank you, Thom A. I did not form my question correctly. What I was interested is how to get ST_ID data for TOP record. 
    The query:
    SELECT * FROM Client WHERE ID IN(Select TOP(1) SUM(TOTAL_PAYMENTS) as highest, ST_ID FROM ORDERS GROUP BY ST_ID ORDER BY highest DESC

    obviously would not work. Any thoughts?

  • rkordonsky 63916 - Tuesday, December 12, 2017 12:21 PM

    Thom A - Tuesday, December 12, 2017 11:21 AM

    Add an ORDER BY clause. Data inside SQL Server isn't stored in any specific order, it's all a "heap". When you use a TOP 1, without an ORDER BY, the result you get back will simply be the first row in the dataset created; which could be anything. Adding an ORDER BY defines the order (surprise that!) the data is returned in, thus resulting in the TOP 1 being the first ordered row.

    Try yourself, and post back if you get stuck.

    Thank you, Thom A. I did not form my question correctly. What I was interested is how to get ST_ID data for TOP record. 
    The query:
    SELECT * FROM Client WHERE ID IN(Select TOP(1) SUM(TOTAL_PAYMENTS) as highest, ST_ID FROM ORDERS GROUP BY ST_ID ORDER BY highest DESC

    obviously would not work. Any thoughts?

    Thom did not say anything about adding a subquery, just an ORDER BY clause.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • rkordonsky 63916 - Tuesday, December 12, 2017 12:21 PM

    Thom A - Tuesday, December 12, 2017 11:21 AM

    Add an ORDER BY clause. Data inside SQL Server isn't stored in any specific order, it's all a "heap". When you use a TOP 1, without an ORDER BY, the result you get back will simply be the first row in the dataset created; which could be anything. Adding an ORDER BY defines the order (surprise that!) the data is returned in, thus resulting in the TOP 1 being the first ordered row.

    Try yourself, and post back if you get stuck.

    Thank you, Thom A. I did not form my question correctly. What I was interested is how to get ST_ID data for TOP record. 
    The query:
    SELECT * FROM Client WHERE ID IN(Select TOP(1) SUM(TOTAL_PAYMENTS) as highest, ST_ID FROM ORDERS GROUP BY ST_ID ORDER BY highest DESC

    obviously would not work. Any thoughts?

    So it looks like you're trying to get that ID from the orders table and search client for it.  You don't have to include the aggregate in the SELECT columns to be able to order by it 😀  So it looks like you would just want this in your sub query(assuming ST_ID is the column that joins to clients).

    Select TOP(1) ST_ID FROM ORDERS GROUP BY ST_ID ORDER BY SUM(TOTAL_PAYMENTS) DESC

  • drew.allen - Tuesday, December 12, 2017 12:31 PM

    rkordonsky 63916 - Tuesday, December 12, 2017 12:21 PM

    Thom A - Tuesday, December 12, 2017 11:21 AM

    Add an ORDER BY clause. Data inside SQL Server isn't stored in any specific order, it's all a "heap". When you use a TOP 1, without an ORDER BY, the result you get back will simply be the first row in the dataset created; which could be anything. Adding an ORDER BY defines the order (surprise that!) the data is returned in, thus resulting in the TOP 1 being the first ordered row.

    Try yourself, and post back if you get stuck.

    Thank you, Thom A. I did not form my question correctly. What I was interested is how to get ST_ID data for TOP record. 
    The query:
    SELECT * FROM Client WHERE ID IN(Select TOP(1) SUM(TOTAL_PAYMENTS) as highest, ST_ID FROM ORDERS GROUP BY ST_ID ORDER BY highest DESC

    obviously would not work. Any thoughts?

    Thom did not say anything about adding a subquery, just an ORDER BY clause.

    Drew

    Obviously he did not. I have to apologize for not forming the question correctly. Sorry for that.

  • rkordonsky 63916 - Tuesday, December 12, 2017 12:21 PM

    Thom A - Tuesday, December 12, 2017 11:21 AM

    Add an ORDER BY clause. Data inside SQL Server isn't stored in any specific order, it's all a "heap". When you use a TOP 1, without an ORDER BY, the result you get back will simply be the first row in the dataset created; which could be anything. Adding an ORDER BY defines the order (surprise that!) the data is returned in, thus resulting in the TOP 1 being the first ordered row.

    Try yourself, and post back if you get stuck.

    Thank you, Thom A. I did not form my question correctly. What I was interested is how to get ST_ID data for TOP record. 
    The query:
    SELECT * FROM Client WHERE ID IN(Select TOP(1) SUM(TOTAL_PAYMENTS) as highest, ST_ID FROM ORDERS GROUP BY ST_ID ORDER BY highest DESC

    obviously would not work. Any thoughts?

    The reason this query doesn't work has nothing to do with Thom's suggestion.  You have a subquery that returns two columns, which is not allowed in most circumstances (APPLY being the main exception).

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • rkordonsky 63916 - Tuesday, December 12, 2017 12:34 PM

    drew.allen - Tuesday, December 12, 2017 12:31 PM

    rkordonsky 63916 - Tuesday, December 12, 2017 12:21 PM

    Thom A - Tuesday, December 12, 2017 11:21 AM

    Add an ORDER BY clause. Data inside SQL Server isn't stored in any specific order, it's all a "heap". When you use a TOP 1, without an ORDER BY, the result you get back will simply be the first row in the dataset created; which could be anything. Adding an ORDER BY defines the order (surprise that!) the data is returned in, thus resulting in the TOP 1 being the first ordered row.

    Try yourself, and post back if you get stuck.

    Thank you, Thom A. I did not form my question correctly. What I was interested is how to get ST_ID data for TOP record. 
    The query:
    SELECT * FROM Client WHERE ID IN(Select TOP(1) SUM(TOTAL_PAYMENTS) as highest, ST_ID FROM ORDERS GROUP BY ST_ID ORDER BY highest DESC

    obviously would not work. Any thoughts?

    Thom did not say anything about adding a subquery, just an ORDER BY clause.

    Drew

    Obviously he did not. I have to apologize for not forming the question correctly. Sorry for that.

    This is why we ask for expected results as well as sample data.  Instead of trying to describe what you want, show what you want.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • I suspect you just need this:
    CREATE TABLE dbo.Orders (
        ORDER_NUMBER numeric(15, 2) NOT NULL CONSTRAINT [DF_Orders_ORDER_NUMBER] DEFAULT ((0)),
        ST_ID numeric(10) NOT NULL CONSTRAINT [DF_Orders_ST_ID] DEFAULT ((0)),
      TOTAL_PAYMENTS money NOT NULL CONSTRAINT [DF_Orders_TOTAL_PAYMENTS] DEFAULT ((0))
        CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
            (
            ORDER_NUMBER ASC
            )
        WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
    );
    GO

    SET ANSI_PADDING ON;
    GO

    INSERT INTO dbo.Orders (ORDER_NUMBER, ST_ID, TOTAL_PAYMENTS)
        VALUES    (20650917.00,111156,220.50)
                ,(21888549.00,111156,220.50)
                ,(23475974.00,111156,245.00)
                ,(15438023.00,115240,97.00)
                ,(26580872.00,115240,166.00)
                ,(17064426.00,122994,117.50)
                ,(21887826.00,122994,121.00)
                ,(26467822.00,122994,130.00)
                ,(16240398.00,132943,117.50)
                ,(20445595.00,132943,172.50)
                ,(16694116.00,134513,245.00)
                ,(17437801.00,134513,220.50)
                ,(21784193.00,134513,247.50);

    SELECT TOP(1) ST_ID, SUM(TOTAL_PAYMENTS) AS highest
    FROM dbo.Orders
    GROUP BY ST_ID
    ORDER BY SUM(TOTAL_PAYMENTS) DESC;

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply