SQL查询以两列为基础计数一列问题 [英] Sql Query distinct count a column on base of two column Problem

查看:44
本文介绍了SQL查询以两列为基础计数一列问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,


我有一个表Table1,它的列是A,B,C,D ....等.

我需要从表1中选择select A,Count(distinct两列(B,C))*的数据
我如何获取上面的查询数据,我不知道如何在SQL Server的两个字段的基础上计算一个不同的字段值

请帮助我

dear all,


i have one table Table1 and it column are A,B,C,D....etc .

i need data like select A,Count(distinct Two column(B,C)) * from Table1

how can i get the above Query data ,i dont have any idea how count a distinct field value on base of two field in SQL Server

please help me

推荐答案


试试这个

表格结构
Hi,
Try this

Table Structure
CREATE TABLE [dbo].[TestingTable](
    [Col1] [varchar](50) ,
    [Col2] [varchar](50) ,
    [Col3] [varchar](50) ,
    [Col4] [varchar](50)
)




虚拟数据




Dummy Data

1   ABC 123 NULL
1   ABC 124 NULL
2   ABC 123 NULL
2   XYZ 123 NULL
2   ABC 123 NULL
3   XYZ 123 NULL




SQL查询




SQL Query

SELECT Col1, count(forCount)FROM
        (SELECT Col1 , convert(VARCHAR, Col2) + convert(VARCHAR, Col3) AS [forCount]
             FROM TestingTable GROUP BY Col1, Col2, Col3) tt
GROUP BY Col1




返回数据

1 2
2 2
3 1




Return Data

1 2
2 2
3 1




你让我感到困惑:),我认为这种情况可能不会发生,

情况1:如果A列相同.
在这种情况下,无法区分B和C.

情况2:如果A列不相同.
在这种情况下,如果A,B& C不同,那么不可能区分,如果所有A,B,C都相同,那么它将变成正常的不同,例如
Hi,

you make me in confusion :) , i think such scenario may not happen,

case 1 : if column A is identical.
in this case distinct of B and C is not possible.

case 2: if column A is not identical.
in this case if column A,B & C are different then distinct is not possible and if all A,B,C are same then it will become normal distinct like
select distinct A,B,C from table1



请提供一些方案,以便我们进一步考虑.

希望对您有帮助,

谢谢
-amit.



please provide some scenario so we can think more about it.

hope this will help you,

thanks
-amit.


这篇关于SQL查询以两列为基础计数一列问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆