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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:48:50+00:00 2026-05-18T02:48:50+00:00

Or at least I think they’re called subqueries (newbie and self-trained in SQLite). I

  • 0

Or at least I think they’re called subqueries (newbie and self-trained in SQLite). I have two SELECT statements from two tables in the same database. I would like to join these two subqueries along the columns date and symbol. The subqueries work fine separately, but when I try to JOIN I get an error (error in statement: near "JOIN": syntax error). Here’s my query string:

SELECT date, symbol, SUM(oi*contract_settle) AS oi_dollar
    FROM (SELECT date, symbol, oi, contract_settle
            FROM ann
            UNION
            SELECT date, symbol, oi, contract_settle
            FROM qtr) 
    GROUP BY date, symbol
    HAVING oi_dollar > 0
    JOIN
    (SELECT date, symbol, ret FROM crsp
    USING (date, symbol))

Thanks!

  • 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-05-18T02:48:51+00:00Added an answer on May 18, 2026 at 2:48 am

    Your JOIN clause needs to be before the GROUP BY clause. Also, I know sqlite does has a few different “optional” syntaxes for joins, but the following more standard query should work:

    SELECT a.date, a.symbol, SUM(a.oi * a.contract_settle) AS oi_dollar
    FROM (SELECT date, symbol, oi, contract_settle
            FROM ann
            UNION
            SELECT date, symbol, oi, contract_settle
            FROM qtr) a
    INNER JOIN crsp c ON a.date = c.date AND a.symbol = c.symbol
    WHERE a.oi * a.contract_settle > 0
    GROUP BY a.date, a.symbol
    

    If you know slightly more about the oi and contract_settle columns (like, that they can never both be negative), a WHERE clause of a.oi <> 0 AND a.contract_settle <> 0 might have better performance.

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

Sidebar

Related Questions

I'm comparing two DateTime objects (at least I think I am, they look slightly
I have an existing DB2 database at my job. (At least, I think it's
At least I think it's from overflow hidden. I've got a bunch of divs
Since attributes don't inherit in C# (at least I didn't think they did) -
I have two fieldsets (as below) that are inside a div. They have been
I need some math advice... Or, at least I think i do. I have
or at least that's what pingdom says, and i think it's a pretty reliable
I've just wrapped my head around monads (at least I'd like to think I
new_thing = MyTable(last_updated=datetime.datetime.now()) new_thing.save() >>>>select * from MyTable\G; last_updated: 2010-04-01 05:26:21 However, in my
At least two brilliant programmers, Linus Torvalds and Guido von Rossum, disparage the practice

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.