SQL - UNION quiries together - help

Started on the old forum in January 2011 · view the original in the archive · old links redirect here

#rownumber #sql #union

1 reply · 427 views · started 15 years ago · 1 min read

2 participants
PAUL41 Newbie · 5 posts #1

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.

razzbee Developer · 130 posts #2

put the queries in brackets eg.

$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

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close