简体   繁体   English

SQL计数列和列表值计数

[英]SQL count column and list values counted

I am trying to create a query that will output a count of a column and then list the names it counted: 我正在尝试创建一个查询,该查询将输出列的计数,然后列出其计数的名称:

Table 1: license_granted 表1:license_granted

users      checkout  
Bob        2014-11-18 01:00:00  
Steve      2014-11-18 01:00:00
Bob        2014-11-18 01:30:00
Chris      2014-11-18 01:30:00

I am able to do my count and get this output: 我能够计算并获得以下输出:

checkout_time            ctUsers  
2014-11-18 01:00:00         2
2014-11-18 01:30:00         2

SELECT COUNT(DISTINCT users) AS ctUsers, checkout
FROM license_granted
GROUP BY checkout 

What I would like to get is: 我想得到的是:

checkout_time            ctUsers     userlist
2014-11-18 01:00:00         2        Bob,Steve
2014-11-18 01:30:00         2        Bob,Chris

Can this be done in the query? 可以在查询中完成吗?

Thanks 谢谢

UPDATE 11/20 Okay I dug a little further and found a solution that gives me the checkout time and the userlist. UPDATE 11/20好吧,我进一步挖掘了一下,发现了一个可以给我结帐时间和用户列表的解决方案。 Now I am trying to figure out how to count the elements in the userlist: 现在,我试图弄清楚如何计算用户列表中的元素:

SELECT a.ctDate, SUBSTRING(d.users,1, LEN(d.users) - 1) usersList, count(d.users) AS ctUsers
FROM  (
   SELECT DISTINCT convert(varchar(10), deniedTime, 126) AS ctDate
    FROM hyperworks_checkouts
    ) a
    CROSS APPLY
    (
        SELECT DISTINCT [username] + ', ' 
        FROM hyperworks_checkouts AS B 
        WHERE A.ctdate = convert(varchar(10), deniedTime, 126)
        FOR XML PATH('')
    ) d (users)

The output now looks like this: 现在的输出如下所示:

ctDate        usersList
2014-01-15    Bob
2014-01-16    Steve,Bob
2014-01-17    Mike,Chris,Jerry

If I try adding COUNT(d.users) in the Select I get a count of 1 each time because there is one list. 如果我尝试在“选择”中添加COUNT(d.users),则每次都会得到1的计数,因为只有一个列表。 How do I count the users in d.users? 如何计算d.users中的用户?

Thanks 谢谢

I found a solution. 我找到了解决方案。 After looking at a few postings I was able to piece together the solution I needed: 看了几篇文章后,我得以整理出所需的解决方案:

SELECT a.ctDate, SUBSTRING(d.users,1, LEN(d.users) - 1) usersList, 
    (select len(d.users) - len(replace(d.users, ',', ''))) AS ctUsers
FROM  (SELECT DISTINCT convert(varchar(10), deniedTime, 126) AS ctDate
    FROM hyperworks_denials
    ) a
    CROSS APPLY
    (
        SELECT DISTINCT [username] + ', ' 
        FROM hyperworks_denials AS B 
        WHERE A.ctdate = convert(varchar(10), deniedTime, 126)
        FOR XML PATH('')
    ) d (users) 

Thanks everyone that gave suggestions 谢谢大家的建议

I think the GROUP_CONCAT function is what you are looking for. 我认为GROUP_CONCAT函数是您要寻找的。

So for your example, try: 因此,对于您的示例,请尝试:

SELECT COUNT(DISTINCT users) AS ctUsers, checkout, GROUP_CONCAT(DISTINCT users)
FROM license_granted
GROUP BY checkout

You can also define a custom separator for the GROUP_CONCAT values by adding a SEPARATOR clause inside the GROUP_CONCAT query, ie. 您还可以通过在GROUP_CONCAT查询内添加SEPARATOR子句来为GROUP_CONCAT值定义自定义分隔符。 GROUP_CONCAT(DISTINCT users SEPARATOR ' '

Try following query 尝试以下查询

SELECT COUNT(DISTINCT users) AS ctUsers, checkout,to_char(wm_concat(users)) Name
FROM license_granted
GROUP BY checkout 

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

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