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

  • Home
  • SEARCH
  • 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 3430256
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:10:58+00:00 2026-05-18T07:10:58+00:00

We have the below SQL that generates a report from the Audit Tables family_audit

  • 0

We have the below SQL that generates a report from the Audit Tables family_audit and family_address_audit.

(SELECT fam.familyserialno, fam.familyname, addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
  FROM family_audit fam
       INNER JOIN family_address_audit addr USING(txid, family_id)
WHERE (fam.node_id = 8) AND (fam.familyserialno >=1 and fam.familyserialno <=5)
)
UNION
(SELECT fam.familyserialno, fam.familyname,
        addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
   FROM family_audit fam
        FULL OUTER JOIN family_address_audit addr USING(txid, family_id)
  WHERE ((fam.familyserialno IS NULL and addr.family_id in 
             (select family_id 
                from family 
               where familyserialno >=1 and familyserialno <=5 and node_id =8)) 
         OR ((addr.family_id IS NULL) and ((fam.familyserialno >=1 and fam.familyserialno <=5) and fam.node_id=8) ) )
)
ORDER BY add_date DESC;

A typical output from the “above” SQL is as below

 familyserialno | familyname | housenumber |  address1  | op |  usr  |        add_date         | rolename
----------------+------------+-------------+------------+----+-------+-------------------------+----------
              3 | thirda     |             |            | U  | admin | 2010-11-24 15:29:34.312 | admin
                |            | 34324       | sdfdsfdsf  | U  | admin | 2010-11-24 15:28:42.314 | admin
              3 | third      |             |            | I  | admin | 2010-11-24 15:28:34.576 | admin
                |            | 333         | sdfdsf     | U  | admin | 2010-11-24 15:18:57.129 | admin
              2 | aSecond    |             |            | I  | admin | 2010-11-24 14:58:15.477 | admin
              1 | anjanFirst | anjanFirst  | anjanFirst | I  | admin | 2010-11-24 14:12:35.477 | admin

======================

I want to group the output by fam.familyserialno. So, I modified the above SQL as below; However, that results in an error (at the end)

SELECT * from 
((SELECT fam.familyserialno, fam.familyname, addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
  FROM family_audit fam
       INNER JOIN family_address_audit addr USING(txid, family_id)
WHERE (fam.node_id = 8) AND (fam.familyserialno >=1 and fam.familyserialno <=5)
)
UNION
(SELECT fam.familyserialno, fam.familyname,
        addr.housenumber, addr.address1,
        COALESCE(fam.operation, addr.operation) op,
        COALESCE(fam.username, addr.username) usr,
        COALESCE(fam.adddate, addr.adddate) add_date,
        (SELECT r.userrolename 
           FROM userrole_master r, user_master u 
          WHERE u.loginid=COALESCE(fam.username, addr.username) AND r.userrole_id=u.userrole_id) rolename
   FROM family_audit fam
        FULL OUTER JOIN family_address_audit addr USING(txid, family_id)
  WHERE ((fam.familyserialno IS NULL and addr.family_id in 
             (select family_id 
                from family 
               where familyserialno >=1 and familyserialno <=5 and node_id =8)) 
         OR ((addr.family_id IS NULL) and ((fam.familyserialno >=1 and fam.familyserialno <=5) and fam.node_id=8) ) )
))
GROUP BY 1 
ORDER BY 7 DESC;

ERROR: subquery in FROM must have an alias
LINE 2: ((SELECT fam.familyserialno, fam.familyname, addr.housenumbe…
^
HINT: For example, FROM (SELECT …) [AS] foo.

How do I fix this ? We really need all the rows to be grouped by fam.familyserialno.

Thank you very much,

P.S : We are using Postgres 8.4.4.

  • 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-18T07:10:58+00:00Added an answer on May 18, 2026 at 7:10 am

    You need to alias the subquery, as in:

     SELECT * FROM ((SELECT ...) UNION (SELECT ...)) AS foo GROUP BY 1 ORDER BY 7 DESC;
    

    Here foo is an alias for the subquery.

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

Sidebar

Related Questions

I have the below SQL which works just fine: SELECT Message, CreateDate, AccountId, AlertTypeId
I have sql query like shown below the sql server says that their is
SQL Server (2005/2008) Each of the below statements have the same result. Does anyone
I have below a list of text, it is from a popular online game
I am trying to learn more about regular expressions I have one below that
I have a very complex Linq to SQL query that returns a result set
I am trying to make Doctrine generate SQL from a set of models that
I have the below query, which basically it retrieves the 5 top most books
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
The code I have pasted below is meant to display images on the middle

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.