SQL get number of columns in a particular row having a particular value

♀尐吖头ヾ 提交于 2020-01-24 14:27:41

问题


I know that I can get the number of rows having a particular value in a particular column by making use of the COUNT(*) function, but I want to find the number of COLUMNS in a particular row that have a particular value. Any suggestions on how to do this?

I would have posted an example of what I've tried up till now, but I'm completely lost on this one...

Edit 1 - Here's some sample data and the expected result:

Table - trackbill | u1 | u1paid | u2 | u2paid | u3 | u3paid | u4 | u4paid | u5 | u5paid

So what I want to do is when I select a particular row using trackbill, I want to go through all of its columns and see which ones have the value 'false'. So if u1paid and u3paid were the only columns which had the value false, the answer returned should be 2.


回答1:


I would consider your table structure is as follows:

TABLE trackbill
( u1     NUMBER(10,2)
, u1paid VARCHAR(10)  -- contains values 'Yes' or 'No'
, u2     NUMBER(10,2)
, u2paid VARCHAR(10)  -- contains values 'Yes' or 'No'
, u3     NUMBER(10,2)
, u3paid VARCHAR(10)  -- contains values 'Yes' or 'No'
, u4     NUMBER(10,2)
, u4paid VARCHAR(30)  -- contains values 'Yes' or 'No'
, u5     NUMBER(10,2)
, u5paid VARCHAR(30)  -- contains values 'Yes' or 'No'
)

So, to check how many 'Yes's are for the uxpaid columns, we can write an SQL as:

SELECT u1, u1paid, u2, u2paid, u3, u3pai, u4, u4paid, u5, u5paid
       , CASE u1paid WHEN 'Yes' THEN 1 ELSE 0 END
       + CASE u2paid WHEN 'Yes' THEN 1 ELSE 0 END
       + CASE u3paid WHEN 'Yes' THEN 1 ELSE 0 END
       + CASE u4paid WHEN 'Yes' THEN 1 ELSE 0 END
       + CASE u5paid WHEN 'Yes' THEN 1 ELSE 0 END
       AS no_of_paid_columns
  FROM trackbill

I can't help you with SQLite as I don't know it.




回答2:


If your columns are boolean, try to simply add the columns So your count is u1paid + u2paid + u3paid + u4paid. You may have to adjust that a bit, depending on syntax, but as long as the false entries are 0, it can be a way to get to your count.

If the columns are not bool, you can still do a compare on each field that returns a bool, depending on whether it has the value you are looking for, and then add those that are true.




回答3:


The easiest way to do this is to unpivot the data. You can use a CROSS JOIN to create a virtual table:

select trackbill,
  count(*) Total
from
(
  select t.trackbill,
    case c.col
      when 'u1paid' then u1paid
      when 'u2paid' then u2paid
      when 'u3paid' then u3paid
      when 'u4paid' then u4paid
      when 'u5paid' then u5paid
    end as data
  from yourtable t
  cross join
  (
    select 'u1paid' as col
    union all select 'u2paid'
    union all select 'u3paid'
    union all select 'u4paid'
    union all select 'u5paid'
  ) c
) src
where data = 'false'
group by trackbill

See SQL Fiddle with Demo




回答4:


You said you already knew how to do it if it was in a vertical format...

So here is your horizontal table made vertical:

SELECT Trackbill, u1paid FROM table
UNION
SELECT Trackbill, u2paid FROM table
UNION
SELECT Trackbill, u3paid FROM table
UNION
SELECT Trackbill, u4paid FROM table
UNION
SELECT Trackbill, u5paid FROM table

You can use that as a derived table to write another select on.



来源:https://stackoverflow.com/questions/15417706/sql-get-number-of-columns-in-a-particular-row-having-a-particular-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!