选择并计算所有条目,并根据这些条目进行分组。

huangapple go评论68阅读模式
英文:

Select and count all entries and group them according to the entries

问题

ID ClientID Responsiveness1 Responsiveness2 Reliability1
1024 3511 5 4 3
1025 4571 5 3 5
1026 3827 4 5 4
1027 7652 1 1 1
1028 7778 2 2 2
1029 7612 1 1 2

我尝试了手动计数(通过列名选择 Count(Responsiveness1) from table where Responsiveness1 = '5')以及按范围(5,4,3,2,1)计数,然后得到了输出。但不幸的是,我必须按列和范围计数,然后在我的表中显示出来。

有没有更简单、更高效的方法可以得到如下输出:

等级 Responsiveness1 Responsiveness2 Reliability1
5 2 1 1
4 1 1 1
3 0 1 1
2 1 1 2
1 2 2 1
英文:

I have the following table and i am supposed to count the entries(5,4,3,2,1) per column name

ID ClientID Responsiveness1 Responsiveness2 Reliability1
1024 3511 5 4 3
1025 4571 5 3 5
1026 3827 4 5 4
1027 7652 1 1 1
1028 7778 2 2 2
1029 7612 1 1 2

I tried counting it manually (Select Count(Responsiveness1) from table where Responsiveness1 = '5') by column name and by the range (5,4,3,2,1) and got the output. Unfortunately though, I have to count it per column and per range then display it on my table.

Is there an easier and more efficient way to do it to have an output of:

Rate Responsiveness1 Responsiveness2 Reliability1
5 2 1 1
4 1 1 1
3 0 1 1
2 1 1 2
1 2 2 1

Instead of having to count it by column name and then inserting it in the output table?

答案1

得分: 2

以下是您要翻译的内容:

You can join your table with a table of rates and count entries conditionally

with rates(n) as (
  values row(1),row(2),row(3),row(4),row(5)
)
select r.n rate,
   sum(Responsiveness1 = r.n) Responsiveness1,
   sum(Responsiveness2 = r.n) Responsiveness2,
   sum(Reliability1 = r.n) Reliability1
from rates r
left join mytable on Responsiveness1 = r.n or Responsiveness2 = r.n or Reliability1  = r.n 
group by r.n
order by r.n desc

请注意,这是SQL查询语句,用于将您的表与一个包含不同费率的表进行连接,并有条件地计算条目。

英文:

You can join your table with a table of rates and count entries conditionally

with rates(n) as (
  values row(1),row(2),row(3),row(4),row(5)
)
select r.n rate,
   sum(Responsiveness1 = r.n) Responsiveness1,
   sum(Responsiveness2 = r.n) Responsiveness2,
   sum(Reliability1 = r.n) Reliability1
from rates r
left join mytable on Responsiveness1 = r.n or Responsiveness2 = r.n or Reliability1  = r.n 
group by r.n
order by r.n desc

db<>fiddle

答案2

得分: 1

以下是您请求的内容的翻译:

You may use "full join emulation" for MySql thru "union all" and then "pivot" table. This case is usable for an indefinite set of "rate" values.

您可以使用MySQL中的“full join emulation”通过“union all”然后“pivot”表。这种情况适用于一个不确定数量的“rate”值。

create table Rates (ID int, ClientID int, Responsiveness1 int, Responsiveness2 int, Reliability1 int);
insert into Rates values(1024, 3511, 5, 4, 3);
insert into Rates values(1025, 4571, 5, 3, 5);
insert into Rates values(1026, 3827, 4, 5, 4);
insert into Rates values(1027, 7652, 1, 1, 1);
insert into Rates values(1028, 7778, 2, 2, 2);
insert into Rates values(1029, 7612, 1, 1, 2);

select n rate
      ,max(case when code="Responsiveness1" then qty else 0 end) Responsiveness1
      ,max(case when code="Responsiveness2" then qty else 0 end) Responsiveness2
      ,max(case when code="Reliability1" then qty else 0 end) Reliability1
from ( 
  select "Responsiveness1" code, Responsiveness1 n, count(*) qty
      from Rates r group by Responsiveness1
  union all
  select "Responsiveness2" code, Responsiveness2 n, count(*) qty
      from Rates r group by Responsiveness2
  union all
  select "Reliability1" code, Reliability1 n, count(*) qty
      from Rates r group by Reliability1
  ) u
group by n
order by n desc
rate Responsiveness1 Responsiveness2 Reliability1
5 2 1 1
4 1 1 1
3 0 1 1
2 1 1 2
1 2 2 1

I'll add Pivot result example

我将添加Pivot结果示例

select code
      ,max(case when n=1 then qty else 0 end) rate1
      ,max(case when n=2 then qty else 0 end) rate2
      ,max(case when n=3 then qty else 0 end) rate3
      ,max(case when n=4 then qty else 0 end) rate4
      ,max(case when n=5 then qty else 0 end) rate5
from ( 
  select "Responsiveness1" as code, Responsiveness1 n, count(*) qty
      from Rates r group by Responsiveness1
  union all
  select "Responsiveness2" as code, Responsiveness2 n, count(*) qty
      from Rates r group by Responsiveness2
  union all
  select "Reliability1" as code, Reliability1 n, count(*) qty
      from Rates r group by Reliability1
  ) u
group by code

Table

code rate1 rate2 rate3 rate4 rate5
Responsiveness1 2 1 0 1 2
Responsiveness2 2 1 1 1 1
Reliability1 1 2 1 1 1

What case is suitable in a specific case - customer request or developer preferences:)

在特定情况下,哪种情况适用 - 客户要求还是开发人员偏好:)

英文:

You may use full join emulation for MySql thru union all and then &quot;pivot&quot; table. This case is usable for an indefinite set of rate values.

create table Rates (ID int,ClientID int,Responsiveness1 int,Responsiveness2 int,Reliability1 int);
insert into Rates values(1024, 3511, 5, 4, 3);
insert into Rates values(1025, 4571, 5, 3, 5);
insert into Rates values(1026, 3827, 4, 5, 4);
insert into Rates values(1027, 7652, 1, 1, 1);
insert into Rates values(1028, 7778, 2, 2, 2);
insert into Rates values(1029, 7612, 1, 1, 2);

select n rate
      ,max(case when code=&quot;Responsiveness1&quot; then qty else 0 end)Responsiveness1
      ,max(case when code=&quot;Responsiveness2&quot; then qty else 0 end)Responsiveness2
      ,max(case when code=&quot;Reliability1&quot; then qty else 0 end)Reliability1
from ( 
  select &quot;Responsiveness1&quot; code,Responsiveness1 n,count(*) qty
      from Rates r group by Responsiveness1
  union all
  select &quot;Responsiveness2&quot; code,Responsiveness2 n,count(*) qty
      from Rates r group by Responsiveness2
  union all
  select &quot;Reliability1&quot; code,Reliability1 n,count(*) qty
      from Rates r group by Reliability1
  )u
group by n
order by n desc
rate Responsiveness1 Responsiveness2 Reliability1
5 2 1 1
4 1 1 1
3 0 1 1
2 1 1 2
1 2 2 1

I'll add Pivot result example

select code
      ,max(case when n=1 then qty else 0 end) rate1
      ,max(case when n=2 then qty else 0 end) rate2
      ,max(case when n=3 then qty else 0 end) rate3
      ,max(case when n=4 then qty else 0 end) rate4
      ,max(case when n=5 then qty else 0 end) rate5
from ( 
  select &quot;Responsiveness1&quot; as code,Responsiveness1 n,count(*) qty
      from Rates r group by Responsiveness1
  union all
  select &quot;Responsiveness2&quot; as code,Responsiveness2 n,count(*) qty
      from Rates r group by Responsiveness2
  union all
  select &quot;Reliability1&quot; as code,Reliability1 n,count(*) qty
      from Rates r group by Reliability1
  )u
group by code

Table

code rate1 rate2 rate3 rate4 rate5
Responsiveness1 2 1 0 1 2
Responsiveness2 2 1 1 1 1
Reliability1 1 2 1 1 1

What case is suitable in a specific case - customer request or developer preferences:)

huangapple
  • 本文由 发表于 2023年2月27日 16:46:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定