SQL syntax that returns columns from 2 rows of the same table on the same result row. SQL Server 2005

Posted on 2007-10-05
Last Modified: 2010-03-19
Need a SQL statement that returns columns from 2 rows of the same table on the same result row.

I have a Primary key, QuoteDate and Cl.osePrice column in a SQL Server 2005 table.

I need a SQL statement that returns the QuoteDate and ClosePrice for two given dates.


SELECT (QuoteDate on '09/15/2007') As PreviousDate, (ClosePrice on '09/15/2007') As PreviousPrice,
(QuoteDate on '09/30/2007') As RecentDate, (ClosePrice on '09/302007') As RecentPrice
WHERE PrimnaryKey = Value

What are the expressions for PreviousDate, etc.?


Question by:Dovberman
    LVL 18

    Expert Comment

    by:Jinesh Kamdar
    >> QuoteDate on '09/15/2007'
    >> QuoteDate on '09/30/2007'

    Which date-column do you wish to compare for '09/15/2007' and '09/30/2007'
    LVL 25

    Expert Comment

    ther is no problem with a Join to the same table

    select A.Date, as previousdate
    from MyPrice as A
    inner join MyPrice as B
    on (A.product= B.productID  
       and = (select  max( from myprice C
                             where c.productID = a.productId and <

    The inner join will only give you result when there is a previous price

    Author Comment

    I wish to compare the price on two different dates for the same primary key(SyhmbolID).

    I tried this:

    SELECT SymbolID, MarketID,
    (Select ClosePrice WHERE QuoteDate='09/13/2007' AND SymbolID=24986 ) As PreviousPrice,
    (Select ClosePrice WHERE QuoteDate='09/26/2007' AND SymbolID=24986 ) As RecentPrice
    FROM DayQuote WHERE SymbolID=24986

    32 rows returned. PreviousPrice and RecentPrice are returned as NULL
    LVL 25

    Accepted Solution

    I gave a general example because it's not completly clear what you want with the dates.

    Attention! When making it a left join (previous doesn't have to exists) you must be carefull what to put in a where-clause.

    Author Comment

    "not completly clear what you want with the dates"

    I know the dates.

    I need the price on those two dates.


    Author Comment

    I should have made it clear that I needed the price on 2 different dates.

    Ths self join worked.

    select a.ClosePrice As RecentPrice, b.ClosePrice as PreviousPrice,
    ((a.ClosePrice-b.ClosePrice)/b.ClosePrice) * 100 As PctDiff  
    from DayQuote as a
    inner join DayQuote as b
    on (a.SymbolID= b.SymbolID)
    WHERE b.SymbolID=24986 AND a.QuoteDate='09/26/2007'
    AND b.QuoteDate='09/13/2007'


    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.

    Join & Write a Comment

    As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
    Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    728 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

    17 Experts available now in Live!

    Get 1:1 Help Now