Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

UNION Query

Participant ,
Nov 15, 2013 Nov 15, 2013

I have a union query like this that gets info from 2 different tables

Like this

  <cfquery name="getpeople"   datasource="#request.dsn#"    >

SELECT  ID,CityName,  SUM(people) AS sump FROM table 1

GROUP by ID,CityName

UNION

  SELECT  ID,CityName,  SUM(people) AS sump FROM table 2

GROUP by ID,CityName

ORDER BY SUM(people)

</cfquery>

So what I want is the sums from both tables but what I get is the sums of each table.

For example I get

ID-CityName-sump

1- New York -3

1-New York- 4

2-Chicago -5

2-Chicago -6

What I want is
ID-CityName-sump

1-New York- 7

2-Chicago -11

Do I need to do another query after the union query?

TOPICS
Database access
688
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Nov 24, 2013 Nov 24, 2013
LATEST

Something like this?

SELECT  ID, CityName, SUM(people) AS sump

FROM (SELECT  ID, CityName, people

FROM table 1

UNION

SELECT  ID, CityName, people

FROM table 2)

GROUP by ID, CityName

ORDER BY SUM(people)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources