SQL - UNION quiries together - help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    SQL - UNION quiries together - help

    SQL Query Help

    Keywords: ROW_NUMBER and UNION

    I have an SQL query which consists of 3 separate queries which I need to join;

    Query 1: SELECT SUM(amount) amount, SUM(net_amount) net_amount
    FROM table1
    WHERE condition1

    Query 2: This query only selects the first row of results hence uses rownum = 1
    SELECT amount, net_amount
    FROM table 1
    WHERE rownum = 1


    I need to SUM both query 1 and 2 together so these are returned in a single row

    I then also need to UNION this result onto this 3rd remaining query;

    Query 3: This is the more complex query which is causing me problems with my join. This requires me to only select remaining rows (ie rownum > 1)

    SELECT amount, net_amount
    FROM(
    SELECT ROW_NUMBER() OVER (ORDER BY date) as LineNum, amount, net_amount
    FROM table 1
    WHERE linenum > 1


    So basically query 1 + 2 is summed together into 1 row and the remaining rows display query 3. I have tried various methods of doing this by a SELECT sql but with no luck.
    When I try to simply UNION because of the ROW_NUMBER() section I get a message saying SQL not properly ended. Please any advice would be appreciated.

    #2
    put the queries in brackets eg.
    Code:
    $query=mysql_query("(SELECT * FROM WHERE name='razzbee')
    
    UNION ALL
    
    (SELECT * FROM animal WHERE color='green') LIMIT 20;")or die(mysql_error());
    u can also use join but the table must have special interelations

    Comment

    Working...
    X