sub query or something else

sub query or something else

am 04.09.2009 04:46:42 von uYe

I have these query:
SELECT SUM(price)*0.5 AS price1 FROM table WHERE partner = 'A';
SELECT SUM(price)*0.65 AS price2 FROM table WHERE partner = 'B';
Is it possible to make the queries into 1 single query? How to make it
happen? Many thanks for helps.



Willy


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org

Re: sub query or something else

am 04.09.2009 05:11:00 von Robert Citek

It's not clear what exactly you are looking for. Two possible solutions:

1) use a union

2) use a join with another table containing partner and factor fields.

Can you give a short example of what the input looks like and what you
would like the output to look like?

Regards,
- Robert

On Thu, Sep 3, 2009 at 10:46 PM, sangprabv wrote:
> I have these query:
> SELECT SUM(price)*0.5 AS price1 FROM table WHERE partner = 'A';
> SELECT SUM(price)*0.65 AS price2 FROM table WHERE partner = 'B';
> Is it possible to make the queries into 1 single query? How to make it
> happen? Many thanks for helps.

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org

Re: sub query or something else

am 04.09.2009 05:47:17 von uYe

What I'm looking for is to SUM the price from partner A and B with each
result. So the result I expect is like "Partner A total's price 123,
Partner B total's price 456". Can you give me the query example? TIA.



Willy


On Thu, 2009-09-03 at 23:11 -0400, Robert Citek wrote:
> It's not clear what exactly you are looking for. Two possible solutions:
>
> 1) use a union
>
> 2) use a join with another table containing partner and factor fields.
>
> Can you give a short example of what the input looks like and what you
> would like the output to look like?
>
> Regards,
> - Robert
>
> On Thu, Sep 3, 2009 at 10:46 PM, sangprabv wrote:
> > I have these query:
> > SELECT SUM(price)*0.5 AS price1 FROM table WHERE partner = 'A';
> > SELECT SUM(price)*0.65 AS price2 FROM table WHERE partner = 'B';
> > Is it possible to make the queries into 1 single query? How to make it
> > happen? Many thanks for helps.
>


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org

Re: sub query or something else

am 04.09.2009 06:19:41 von Colin Streicher

Because these are two quite distinct queries, I don't see an immediate way of
joining them that would make them more efficient. Something that comes to mind
are sub-select statements for example, but that would make this more complex
than it needs to be.

Like Robert said, you aren't giving us a great deal to go on. just knowing the
output expected is nowhere near enough, If these are the only two partners in
the table, perhaps an aggregate would serve your purpose, If you are looking
at simplifiying your query, perhaps a stored proc is more what you are after.

With more data, we can probably be more helpful.

Colin


On Thursday 03 September 2009 11:47:17 pm sangprabv wrote:
> What I'm looking for is to SUM the price from partner A and B with each
> result. So the result I expect is like "Partner A total's price 123,
> Partner B total's price 456". Can you give me the query example? TIA.
>
>
>
> Willy
>
> On Thu, 2009-09-03 at 23:11 -0400, Robert Citek wrote:
> > It's not clear what exactly you are looking for. Two possible solutions:
> >
> > 1) use a union
> >
> > 2) use a join with another table containing partner and factor fields.
> >
> > Can you give a short example of what the input looks like and what you
> > would like the output to look like?
> >
> > Regards,
> > - Robert
> >
> > On Thu, Sep 3, 2009 at 10:46 PM, sangprabv wrote:
> > > I have these query:
> > > SELECT SUM(price)*0.5 AS price1 FROM table WHERE partner = 'A';
> > > SELECT SUM(price)*0.65 AS price2 FROM table WHERE partner = 'B';
> > > Is it possible to make the queries into 1 single query? How to make it
> > > happen? Many thanks for helps.
>

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org

Re: sub query or something else

am 04.09.2009 07:33:01 von Manasi Save

may be you can use IN clause:

SELECT SUM(price)*0.5 AS price1, SUM(price)*0.65 AS price2 FROM table
WHERE partner IN ('A', 'B');

--
Thanks and Regards,
Manasi Save
Artificial Machines Pvt Ltd.


> I have these query:
> SELECT SUM(price)*0.5 AS price1 FROM table WHERE partner = 'A';
> SELECT SUM(price)*0.65 AS price2 FROM table WHERE partner = 'B';
> Is it possible to make the queries into 1 single query? How to make it
> happen? Many thanks for helps.
>
>
>
> Willy
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=manasi.save@artificialmac hines.com
>
>



--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org

Re: sub query or something else

am 04.09.2009 08:09:25 von Wolfgang Schaefer

sangprabv wrote:
> I have these query:
> SELECT SUM(price)*0.5 AS price1 FROM table WHERE partner = 'A';
> SELECT SUM(price)*0.65 AS price2 FROM table WHERE partner = 'B';
> Is it possible to make the queries into 1 single query? How to make it
> happen? Many thanks for helps.
>
>
>
> Willy
>
>
>
You can group by partners and then calculate the price for the certain
partner, if that is what you want.

SELECT partner, IF(partner = 'A', sum(price)*0.5, '-') as price1,
IF(partner = 'B', sum(price)*0.65, '-')
as price2
FROM table
WHERE partner IN ('A', 'B')
GROUP BY partner;

cheers,
wolfgang

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org

Re: sub query or something else

am 04.09.2009 09:19:12 von uYe

Many thanks for the query. It works ;)



Willy


On Fri, 2009-09-04 at 08:09 +0200, Wolfgang Schaefer wrote:
> sangprabv wrote:
> > I have these query:
> > SELECT SUM(price)*0.5 AS price1 FROM table WHERE partner = 'A';
> > SELECT SUM(price)*0.65 AS price2 FROM table WHERE partner = 'B';
> > Is it possible to make the queries into 1 single query? How to make it
> > happen? Many thanks for helps.
> >
> >
> >
> > Willy
> >
> >
> >
> You can group by partners and then calculate the price for the certain
> partner, if that is what you want.
>
> SELECT partner, IF(partner = 'A', sum(price)*0.5, '-') as price1,
> IF(partner = 'B', sum(price)*0.65, '-')
> as price2
> FROM table
> WHERE partner IN ('A', 'B')
> GROUP BY partner;
>
> cheers,
> wolfgang


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org

Re: sub query or something else

am 04.09.2009 09:20:18 von uYe

Many thanks for your query, seems we need to group it like Wolfgang's
does.



Willy


On Thu, 2009-09-03 at 22:33 -0700, Manasi Save wrote:
> may be you can use IN clause:
>
> SELECT SUM(price)*0.5 AS price1, SUM(price)*0.65 AS price2 FROM table
> WHERE partner IN ('A', 'B');
>


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org