Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crosstabs in SELECT [CORE1738] #2161

Open
firebird-automations opened this issue Feb 11, 2008 · 8 comments
Open

Crosstabs in SELECT [CORE1738] #2161

firebird-automations opened this issue Feb 11, 2008 · 8 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: @WarmBooter

Votes: 18

I receive many questions about "crosstabs" support in Firebird (mostly from people coming from SQLServer or Microsoft Access). Afaik, ANSI SQL doesn't defines any syntax for crosstabs while Microsoft databases uses PIVOT.

As BI and Data Mining applications are becoming more and more common every day, it would be good to have Firebird's select syntax extended to support crosstabs easily.

@firebird-automations
Copy link
Collaborator Author

Commented by: @dyemanov

Unfortunately, this is against the whole FB architecture. By design, the result set width is fixed at the moment of prepare.

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

The MSSQL PIVOT defines fixed columns. So we can implement it in the same manner.

@firebird-automations
Copy link
Collaborator Author

Commented by: Pabloj (pabloj_sourceforge)

Should be closed as duplicate of CORE650

@firebird-automations
Copy link
Collaborator Author

Commented by: Luke (lplatypus)

Oracle 11g also introduced a PIVOT clause like what was introduced in MSSQL 2005

MSSQL 2005 docs: http://msdn.microsoft.com/en-us/library/ms177410.aspx
Oracle article: http://www.oracle.com/technology/pub/articles/oracle-database-11g-top-features/11g-pivot.html
Oracle reference docs: http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/statements_10002.htm#sthref6753

This is not a duplicate of CORE650 (SELECT ... GROUP BY ... WITH CUBE) as that query does not directly produce a cross table (though it was a common way to gather the data required to build a cross table prior to the introduction of the PIVOT syntax).

@firebird-automations
Copy link
Collaborator Author

Commented by: Ivan (patuljak)

postgresql has Crosstab
http://www.postgresql.org/docs/current/static/tablefunc.html

example from postgresql

create table sales(year int, month int, qty int);
insert into sales values(2007, 1, 1000);
insert into sales values(2007, 2, 1500);
insert into sales values(2007, 7, 500);
insert into sales values(2007, 11, 1500);
insert into sales values(2007, 12, 2000);
insert into sales values(2008, 1, 1000);

select * from crosstab(
'select year, month, qty from sales order by 1',
'select m from generate_series(1,12) m'
) as (
year int,
"Jan" int,
"Feb" int,
"Mar" int,
"Apr" int,
"May" int,
"Jun" int,
"Jul" int,
"Aug" int,
"Sep" int,
"Oct" int,
"Nov" int,
"Dec" int
);
year | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
------+------+------+-----+-----+-----+-----+-----+-----+-----+-----+------+------
2007 | 1000 | 1500 | | | | | 500 | | | | 1500 | 2000
2008 | 1000 | | | | | | | | | | |
(2 rows)

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

Current (3.0) external procedures allows the creation of (very) similar feature as showed by Ivan.

@firebird-automations
Copy link
Collaborator Author

Commented by: Marcin (wodzu)

I was hoping that this feature will be implemented in 3.0. "PIVOT" has a crucial meaning in structures like EAV: http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model

Adriano I do not see how external procedures would help in creating such result set, will they allow to return different number of columns each time they are called?

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

If I may recommend you something, I'd say stay away from EAV. ;)

PIVOT (as in Oracle) is defined when a query is compiled. It may not change the columns depending on the data.

Re. external procedures, you'd need to define an external procedure for each query (result columns), while may passing the same external procedure name for each of them. External procedures can inspect the object metadata being called and known what columns (output parameters) were declared.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant