还有一个问题,比如 A表 ( ID, //ID trade //交易代码 其中可选 1,2 ) 现在我要获取如下样式 ID 代号为1交易量总量 代码为1的交易比率 代码为2的交易总量 代码为2的交易比率代码1的交易比率=代码1的交易总量/总的交易总量 像这样的一个东西,SQL该怎么写比较高效呢。 我写的好像不是很高效。 select a.ID ,b.tradecount ,b.tradecount/a.tradecount,c.tradecount ,c.tradecount /a.tradecount from (select id,count(trade) as tradecount from A group by id)a ,(select id,count(trade) as tradecount from A where trade=1 group by id)b, ( select id,count(trade) as tradecount from A group by id)c where a.id=b.id and a.id=c.id
A表
(
ID, //ID
trade //交易代码 其中可选 1,2
)
现在我要获取如下样式
ID 代号为1交易量总量 代码为1的交易比率 代码为2的交易总量 代码为2的交易比率代码1的交易比率=代码1的交易总量/总的交易总量
像这样的一个东西,SQL该怎么写比较高效呢。
我写的好像不是很高效。 select a.ID ,b.tradecount ,b.tradecount/a.tradecount,c.tradecount ,c.tradecount /a.tradecount from (select id,count(trade) as tradecount from A group by id)a ,(select id,count(trade) as tradecount from A where trade=1 group by id)b, ( select id,count(trade) as tradecount from A group by id)c where a.id=b.id and a.id=c.id
发现行转列的效果不怎么好,统计同一个视图。用我上面的方法,才耗时9秒,用行转列以后,查询需要20秒。