简体   繁体   English

COUNT()函数和带有多列DISTINCT的SELECT

[英]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. 因此,例如,由于重复了“ P”和“ T”,因此计数应返回6。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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