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 7974853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:29:55+00:00 2026-06-04T08:29:55+00:00

How do you re-write this into simpler SQL: SELECT tab1.ScenarioID, tab1.TradeID, tab1.Value AS Val1,

  • 0

How do you re-write this into “simpler” SQL:

SELECT tab1.ScenarioID, 
       tab1.TradeID, 
       tab1.Value AS Val1, 
       tab2.Value, 
       AS         Val2, 
       tab2.Value - tab1.Value 
FROM   (SELECT ScenarioID, 
               TradeID, 
               FileName, 
               Value, 
               Change 
        FROM   CalypsoResults 
        WHERE  FileName = 'x' 
               AND Date = '2012-05-17') tab1 
       LEFT JOIN (SELECT ScenarioID, 
                         TradeID, 
                         FileName, 
                         Value, 
                         Change 
                  FROM   CalypsoResults 
                  WHERE  FileName = 'x' 
                         AND Date = '2012-05-18') tab2 
         ON tab1.ScenarioID = tab2.ScenarioID 
            AND tab1.ID = tab2.ID 

I tried this but I am not getting the same result:

SELECT a.ScenarioID, 
       a.ID, 
       a.Date, 
       a.Value           AS Val1, 
       b.Value           AS Val2, 
       b.Value - a.Value AS Change 
FROM   CalypsoResults a 
       LEFT JOIN CalypsoResults b 
         ON a.ScenarioID = b.ScenarioID 
            AND a.ID = b.ID 
            AND a.FileName = B.FileName 
WHERE  a.Date = '2012-05-17' 
       AND ( b.Date = '2012-05-18' 
              OR b.Date IS NULL ) 
       AND a.FileName = 'x' 

the second is missing out the instances where there is a row in a, but not in b.

  • 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-04T08:29:56+00:00Added an answer on June 4, 2026 at 8:29 am

    Put all of the predicates for the second instance of the table into the JOIN, not the WHERE.

    SELECT
        tab1.ScenarioID,
        tab1.TradeID,
        tab1.Value AS Val1,
        tab2.Value AS Val2,
        tab2.Value - tab1.Value
    FROM CalypsoResults AS tab1
        LEFT JOIN CalypsoResults AS tab2 ON tab1.ScenarioID = tab2.ScenarioID
            AND tab2.FileName = 'x'
            AND tab2.Date = '2012-05-18'
            AND tab1.ID = tab2.ID
    WHERE tab1.FileName = 'x'
        AND tab1.Date = '2012-05-17'
    

    Also, you’re joining on FileName in your second version, but not in the first.

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

Sidebar

Related Questions

I want to load this simple something into my Editor: Write:-repeat,write(hi),nl,fail. So that it
How does this program access other processes memory? How can it write into the
I wrote this query: INSERT INTO KeysTable (KeyText, Id) SELECT KeyText as BKT, KeysTable.ID
I write this function, but what's its return value. (defn read-data [file] (let [code
Say I write this: from subprocessing import Popen, STDOUT, PIPE p = Popen([myproc], stderr=STDOUT,
I read this already: The Best Way to shred XML data into SQL Server
This SQL query disgusts me. I didn't write it, but it's a massive cause
Is there simpler way to convert CSV files to SQL file than this?: BufferedReader
I know that with mysql you can write SQL statements into a .sql file
I just wrote this into my WebForms .aspx: <span id=GenerateChartButton runat=server></span> I went to

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.