pg.ddx.io pgsql-sql@postgresql.org mailing list archivehelp / color / mirror / Atom feed
Creative use of CASE in a GROUP BY clause 2+ messages / 2 participants [nested] [flat]
* Creative use of CASE in a GROUP BY clause @ 1970-01-01 00:00 Dorian Taylor <leg0@hotmail.com> 0 siblings, 1 reply; 2+ messages in thread From: Dorian Taylor @ 1970-01-01 00:00 UTC (permalink / raw) To: pgsql-sql Greets, I'm trying to figure out how to craft a query that I can embed into a piece of code that can be passed a bind value in order to dictate how it should behave on GROUP BY, sparing me from having to write separate queries for N functions and/or columns I may want to group my query by. e.g. SELECT COUNT(foo) FROM bar GROUP BY CASE (? IS NOT NULL) THEN thiscol ELSE thatcol END If thiscol and thatcol aren't the same type, the CASE bombs out. I suppose ultimately it would be a matter of reducing each column definition in the CASE to a boolean expression, but I'm not sure what that would syntactically look like, as simply declaring a column in GROUP BY is obviously shorthand for something. Thanks Dorian _________________________________________________________________ Send and receive Hotmail on your mobile device: http://mobile.msn.com ^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Creative use of CASE in a GROUP BY clause @ 2002-03-18 15:58 Masaru Sugawara <rk73@sea.plala.or.jp> parent: Dorian Taylor <leg0@hotmail.com> 0 siblings, 0 replies; 2+ messages in thread From: Masaru Sugawara @ 2002-03-18 15:58 UTC (permalink / raw) To: Dorian Taylor <leg0@hotmail.com>; +Cc: pgsql-sql On Mon, 18 Mar 2002 13:14:46 "Dorian Taylor" <leg0@hotmail.com> wrote: > I'm trying to figure out how to craft a query that I can embed into a piece > of code that can be passed a bind value in order to dictate how it should > behave on GROUP BY, sparing me from having to write separate queries for N > functions and/or columns I may want to group my query by. > > e.g. SELECT COUNT(foo) FROM bar GROUP BY CASE (? IS NOT NULL) THEN thiscol > ELSE thatcol END > > If thiscol and thatcol aren't the same type, the CASE bombs out. I suppose If so, you may need to cast one of them. e.g. SELECT COUNT(foo) FROM bar GROUP BY CASE WHEN (? IS NOT NULL) THEN thiscol::TEXT ELSE thatcol -- if the type of thatcol is TEXT END; > ultimately it would be a matter of reducing each column definition in the > CASE to a boolean expression, but I'm not sure what that would syntactically > look like, as simply declaring a column in GROUP BY is obviously shorthand > for something. > Regards, Masaru Sugawara ^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2002-03-18 15:58 UTC | newest] Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 1970-01-01 00:00 Creative use of CASE in a GROUP BY clause Dorian Taylor <leg0@hotmail.com> 2002-03-18 15:58 ` Masaru Sugawara <rk73@sea.plala.or.jp>
This inbox is served by DDX for PostgreSQL; see mirroring instructions for how to clone and mirror all data and code used for this inbox