这是查询:
SELECT DISTINCT
completed_phases,
CAST(completed_phases::bit(8) AS VARCHAR),
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=1 THEN 'FT' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=2 THEN 'ED' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=3 THEN 'MC' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=4 THEN 'HC' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=5 THEN 'UV' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=6 THEN 'TT' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=7 THEN 'RX' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=8 THEN 'PI' ELSE '' END
FROM rx_sales_order
如果completed_phase为129
,则最终列的输出应为FTPI。但它只显示了FT。只有第一个案例陈述似乎有效,即使它们都是不同的。
答案 0 :(得分:2)
STRPOS()
将始终返回搜索到的字符串的第一次。因此,对strpos()
的所有调用都将返回1作为输入值129。
您可以改为使用substring()
:
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),1,1)='1' THEN 'FT' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),2,1)='1' THEN 'ED' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),3,1)='1' THEN 'MC' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),4,1)='1' THEN 'HC' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),5,1)='1' THEN 'UV' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),6,1)='1' THEN 'TT' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),7,1)='1' THEN 'RX' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),8,1)='1' THEN 'PI' ELSE '' END
另一个选择是使用get_bit()
分别测试每个位:
case when get_bit(completed_phases::bit(8), 0) = 1 then 'FT' else '' END||
case when get_bit(completed_phases::bit(8), 1) = 1 then 'ED' else '' END||
case when get_bit(completed_phases::bit(8), 2) = 1 then 'MC' else '' END||
case when get_bit(completed_phases::bit(8), 3) = 1 then 'HC' else '' END||
case when get_bit(completed_phases::bit(8), 4) = 1 then 'UV' else '' END||
case when get_bit(completed_phases::bit(8), 5) = 1 then 'TT' else '' END||
case when get_bit(completed_phases::bit(8), 6) = 1 then 'RX' else '' END||
case when get_bit(completed_phases::bit(8), 7) = 1 then 'PI' else '' END
更灵活的方法是将这些位转换为行并使用数组作为查找。类似的东西:
with lookup (codes) as (
values (array['FT','ED','MC','HC','UV','TT','RX','PI'])
)
SELECT completed_phases,
completed_phases::bit(8),
x.code
FROM rx_sales_order
join lateral (
select string_agg(codes[i],'') as code
from lookup, unnest(string_to_array(completed_phases::bit(8)::text, null)) with ordinality as t(b,i)
where b = '1'
) as x on true
部分regexp_split_to_table(completed_phases::bit(8)::text, '') with ordinality as t(b,i)
将返回以下值129:
b | i
--+--
1 | 1
0 | 2
0 | 3
0 | 4
0 | 5
0 | 6
0 | 7
1 | 8
code[i]
使用索引查找匹配的代码,string_agg()
然后将所有选定的代码再次放在一个字符串中。条件where b = '1'
仅选择已设置的位。
该解决方案将比硬编码的case
表达式慢得多(因为它增加了行数,只是为了再次减少它们) - 但它更灵活,更易于维护。
如果您需要那么多,最好的选择是将case表达式放入函数中并在查询中使用该函数。
create or replace function get_codes(p_phases integer)
returns text
as
$$
select
case when get_bit(p_phases::bit(8), 0) = 1 then 'FT' else '' END||
case when get_bit(p_phases::bit(8), 1) = 1 then 'ED' else '' END||
case when get_bit(p_phases::bit(8), 2) = 1 then 'MC' else '' END||
case when get_bit(p_phases::bit(8), 3) = 1 then 'HC' else '' END||
case when get_bit(p_phases::bit(8), 4) = 1 then 'UV' else '' END||
case when get_bit(p_phases::bit(8), 5) = 1 then 'TT' else '' END||
case when get_bit(p_phases::bit(8), 6) = 1 then 'RX' else '' END||
case when get_bit(p_phases::bit(8), 7) = 1 then 'PI' else '' END
$$
language sql;
然后使用:
SELECT DISTINCT
completed_phases,
get_codes(completed_phases) as codes
FROM rx_sales_order
答案 1 :(得分:1)
正如the answer by a_horse_with_no_name中指出的那样,strpos
将返回搜索字符串的第一个次出现。无论如何,最好使用get_bit
而不是强制转换为VARCHAR
来检查是否设置了位。另外,您可以使用||
代替concat
,它将null
作为空字符串处理。然后可以将您的查询重写为:
SELECT DISTINCT
completed_phases,
CAST(completed_phases::bit(8) AS VARCHAR),
concat(CASE get_bit(completed_phases::bit(8), 0) WHEN 1 THEN 'FT' END,
CASE get_bit(completed_phases::bit(8), 1) WHEN 1 THEN 'ED' END,
CASE get_bit(completed_phases::bit(8), 2) WHEN 1 THEN 'MC' END,
CASE get_bit(completed_phases::bit(8), 3) WHEN 1 THEN 'HC' END,
CASE get_bit(completed_phases::bit(8), 4) WHEN 1 THEN 'UV' END,
CASE get_bit(completed_phases::bit(8), 5) WHEN 1 THEN 'TT' END,
CASE get_bit(completed_phases::bit(8), 6) WHEN 1 THEN 'RX' END,
CASE get_bit(completed_phases::bit(8), 7) WHEN 1 THEN 'PI' END)
FROM rx_sales_order;
在旁注中,如果您可以选择这样做,我建议您更改数据库架构,将阶段存储为单独的boolean
列,而不是使用位图。有关原因的详细讨论,请参阅Any disadvantages to bit flags in database columns?。