Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8920701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:20:36+00:00 2026-06-15T06:20:36+00:00

I have two tables: States, and Items. States : +—-+——+——-+———-+ | id | name

  • 0

I have two tables: States, and Items.

States:

+----+------+-------+----------+
| id | name | state | priority |
+----+------+-------+----------+
|  1 |   AA |    10 |        1 |
|  2 |   AB |    10 |        2 |
|  3 |   AC |    10 |        3 |
|  4 |   BA |    20 |        1 |
|  5 |   BB |    20 |        5 |
|  6 |   BC |    20 |       10 |
|  7 |   BD |    20 |       50 |
+----+------+-------+----------+

Items:

+----+--------+-------+
| id | item   | state |
+----+--------+-------+
|  1 |   Blue |    10 |
|  2 |    Red |    20 |
|  3 |  Green |    20 |
|  4 | Yellow |    10 |
|  5 |  Brown |    10 |
+----+--------+-------+

The priority column is not used in the Items table, but complicates getting the data I need, as shown below.

What I want is a list of the rows in the Items table, replacing the state.id value in each row with the name of the highest priority state.

Results would look like this:

+----+--------+-------+
| id | item   | state |
+----+--------+-------+
|  1 |   Blue |    AC |
|  2 |    Red |    BD |
|  3 |  Green |    BD |
|  4 | Yellow |    AC |
|  5 |  Brown |    AC |
+----+--------+-------+

Here’s the tiny monster I’ve come up with. Is this the best way, or can I be more efficient / less verbose? (Sub-sub-selects make my palms itch. 😛 )

SELECT *
FROM
  Items AS itm
  INNER JOIN (SELECT sta.name, sta.state
              FROM (SELECT state, MAX(priority) [highest]
                    FROM States
                    GROUP BY state) AS pri
                INNER JOIN States AS sta
                  ON sta.state = pri.state
                     AND sta.priority = pri.highest) AS nam
    ON item.state = name.state

Update: I’m using MS-SQL 2005 and MS-SQL 2008R2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:20:37+00:00Added an answer on June 15, 2026 at 6:20 am

    You did not post your version of SQL-Server. Assuming you are on 2005 or later you can use the ROW_NUMBER() function together with a cross apply like this:

     CREATE TABLE dbo.States(id INT, name NVARCHAR(25), state INT, priority INT);
     INSERT INTO dbo.States
     VALUES
    (  1 ,'AA',    10 ,        1 ),
    (  2 ,'AB',    10 ,        2 ),
    (  3 ,'AC',    10 ,        3 ),
    (  4 ,'BA',    20 ,        1 ),
    (  5 ,'BB',    20 ,        5 ),
    (  6 ,'BC',    20 ,       10 ),
    (  7 ,'BD',    20 ,       50 );
    
    CREATE TABLE dbo.Items( id INT ,item NVARCHAR(25), state INT );
    INSERT INTO dbo.Items
    VALUES
    (  1 ,'Blue',    10 ),
    (  2 ,'Red',    20 ),
    (  3 ,'Green',    20 ),
    (  4 ,'Yellow',    10 ),
    (  5 ,'Brown',    10 );
    
    
    SELECT  i.id,
            i.item,
            s.name,
            s.priority
    FROM dbo.Items i
    CROSS APPLY (
    SELECT *,ROW_NUMBER()OVER(ORDER BY priority DESC) rn FROM dbo.States si WHERE si.state = i.state 
    )s
    WHERE s.rn = 1;
    

    The cross apply works like a join but allows to reference columns on the left side in the right side as you can see in the where clause. The ROW_NUMBER() function numbers all rows in the states table that match the current state value in reverse priority order so that the row with the highest priority always gets the number 1. The final where clause is filtering out just those rows.

    EDIT:

    I just started a blog series about joins: A Join A Day
    The Cross Apply will be topic of day 8 (12/8/2012).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question: I have two MSSQL tables, items and states, that are
I have two tables: Client(id,name,...) Purchase(id,item,date,client_id,...) They have their respective Model, with their validations.
In MySQL, I have two tables with a 1:n relationship. Table items has products,
I have two tables users and items in a MySQL DB Users pk username
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
We have an issue with two tables, let's call them Item and ItemStatuses. We
In my DB I have two tables Items(Id, ..., ToatlViews int) and ItemViews (id,
I have two MySQL tables: stats (left) and messages (right) +------------+---------+ +---------+------------+-----------+----------+ | _date
I have two tables: servers and stats servers has a column called id that
I have two tables: Sales_Order Order_ID Buyer_ID Emp_ID Status (ENUM) 1 2 3 P

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.