Links

   Quran Explorer - Interactive Audio Recitations & Translations

Tuesday, March 12, 2013

CrossTab query in Postgres

Hello,

Ever wondered how to display the following output via an SQL query?

StudentCalculusSimmulationProgrammingComm Skills
Ibrahim Itambo67988359
Abdulswamad68947740
Shamim70765644
Samira36986890

There are several ways of doing it but the most common and annoying hack is to use CASE...WHEN and/or aggregate functions with GROUP BY.

It may work but they are usualy overly complex, unreadable and hard to maintain.

A better solution in Postgres is to use crosstab function available via the tablefunc module.


Example code

CREATE TABLE student(
student_id serial primary key,
student_name varchar(50),
remarks test
);
INSERT INTO student(student_name) VALUES('Ibrahim');
INSERT INTO student(student_name) VALUES('Kilui');
INSERT INTO student(student_name) VALUES('Ali Babababa');
INSERT INTO student(student_name) VALUES('Buzi');

CREATE TABLE subject(
subject_id serial primary key,
subject_name varchar(50),
remarks text
);

INSERT INTO subject(subject_name) VALUES('Calculus I');
INSERT INTO subject(subject_name) VALUES('Simulation');
INSERT INTO subject(subject_name) VALUES('Adv Programming');
INSERT INTO subject(subject_name) VALUES('Comm Skills');

CREATE TABLE exam(
exam_id serial primary key,
exam_title varchar(50),
student_id integer references student,
subject_id integer references subject,
exam_mark real default 0 not null,
remarks text
);
//FINALY INSERT RESULTS
//INSERT INTO exam(exam_title, student_id, subject_id, exam_mark) VALUES(......); you know what to do here

SELECT * FROM crosstab(
    'select student.student_name::text, subject.subject_name, exam.exam_mark
    from exam
    inner join student on exam.student_id = student.student_id
    inner join subject on exam.subject_id = subject.subject_id
    order by 1,2',
    'SELECT subject_name FROM subject ORDER BY 1')
AS
  CT("Student" text, "Calculus" real, "Simulation" real, "Prog" real, "Comm Skills" real);


Adios

Thursday, February 28, 2013

Photos from DB to JasperReport

In my previous post i mentioned byte array datatype.
Its a simple(or is it simplistic) way of saving binary data into a db table. One example immediately comes to mind... yes photos!! "i want to save photos and render them in a printable report"

POSTGRES SQL

TABLE CREATE TABLE student(
student_id serial primary key,
reg_no varchar(7) not null,
photo bytea,
..
..
..
remarks text
);

The only field of interest is the photo field which is a byte array type

Most of the time i use iReport to design JasperReports and in this example I will use iReport 4.0

[[]]


Never mind.... the most important part is the ImageExpression class in the dynamic image.

Make sure its class is java.io.InputStream.

This may not be available in the iReport properties section so what u will do in such a case is click on the XML tab and type java.io.InputStream save then have fun coding

Migrating from PostgreSQL 8.3 to 9.0 and beyond - Byte Array perspective

My Nerd migos,

Its bn quite sometime since my last post.

I want to share my agonies (and help u avoid the same) when migrating applications from Postgres 8.3 (or below) to 9.0 and beyond. If you had/have tables with bytea then they will/may JUST stop working.

Some guy(s) with infinite wisdom decided to change the representation of byte array from the default (escape) to hex !!! I googled my head silly until i found a solution at stackoverflow.com (forgot the thread sorry!!!)

Ok straight to the solution. Export the 8.3 db (i prefer pg_dump --inserts dbname > /path/to/dumpfile.sql) then import (just use psql or ur favorite tool to run the script created by the pg_dump command).

The next thing is to change a configuration flag in postgresql.conf. Change bytea_output to 'escape' instead of the default 'hex'. restart the server and hug yourself.

"At least the postgres guy with infinite wisdom never forgot to add this flag for backward compatibility"

Plz dont ask me what is a bytea datatype. If u dont know it means u dont have it in ur DB so relax

Hope this helps

Wednesday, February 15, 2012

Geospatial Systems - 2D Geometry

A. Geometry primitives (2D)

* POINT (30 10)
- eg a single wifi hostspot
* LINESTRING (30 10, 10 30, 40 40)
- eg a single road a river, etc
* POLYGON
a. ((30 10, 10 20, 20 40, 40 40, 30 10)) - eg simple (ir)regular shape
b. ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30)) - with inner + exterior ring eg a doughnut


B. Multipart geometries (2D)
* MULTIPOINT ((10 40), (40 30), (20 20), (30 10)) or MULTIPOINT (10 40, 40 30, 20 20, 30 10)
- eg several wifi hotsposts in a given area

* MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))
- eg several roads and possibly crossing each other

* MULTIPOLYGON (((30 20, 10 40, 45 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))
- eg a marp showing several wards in a constituency

Linux Cron Jobs

Cron Jobs aka Scheduled Tasks

1. To see crontabs for root
#crontab -u root -l


2. To edit
#crontab -u root -e
Edit the time (min hh) to 00 10 - so that this task runs every day at 10 a.m.


a. Run at 9pm everyday
00 21 * * * wget -q "http://192.168.1.254:9090/bizypalace/push?to=0202058726&msg=NOTICE (9 PM) Server OK: $(date)"

b. At midnight 00 00 hours everyday
00 00 * * * wget -q "http://192.168.1.254:9090/bizypalace/push?to=0202058726&msg=CRON AT MIDNIGHT"

c. every 2 hours from 4am till 6pm
00 04,06,08,10,12,14,16,18 * * *

Note: in my examples am running wget but u can use any bash command

3. To check Log
tailf /var/log/cron