How do you write partition over partition selecting top 5 records?

I have written this SP as a starting point for what I'm trying to do.  I have included a pic of the results from running it.

I need the results to be limited to the top 5 bid timestamp records for each symbol and I also need the top 5 ask timestamps for each symbol.  I'm sure the SP below needs to be partitioned but I don't know how to do it at 2 levels - ask and bid

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[usp_LiveCoinGetRecentFiveAllOrderBook]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[usp_LiveCoinGetRecentFiveAllOrderBook];
GO
CREATE PROCEDURE [dbo].[usp_LiveCoinGetRecentFiveAllOrderBook]
AS
BEGIN	
SELECT [Symbol], [BidQuantity], BidTimeStamp, [Bid], [AskQuantity], [Ask], AskTimeStamp
FROM (
	SELECT 
		x.[Symbol],
		b.[BidQuantity],
		b.[Bid],
		b.[TimeStamp] As 'BidTimeStamp',		
		c.[AskQuantity],
		c.[Ask],
		c.[TimeStamp] AS 'AskTimeStamp'	      
   FROM [dbo].[LiveCoinAllOrderBook] x
   Join [dbo].[LiveCoinAllOrderBookBids] b ON x.[LiveCoinAllOrderBookID] = b.[LiveCoinAllOrderBookID]
   Join [dbo].[LiveCoinAllOrderBookAsks] c ON x.[LiveCoinAllOrderBookID] = c.[LiveCoinAllOrderBookID] 
   GROUP By 
		x.[Symbol],
		b.[BidQuantity],
		b.[Bid],
		b.[TimeStamp],		
		c.[AskQuantity],
		c.[Ask],
		c.[TimeStamp] ) z 
	ORDER BY z.[Symbol] DESC
END
GO

Open in new window



results
CAMPzxzxDeathzxzxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulEE Topic AdvisorCommented:
Use apply operators instead of joins. These are good for "top n per group" needs such as this.
SELECT
    x.[Symbol]
  , b.[BidQuantity]
  , b.[Bid]
  , b.[TimeStamp] AS 'BidTimeStamp'
  , c.[AskQuantity]
  , c.[Ask]
  , c.[TimeStamp] AS 'AskTimeStamp'
FROM [dbo].[LiveCoinAllOrderBook] x
OUTER APPLY (
    SELECT TOP (5)
        BidQuantity
      , Bid
      , TimeStamp
    FROM [dbo].[LiveCoinAllOrderBookBids] b
    WHERE b.[LiveCoinAllOrderBookID] = x.[LiveCoinAllOrderBookID]
    ORDER BY TimeStamp DESC
    ) b
OUTER APPLY (
    SELECT TOP (5)
      AskQuantity
    , Ask
    , TimeStamp
    FROM [dbo].[LiveCoinAllOrderBookAsks] c
    WHERE c.[LiveCoinAllOrderBookID] = x.[LiveCoinAllOrderBookID]
    ORDER BY TimeStamp DESC
    ) c
ORDER BY
    x.[Symbol]
  , b.[TimeStamp]
  , c.[TimeStamp]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CAMPzxzxDeathzxzxAuthor Commented:
Same result.  Maybe I should get my intended results from two different SP and combining them in code?

Capture.JPG
0
CAMPzxzxDeathzxzxAuthor Commented:
I got it thanks to you!  I added Group By

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID(N'[dbo].[usp_LiveCoinGetRecentFiveAllOrderBook]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[usp_LiveCoinGetRecentFiveAllOrderBook];
GO
CREATE PROCEDURE [dbo].[usp_LiveCoinGetRecentFiveAllOrderBook]
AS
BEGIN	
SELECT
    x.[Symbol]
  , b.[BidQuantity]
  , b.[Bid]
  , b.[TimeStamp] AS 'BidTimeStamp'
  , c.[AskQuantity]
  , c.[Ask]
  , c.[TimeStamp] AS 'AskTimeStamp'
FROM [dbo].[LiveCoinAllOrderBook] x
OUTER APPLY (
    SELECT TOP (5)
        BidQuantity
      , Bid
      , TimeStamp
    FROM [dbo].[LiveCoinAllOrderBookBids] b
    WHERE b.[LiveCoinAllOrderBookID] = x.[LiveCoinAllOrderBookID]
	ORDER BY TimeStamp DESC
    ) b
OUTER APPLY (
    SELECT TOP (5)
      AskQuantity
    , Ask
    , TimeStamp
    FROM [dbo].[LiveCoinAllOrderBookAsks] c
    WHERE c.[LiveCoinAllOrderBookID] = x.[LiveCoinAllOrderBookID]
	ORDER BY TimeStamp DESC
    ) c
	GROUP BY x.[Symbol], b.[BidQuantity], b.[Bid], b.[TimeStamp],c.[AskQuantity], c.[Ask], c.[TimeStamp]
ORDER BY 
    x.[Symbol]
  , b.[TimeStamp]
  , c.[TimeStamp]
END
GO

Open in new window

0
CAMPzxzxDeathzxzxAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.