mysql 如何使用数值范围对行进行分组sql [已关闭]

baubqpgj  于 2023-03-11  发布在  Mysql
关注(0)|答案(1)|浏览(119)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
3小时前关门了。
Improve this question
有人请帮助我新SQL我想有一个查询,显示类似output
根据该表table
我有列,我保存的利率1最低5最高我想返回什么利率和计数相同的利率在每一列
我尝试使用group by

xesrikrc

xesrikrc1#

这在SQL中并不常见,我设法解决了它,但查询很长。下面是SQL查询:

SELECT ifnull(tab1.q1, "") as q1, ifnull(tab1.q1Count, "") as q1Count, 
ifnull(tab2.q2, "") as q2, ifnull(tab2.q2Count, "") as q2Count, 
ifnull(tab3.q3, "") as q3, ifnull(tab3.q3Count, "") as q3Count,
ifnull(tab4.q4, "") as q4, ifnull(tab4.q4Count, "") as q4Count, 
ifnull(tab5.q5,"") as q5, ifnull(tab5.q5Count,"") as q5Count,
ifnull(tab6.q6,"") as q6, ifnull(tab6.q6Count, "") as q6Count,
ifnull(tab7.q7, "") as q7, ifnull(tab7.q7Count, "") as q7Count,
ifnull(tab8.q8,"") as q8,  ifnull(tab8.q8Count, "") as q8Count
FROM (
  SELECT id, q2, COUNT(q2) as q2Count 
  from question_table GROUP BY(q2))as tab2 
LEFT OUTER JOIN (
  SELECT id, q1, COUNT(q1) as q1Count 
  from question_table 
GROUP BY(q1))as tab1 on tab1.id = tab2.id
LEFT OUTER JOIN (
  SELECT id, q3, COUNT(q3) as q3Count 
  from question_table 
GROUP BY(q3))as tab3 on tab3.id = tab2.id
LEFT OUTER JOIN (
  SELECT id, q4, COUNT(q4) as q4Count 
  from question_table 
GROUP BY(q4))as tab4 on tab4.id = tab2.id
LEFT OUTER JOIN (
  SELECT id, q5, COUNT(q5) as q5Count 
  from question_table 
GROUP BY(q5))as tab5 on tab5.id = tab2.id
LEFT OUTER JOIN (
  SELECT id, q6, COUNT(q6) as q6Count 
  from question_table 
GROUP BY(q6))as tab6 on tab6.id = tab2.id
LEFT OUTER JOIN (
  SELECT id, q7, COUNT(q7) as q7Count 
  from question_table 
GROUP BY(q7))as tab7 on tab7.id = tab2.id
LEFT OUTER JOIN (
  SELECT id, q8, COUNT(q8) as q8Count 
  from question_table 
GROUP BY(q8))as tab8 on tab8.id = tab2.id

这是我得到的结果:
第一节第一节第一节第一节第一次
使用这个查询我得到了你想要的结果,但是排序是不一样的,我不知道如何用普通SQL实现,它可以在SQL Server中的TSQL中更容易地完成。这是我能想到的最好的。
我希望这能帮助你,如果有帮助,请给我反馈。

相关问题