Check level with t-sql

Posted on 2011-10-31
Last Modified: 2012-05-12

I have this table looking like this.
premiumID int
premiumName nvarchar
premiumLevel int

the data in the table can look like this
premiumID    premiumName    premiumLevel
1                   Goldcustomer   10000
2                   Silvercustomer   7500
3                   Bronscustomer   5000

Now I want to check what level a customers total shopping amount is. So for example if the customers amount is 6543 the query would return premiumID=3 since the amount is larger than 5000 but less then 7500. If the amount is 8500 the premiumID = 2 and so on. How can I achieve this with a t-sql query?

Thanks for help!

Question by:peternordberg
    LVL 33

    Accepted Solution


    select top 1 premiumID
    from customerPremium
    where premiumLevel <= 6543   -- this is the amount spent
    order by premiumID desc
    LVL 12

    Expert Comment

    Select * From
      T.*,  ROW_NUMBER() OVER (Order by premiumLevel) as RN
      WHERE premiumLevel >=    myCustomerSpend
    ) Q
    where RN = 1

    This should return the first qualifying level
    LVL 8

    Assisted Solution

    @ghtEknight : Minor mistake in your answer. In order by, I have replace 'premiumID' by 'premiumLevel'

    select top 1 premiumID
    from customerPremium
    where premiumLevel <= 6543   -- this is the amount spent
    order by premiumLevel desc
    LVL 33

    Expert Comment

    yes, that was precisely my intent - good catch.

    Author Closing Comment

    Thanks for help!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
    How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now