简体   繁体   English

如何从 sql 中的表行记录中获取唯一记录数

[英]How to get count of Unique records from table row record in sql

Simple SQL question, I searched but can't seem to find the answer already asked.简单的 SQL 问题,我搜索但似乎找不到已经问过的答案。

I have SQL table with unique numbers in the same row and I would like to get a count of unique records in the same row.我有 SQL 表在同一行中具有唯一编号,我想获得同一行中唯一记录的计数。 I will use it as a PHP variable later.稍后我会将它用作 PHP 变量。

Please, somebody, help me to get the count of unique records.请有人帮我计算唯一记录的数量。

Table Image表格图像

Expected result is预期结果是
Total count of 1 = 6总数 1 = 6
Total count of 4 = 2总数 4 = 2
Total count of 5 = 2总数 5 = 2
Etc.. ETC..

I'm sure this is a duplicate but I can't lay my hands on it so.. Unpivot using union then group by我确定这是重复的,但我不能动手,所以.. Unpivot 使用 union 然后 group by

with cte as
(select id,'q1' as question,question1 from <table>
union all
select id,'q2' as question,question2 from <table>
union all
select id,'q3' as question,question3 from <table>
....
)
select id,question,count(*)
from cte 
group by id,question;

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

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