?
Solved

Query to fetch rows with matching values for just few columns column as a single row

Posted on 2014-02-14
3
Medium Priority
?
291 Views
Last Modified: 2014-02-25
Hello,
I have a table with rows having same id and code value, but different Effective date.
Id         Code          EffectiveDate     cost
1          basic          01-jan-2010        5.00
1          basic          06-feb-2011        5.00
1          exclusive    09-aug-2012        15.00

Open in new window


My query needs to return only 2 rows, one for 1 basic, and another for 1 exclusive.
Result should be like:
Id            Code                 cost
1              Basic                5.00
1              Exclusive          15.00

Open in new window


DISTINCT would not work as the Effective date is different between the two records with Id=1 and Code = 'Basic'.

Thanks!
0
Comment
Question by:sath350163
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 total points
ID: 39860782
I assume you want the latest date??  You don't indicate anything about which row should be selected and why ... and they both have the same price?!

SELECT
    id, code, cost
FROM (
    SELECT id, code, cost,
        ROW_NUMBER() OVER(PARTITION BY id ORDER BY EffectiveDate DESC) AS row_num
    FROM tablename
) AS derived
WHERE
    row_num = 1
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39861308
select distinct id, code, cost
from YourTable
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 39861780
both code above would work, but I will just post my article for you to understand the issue and find sql code solutions like above:
http://www.experts-exchange.com/Database/Miscellaneous/A_3203-DISTINCT-vs-GROUP-BY-and-why-does-it-not-work-for-my-query.html
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

762 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