1

Trying to get an slightly more complex sql statement structured but can't seem to get the syntax right. Trying to select counts, of various columns, in two different tables.

SELECT 
     SUM(ColumninTable1), 
     SUM(Column2inTable1), 
     COUNT(DISTINCT(Column3inTable1)) 
FROM TABLE1

This works, however I can't for the life of me figure out how to add in a COUNT(DISTINCT(Column1inTable2) FROM TABLE2 with what syntax.

6
  • I am not sure what you want to achieve here. Commented Feb 25, 2014 at 19:45
  • Just trying to run a report. However I wish to do one more count(distinct) from another table. SELECT SUM(IMG_RAM), SUM(IMG_CORE_COUNT), COUNT(DISTINCT(IMG_NAME)) FROM IMAGES Commented Feb 25, 2014 at 20:35
  • Everything is possible, but you usually only query two tables if they have a logical relationship, and that relationship might (need to) influence the result of the count. Commented Feb 25, 2014 at 20:40
  • Ok. They have no logical relationship, for this report, so two queries it is... Commented Feb 25, 2014 at 20:41
  • It is still possible with a single query. Commented Feb 25, 2014 at 20:42

2 Answers 2

1

There are several solutions you can take:

Disjunct FULL OUTER JOIN

SELECT 
    SUM(MYTABLE.ID) as theSum,
    COUNT(DISTINCT MYTABLE.SOMEVALUE) as theCount,
    COUNT(DISTINCT MYOTHERTABLE.SOMEOTHERVALUE) as theOtherCount
FROM MYTABLE
FULL OUTER JOIN MYOTHERTABLE ON 1=0

UNION two queries and leave the column for the other table null

SELECT 
    MAX(theSum) as theSum, 
    MAX(theCount) as theCount, 
    MAX(theOtherCount) AS theOtherCount
FROM (
    SELECT
        SUM(ID) as theSum,
        COUNT(DISTINCT SOMEVALUE) as theCount,
        NULL as theOtherCount
    FROM MYTABLE
    UNION ALL
    SELECT
        NULL,
        NULL,
        COUNT(DISTINCT SOMEOTHERVALUE)
    FROM MYOTHERTABLE
)

Query 'with a query per column' against a single record table (eg RDB$DATABASE)

SELECT
    (SELECT SUM(ID) FROM MYTABLE) as theSum,
    (SELECT COUNT(DISTINCT SOMEVALUE) FROM MYTABLE) as theCount,
    (SELECT COUNT(DISTINCT SOMEOTHERVALUE) FROM MYOTHERTABLE) as theOtherCount
FROM RDB$DATABASE

CTE per table + cross join

WITH query1 AS (
    SELECT
        SUM(ID) as theSum,
        COUNT(DISTINCT SOMEVALUE) as theCount
    FROM MYTABLE
),
query2 AS (
    SELECT
        COUNT(DISTINCT SOMEOTHERVALUE) as theOtherCount
    FROM MYOTHERTABLE
)
SELECT 
    query1.theSum,
    query1.theCount,
    query2.theOtherCount
FROM query1
CROSS JOIN query2

There are probably some more solutions. You might want to ask yourself if it is worth the effort of coming up with a (convoluted, hard to understand) single query to get this data were two queries are sufficient, easier to understand and in the case of large datasets: two separate queries might be faster.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. As you said, 2 queries seems to be the better approach.
0

In this case all "count" would return the same value.

Try to do the same using sub queries:

  Select 
   (Select count (*) from Table1), 
   (Select count (*) from table2) 
  from Table3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.