3

I have two tables that are the same in their structure, but all I really want out of them is a distinct count from a single row from each into a multi-column result set.

I keep getting syntax errors, but so far haven't been able to get something that delivers the data I want, and makes it through the parser. I'm trying to figure out if this is a SQL problem (possible given that I'm using a website's implementation, rather than native MySQl/SQL/Oracle) or a me problem (much more likely).

So what I want is two unrelated (and un-primary-keyed) tables to return a COUNT(DISTINCT column) into a single result. I have tried a couple of different approaches:

select 1,2 FROM
    (SELECT COUNT(DISTINCT col1) as 1 from table1),
    (SELECT COUNT(DISTINCT col2) as 2 from table2)

SELECT *
FROM (
    SELECT COUNT(DISTINCT col1) AS 1
    FROM table1 
    )
CROSS JOIN (
    SELECT COUNT(DISTINCT col2) AS 2
    FROM table2
    )

I have also messed around with 4-5 different uses of union || union all to no avail. Curious what thoughts someone more schooled in the arts of SQL might have. Thanks.

2
  • Name your columns with words, not number, though don't recall if a number is not allowed as an alias. Try ... as col1... and ... as col2 ... Commented Apr 8, 2017 at 21:48
  • I guess you cannot use an integer literal as column alias Commented Apr 8, 2017 at 21:54

1 Answer 1

4

Depending on the database RDBM you are, a few things could change in syntax. In the ANSI Sql definition your query should be:

select col1, col2 FROM
    (SELECT COUNT(DISTINCT col1) as col1 from table1) as tab1,
    (SELECT COUNT(DISTINCT col2) as col22 from table2) as tab2

You have to add alias for all sub queries. Also name your columns with words, not number, it is easier to understand. Though I don't recall if a number is not allowed as an alias in SQL ANSI.

Without aliases for the subqueries you can use like this:

-- For MySql, PostgreSql, SQL Server (not sure though)
select (SELECT COUNT(DISTINCT col1)
          from table1) as col1,
       (SELECT COUNT(DISTINCT col2) as col22 
          from table2) as col2

-- For Oracle
select (SELECT COUNT(DISTINCT col1)
          from table1) as col1,
       (SELECT COUNT(DISTINCT col2) as col22 
          from table2) as col2
  from dual

-- For DB2
select (SELECT COUNT(DISTINCT col1)
          from table1) as col1,
       (SELECT COUNT(DISTINCT col2) as col22 
          from table2) as col2
  from sysibm.sysdummy1

Side note: you can use a number as an alias if you surround it with double quotes " (this is SQL ANSI and will work everywhere) like this:

select "1", "2" FROM
    (SELECT COUNT(DISTINCT col1) as "1" from table1) a, --don't forget the table alias
    (SELECT COUNT(DISTINCT col2) as "2" from table2) b

Mysql Also allows you to use back ticks:

select `1`, `2` FROM
    (SELECT COUNT(DISTINCT col1) as `1` from table1) a,
    (SELECT COUNT(DISTINCT col2) as `2` from table2) b
Sign up to request clarification or add additional context in comments.

3 Comments

I was confused by the double aliases in the first solution, but it works in MSSQL
@devio That is just a renaming of a column been specified in the COUNT function. When you use a count function the database (all of then) doesn't get the same name (normally it uses the count function as name), so you name it as the column name you are counting (of course if you prefer to do so.)
This rundown was perfect for what I needed, when I moved to a real platform. Digging into the web implementation (just got access to code) it looks like sub queries aren't supported. Thanks for taking the time!

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.