Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:32:57+00:00 2026-06-05T12:32:57+00:00

I’m using mysql version 5.1.41-3ubuntu12.10 (Ubuntu) . mysql> show create table tt\G *************************** 1.

  • 0

I’m using mysql version 5.1.41-3ubuntu12.10 (Ubuntu).

mysql> show create table tt\G
*************************** 1. row ***************************
       Table: tt
Create Table: CREATE TABLE `tt` (
  `pz` int(8) DEFAULT NULL,
  `os` varchar(8) DEFAULT NULL,
  `uz` int(11) NOT NULL,
  `p` bigint(21) NOT NULL DEFAULT '0',
  `c` decimal(23,0) DEFAULT NULL,
  KEY `pz` (`pz`),
  KEY `uz` (`uz`),
  KEY `os` (`os`),
  KEY `pz_2` (`pz`,`uz`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> select pz,uz,pz*uz,
    -> if(pz*uz,1,.5),
    -> left(pz,2) pl,left(lpad(uz,5,0),2) ul,
    -> p from tt limit 10;
+-------+----+-------+----------------+--------+----+--------+
|   pz  | uz | pz*uz | if(pz*uz,1,.5) |   pl   | ul |    p   |
+-------+----+-------+----------------+--------+----+--------+
|  NULL |  0 |  NULL |            0.5 | NULL   | 00 |   4080 |
|  NULL |  0 |  NULL |            0.5 | NULL   | 00 | 323754 |
| 89101 |  0 |     0 |            0.5 | 89     | 00 |   6880 |
|     0 |  0 |     0 |            0.5 | 0      | 00 |  11591 |
| 89110 |  0 |     0 |            0.5 | 89     | 00 |     72 |
| 78247 |  0 |     0 |            0.5 | 78     | 00 |     27 |
| 90062 |  0 |     0 |            0.5 | 90     | 00 |      5 |
| 63107 |  0 |     0 |            0.5 | 63     | 00 |      4 |
|  NULL |  0 |  NULL |            0.5 | NULL   | 00 |  54561 |
| 94102 |  0 |     0 |            0.5 | 94     | 00 |  12499 |
+-------+----+-------+----------------+--------+----+--------+

So far so good. As you see, 0.5 appears as a value of if(pz*uz,1,.5). The problem is:

mysql> select os,
    -> if(pz*uz,left(pz,2)<=>left(lpad(uz,5,0),2),.5) uptwo,
    -> if(pz*uz,left(pz,3)<=>left(lpad(uz,5,0),3),.5) upthree,
    -> sum(p) p,sum(c) c
    -> from tt t
    -> group by os,uptwo,upthree order by null;

+----+-------+---------+---------+-------+
| os | uptwo | upthree |    p    |   c   |
+----+-------+---------+---------+-------+
| u  |     1 |       1 |   52852 |   318 |
| i  |     1 |       1 | 7046563 | 21716 |
| m  |     1 |       1 | 1252166 |  7337 |
| i  |     0 |       0 | 1830284 |  4033 |
| m  |     0 |       0 |  294612 |  1714 |
| i  |     1 |       0 |  911486 |  3560 |
| m  |     1 |       0 |  145182 |  1136 |
| u  |     0 |       0 |   12144 |    23 |
| u  |     1 |       0 |    1571 |     8 |
+----+-------+---------+---------+-------+

Although I group by uptwo, 0.5 doesn’t appear in that column. What happened to the 0.5 values?


Edit: As noted in the comments to Todd Gibson’s answer, I also tried it with
if(pz*uz,cast(left(pz,2)<=>left(lpad(uz,5,0),2) as decimal),.5) instead of
if(pz*uz,left(pz,2)<=>left(lpad(uz,5,0),2),.5), but it, too, didn’t work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:33:00+00:00Added an answer on June 5, 2026 at 12:33 pm

    Instead of .5 as the false condition of the IF(), use 0.5.

    if(pz*uz,left(pz,2)<=>left(lpad(uz,5,0),2),0.5) uptwo
    
    if(pz*uz,left(pz,3)<=>left(lpad(uz,5,0),3),0.5) upthree
    

    I believe what is happening is that after the GROUP BY is evaluated, the values in the conditional column must be of the same data type. Since SELECT is evaluated after GROUP BY, the IF() is converting the returned values in favor of an integer (from the boolean expression), and thus your 0.5 gets rounded up to 1 but if you explicitly put a 0 in front of the decimal place, the IF() will treat returned values as a decimal including the result of the boolean expression (i.e. 1.0 or 0.0).

    Or you could even put single quotes around the .4 so that the column values will be treated as strings, so some values would appear to be integers and some decimals. The values should be automatically converted when used in numerical contexts (i.e. SELECT ('2.5' * 3.5) AS test #8.75).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.