Home All Groups Group Topic Archive Search About
Author
22 Dec 2005 9:20 PM
yodarules
I'm having some issuses with ORDER BY with my query.  I'm trying to run the
same query in Oracle and SQL Server.

select a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB", SUM(a.CA)
AS "CA", SUM(a.SA) AS "SA" FROM APP_USER a WHERE a.START_TIME >=
1135044000000 AND a.START_TIME < 1135047600000 AND a.APP_ID = 56 AND GROUP_ID
= 50 GROUP BY a.APP_ID, a.USER_ID ORDER BY sum(cb+sb) DESC

This work fine in SQL Server but not in Oracle, if I replace ORDER BY
sum(cb+sb) DESC with ORDER BY (cb+sb) DESC it works fine in Oracle but not
SQL Server.  Any idea how I can achive the same results but one query to work
in Oracle and SQL Server.  Thanks.

Author
22 Dec 2005 9:35 PM
Rick Sawtell
Show quote
"yodarules" <yodaru***@discussions.microsoft.com> wrote in message
news:D4C09490-E189-4524-95FF-744CCE7F3A7A@microsoft.com...
> I'm having some issuses with ORDER BY with my query.  I'm trying to run
> the
> same query in Oracle and SQL Server.
>
> select a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB",
> SUM(a.CA)
> AS "CA", SUM(a.SA) AS "SA" FROM APP_USER a WHERE a.START_TIME >=
> 1135044000000 AND a.START_TIME < 1135047600000 AND a.APP_ID = 56 AND
> GROUP_ID
> = 50 GROUP BY a.APP_ID, a.USER_ID ORDER BY sum(cb+sb) DESC
>
> This work fine in SQL Server but not in Oracle, if I replace ORDER BY
> sum(cb+sb) DESC with ORDER BY (cb+sb) DESC it works fine in Oracle but not
> SQL Server.  Any idea how I can achive the same results but one query to
> work
> in Oracle and SQL Server.  Thanks.

I don't know if this will work, but you might try:

Order by 3 DESC

3 being the column's ordinal position in the SELECT list.


Rick Sawtell
Author
22 Dec 2005 9:36 PM
David Portas
Show quote
"yodarules" <yodaru***@discussions.microsoft.com> wrote in message
news:D4C09490-E189-4524-95FF-744CCE7F3A7A@microsoft.com...
> I'm having some issuses with ORDER BY with my query.  I'm trying to run
> the
> same query in Oracle and SQL Server.
>
> select a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB",
> SUM(a.CA)
> AS "CA", SUM(a.SA) AS "SA" FROM APP_USER a WHERE a.START_TIME >=
> 1135044000000 AND a.START_TIME < 1135047600000 AND a.APP_ID = 56 AND
> GROUP_ID
> = 50 GROUP BY a.APP_ID, a.USER_ID ORDER BY sum(cb+sb) DESC
>
> This work fine in SQL Server but not in Oracle, if I replace ORDER BY
> sum(cb+sb) DESC with ORDER BY (cb+sb) DESC it works fine in Oracle but not
> SQL Server.  Any idea how I can achive the same results but one query to
> work
> in Oracle and SQL Server.  Thanks.

Try:

SELECT app_id, user_id, cb, sb, ca, sa
FROM
(SELECT a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB",
   SUM(a.CA) AS "CA", SUM(a.SA) AS "SA",
   SUM(a.CB)+SUM(a.SB) AS ord
   FROM APP_USER a
   WHERE a.START_TIME >= 1135044000000
    AND a.START_TIME < 1135047600000
    AND a.APP_ID = 56 AND GROUP_ID = 50
  GROUP BY a.APP_ID, a.USER_ID) AS T
  ORDER BY ord DESC ;

--
David Portas
SQL Server MVP
--
Author
22 Dec 2005 9:36 PM
Aaron Bertrand [SQL Server MVP]
How about using a derived table?

Not sure if this is the exact syntax in Oracle (might want to post to an
Oracle group!) but this should work in SQL Server:


SELECT
APP_ID,
USER_ID,
CB,
SB,
CA,
SA
FROM
(
  select
   a.APP_ID,
   a.USER_ID,
   SUM(a.CB) AS "CB",
   SUM(a.SB) AS "SB",
   SUM(a.CA) AS "CA",
   SUM(a.SA) AS "SA"
  FROM
   APP_USER a
  WHERE
   a.START_TIME >= 1135044000000
   AND a.START_TIME < 1135047600000
   AND a.APP_ID = 56
   AND GROUP_ID = 50
  GROUP BY
   a.APP_ID,
   a.USER_ID
) x
ORDER BY
CB+SB DESC;


Show quote
"yodarules" <yodaru***@discussions.microsoft.com> wrote in message
news:D4C09490-E189-4524-95FF-744CCE7F3A7A@microsoft.com...
> I'm having some issuses with ORDER BY with my query.  I'm trying to run
> the
> same query in Oracle and SQL Server.
>
> select a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB",
> SUM(a.CA)
> AS "CA", SUM(a.SA) AS "SA" FROM APP_USER a WHERE a.START_TIME >=
> 1135044000000 AND a.START_TIME < 1135047600000 AND a.APP_ID = 56 AND
> GROUP_ID
> = 50 GROUP BY a.APP_ID, a.USER_ID ORDER BY sum(cb+sb) DESC
>
> This work fine in SQL Server but not in Oracle, if I replace ORDER BY
> sum(cb+sb) DESC with ORDER BY (cb+sb) DESC it works fine in Oracle but not
> SQL Server.  Any idea how I can achive the same results but one query to
> work
> in Oracle and SQL Server.  Thanks.
Author
22 Dec 2005 9:52 PM
yodarules
I don't understand the 'x' before the ORDER BY, could you explain that please.

Show quote
"Aaron Bertrand [SQL Server MVP]" wrote:

> How about using a derived table?
>
> Not sure if this is the exact syntax in Oracle (might want to post to an
> Oracle group!) but this should work in SQL Server:
>
>
> SELECT
>  APP_ID,
>  USER_ID,
>  CB,
>  SB,
>  CA,
>  SA
> FROM
>  (
>   select
>    a.APP_ID,
>    a.USER_ID,
>    SUM(a.CB) AS "CB",
>    SUM(a.SB) AS "SB",
>    SUM(a.CA) AS "CA",
>    SUM(a.SA) AS "SA"
>   FROM
>    APP_USER a
>   WHERE
>    a.START_TIME >= 1135044000000
>    AND a.START_TIME < 1135047600000
>    AND a.APP_ID = 56
>    AND GROUP_ID = 50
>   GROUP BY
>    a.APP_ID,
>    a.USER_ID
>  ) x
> ORDER BY
>  CB+SB DESC;
>
>
> "yodarules" <yodaru***@discussions.microsoft.com> wrote in message
> news:D4C09490-E189-4524-95FF-744CCE7F3A7A@microsoft.com...
> > I'm having some issuses with ORDER BY with my query.  I'm trying to run
> > the
> > same query in Oracle and SQL Server.
> >
> > select a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB",
> > SUM(a.CA)
> > AS "CA", SUM(a.SA) AS "SA" FROM APP_USER a WHERE a.START_TIME >=
> > 1135044000000 AND a.START_TIME < 1135047600000 AND a.APP_ID = 56 AND
> > GROUP_ID
> > = 50 GROUP BY a.APP_ID, a.USER_ID ORDER BY sum(cb+sb) DESC
> >
> > This work fine in SQL Server but not in Oracle, if I replace ORDER BY
> > sum(cb+sb) DESC with ORDER BY (cb+sb) DESC it works fine in Oracle but not
> > SQL Server.  Any idea how I can achive the same results but one query to
> > work
> > in Oracle and SQL Server.  Thanks.
>
>
>
Author
22 Dec 2005 10:00 PM
David Portas
>I don't understand the 'x' before the ORDER BY, could you explain that
>please.
>

The X is an alias for the derived table (the bracketed query after the FROM
clause).

--
David Portas
SQL Server MVP
--
Author
22 Dec 2005 10:02 PM
Trey Walpole
a derived table must have an alias - 'x' is as good as any. :)

yodarules wrote:
Show quote
> I don't understand the 'x' before the ORDER BY, could you explain that please.
>
> "Aaron Bertrand [SQL Server MVP]" wrote:
>
>
>>How about using a derived table?
>>
>>Not sure if this is the exact syntax in Oracle (might want to post to an
>>Oracle group!) but this should work in SQL Server:
>>
>>
>>SELECT
>> APP_ID,
>> USER_ID,
>> CB,
>> SB,
>> CA,
>> SA
>>FROM
>> (
>>  select
>>   a.APP_ID,
>>   a.USER_ID,
>>   SUM(a.CB) AS "CB",
>>   SUM(a.SB) AS "SB",
>>   SUM(a.CA) AS "CA",
>>   SUM(a.SA) AS "SA"
>>  FROM
>>   APP_USER a
>>  WHERE
>>   a.START_TIME >= 1135044000000
>>   AND a.START_TIME < 1135047600000
>>   AND a.APP_ID = 56
>>   AND GROUP_ID = 50
>>  GROUP BY
>>   a.APP_ID,
>>   a.USER_ID
>> ) x
>>ORDER BY
>> CB+SB DESC;
>>
>>
>>"yodarules" <yodaru***@discussions.microsoft.com> wrote in message
>>news:D4C09490-E189-4524-95FF-744CCE7F3A7A@microsoft.com...
>>
>>>I'm having some issuses with ORDER BY with my query.  I'm trying to run
>>>the
>>>same query in Oracle and SQL Server.
>>>
>>>select a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB",
>>>SUM(a.CA)
>>>AS "CA", SUM(a.SA) AS "SA" FROM APP_USER a WHERE a.START_TIME >=
>>>1135044000000 AND a.START_TIME < 1135047600000 AND a.APP_ID = 56 AND
>>>GROUP_ID
>>>= 50 GROUP BY a.APP_ID, a.USER_ID ORDER BY sum(cb+sb) DESC
>>>
>>>This work fine in SQL Server but not in Oracle, if I replace ORDER BY
>>>sum(cb+sb) DESC with ORDER BY (cb+sb) DESC it works fine in Oracle but not
>>>SQL Server.  Any idea how I can achive the same results but one query to
>>>work
>>>in Oracle and SQL Server.  Thanks.
>>
>>
>>
Author
22 Dec 2005 9:40 PM
Trey Walpole
how about just adding a column for

sum(a.CB+a.SB) as CBSB

and then

order by CBSB desc


yodarules wrote:
Show quote
> I'm having some issuses with ORDER BY with my query.  I'm trying to run the
> same query in Oracle and SQL Server.
>
> select a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB", SUM(a.CA)
> AS "CA", SUM(a.SA) AS "SA" FROM APP_USER a WHERE a.START_TIME >=
> 1135044000000 AND a.START_TIME < 1135047600000 AND a.APP_ID = 56 AND GROUP_ID
> = 50 GROUP BY a.APP_ID, a.USER_ID ORDER BY sum(cb+sb) DESC
>
> This work fine in SQL Server but not in Oracle, if I replace ORDER BY
> sum(cb+sb) DESC with ORDER BY (cb+sb) DESC it works fine in Oracle but not
> SQL Server.  Any idea how I can achive the same results but one query to work
> in Oracle and SQL Server.  Thanks.
Author
22 Dec 2005 9:48 PM
yodarules
Thanks guys,

I already tried that, but adding another columns in the select list is ruled
out, since we don't need the sum of these there.  Its only for display
purpose that we need the sum of these two columns. 

The reply by Rick,  what I need is the sum of the two columns, in your case
giving the position is only going to do it for that one column.

Show quote
"Trey Walpole" wrote:

> how about just adding a column for
>
> sum(a.CB+a.SB) as CBSB
>
> and then
>
> order by CBSB desc
>
>
> yodarules wrote:
> > I'm having some issuses with ORDER BY with my query.  I'm trying to run the
> > same query in Oracle and SQL Server.
> >
> > select a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB", SUM(a.CA)
> > AS "CA", SUM(a.SA) AS "SA" FROM APP_USER a WHERE a.START_TIME >=
> > 1135044000000 AND a.START_TIME < 1135047600000 AND a.APP_ID = 56 AND GROUP_ID
> > = 50 GROUP BY a.APP_ID, a.USER_ID ORDER BY sum(cb+sb) DESC
> >
> > This work fine in SQL Server but not in Oracle, if I replace ORDER BY
> > sum(cb+sb) DESC with ORDER BY (cb+sb) DESC it works fine in Oracle but not
> > SQL Server.  Any idea how I can achive the same results but one query to work
> > in Oracle and SQL Server.  Thanks.
>
Author
22 Dec 2005 10:04 PM
Trey Walpole
so don't display it :)

yodarules wrote:
Show quote
> Thanks guys,
>
> I already tried that, but adding another columns in the select list is ruled
> out, since we don't need the sum of these there.  Its only for display
> purpose that we need the sum of these two columns. 
>
> The reply by Rick,  what I need is the sum of the two columns, in your case
> giving the position is only going to do it for that one column.
>
> "Trey Walpole" wrote:
>
>
>>how about just adding a column for
>>
>>sum(a.CB+a.SB) as CBSB
>>
>>and then
>>
>>order by CBSB desc
>>
>>
>>yodarules wrote:
>>
>>>I'm having some issuses with ORDER BY with my query.  I'm trying to run the
>>>same query in Oracle and SQL Server.
>>>
>>>select a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB", SUM(a.CA)
>>>AS "CA", SUM(a.SA) AS "SA" FROM APP_USER a WHERE a.START_TIME >=
>>>1135044000000 AND a.START_TIME < 1135047600000 AND a.APP_ID = 56 AND GROUP_ID
>>>= 50 GROUP BY a.APP_ID, a.USER_ID ORDER BY sum(cb+sb) DESC
>>>
>>>This work fine in SQL Server but not in Oracle, if I replace ORDER BY
>>>sum(cb+sb) DESC with ORDER BY (cb+sb) DESC it works fine in Oracle but not
>>>SQL Server.  Any idea how I can achive the same results but one query to work
>>>in Oracle and SQL Server.  Thanks.
>>
Author
22 Dec 2005 10:52 PM
Gert-Jan Strik
Does this run in Oracle?

select a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB",
SUM(a.CA)
AS "CA", SUM(a.SA) AS "SA" FROM APP_USER a WHERE a.START_TIME >=
1135044000000 AND a.START_TIME < 1135047600000 AND a.APP_ID = 56 AND
GROUP_ID
= 50 GROUP BY a.APP_ID, a.USER_ID
ORDER BY SUM(a.CB)+SUM(a.SB) DESC

Gert-Jan

yodarules wrote:
Show quote
>
> I'm having some issuses with ORDER BY with my query.  I'm trying to run the
> same query in Oracle and SQL Server.
>
> select a.APP_ID, a.USER_ID, SUM(a.CB) AS "CB", SUM(a.SB) AS "SB", SUM(a.CA)
> AS "CA", SUM(a.SA) AS "SA" FROM APP_USER a WHERE a.START_TIME >=
> 1135044000000 AND a.START_TIME < 1135047600000 AND a.APP_ID = 56 AND GROUP_ID
> = 50 GROUP BY a.APP_ID, a.USER_ID ORDER BY sum(cb+sb) DESC
>
> This work fine in SQL Server but not in Oracle, if I replace ORDER BY
> sum(cb+sb) DESC with ORDER BY (cb+sb) DESC it works fine in Oracle but not
> SQL Server.  Any idea how I can achive the same results but one query to work
> in Oracle and SQL Server.  Thanks.

AddThis Social Bookmark Button