Subject | Re: How many Unions allowed in firebird Select? |
---|---|
Author | Tony Masefield |
Post date | 2005-11-06T05:52:58Z |
--- In firebird-support@yahoogroups.com, "Adam" <s3057043@y...>
wrote:
descriptor field naming and also setting up the initial field types
(casting).
Thanks for the comprehensive reply, your time and help!
wrote:
>looks
> Tony,
>
> Consider the following simplified example, assuming Col_1 and Col_2
> are some sort of integer.
>
> MyTable:
>
> Col_1 Col_2
> ----- ------
> 1 1
> 2 2
>
> Now your query looks like this:
>
> Select 'Minimum', Min(Col_1), Min(Col_2) from MyTable
> Union
> Select 'Maximum', Max(Col_1), Max(Col_2) from MyTable
> Union
> Select 'Average', Avg(Col_1), Avg(Col_2) from MyTable
>
> Firebird uses the first query within the union to determine the
> required datatypes, so it decides that the first column is to be a
> char or varchar, the second column is to be integer and the third
> column is to be integer, so the first record to be returned is:
>
> 'Minimum', 1, 1
>
> All is well so far, so the first union is done, and the result
> like this so far:is
>
> 'Minimum', 1, 1
> 'Maximum', 2, 2
>
> Now the third union is attempted but there is a problem. Avg Col_1
> 1.5, and it isn't going to fit inside an integer field. I meanresults
> Firebird could force it to fit by rounding it, but then your
> would not be as expected. So you end up with an error message.will
>
> Select 'Average', Avg(Col_1), Avg(Col_2) from MyTable
> Union
> Select 'Minimum', Min(Col_1), Min(Col_2) from MyTable
> Union
> Select 'Maximum', Max(Col_1), Max(Col_2) from MyTable
>
> Does this work? I haven't tested it at all, but I think this one
>For the records, no it doesn't work.
> force the field to become a float which can hold both the result ofto
> avg and an integer without problem.
>
> BUT
>
> It is one thing to explain the phenomenon, it is another to come up
> with a solution that works every time.
>
> Firstly, you need to name the fields or the field names are going
> be things like Min_1 etc, with no guarantee of not being renamedin a
> future version of Firebird, and with no sensible meaning forMaximum
> or Average.the
>
> Secondly, you are lucky in this case that the description field is
> same length on all lines so that will not cause the issue. Although(Col_1)
> the following query is a bit longer, it should work better.
>
> Select Cast('Minimum' as varchar(7)) as Description, Cast(Min
> as float) as Col_1, Cast(Min(Col_2) as float) as Col_2 from MyTableis
> Union
> Select 'Maximum', Max(Col_1), Max(Col_2) from MyTable
> Union
> Select 'Average', Avg(Col_1), Avg(Col_2) from MyTable
>
> Also, given that duplicates are not possible in this query, union
> equivalent to union all. Union all does not have to check foris no
> duplicates, which may or may not make a difference to performance.
>
> (It is like running select distinct pk_field from mytable, there
> advantage in using distinct here).Thanks Adam, You're right and I will follow your advice about
>
> Hope that helps
> Adam
>
descriptor field naming and also setting up the initial field types
(casting).
Thanks for the comprehensive reply, your time and help!