简体   繁体   English

对每个ID的每一行计数一次,并获得计数SQL Select查询

[英]Counting each row once for each id and getting the count SQL Select query

So, I have a series of ids in a table with attributes cid, wid, did, where entries in this table can be null, but if they are not null then the entries along a row will be equal. 因此,我在具有属性cid,wid,did的表中具有一系列ID,其中该表中的条目可以为null,但是如果它们不为null,则行中的条目将相等。 This corresponds to the ids of members of a film crew, writers and directors ids. 这对应于电影摄制组成员的ID,作家和导演的ID。

What I'm looking to do is to choose those ids who have worked on 3 or more films. 我想要做的是选择曾拍摄3部或以上影片的ID。 Thinking about it as a for loop, I'm imagining I have a dictionary with the key as the id, and I want to iterate through each row in the array, adding one to whatever id it is in the row's dictionary, and then I can just use a HAVING clause to select out those with COUNT >= 3. 考虑它是一个for循环,我想象我有一个字典,其键为id,并且我想遍历数组的每一行,在该行的字典的id中添加一个,然后我可以只使用HAVING子句选择COUNT> = 3的子句。

My major problem is that I don't want to count the id more than once even if it appears multiple times in a row. 我的主要问题是,即使ID连续出现多次,我也不想重复计算ID。

Here is a sample of the data: 这是数据示例:

   cid    |   did    |   wid
----------+----------+----------
 00000027 |          |
 00000209 |          |
 00000205 |          |
 00000206 |          |
 00000207 |          |
 00000208 |          |
 00001140 |          |
 00000306 |          |
 00000307 |          |
 00000325 |          |
 00000349 |          |
 00001077 |          |
 00001078 |          |
 00001079 | 00001079 | 00001079
 00001079 | 00001079 | 00001079
 00001082 |          |
 00001083 |          |
 00001084 |          |
 00001085 |          |
 00001091 |          |
 00001092 |          |
 00001093 |          |
 00001094 |          |
 00001095 |          |
 00001096 | 00001096 |
 00001101 |          |
 00001102 |          |
 00001104 | 00001104 | 00001104
 00001104 | 00001104 | 00001104
 00001104 | 00001104 | 00001104
 00001314 |          |
 00001315 |          |
 00001316 |          |
 00001321 |          |
 00001322 |          |
 00001323 |          |
 00001328 |          |
 00001329 |          |
 00001330 |          |
 00001335 |          |
 00001336 |          |
 00001337 |          |
 00001345 |          |
 00001345 |          |
          |          | 00001344
          |          | 00000461
          |          | 00001344
          |          | 00001332

SELECT cid, count(*) "nrOfFilms"
  FROM people
 GROUP BY cid
HAVING nrOfFilms >= 3

You may try this, first you need to combine all of the columns into one on basis of any non value, as per your statement. 您可以尝试一下,首先,根据您的陈述,需要根据任何非值将所有列合并为一个。 After that you can easily apply group by and having to get your desired result. 之后,您可以轻松地应用group byhaving必须获得所需的结果。

; with cte as (
select case when isnull(cid,0)=0 
            then case when isnull(did,0)=0 
                 then isnull(wid,0) 
                 else isnull(did,0) end
            else isnull(cid,0) end as Uid  from table
)
select distinct uid, count(1) as CountId from cte 
group by uid having count(uid)>2

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

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