PGSQL selecting columns on certain conditions - postgresql

In a table I have 5 columns day1, day2... day5. Records in the table can have all days set to TRUE or few days set to TRUE.
Is there any way in PGSQL to select only those columns of a record which have boolean value as TRUE
Example:
My table is: Course, with columns as Course Name, Day1, Day2, Day3, Day4,Day5 with record set as
English,True,False,True,False,True
German,False,False,True,True,True
French,False,True,False,True,True
What I need to display as result set is:
English,Mon,Wed,Fri
German,Wed,Thu,Fri
French,Tue,Thu,Fri

I believe something like the following should do the job. It's a bit ugly because your schema isn't the most awesome. This should work on Postgres 9+
SELECT course, string_agg(day, ',') as days_of_week
FROM
(
SELECT course, 'Mon' as day FROM yourtable WHERE day1 = 'True'
UNION ALL
SELECT course, 'Tue' as day FROM yourtable WHERE day2 = 'True'
UNION ALL
SELECT course, 'Wed' as day FROM yourtable WHERE day3 = 'True'
UNION ALL
SELECT course, 'Thu' as day FROM yourtable WHERE day4 = 'True'
UNION ALL
SELECT course, 'Fri' as day FROM yourtable WHERE day5 = 'True'
) sub

Function like iif missed suddenly but you could to create it simply:
create or replace function iif(boolean, anyelement, anyelement = null) returns anyelement
language sql
immutable
as $$
select case when $1 is null then null when $1 then $2 else $3 end
$$;
then:
select
course_name,
concat_ws(',', iif(day1,'Mon'), iif(day2,'Tue'), iif(day3,'Wed'), iif(day4,'Thu'), iif(day5, 'Fri'))
from course;

Related

Removing all the Alphabets from a string using a single SQL Query [duplicate]

I'm currently doing a data conversion project and need to strip all alphabetical characters from a string. Unfortunately I can't create or use a function as we don't own the source machine making the methods I've found from searching for previous posts unusable.
What would be the best way to do this in a select statement? Speed isn't too much of an issue as this will only be running over 30,000 records or so and is a once off statement.
You can do this in a single statement. You're not really creating a statement with 200+ REPLACEs are you?!
update tbl
set S = U.clean
from tbl
cross apply
(
select Substring(tbl.S,v.number,1)
-- this table will cater for strings up to length 2047
from master..spt_values v
where v.type='P' and v.number between 1 and len(tbl.S)
and Substring(tbl.S,v.number,1) like '[0-9]'
order by v.number
for xml path ('')
) U(clean)
Working SQL Fiddle showing this query with sample data
Replicated below for posterity:
create table tbl (ID int identity, S varchar(500))
insert tbl select 'asdlfj;390312hr9fasd9uhf012 3or h239ur ' + char(13) + 'asdfasf'
insert tbl select '123'
insert tbl select ''
insert tbl select null
insert tbl select '123 a 124'
Results
ID S
1 390312990123239
2 123
3 (null)
4 (null)
5 123124
CTE comes for HELP here.
;WITH CTE AS
(
SELECT
[ProductNumber] AS OrigProductNumber
,CAST([ProductNumber] AS VARCHAR(100)) AS [ProductNumber]
FROM [AdventureWorks].[Production].[Product]
UNION ALL
SELECT OrigProductNumber
,CAST(STUFF([ProductNumber], PATINDEX('%[^0-9]%', [ProductNumber]), 1, '') AS VARCHAR(100) ) AS [ProductNumber]
FROM CTE WHERE PATINDEX('%[^0-9]%', [ProductNumber]) > 0
)
SELECT * FROM CTE
WHERE PATINDEX('%[^0-9]%', [ProductNumber]) = 0
OPTION (MAXRECURSION 0)
output:
OrigProductNumber ProductNumber
WB-H098 098
VE-C304-S 304
VE-C304-M 304
VE-C304-L 304
TT-T092 092
RichardTheKiwi's script in a function for use in selects without cross apply,
also added dot because in my case I use it for double and money values within a varchar field
CREATE FUNCTION dbo.ReplaceNonNumericChars (#string VARCHAR(5000))
RETURNS VARCHAR(1000)
AS
BEGIN
SET #string = REPLACE(#string, ',', '.')
SET #string = (SELECT SUBSTRING(#string, v.number, 1)
FROM master..spt_values v
WHERE v.type = 'P'
AND v.number BETWEEN 1 AND LEN(#string)
AND (SUBSTRING(#string, v.number, 1) LIKE '[0-9]'
OR SUBSTRING(#string, v.number, 1) LIKE '[.]')
ORDER BY v.number
FOR
XML PATH('')
)
RETURN #string
END
GO
Thanks RichardTheKiwi +1
Well if you really can't use a function, I suppose you could do something like this:
SELECT REPLACE(REPLACE(REPLACE(LOWER(col),'a',''),'b',''),'c','')
FROM dbo.table...
Obviously it would be a lot uglier than that, since I only handled the first three letters, but it should give the idea.

How to pivot or crosstab in postgresql without writing a function?

I have a dataset that looks something like this:
I'd like to aggregate all co values on one row, so the final result looks something like:
Seems pretty easy, right? Just write a query using crosstab, as suggested in this answer. Problem is that requires that I CREATE EXTENSION tablefunc; and I don't have write access to my DB.
Can anyone recommend an alternative?
Conditional aggregation:
SELECT co,
MIN(CASE WHEN ontology_type = 'industry' THEN tags END) AS industry,
MIN(CASE WHEN ontology_type = 'customer_type' THEN tags END) AS customer_type,
-- ...
FROM tab_name
GROUP BY co
You can use DO to generate and PREPARE your own SQL with crosstab columns, then EXECUTE it.
-- replace tab_name to yours table name
DO $$
DECLARE
_query text;
_name text;
BEGIN
_name := 'prepared_query';
_query := '
SELECT co
'||(SELECT ', '||string_agg(DISTINCT
' string_agg(DISTINCT
CASE ontology_type WHEN '||quote_literal(ontology_type)||' THEN tags
ELSE NULL
END, '',''
) AS '||quote_ident(ontology_type),',')
FROM tab_name)||'
FROM tab_name
GROUP BY co
';
BEGIN
EXECUTE 'DEALLOCATE '||_name;
EXCEPTION
WHEN invalid_sql_statement_name THEN
END;
EXECUTE 'PREPARE '||_name||' AS '||_query;
END
$$;
EXECUTE prepared_query;
Since Ver. 9.4 there's json_object_agg(), which lets us do part of the necessary magic dynamically.
However to be totally dynamic, a temp type (a temp table) has to be FIRST built by running a SQL-EXEC inside an anonymous procedure.
DB FIDDLE (UK):
https://dbfiddle.uk/Sn7iO4zL
DISCLAIMER: Typically the ability to create TEMP TABLES are granted to end-users, but YMMV. Another concern is whether anon. procedures can be exec'd as in-line code by regular users.
-- /**
-- begin test data
-- begin test data
-- begin test data
-- */
DROP TABLE IF EXISTS tmpSales ;
CREATE TEMP TABLE tmpSales AS
SELECT
sale_id
,TRUNC(RANDOM()*12)+1 AS book_id
,TRUNC(RANDOM()*100)+1 AS customer_id
,(date '2010-01-01' + random() * (timestamp '2016-12-31' - timestamp '2010-01-01')) AS sale_date
FROM generate_series(1,10000) AS sale_id;
DROP TABLE IF EXISTS tmp_month_total ;
CREATE TEMP TABLE tmp_month_total AS
SELECT
date_part( 'year' , sale_date ) AS year
,date_part( 'month', sale_date ) AS mn
,to_char(sale_date, 'mon') AS month
,COUNT(*) AS total
FROM tmpSales
GROUP BY date_part('year', sale_date), to_char(sale_date, 'mon') ,date_part( 'month', sale_date )
;
DATA:
+----+--+-----+-----+
|year|mn|month|total|
+----+--+-----+-----+
|2010|1 |jan |127 |
|2010|2 |feb |117 |
|2010|3 |mar |121 |
|2010|4 |apr |131 |
|2010|5 |may |106 |
|2010|6 |jun |121 |
|2010|7 |jul |129 |
|2010|8 |aug |114 |
|2010|9 |sep |115 |
|2010|10|oct |110 |
|2010|11|nov |133 |
|2010|12|dec |108 |
+----+--+-----+-----+
-- /**
-- END test data
-- END test data
-- END test data
-- */
-- /**
-- dyn. build a temporary row-type based on existing data, not hard-coded
-- dyn. build a temporary row-type based on existing data, not hard-coded
-- dyn. build a temporary row-type based on existing data, not hard-coded
-- **/
DROP TABLE IF EXISTS tmpTblTyp CASCADE ;
DO LANGUAGE plpgsql $$ DECLARE v_sqlstring VARCHAR = ''; BEGIN
v_sqlstring := CONCAT( 'CREATE TEMP TABLE tmpTblTyp AS SELECT '
,(SELECT STRING_AGG( CONCAT('NULL::int AS ' , month )::TEXT , ' ,'
ORDER BY mn
)::TEXT
FROM
(SELECT DISTINCT month, mn FROM tmp_month_total )a )
,' LIMIT 0 '
) ; -- RAISE NOTICE '%', v_sqlstring ;
EXECUTE( v_sqlstring ) ; END $$;
DROP TABLE IF EXISTS tmpMoToJson ;
CREATE TEMP TABLE tmpMoToJson AS
SELECT
year AS year
,(json_build_array( months )) AS js_months_arr
,json_populate_recordset ( NULL::tmpTblTyp /** use temp table as a record type!! **/
, json_build_array( months )
) jprs /** builds row-type column that can be expanded with (jprs).*
**/
FROM ( SELECT year
-- accum data into JSON array
,json_object_agg(month,total) AS months
FROM tmp_month_total
GROUP BY year
ORDER BY year
) a
;
SELECT
year
,(ROW((jprs).*)::tmpTblTyp).* -- explode the composite type row
FROM tmpMoToJson ;
+----+---+---+---+---+---+---+---+---+---+---+---+---+
|year|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|
+----+---+---+---+---+---+---+---+---+---+---+---+---+
|2010|127|117|121|131|106|121|129|114|115|110|133|108|
|2011|117|112|117|115|139|116|119|152|117|112|115|103|
|2012|129|111|98 |140|109|131|114|110|112|115|100|121|
|2013|128|112|141|127|141|102|113|109|111|110|123|116|
|2014|129|114|117|118|111|123|106|111|127|121|124|145|
|2015|118|113|131|122|120|121|140|114|118|108|114|131|
|2016|117|110|139|100|110|116|112|109|131|117|122|132|
+----+---+---+---+---+---+---+---+---+---+---+---+---+
By using pivot also we can achieve your required out put
SELECT co
,industry
,customer_type
,product_type
,sales_model
,stage
FROM dataSet
PIVOT(max(tags) FOR ontologyType IN (
industry
,customer_type
,product_type
,sales_model
,stage
)) AS PVT

Postgres select concatenated column name

For the sake of example, there's five columns in a table month:
month.week1
month.week2
month.week3
month.week4
month.week5
The number of col is determined by a function
EXTRACT(WEEK FROM NOW()) - EXTRACT(WEEK FROM DATE_TRUNC('month', NOW())) + 1
How can I select the column colX? This is what I have so far
SELECT month.week || (
EXTRACT(WEEK FROM NOW())
- EXTRACT(WEEK FROM DATE_TRUNC('month', NOW())) + 1
)::text
FROM month
But that gives me the error
ERROR: column month.week doesn't exist
SQL state: 42703
Use case statement
SELECT
CASE WHEN (week expresion) = 1 THEN month.week1
WHEN (week expresion) = 2 THEN month.week2
WHEN (week expresion) = 3 THEN month.week3
WHEN (week expresion) = 4 THEN month.week4
ELSE month.week5
END as WeekValue
FROM month
OR
SELECT
CASE (week expresion)
WHEN 1 THEN month.week1
WHEN 2 THEN month.week2
WHEN 3 THEN month.week3
WHEN 4 THEN month.week4
ELSE month.week5
END as WeekValue
FROM month
dynamic sql sample:
t=# create table so58(i int,w1 text);
CREATE TABLE
t=# create or replace function so59(_n int) returns table (i int,w text) as $$begin
return query execute format('select i,w%s from so58',_n);
end;
$$ language plpgsql;
CREATE FUNCTION
t=# select * from so59(1);
i | w
---+---
(0 rows)

casting text to date in redshift

I have saved date as text type. There are a few invalid dates those are preventing me from running any date related operation. For e.g.
select case when deliver_date = '0000-00-00 00:00:00' then '2014-01-01' else deliver_date::date end as new_date, count(*) as cnt from some_table group by new_date
Error in query: ERROR: Error converting text to date
I am using the following work-around that seems to be working.
select left(deliver_date,10) as new_date, count(*) as cnt from sms_dlr group by new_date
But I will like to know if it is possible to convert this column to date.
You need to separate the valid and invalid date values.
One solution is to use regular expressions- I'll let you decide how thorough you want to be, but this will broadly cover date and datetime values:
SELECT
CASE
WHEN
deliver_date SIMILAR TO '[0-9]{4}-[0-9]{2}-[0-9]{2}'
OR deliver_date SIMILAR TO '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}'
THEN TO_DATE(deliver_date, 'YYYY-MM-DD')
ELSE TO_DATE('2014-01-01', 'YYYY-MM-DD')
END as new_date,
COUNT(*) as cnt
FROM some_table
GROUP BY new_date
Try dropping the ::date part:
select
cast(
case
when deliver_date = '0000-00-00 00:00:00' then '2014-01-01'
else deliver_date
end
as date
) as new_date,
count(*) as cnt
from some_table
group by new_date

Using a trigger to retrieve the last records of each ID

This is my table :
ID Last_modified_date status
1 06-01-2013 17:05 New
1 06-02-2013 12:08 Assigned
2 07-03-2013 18:17 New
2 08-03-2013 15:12 Assigned
2 08-03-2013 18:05 Fixed
I need the following result
ID Last_modified_date status Trigger
1 06-01-2013 17:05 New False
1 06-02-2013 12:08 Assigned True
2 07-03-2013 18:17 New False
2 08-03-2013 15:12 Assigned False
2 08-03-2013 18:05 Fixed True
I can use this trigger to retrieve the last records of each ID.
this one will give you desired result, but I'm not sure that this is logic you want:
with cte as (
select
*,
row_number() over(partition by id order by last_modified_date desc) as row_num
from Table1
)
select
id, last_modified_date, status,
case when row_num = 1 then 'True' else 'False' end as "Trigger"
from cte
order by id, last_modified_date asc
Or if you just want last rows for each id, you can use distinct on:
select distinct on (id)
id, last_modified_date, status
from Table1
order by id, last_modified_date desc
=> sql fiddle demo
update
If you need trigger, try something like this:
create or replace function tr_Table1_func()
returns trigger
as
$$
begin
update Table1 as t set
tr = t.last_modified_date > new.last_modified_date
where t.id = new.id and t.tr = TRUE
returning not tr into new.tr;
new.tr = coalesce(new.tr, TRUE);
return new;
end;
$$
language plpgsql;
create trigger tr_Table1
before insert on Table1
for each row execute procedure tr_Table1_func();
=> sql fiddle demo