[thelist] Help Combining SQL Results
Jay Turley
jayturley at gmail.com
Thu Mar 5 13:36:26 CST 2009
Rudy,
Thanks a TON! That worked like a charm. I didn't even think of using
derived tables. Or UNION ALL
And FWIW, it's actually MS SQL Server. Go figure.
On Thu, Mar 5, 2009 at 11:49 AM, r937 <rudy at r937.com> wrote:
> first thing i noticed, jay, is that your GROUP BY is invalid
>
> which, since you say the query runs, means that this can only be mysql
>
> here's how you sum them --
>
> SELECT id
> , value
> , MAX(c_fk_id) AS idunnowhich
> , SUM(num_entries) AS total_entries
> FROM (
> SELECT a.id
> , a.value
> , a.c_fk_id
> , COUNT(*) AS num_entries
> FROM A AS a
> GROUP
> BY a.id
> , a.value
> UNION ALL -- clearly UNION ALL, not UNION
> SELECT b.id
> , b.value
> , b.c_fk_id
> , COUNT(*)
> FROM B AS b
> GROUP
> BY b.id
> , b.value
> ) AS u
> GROUP
> BY id
> , value
>
>
> ;o)
>
> --
>
> * * Please support the community that supports you. * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>
More information about the thelist
mailing list