Обсуждение: How to return a default value if no result

Поиск
Список
Период
Сортировка

How to return a default value if no result

От
"Rob - TEAM Systems Ltd"
Дата:

I want my query to return at least one row with a default result even if there are no matches in the database.

As a simple example, getting the valid VAT rate for a transaction date and VAT code:

Select vat_rate from vatrates where vat_code=’1’ and vat_date<=’2014-08-01’ order by vat_date desc limit 1

 

This is fine as long as there is a match.  If there are no matches, I’d like it to return a default (0) rather than no result.

I could make sure there is always a result by having a UNION with a hard-coded record, but this doesn’t let me order by a field not in the result.

 

In this example I could just make sure there is a dummy row in the table with a very old date and do something like this:

Select vat_rate from vatrates where (vat_code=’1’ or vat_code=’DUMMY’) and vat_date<=’2014-08-01’ order by vat_date desc limit 1

But it seems like a bodge to me.

 

Is there a nice way of forcing a default result somehow?

 

Thanks,

 

Rob

Re: How to return a default value if no result

От
"Rob - TEAM Systems Ltd"
Дата:

OK, we’ve thought of another possibility, still seems a bodge though:

 

Select case when exists(Select vat_rate from vatrates where vat_code=’1’ and vat_date<=’2014-08-01’)

Then (Select vat_rate from vatrates where vat_code=’1’ and vat_date<=’2014-08-01’ order by vat_date desc limit 1)

Else (select 0 as vat_rate from vatrates limit 1)

 

From: pgsql-novice-owner@postgresql.org [mailto:pgsql-novice-owner@postgresql.org] On Behalf Of Rob - TEAM Systems Ltd
Sent: 22 August 2014 12:19
To: pgsql-novice@postgresql.org
Subject: [NOVICE] How to return a default value if no result

 

I want my query to return at least one row with a default result even if there are no matches in the database.

As a simple example, getting the valid VAT rate for a transaction date and VAT code:

Select vat_rate from vatrates where vat_code=’1’ and vat_date<=’2014-08-01’ order by vat_date desc limit 1

 

This is fine as long as there is a match.  If there are no matches, I’d like it to return a default (0) rather than no result.

I could make sure there is always a result by having a UNION with a hard-coded record, but this doesn’t let me order by a field not in the result.

 

In this example I could just make sure there is a dummy row in the table with a very old date and do something like this:

Select vat_rate from vatrates where (vat_code=’1’ or vat_code=’DUMMY’) and vat_date<=’2014-08-01’ order by vat_date desc limit 1

But it seems like a bodge to me.

 

Is there a nice way of forcing a default result somehow?

 

Thanks,

 

Rob

Re: How to return a default value if no result

От
David G Johnston
Дата:
Rob Northcott wrote
> Is there a nice way of forcing a default result somehow?

The Coalesce function is your friend:

SELECT COALESCE( (SELECT subquery), <default> )

David J.



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/How-to-return-a-default-value-if-no-result-tp5815850p5815860.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.


Re: How to return a default value if no result

От
"Rob - TEAM Systems Ltd"
Дата:
Thanks David,
That's actually what we did in the end, works fine in the case where there
is only one field (sorry, column!) in the result.
Just out of curiosity (luckily all the places I need to do this at the
moment will work with coalesce), is there a way to return a default result
with a multiple-column query, without resorting to unions or the long-winded
case statement?
Something like this would be nice (but I don't think such a syntax exists,
at least not that I can find):
SELECT col1, col2, col3 FROM mytable
WHERE key='A'
DEFAULT (0,'No','')


-----Original Message-----
From: pgsql-novice-owner@postgresql.org
[mailto:pgsql-novice-owner@postgresql.org] On Behalf Of David G Johnston
Sent: 22 August 2014 15:07
To: pgsql-novice@postgresql.org
Subject: Re: [NOVICE] How to return a default value if no result

Rob Northcott wrote
> Is there a nice way of forcing a default result somehow?

The Coalesce function is your friend:

SELECT COALESCE( (SELECT subquery), <default> )

David J.



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/How-to-return-a-default-value-if-no-
result-tp5815850p5815860.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.


--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org) To make
changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice




Re: How to return a default value if no result

От
David G Johnston
Дата:
For a really clean solution you would want to create a composite type.

SELECT COALESCE ( (SELECT ROW(...,...,...)::type FROM ...), ROW(val,val,val)::type )

You can make use of an anonymous/record type here but you will then be unable to expand it back into individual columns.

The "ROW" is optional but makes it clear in examples like this what is intended.

see the CREATE TYPE documentation for the syntax to create a custom composite type.

David J.


On Fri, Aug 22, 2014 at 11:33 AM, Rob Northcott [via PostgreSQL] <[hidden email]> wrote:
Thanks David,
That's actually what we did in the end, works fine in the case where there
is only one field (sorry, column!) in the result.
Just out of curiosity (luckily all the places I need to do this at the
moment will work with coalesce), is there a way to return a default result
with a multiple-column query, without resorting to unions or the long-winded
case statement?
Something like this would be nice (but I don't think such a syntax exists,
at least not that I can find):
SELECT col1, col2, col3 FROM mytable
WHERE key='A'
DEFAULT (0,'No','')


-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of David G Johnston
Sent: 22 August 2014 15:07
To: [hidden email]
Subject: Re: [NOVICE] How to return a default value if no result

Rob Northcott wrote
> Is there a nice way of forcing a default result somehow?

The Coalesce function is your friend:

SELECT COALESCE( (SELECT subquery), <default> )

David J.



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/How-to-return-a-default-value-if-no-
result-tp5815850p5815860.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.


--
Sent via pgsql-novice mailing list ([hidden email]) To make
changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice




--
Sent via pgsql-novice mailing list ([hidden email])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice



If you reply to this email, your message will be added to the discussion below:
http://postgresql.1045698.n5.nabble.com/How-to-return-a-default-value-if-no-result-tp5815850p5815879.html
To unsubscribe from How to return a default value if no result, click here.
NAML



View this message in context: Re: How to return a default value if no result
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.