SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to get TOP ID in query?


How to get TOP ID in query?

Author
Message
rkordonsky 63916
rkordonsky 63916
SSC Veteran
SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)

Group: General Forum Members
Points: 249 Visits: 58

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.

Thom A
Thom A
SSC Guru
SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)

Group: General Forum Members
Points: 71266 Visits: 18554
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 :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
rkordonsky 63916
rkordonsky 63916
SSC Veteran
SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)

Group: General Forum Members
Points: 249 Visits: 58
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?

drew.allen
drew.allen
SSC Guru
SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)

Group: General Forum Members
Points: 56904 Visits: 14834
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
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
ZZartin
ZZartin
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22285 Visits: 15937
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 BigGrin 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

rkordonsky 63916
rkordonsky 63916
SSC Veteran
SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)SSC Veteran (249 reputation)

Group: General Forum Members
Points: 249 Visits: 58
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.

drew.allen
drew.allen
SSC Guru
SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)

Group: General Forum Members
Points: 56904 Visits: 14834
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
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
drew.allen
drew.allen
SSC Guru
SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)

Group: General Forum Members
Points: 56904 Visits: 14834
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
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
sgmunson
sgmunson
SSC Guru
SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)

Group: General Forum Members
Points: 75330 Visits: 6361
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)
Smile Smile Smile
Health & Nutrition
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search