请 sql 老司机 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
xi2008wang
V2EX    MySQL

请 sql 老司机

  •  
  •   xi2008wang 2017-12-08 11:21:15 +08:00 4287 次点击
    这是一个创建于 2914 天前的主题,其中的信息可能已经有所发展或是发生改变。

    怎么写 sql 将数字转成占比?

    20171101 A 3

    20171101 B 5

    20171102 A 7

    20171102 B 3

    =》

    20171101 A 3/3+5

    20171101 B 5/3+5

    20171102 A 7/3+7

    20171102 B 3/3+7

    10 条回复    2017-12-08 12:06:06 +08:00
    yulitian888
        1
    yulitian888  
       2017-12-08 11:27:06 +08:00
    第一反应是开窗函数,然后看了下是 MySQL,呃,不清楚有没有这个功能,我说的是 Server server
    master13
        2
    master13  
       2017-12-08 11:38:49 +08:00
    这种问题自行谷歌或者请新司机解答,老司机不开你这条路
    whx20202
        3
    whx20202  
       2017-12-08 11:44:34 +08:00
    select col1, col2, 字符串拼接函数( col3,const1,const2 ) from tbl
    就可以吧

    如果要求数字运算可以搜一下
    slgz
        4
    slgz  
       2017-12-08 11:46:06 +08:00
    我都没看懂题
    xi2008wang
        5
    xi2008wang  
    OP
       2017-12-08 11:50:23 +08:00
    @master13 提问之前我已经 google 了
    mooncakejs
        6
    mooncakejs  
       2017-12-08 11:52:06 +08:00
    小学奥数找规律? 每日 /B 占比?
    不开窗不好搞。
    xi2008wang
        7
    xi2008wang  
    OP
       2017-12-08 11:57:04 +08:00
    咨询部门同事,得到了一个效率不太高的 sql,
    select a.date,a.value1,a.value1/b.sum_v from x a,(select date,sum(value1) as sum_v from x ) b where a.date=b.date;
    mooncakejs
        8
    mooncakejs  
       2017-12-08 11:59:33 +08:00
    支持开窗:
    select *,num/sum(num) over d as pct from test window d as (partition by date)
    不支持:
    select t.*,t.num/g.sum as pct from test t join (select date,sum(num) from test group by date) g on t.date = g.date
    xi2008wang
        9
    xi2008wang  
    OP
       2017-12-08 12:05:35 +08:00
    @xi2008wang 刚刚漏了 group by

    select a.date,a.value1,a.value1/b.sum_v from x a,(select date,sum(value1) as sum_v from x group by date) b where a.date=b.date;
    xi2008wang
        10
    xi2008wang  
    OP
       2017-12-08 12:06:06 +08:00
    @mooncakejs 先谢了,我试试
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1123 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 17:08 PVG 01:08 LAX 09:08 JFK 12:08
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86