简体   繁体   English

如何获取特定行的列值的计数?

[英]how to get the count of the column value of a particular row?

I have a table below. 我有一张桌子。

在此输入图像描述

how to get the count of the column value whose value is 'M' in a particular row? 如何获取特定行中值为“M”的列值的计数?

ex: If i give the condition, where shape_name='Rectangle' it will return the count of the column whose value is = 'M'. 例如:如果我给出条件,其中shape_name ='Rectangle',它将返回值为''M'的列的计数。 Result: count is 2 结果:计数为2

If True L-Left=> Result: count is 4 How to get the answer.? 如果是真L左=>结果:计数是4如何得到答案。? plz help me to fix it. 请帮助我解决它。

A solution with CASE WHEN is like this: CASE WHEN的解决方案是这样的:

SELECT shape_name,
      CASE WHEN a = 'm' THEN 1 ELSE 0 END +
      CASE WHEN b = 'm' THEN 1 ELSE 0 END +
      CASE WHEN c = 'm' THEN 1 ELSE 0 END as mcount
  FROM t1

SQLFiddle SQLFiddle

Just adjust the SQL to your table structure - you will need to find more CASE WHEN statements for each column 只需将SQL调整为表结构 - 您需要为每列找到更多CASE WHEN语句

非常古老的解决方案 - 获取所有列,遍历所有列,并计算M出现次数。

  select SHAPE_NAME ,
            DECODE(COLUMN_NAME1,'M',1,0)+
            DECODE(COLUMN_NAME2,'M',1,0)+
            DECODE(COLUMN_NAME3,'M',1,0)+
            DECODE(COLUMN_NAME4,'M',1,0) your_count
 from  YOUR_TABLE where shape_name='Rectangle' ;

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

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