0

I have two simple queries as following, which I fetch theme as two record(1 column) from PostgreSQL.

select count(*) from root.str
union all
select count(*) from back.str0

which returns something like this:

+===+
|103|
+===+
|98 |
+===+

But I'd like to have something like this:

+===+===+
|103|98 |
+===+===+

I've tried this, but Postgres raises error at crosstab() function and says the function is not exist.

select * from crosstab(
   'select count(*) from root.str
    union all
    select count(*) from back.str0'::text) as (int r,int e)
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228

2 Answers2

2

I don't see why you would need the crosstab module for that:

select (select count(*) from root.str) as str_count,
       (select count(*) from back.str0) as str0_count;
1

You need to install the additional module tablefunc which provides the crosstab() function.

Consider this related answer with instructions and more advice:

Also, what @a_horse wrote ...

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • Thanks, unfortunately crosstab functions only accepts physical available tables, it doesn't find temporary or alias selects(`with`). –  Jul 28 '14 at 08:02
  • @user2889419: A **CTE** (`WITH` clause) is only visible in the scope of the query it is attached to, so it has to be part of the query string passed to `crosstab()`. **Temporary tables** are visible in the current session and work just fine with `crosstab()`! – Erwin Brandstetter Jul 28 '14 at 08:42