简体   繁体   中英

COUNT() function and SELECT with DISTINCT on multiple columns

Hoping someone can help me figure why I'm getting a syntax error here. I'm counting all rows that have two distinct column values.

select count (*) as 'Distinct' from (
    select distinct p.IDD, p.num
    from PERF1 p
    inner join MAST1 m
    on (m.id_table = p.id_table and m.SOURCE_TABLE = p.SOURCE_TABLE)
    where m.DATE > '2012-12-31' )

I'm getting an error on my last close bracket but can't for the life of me figure what I'm doing wrong here. ...............................

Kind of a huge "edit" but I'm a bit stuck. I thought that this query would count the number of distinct rows (for both columns) across this table. So for instance the count should return 6 since "P" and "T" are repeated. I thought I had it with this:

select count (*) as 'Distinct' from (
select distinct p.IDD, p.co
from PERF1 p
inner join MAST1 m
on (m.id_table = p.id_table and m.SOURCE = p.SOURCE)
where m.DATE > '2012-12-31' ) TempTable

but I'm getting a much higher number than I think I should so I'm hoping that my query is incorrect.

+------+------+
| IDD  | CO   |
+------+------+
| 11   | P    |
| 12   | P    |
| 13   | T    |
| 14   | T    |
| 15   | R    |
| 16   | S    |
| 17   | U    |
| 18   | K    |

You need to add a alias name for the subquery

select count (*) as 'Distinct' from (
    select distinct p.IDD, p.num
    from PERF1 p
    inner join MAST1 m
    on (m.id_table = p.id_table and m.SOURCE_TABLE = p.SOURCE_TABLE)
    where m.DATE > '2012-12-31' ) TempTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM