Search code examples
mysqlcreate-table

how to set column type with create-select statement


I am trying to create a table with a select statement and that doesn't require me to set column types. However i need the column percent to be a float but what i am getting are 0 and 1s. How do i set that column as a float or decimal?

create table C as(
    select a.pid, b.date,
    ((select a.count(cost) where cost > '10')/(select a.count(cost))) AS percent
    from A a join B b
    on a.pid = b.pid
    group by 1,2,3);

my output is something like below but i need it the third col to be a float:

pid date percent
1   2015-01-01  1
2   2015-04-03  0

Solution

  • How about:

    create table C as(
        select a.pid, b.date,
        ((select a.count(cost)*1.000 where cost > '10')/(select a.count(cost))) AS percent
    from A a join B b
    on a.pid = b.pid
    group by 1,2,3);
    

    Edit: nevermind the above

    The following was tested and appears happy as a float.

    create table i1
    (
        h varchar(10),
        g varchar(10)
    );
    insert i1 (h,g) values ('a','b'),('a','b');
    
    -- drop table c;
    
    create table c (percent float not null) as 
    select h,count(*) as percent from i1
    
    describe c;