Wednesday, May 30, 2018

perl5/perlbrew/etc/bashrc: line 103: perlbrew: command not found

If you're getting /path/to/perl5/perlbrew/etc/bashrc: line 103: perlbrew: command not found Try checking to see you don't have a $HOME/.perlbrew/init file which points to an old version of perlbrew which has been deleted.

Say no to whichcraft

Interesting and useful history and alternatives to which command https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then/85250#85250

Tuesday, May 15, 2018

Lazy way to resort a table in SQL

When you have a query and you want to sort the output but the query is too complex to see what to sort on. (say your output column isn't a selected column, but rather generated data)

select
case
        when substring(t1.some_column, 1, 1) = '#' then rtrim(t1.some_column)
        when 'some_group' in ('group_a', 'group_b', 'group_c', 'group_d', 'group_e') then  '#' + rtrim(t1.some_column)
        else rtrim(t1.some_column)
end as 'op_col_1',
case
 when t1.ip_col_1 = 'a' then 'ant'
 when t1.ip_col_1 = 'b' then 'beetle'
 when t1.ip_col_1 = 'c' then 'centipede'
 else 'no_bugs'
end as 'op_col_2',
from table_one t1

You can wrap the whole lot in brackets and sort by the column order or the generated name:

select * from (
select
case
        when substring(t1.some_column, 1, 1) = '#' then rtrim(t1.some_column)
        when 'some_group' in ('group_a', 'group_b', 'group_c', 'group_d', 'group_e') then  '#' + rtrim(t1.some_column)
        else rtrim(t1.some_column)
end as 'op_col_1',
case
 when t1.ip_col_1 = 'a' then 'ant'
 when t1.ip_col_1 = 'b' then 'beetle'
 when t1.ip_col_1 = 'c' then 'centipede'
 else 'no_bugs'
end as 'op_col_2',
from table_one t1
) query_out
where op_col_1 like 'blah%'
order by op_col_2
or
order by 2

Thanks Chris!