pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Constructing colum name as alias
7+ messages / 5 participants
[nested] [flat]

* Constructing colum name as alias
@ 2021-11-11 07:46  aditya desai <admad123@gmail.com>
  0 siblings, 2 replies; 7+ messages in thread

From: aditya desai @ 2021-11-11 07:46 UTC (permalink / raw)
  To: pgsql-sql <pgsql-sql@lists.postgresql.org>

Hi,
I want to construct column name alias by using different queries. E.g.
below.

postgres=# Select ('Status as on '||date_part('day', (SELECT
current_timestamp))||'th '||TO_CHAR(current_timestamp, 'Mon'))
postgres-# ;
       ?column?
-----------------------
 Status as on 11th Nov

" Status as on 11th Nov" should be my column name in a query.

SELECT JoiningDate as "Status as on 11th Nov" from empdetails;

How can I achieve this?

Regards,
Aditya.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Constructing colum name as alias
@ 2021-11-11 11:10  Torsten Grust <teggy@fastmail.com>
  parent: aditya desai <admad123@gmail.com>
  1 sibling, 0 replies; 7+ messages in thread

From: Torsten Grust @ 2021-11-11 11:10 UTC (permalink / raw)
  To: aditya desai <admad123@gmail.com>; pgsql-sql <pgsql-sql@lists.postgresql.org>

Hi,

since column names (or table names) are not first-class values in SQL, computing such names at query runtime is impossible.

Best wishes,  
   —Torsten

On Thu, Nov 11, 2021, at 08:46, aditya desai wrote:
> Hi,
> I want to construct column name alias by using different queries. E.g. below.
> 
> postgres=# Select ('Status as on '||date_part('day', (SELECT current_timestamp))||'th '||TO_CHAR(current_timestamp, 'Mon'))
> postgres-# ;
>        ?column?
> -----------------------
>  Status as on 11th Nov
> 
> " Status as on 11th Nov" should be my column name in a query.
> 
> SELECT JoiningDate as "Status as on 11th Nov" from empdetails;
> 
> How can I achieve this?
> 
> Regards,
> Aditya.
> 

--
| Torsten Grust
| teggy@fastmail.com

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Constructing colum name as alias
@ 2021-11-11 13:19  David G. Johnston <david.g.johnston@gmail.com>
  parent: aditya desai <admad123@gmail.com>
  1 sibling, 1 reply; 7+ messages in thread

From: David G. Johnston @ 2021-11-11 13:19 UTC (permalink / raw)
  To: aditya desai <admad123@gmail.com>; +Cc: pgsql-sql <pgsql-sql@lists.postgresql.org>

On Thursday, November 11, 2021, aditya desai <admad123@gmail.com> wrote:

>
> " Status as on 11th Nov" should be my column name in a query.
>
> SELECT JoiningDate as "Status as on 11th Nov" from empdetails;
>
> How can I achieve this?
>

Dynamic SQL and two independent queries.  One to get the column name.  Then
a dynamic one  (say using pl/pgsql) that uses that name.

David J.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Constructing colum name as alias
@ 2021-11-11 13:22  Theodore M Rolle, Jr. <stercor@gmail.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Theodore M Rolle, Jr. @ 2021-11-11 13:22 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: aditya desai <admad123@gmail.com>; pgsql-sql <pgsql-sql@lists.postgresql.org>

May we have an example?

On Thu, Nov 11, 2021, 08:19 David G. Johnston <david.g.johnston@gmail.com>
wrote:

> On Thursday, November 11, 2021, aditya desai <admad123@gmail.com> wrote:
>
>>
>> " Status as on 11th Nov" should be my column name in a query.
>>
>> SELECT JoiningDate as "Status as on 11th Nov" from empdetails;
>>
>> How can I achieve this?
>>
>
> Dynamic SQL and two independent queries.  One to get the column name.
> Then a dynamic one  (say using pl/pgsql) that uses that name.
>
> David J.
>
>

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Constructing colum name as alias
@ 2021-11-11 14:14  David G. Johnston <david.g.johnston@gmail.com>
  parent: Theodore M Rolle, Jr. <stercor@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: David G. Johnston @ 2021-11-11 14:14 UTC (permalink / raw)
  To: stercor@gmail.com; +Cc: aditya desai <admad123@gmail.com>; pgsql-sql <pgsql-sql@lists.postgresql.org>

On Thu, Nov 11, 2021 at 6:23 AM Theodore M Rolle, Jr. <stercor@gmail.com>
wrote:

> May we have an example?
>

Here are the docs detailing writing dynamic SQL commands in pl/pgsql.

https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

David J.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Constructing colum name as alias
@ 2021-11-11 14:16  David G. Johnston <david.g.johnston@gmail.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: David G. Johnston @ 2021-11-11 14:16 UTC (permalink / raw)
  To: stercor@gmail.com; +Cc: aditya desai <admad123@gmail.com>; pgsql-sql <pgsql-sql@lists.postgresql.org>

On Thu, Nov 11, 2021 at 7:14 AM David G. Johnston <
david.g.johnston@gmail.com> wrote:

> On Thu, Nov 11, 2021 at 6:23 AM Theodore M Rolle, Jr. <stercor@gmail.com>
> wrote:
>
>> May we have an example?
>>
>
> Here are the docs detailing writing dynamic SQL commands in pl/pgsql.
>
>
> https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
>
>
Though, as you probably want to see these columns in your application you
will probably need to translate the concept to whatever programming
language and database client interface you are using.

David J.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Constructing colum name as alias
@ 2021-11-11 14:23  chris <yuanzefuwater@126.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 7+ messages in thread

From: chris @ 2021-11-11 14:23 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: aditya desai <admad123@gmail.com>; pgsql-sql <pgsql-sql@lists.postgresql.org>; stercor@gmail.com <stercor@gmail.com>

A flexible way to application, don’t know the actually value🤦‍♂️ see below:


select * from (Select ('Status as on '||date_part('day', (SELECT current_timestamp))||'th '||TO_CHAR(current_timestamp, 'Mon'))
union all
select now()::text from t1) foo limit 11;


Regards,
Chris
On 11/11/2021 22:16,David G. Johnston<david.g.johnston@gmail.com> wrote:
On Thu, Nov 11, 2021 at 7:14 AM David G. Johnston <david.g.johnston@gmail.com> wrote:

On Thu, Nov 11, 2021 at 6:23 AM Theodore M Rolle, Jr. <stercor@gmail.com> wrote:

May we have an example?


Here are the docs detailing writing dynamic SQL commands in pl/pgsql.


https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN




Though, as you probably want to see these columns in your application you will probably need to translate the concept to whatever programming language and database client interface you are using.


David J.



Attachments:

  [image/png] 570999AB-25A0-4066-8155-6FEB5AE562F2.png (201.9K, ../../18007723.72e9.17d0f62427a.Coremail.yuanzefuwater@126.com/3-570999AB-25A0-4066-8155-6FEB5AE562F2.png)
  download | view image

^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2021-11-11 14:23 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-11-11 07:46 Constructing colum name as alias aditya desai <admad123@gmail.com>
2021-11-11 11:10 ` Torsten Grust <teggy@fastmail.com>
2021-11-11 13:19 ` David G. Johnston <david.g.johnston@gmail.com>
2021-11-11 13:22   ` Theodore M Rolle, Jr. <stercor@gmail.com>
2021-11-11 14:14     ` David G. Johnston <david.g.johnston@gmail.com>
2021-11-11 14:16       ` David G. Johnston <david.g.johnston@gmail.com>
2021-11-11 14:23         ` chris <yuanzefuwater@126.com>

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