Skip to content

Instantly share code, notes, and snippets.

@lukaseder
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaseder/10462127 to your computer and use it in GitHub Desktop.
Save lukaseder/10462127 to your computer and use it in GitHub Desktop.
Ain't no SQL hard enough (for jOOQ)
CommonTableExpression<Record2<Integer, String>> t1 = name("t1").fields("f1", "f2").as(select(val(1), val("a")));
CommonTableExpression<Record2<Integer, String>> t2 = name("t2").fields("f3", "f4").as(select(val(2), val("b")));
// Try renaming the CTEs and their columns when referencing them
Table<Record2<Integer, String>> b1 = t1.as("a1", "i1", "s1");
Table<Record2<Integer, String>> b2 = t2.as("a2", "i2", "s2");
Result<?> result4 =
create().with(t1)
.with(t2)
.select(
b1.field("i1").add(b2.field("i2")).as("add"),
b1.field("s1").concat(b2.field("s2")).as("concat"))
.from(b1, b2)
.fetch();
// the above renders:
// ------------------
with
[t1]([f1], [f2]) as (
select
1,
'a'
),
[t2]([f3], [f4]) as (
select
2,
'b'
)
select
([a1].[i1] + [a2].[i2]) [add],
([a1].[s1] + [a2].[s2]) [concat]
from
// The following renames must be done in derived tables, as
// simple renaming of table references [t1] to [a1]([i1], [i2])
// is not possible in SQL Server
(
select *
from [t1] [a1]
) [a1]([i1], [s1]),
(
select *
from [t2] [a2]
) [a2]([i2], [s2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment