Need help with a query. Probably a self join or a nested query.

Posted on 2012-08-27
Last Modified: 2012-08-27
I am having difficulty with a query. I have tried a nested query and
self join, but just can't get it. So possibly someone can help me out?

I have a table below and obviously it has redundant data. basically all of
the codes are associated with a description. I don't care which description
is returned from my query. I only want a unique code, and the description
associated with the unique code. Can someone help me out? Below is my table
Apartements and below that is the resulting query of data I would like returned.

Table Name "Apartements"

Code     Description
TB       Two Bedroom 1 bath
TB       Two Bedroom
TB       Two Bedroom plus patio
OB       One Bedroom
OB       One Bedroom plus sink
OB       One Bedroom plus 1 bath
FB       Four Bedroom
FB       Four Bedroom plus 2 bathrooms
RU       One night rental unit
RU       One Night with breakfast

Resulting Query :

TB      TWo Bedroom 1 bath
OB      One Bedroom
FB      Four Bedroom
RU      One night with breakfast
Question by:brgdotnet
    LVL 25

    Accepted Solution

    SELECT code, max(Description) as Description
    FROM Apartements
    GROUP BY code

    Open in new window

    LVL 25

    Expert Comment

    SELECT code, Description FROM (
    SELECT code, Description, row_number()over(partition by code order by Description) rn
    FROM Apartements)v
    WHERE rn = 1

    Open in new window

    LVL 2

    Author Closing Comment

    Thank you so much !!!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    Viewers will learn how the fundamental information of how to create a table.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    758 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

    10 Experts available now in Live!

    Get 1:1 Help Now