order by numeric value

order by numeric value

am 27.04.2010 23:52:46 von Keith Clark

I have the following statement:

select chart_of_accounts.accountname as Account,
concat('$',format(coalesce(sum(sales_journal_entries.debit), 0),2)) as
Debit,
concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0),2)) as
Credit,
concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0)-coalesce(sum(sales_journal_entries.debit),0),2)) as Balance
from sales_journal_entries
left join sales_journal
on sales_journal.journalID=sales_journal_entries.journalID
left join chart_of_accounts
on chart_of_accounts.accountID=sales_journal_entries.accountID
where sales_journal.date > '2008-12-31'
and sales_journal.date < '2010-01-01'
group by sales_journal_entries.accountID
order by Balance asc;

and I'd like the output to be sorted by the Balance according to the
numberic value, but it is sorting by the string result. I tried
abs(Balance) but I get the following error:

1247 Reference 'Balance' not supported (reference to group function)

I'm not sure I understand the error.


--
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: order by numeric value

am 28.04.2010 00:18:56 von Carsten Pedersen

Keith Clark skrev:
> I have the following statement:
>
> select chart_of_accounts.accountname as Account,
> concat('$',format(coalesce(sum(sales_journal_entries.debit), 0),2)) as
> Debit,
> concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0),2)) as
> Credit,
> concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0)-coalesce(sum(sales_journal_entries.debit),0),2)) as Balance
> from sales_journal_entries
> left join sales_journal
> on sales_journal.journalID=sales_journal_entries.journalID
> left join chart_of_accounts
> on chart_of_accounts.accountID=sales_journal_entries.accountID
> where sales_journal.date > '2008-12-31'
> and sales_journal.date < '2010-01-01'
> group by sales_journal_entries.accountID
> order by Balance asc;
>
> and I'd like the output to be sorted by the Balance according to the
> numberic value, but it is sorting by the string result. I tried
> abs(Balance) but I get the following error:
>
> 1247 Reference 'Balance' not supported (reference to group function)
>
> I'm not sure I understand the error.

Balance is the result of a string operation (concat), and abs is a
numeric function that won't work on strings.

You should add a field to the result with the numerical value of
Balance, and then sort on that.

/ Carsten


--
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: order by numeric value

am 28.04.2010 00:41:40 von Keith Clark

On Wed, 2010-04-28 at 00:18 +0200, Carsten Pedersen wrote:
> Keith Clark skrev:
> > I have the following statement:
> >
> > select chart_of_accounts.accountname as Account,
> > concat('$',format(coalesce(sum(sales_journal_entries.debit), 0),2)) as
> > Debit,
> > concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0),2)) as
> > Credit,
> > concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0)-coalesce(sum(sales_journal_entries.debit),0),2)) as Balance
> > from sales_journal_entries
> > left join sales_journal
> > on sales_journal.journalID=sales_journal_entries.journalID
> > left join chart_of_accounts
> > on chart_of_accounts.accountID=sales_journal_entries.accountID
> > where sales_journal.date > '2008-12-31'
> > and sales_journal.date < '2010-01-01'
> > group by sales_journal_entries.accountID
> > order by Balance asc;
> >
> > and I'd like the output to be sorted by the Balance according to the
> > numberic value, but it is sorting by the string result. I tried
> > abs(Balance) but I get the following error:
> >
> > 1247 Reference 'Balance' not supported (reference to group function)
> >
> > I'm not sure I understand the error.
>
> Balance is the result of a string operation (concat), and abs is a
> numeric function that won't work on strings.
>
> You should add a field to the result with the numerical value of
> Balance, and then sort on that.
>
> / Carsten
>
>
You cannot CONVERT at the order by statement?



--
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: order by numeric value

am 28.04.2010 00:46:15 von DaWiz

--COMODO_27_Apr_2010_16_46_16
Content-Type: text/plain;
format=flowed;
charset="utf-8";
reply-type=original
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Windows Mail 6.0.6002.18005
X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6002.18005

Try

order by CAST(Balance as decimal(8,2)) asc;


Cast will work in the order by.

Glenn Vaughn

----- Original Message -----
From: "Keith Clark"
To:
Sent: Tuesday, April 27, 2010 3:52 PM
Subject: order by numeric value


>I have the following statement:
>
> select chart_of_accounts.accountname as Account,
> concat('$',format(coalesce(sum(sales_journal_entries.debit), 0),2)) as
> Debit,
> concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0),2)) as
> Credit,
> concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0)-coalesce(sum(sales_journal_entries.debit),0),2))
> as Balance
> from sales_journal_entries
> left join sales_journal
> on sales_journal.journalID=sales_journal_entries.journalID
> left join chart_of_accounts
> on chart_of_accounts.accountID=sales_journal_entries.accountID
> where sales_journal.date > '2008-12-31'
> and sales_journal.date < '2010-01-01'
> group by sales_journal_entries.accountID
> order by Balance asc;
>
> and I'd like the output to be sorted by the Balance according to the
> numberic value, but it is sorting by the string result. I tried
> abs(Balance) but I get the following error:
>
> 1247 Reference 'Balance' not supported (reference to group function)
>
> I'm not sure I understand the error.
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=mysql@dawiz.net

--COMODO_27_Apr_2010_16_46_16
Content-Type: application/x-pkcs7-signature;
name="smime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="smime.p7s"

MIIHwgYJKoZIhvcNAQcCoIIHszCCB68CAQExCzAJBgUrDgMCGgUAMAsGCSqG SIb3DQEHAaCCBaQw
ggWgMIIEiKADAgECAhAYAofBaR8Q9AvLXcnn6IwMMA0GCSqGSIb3DQEBBQUA MIGuMQswCQYDVQQG
EwJVUzELMAkGA1UECBMCVVQxFzAVBgNVBAcTDlNhbHQgTGFrZSBDaXR5MR4w HAYDVQQKExVUaGUg
VVNFUlRSVVNUIE5ldHdvcmsxITAfBgNVBAsTGGh0dHA6Ly93d3cudXNlcnRy dXN0LmNvbTE2MDQG
A1UEAxMtVV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hkiG9w0BCQQxFgQU
RgYOcvUbNE9aMPGBzDoIBY8rsGMwDQYJKoZIhvcNAQEBBQAEgYASZed/7qzb 35qtfFG2/Z6ur/HE
bVUZOB42hY6C4/SiV2GM+4yIl25AGkucznjwJrb3PbjtAnF8GT1q6/CUo2Bt Akzt1tja2FHsibJ7
gcf+kqo+FJkTSFXsGApclc6POgguFeSFFoZAb7pvAHTfumgAT95sokwiJ4yP NtgL3namJA==

--COMODO_27_Apr_2010_16_46_16--

RE: order by numeric value

am 28.04.2010 00:49:46 von Gavin Towey

VGhhdCB3b24ndCB3b3JrIG9uIGEgdmFsdWUgbGlrZSAiJDEuMDAiDQoNCnNl bGVjdCBDQVNUKCck
MS4wMCcgYXMgZGVjaW1hbCg4LDIpKTsNCistLS0tLS0tLS0tLS0tLS0tLS0t LS0tLS0tLS0tLS0t
Kw0KfCBDQVNUKCckMS4wMCcgYXMgZGVjaW1hbCg4LDIpKSB8DQorLS0tLS0t LS0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0tLSsNCnwgICAgICAgICAgICAgICAgICAgICAgICAgIDAu MDAgfA0KKy0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0rDQoxIHJvdyBpbiBzZXQsIDEg d2FybmluZyAoMC4w
MCBzZWMpDQoNCg0KKy0tLS0tLS0tLSstLS0tLS0rLS0tLS0tLS0tLS0tLS0t LS0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0rDQp8IExldmVsICAgfCBDb2RlIHwgTWVzc2Fn ZSAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgIHwNCistLS0tLS0tLS0rLS0tLS0t Ky0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tKw0KfCBXYXJuaW5n IHwgMTI5MiB8IFRy
dW5jYXRlZCBpbmNvcnJlY3QgREVDSU1BTCB2YWx1ZTogJyQxLjAwJyB8DQor LS0tLS0tLS0tKy0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N1bm55dmFsZSwgQ0EgOTQw ODksIFVTQSwgRnJp
ZW5kRmluZGVyLmNvbQ0K

RE: order by numeric value

am 28.04.2010 00:53:57 von Keith Clark

But I'd prefer not to see the extra sorting field.

Keith

On Tue, 2010-04-27 at 15:49 -0700, Gavin Towey wrote:
> That won't work on a value like "$1.00"
>
> select CAST('$1.00' as decimal(8,2));
> +-------------------------------+
> | CAST('$1.00' as decimal(8,2)) |
> +-------------------------------+
> | 0.00 |
> +-------------------------------+
> 1 row in set, 1 warning (0.00 sec)
>
>
> +---------+------+------------------------------------------ --+
> | Level | Code | Message |
> +---------+------+------------------------------------------ --+
> | Warning | 1292 | Truncated incorrect DECIMAL value: '$1.00' |
> +---------+------+------------------------------------------ --+
> 1 row in set (0.00 sec)
>
>
> It would have to be something like:
>
> select CAST(REPLACE('$1.00','$','') as decimal(8,2)) ;
> +-----------------------------------------------+
> | CAST(REPLACE('$1.00','$','') as decimal(8,2)) |
> +-----------------------------------------------+
> | 1.00 |
> +-----------------------------------------------+
> 1 row in set (0.00 sec)
>
>
> Which in that case, it's better to just select balance without the dollar sign and order on that column.
>
> Regards,
> Gavin Towey
>
> -----Original Message-----
> From: DaWiz [mailto:dawiz@dawiz.net]
> Sent: Tuesday, April 27, 2010 3:46 PM
> To: Keith Clark; mysql@lists.mysql.com
> Subject: Re: order by numeric value
>
> Try
>
> order by CAST(Balance as decimal(8,2)) asc;
>
>
> Cast will work in the order by.
>
> Glenn Vaughn
>
> ----- Original Message -----
> From: "Keith Clark"
> To:
> Sent: Tuesday, April 27, 2010 3:52 PM
> Subject: order by numeric value
>
>
> >I have the following statement:
> >
> > select chart_of_accounts.accountname as Account,
> > concat('$',format(coalesce(sum(sales_journal_entries.debit), 0),2)) as
> > Debit,
> > concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0),2)) as
> > Credit,
> > concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0)-coalesce(sum(sales_journal_entries.debit),0),2))
> > as Balance
> > from sales_journal_entries
> > left join sales_journal
> > on sales_journal.journalID=sales_journal_entries.journalID
> > left join chart_of_accounts
> > on chart_of_accounts.accountID=sales_journal_entries.accountID
> > where sales_journal.date > '2008-12-31'
> > and sales_journal.date < '2010-01-01'
> > group by sales_journal_entries.accountID
> > order by Balance asc;
> >
> > and I'd like the output to be sorted by the Balance according to the
> > numberic value, but it is sorting by the string result. I tried
> > abs(Balance) but I get the following error:
> >
> > 1247 Reference 'Balance' not supported (reference to group function)
> >
> > I'm not sure I understand the error.
> >
> >
> > --
> > MySQL General Mailing List
> > For list archives: http://lists.mysql.com/mysql
> > To unsubscribe: http://lists.mysql.com/mysql?unsub=mysql@dawiz.net
>
> This message contains confidential information and is intended only for the individual named. If you are not the named addressee, you are notified that reviewing, disseminating, disclosing, copying or distributing this e-mail is strictly prohibited. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any loss or damage caused by viruses or errors or omissions in the contents of this message, which arise as a result of e-mail transmission. [FriendFinder Networks, Inc., 220 Humbolt
court, Sunnyvale, CA 94089, USA, FriendFinder.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: order by numeric value

am 28.04.2010 00:57:31 von Jesper Wisborg Krogh

On Wed, 28 Apr 2010 08:53:57 Keith Clark wrote:
> But I'd prefer not to see the extra sorting field.

You don't need to select a field in order to be able to order by it.

So

select chart_of_accounts.accountname as Account,
concat('$',format(coalesce(sum(sales_journal_entries.debit), 0),2)) as
Debit,
concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0),2)) as
Credit,
concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0)-coalesce(sum(sales_journal_entries.debit),0),2))
as Balance
from sales_journal_entries
left join sales_journal
on sales_journal.journalID=sales_journal_entries.journalID
left join chart_of_accounts
on chart_of_accounts.accountID=sales_journal_entries.accountID
where sales_journal.date > '2008-12-31'
and sales_journal.date < '2010-01-01'
group by sales_journal_entries.accountID
order by
coalesce(sum(sales_journal_entries.credit),0)-coalesce(sum(s ales_journal_entries.debit),0)
asc;

should do the trick.

Jesper

--
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: order by numeric value

am 28.04.2010 01:02:49 von Keith Clark

On Wed, 2010-04-28 at 08:57 +1000, Jesper Wisborg Krogh wrote:
> On Wed, 28 Apr 2010 08:53:57 Keith Clark wrote:
> > But I'd prefer not to see the extra sorting field.
>
> You don't need to select a field in order to be able to order by it.
>
> So
>
> select chart_of_accounts.accountname as Account,
> concat('$',format(coalesce(sum(sales_journal_entries.debit), 0),2)) as
> Debit,
> concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0),2)) as
> Credit,
> concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0)-coalesce(sum(sales_journal_entries.debit),0),2))
> as Balance
> from sales_journal_entries
> left join sales_journal
> on sales_journal.journalID=sales_journal_entries.journalID
> left join chart_of_accounts
> on chart_of_accounts.accountID=sales_journal_entries.accountID
> where sales_journal.date > '2008-12-31'
> and sales_journal.date < '2010-01-01'
> group by sales_journal_entries.accountID
> order by
> coalesce(sum(sales_journal_entries.credit),0)-coalesce(sum(s ales_journal_entries.debit),0)
> asc;
>
> should do the trick.
>
> Jesper
>
Perfectamundo! I thought there would have been a more elegant way but
this works just fine. Thanks.

Keith



--
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: order by numeric value

am 28.04.2010 16:04:38 von Martijn Tonies

>> > But I'd prefer not to see the extra sorting field.
>>
>> You don't need to select a field in order to be able to order by it.
>>
>> So
>>
>> select chart_of_accounts.accountname as Account,
>> concat('$',format(coalesce(sum(sales_journal_entries.debit), 0),2)) as
>> Debit,
>> concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0),2)) as
>> Credit,
>> concat('$',format(coalesce(sum(sales_journal_entries.credit) ,0)-coalesce(sum(sales_journal_entries.debit),0),2))
>> as Balance
>> from sales_journal_entries
>> left join sales_journal
>> on sales_journal.journalID=sales_journal_entries.journalID
>> left join chart_of_accounts
>> on chart_of_accounts.accountID=sales_journal_entries.accountID
>> where sales_journal.date > '2008-12-31'
>> and sales_journal.date < '2010-01-01'
>> group by sales_journal_entries.accountID
>> order by
>> coalesce(sum(sales_journal_entries.credit),0)-coalesce(sum(s ales_journal_entries.debit),0)
>> asc;
>>
>> should do the trick.
>>
>> Jesper
>>
> Perfectamundo! I thought there would have been a more elegant way but
> this works just fine. Thanks.

There is a more elegant way: do not do your $ at the database, but rather
in the presentation layer.

With regards,

Martijn Tonies
Upscene Productions
http://www.upscene.com

Download Database Workbench for Oracle, MS SQL Server, Sybase SQL
Anywhere, MySQL, InterBase, NexusDB and Firebird!

Database questions? Check the forum:
http://www.databasedevelopmentforum.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