Wednesday, April 2, 2014

Cartoon in pg.


I hope this gives you a bit FUN with pg SQL.



select * from
(select array_to_string(array_agg(CASE WHEN (power((xx.x-25),2)/130+power((yy.y-25),2)/130)=1 THEN
'$' WHEN (sqrt(power(xx.x-20,2)+power(yy.y-20,2)))<2 THEN '#' WHEN (sqrt(power(xx.x-20,2)+power(yy.y-30,2)))<2 THEN
 '#' WHEN (sqrt(power(xx.x-29,2)+power(yy.y-25,2)))<4 THEN '#' WHEN (power((xx.x-10),2)/40+power((yy.y-10),2)/40)=1 THEN
 '$' WHEN (power((xx.x-10),2)/40+power((yy.y-40),2)/40=1) THEN '$' ELSE ' ' END),' ') as cartoon from
(select generate_series(1,40) as x) as xx,(select generate_series(1,50) as y) as yy group by xx.x order by xx.x)
as co_ord;

Oracle Mode

CREATE OR REPLACE TYPE series AS TABLE OF NUMBER;

CREATE OR REPLACE FUNCTION generate_series(n INT, m INT) RETURN series PIPELINED
IS
BEGIN
FOR i IN n..m LOOP
PIPE ROW (i);
END LOOP;
RETURN;
END;

SELECT 
  REPLACE(WM_CONCAT(
    CASE
        WHEN (power((xx.COLUMN_VALUE-25),2)/130+power((yy.COLUMN_VALUE-25),2)/130)=1
        THEN '$'
        WHEN (sqrt(power(xx.COLUMN_VALUE-20,2)+power(yy.COLUMN_VALUE-20,2)))<2
        THEN '#'
        WHEN (sqrt(power(xx.COLUMN_VALUE-20,2)+power(yy.COLUMN_VALUE-30,2)))<2
        THEN '#'
        WHEN (sqrt(power(xx.COLUMN_VALUE-29,2)+power(yy.COLUMN_VALUE-25,2)))<4
        THEN '#'
        WHEN (power((xx.COLUMN_VALUE-10),2)/40+power((yy.COLUMN_VALUE-10),2)/40)=1
        THEN '$'
        WHEN (power((xx.COLUMN_VALUE-10),2)/40+power((yy.COLUMN_VALUE-40),2)/40=1)
        THEN '$'
        ELSE ' '
      END
    ), ',', ' ') cartoon
FROM
  ( SELECT * FROM TABLE(generate_series(1, 40))) xx,
  ( SELECT * FROM TABLE(generate_series(1, 50))) yy
  GROUP BY xx.COLUMN_VALUE
  ORDER BY xx.COLUMN_VALUE;
  
Dinesh Kumar

6 comments: