Subject | [firebird-support] Re: Select statement column lists question - Invalid Token |
---|---|
Author | Svein Erling Tysvær |
Post date | 2008-03-03T08:44:39Z |
Hi Peter!
Your select below isn't quite the same as using a LEFT JOIN. It is identical if every order contains a proc_id that is in procs, but it is different if orders may have NULL in proc_id or if it can be a value that doesn't exist in procs. The LEFT JOIN is identical to your original subselect except where a proc_id is identical for two rows in procs (your original subselect would give an error about getting multiple values where expecting a singular result, whereas my LEFT JOIN would count each row in orders with such proc_id twice - just like your statement below).
I'm sorry to say that it is unlikely that I'll remember the way I thought was possible, the closest I can get to an answer is that I think it was written in this list less than one year ago (though time flies quickly). I know the reasoning behind the answer was sufficiently complex, so I believe it was written by Arno Brinkman or Dmitry Yemanov. But it might have been a similar situation to yours, without being identical.
Set
-----Original Message-----
From: firebird-support@yahoogroups.com [mailto:firebird-support@yahoogroups.com] On Behalf Of Peter Welch
Sent: 2. mars 2008 18:34
To: firebird-support@yahoogroups.com
Subject: [firebird-support] Re: Select statement column lists question - Invalid Token
Svein,
Thank you for your quick reply (and sorry for my late one). Yes, the
following works:
select count(*)
, cast(o.aDT as date)
, p.category
from orders o, procs p
where p.proc_id = o.proc_id
group by 2, 3
order by 1;
Actually, as I investigate all the issues, it seems I will be best
served by creating a mini-datawarehouse, since the long term usage
will involve several dimensions. Originally, I was hoping to do a
simple "table lookup" for each column of IDs but, in the end, that
means a join for each.
If you ever think of the way to accomplish my original Sql effort as
you thought might be possible, I'd be glad to see it.
Thanks again,
Pete
--- In firebird-support@yahoogroups.com, Svein Erling Tysvaer
<svein.erling.tysvaer@...> wrote:
Your select below isn't quite the same as using a LEFT JOIN. It is identical if every order contains a proc_id that is in procs, but it is different if orders may have NULL in proc_id or if it can be a value that doesn't exist in procs. The LEFT JOIN is identical to your original subselect except where a proc_id is identical for two rows in procs (your original subselect would give an error about getting multiple values where expecting a singular result, whereas my LEFT JOIN would count each row in orders with such proc_id twice - just like your statement below).
I'm sorry to say that it is unlikely that I'll remember the way I thought was possible, the closest I can get to an answer is that I think it was written in this list less than one year ago (though time flies quickly). I know the reasoning behind the answer was sufficiently complex, so I believe it was written by Arno Brinkman or Dmitry Yemanov. But it might have been a similar situation to yours, without being identical.
Set
-----Original Message-----
From: firebird-support@yahoogroups.com [mailto:firebird-support@yahoogroups.com] On Behalf Of Peter Welch
Sent: 2. mars 2008 18:34
To: firebird-support@yahoogroups.com
Subject: [firebird-support] Re: Select statement column lists question - Invalid Token
Svein,
Thank you for your quick reply (and sorry for my late one). Yes, the
following works:
select count(*)
, cast(o.aDT as date)
, p.category
from orders o, procs p
where p.proc_id = o.proc_id
group by 2, 3
order by 1;
Actually, as I investigate all the issues, it seems I will be best
served by creating a mini-datawarehouse, since the long term usage
will involve several dimensions. Originally, I was hoping to do a
simple "table lookup" for each column of IDs but, in the end, that
means a join for each.
If you ever think of the way to accomplish my original Sql effort as
you thought might be possible, I'd be glad to see it.
Thanks again,
Pete
--- In firebird-support@yahoogroups.com, Svein Erling Tysvaer
<svein.erling.tysvaer@...> wrote:
>orders.proc_id )
> Hi, Peter!
>
> With Firebird 2.1 (I don't know about Firebird 2.0), you can do
>
> select count(*), cast(dt.aDT as date), dt.category
> from
> (select o.*
> ,(select p.category from procedures p where proc_id =
> as categorythis
> from orders o) dt
> group by 2, 3
> order by 1;
>
> All versions of Firebird should be able to handle something like
> using a VIEW, and - of course - the obvious LEFT JOIN that I simplyare
> forgot and that should work almost regardless of which version you
> using (this would be my preferred way to do what you're trying todo):
>ago
> select count(*)
> , cast(o.aDT as date)
> , p.category
> from orders o
> left join procedures p on p.proc_id = o.proc_id
> group by 2, 3
> order by 1;
>
> I think your original select may work as well if you do something
> clever. At least I think I had a slightly similar situation a while
> that got solved on this list using an additional field in the groupby
> (I remember it surprised me when I saw the solution, but I don'tquite
> remember what was my problem, nor what the solution was).orders.proc_id )
>
> HTH,
> Set
>
> Peter Welch wrote:
> > I want to do a fairly simple 'group by' select statement but only
> > certain combinations work.
> >
> > What I want is the following to work:
> >
> > select count(*)
> > , cast(aDT as date)
> > ,(select category from procedures where proc_id =
> > from ordersor
> > group by 1,2
> > order by 1;
> >
> > Running the above causes the ambiguous Invalid Token error (with
> > without the count(*) column).orders.proc_id )
> >
> > I can do the following:
> >
> > select
> > cast(aDT as date)
> > ,(select category from procedures where proc_id =
> > from ordersdon't
> > order by 1;
> >
> > but no 'group by' or count(*)
> >
> > Or, I can do the following:
> >
> > select count(*)
> > , cast(aDT as date)
> > from orders
> > group by 2
> > order by 1;
> >
> > But no '(select category from ....'
> >
> > I would prefer not to turn this into a stored procedure, but
> > see any other way.
> >
> > Any suggestions greatly appreciated,
> >
> > Pete