求代码,谢谢

penguinhzf 2019-07-08 11:38:06
请求以下代码:

假设我有一个数据库,里面有以下数据

CREATE TABLE IF NOT EXISTS `tb_shareinfo` (
`fd_Code` varchar(10) NOT NULL COMMENT ' 股票代码',
`fd_Name` varchar(20) CHARACTER SET utf8 NOT NULL COMMENT ' 股票名称',
`fd_OpeningPrice` float NOT NULL COMMENT ' 开盘价',
`fd_ClosingPrice` float NOT NULL COMMENT ' 收盘价',
`fd_NowPrice` float NOT NULL COMMENT ' 当前价格',
`fd_HighPrice` float NOT NULL COMMENT ' 最高价',
`fd_LowPrice` float NOT NULL COMMENT ' 最低价',
`fd_UpdateDate` varchar(10) NOT NULL COMMENT ' 获取日期',
PRIMARY KEY (`fd_Code`,`fd_UpdateDate`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

求以下代码:
我如何根据一定的时间内(假设100天),计算每一只股票的“当前价格”与指定日期内(即前面提到的100天)的平均“当前价格”的比值最高的前10只股票呢?

请根据我的表写出代码,谢谢各位~~~
...全文
1659 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 7 楼 penguinhzf的回复:
[quote=引用 6 楼 Edmond1023 的回复:] [quote=引用 5 楼 penguinhzf 的回复:] [quote=引用 4 楼 Edmond1023 的回复:] select t.fd_Code,t.fd_Name,t.fd_NowPrice, t1.avg_price, t.fd_NowPrice/t1.avg_price e_value (select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(partition by fd_Code order by fd_UpdateDate desc) rn from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' ) t where t.rn=1 ) t -------当前价格 left join ( select fd_Code,fd_Name, sum(fd_NowPrice)/count(1) avg_price --平均价格 from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' group by fd_Code,fd_Name ) t1 on t.fd_Code=t1.fd_Code order by t.fd_NowPrice/t1.avg_price desc limit 10; 逻辑是 t表获取最新的股票价格,t1获取统计平均值,然后关联计算
非常感谢你的回复,但我运行发现抱错,错误如下: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(par' at line 4 请问是什么问题导致呢,谢谢~[/quote] 第四行 少了一个 from 抱歉啊[/quote] 再次感谢你的回复,这次我加了from之后,还是会报错,错误如下: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(partition by fd_Code order by fd_UpdateDate desc) rn from tb_shareinfo where' at line 7 请麻烦再帮忙看看,谢谢~~~[/quote] 有个语法错误,在第七行
  • 打赏
  • 举报
回复
引用 5 楼 penguinhzf的回复:
[quote=引用 4 楼 Edmond1023 的回复:] select t.fd_Code,t.fd_Name,t.fd_NowPrice, t1.avg_price, t.fd_NowPrice/t1.avg_price e_value (select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(partition by fd_Code order by fd_UpdateDate desc) rn from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' ) t where t.rn=1 ) t -------当前价格 left join ( select fd_Code,fd_Name, sum(fd_NowPrice)/count(1) avg_price --平均价格 from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' group by fd_Code,fd_Name ) t1 on t.fd_Code=t1.fd_Code order by t.fd_NowPrice/t1.avg_price desc limit 10; 逻辑是 t表获取最新的股票价格,t1获取统计平均值,然后关联计算
非常感谢你的回复,但我运行发现抱错,错误如下: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(par' at line 4 请问是什么问题导致呢,谢谢~[/quote] 像是语法错误,应该在第四行
不掉发 2019-07-14
  • 打赏
  • 举报
回复
引用 5 楼 penguinhzf 的回复:
[quote=引用 4 楼 Edmond1023 的回复:] select t.fd_Code,t.fd_Name,t.fd_NowPrice, t1.avg_price, t.fd_NowPrice/t1.avg_price e_value (select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(partition by fd_Code order by fd_UpdateDate desc) rn from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' ) t where t.rn=1 ) t -------当前价格 left join ( select fd_Code,fd_Name, sum(fd_NowPrice)/count(1) avg_price --平均价格 from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' group by fd_Code,fd_Name ) t1 on t.fd_Code=t1.fd_Code order by t.fd_NowPrice/t1.avg_price desc limit 10; 逻辑是 t表获取最新的股票价格,t1获取统计平均值,然后关联计算
非常感谢你的回复,但我运行发现抱错,错误如下: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(par' at line 4 请问是什么问题导致呢,谢谢~[/quote]sql语句
penguinhzf 2019-07-12
  • 打赏
  • 举报
回复
引用 11 楼 Edmond1023 的回复:
那你改成大于小于好了
好,我试试,感谢你的回复~
penguinhzf 2019-07-12
  • 打赏
  • 举报
回复
引用 13 楼 文修 的回复:
楼主你好,出现 #1270 - Illegal mix of collations (latin1_swedish_ci,IMPLICIT), (utf8_general_ci,COERCIBLE), (utf8_general_ci,COERCIBLE) for operation 'between'的问题,是你数据库的编码问题,参考下面文档改一下即可 https://blog.csdn.net/Yetmoon/article/details/79941866 另外多说几句 1、mysql安装玩之后进来更改配置文件里面的编码方式为utf8,建数据库时也尽量选用uft8,除非你的数据库没有一点中文,否则多少会出 一点和编码相关的问题 2、又是或过于长的sql语句并不是高效率的,向楼上有位仁兄的建议可以适当采纳,先查出部分数据,通过程序得到想要的结果,因为sql的连接速度是慢于程序处理的,尤其是表数据比较多,差距就更加明显。
好的,明白
penguinhzf 2019-07-10
  • 打赏
  • 举报
回复
引用 6 楼 Edmond1023 的回复:
[quote=引用 5 楼 penguinhzf 的回复:] [quote=引用 4 楼 Edmond1023 的回复:] select t.fd_Code,t.fd_Name,t.fd_NowPrice, t1.avg_price, t.fd_NowPrice/t1.avg_price e_value (select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(partition by fd_Code order by fd_UpdateDate desc) rn from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' ) t where t.rn=1 ) t -------当前价格 left join ( select fd_Code,fd_Name, sum(fd_NowPrice)/count(1) avg_price --平均价格 from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' group by fd_Code,fd_Name ) t1 on t.fd_Code=t1.fd_Code order by t.fd_NowPrice/t1.avg_price desc limit 10; 逻辑是 t表获取最新的股票价格,t1获取统计平均值,然后关联计算
非常感谢你的回复,但我运行发现抱错,错误如下: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(par' at line 4 请问是什么问题导致呢,谢谢~[/quote] 第四行 少了一个 from 抱歉啊[/quote] 再次感谢你的回复,这次我加了from之后,还是会报错,错误如下: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(partition by fd_Code order by fd_UpdateDate desc) rn from tb_shareinfo where' at line 7 请麻烦再帮忙看看,谢谢~~~
Edmond1023 2019-07-10
  • 打赏
  • 举报
回复
引用 5 楼 penguinhzf 的回复:
[quote=引用 4 楼 Edmond1023 的回复:] select t.fd_Code,t.fd_Name,t.fd_NowPrice, t1.avg_price, t.fd_NowPrice/t1.avg_price e_value (select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(partition by fd_Code order by fd_UpdateDate desc) rn from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' ) t where t.rn=1 ) t -------当前价格 left join ( select fd_Code,fd_Name, sum(fd_NowPrice)/count(1) avg_price --平均价格 from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' group by fd_Code,fd_Name ) t1 on t.fd_Code=t1.fd_Code order by t.fd_NowPrice/t1.avg_price desc limit 10; 逻辑是 t表获取最新的股票价格,t1获取统计平均值,然后关联计算
非常感谢你的回复,但我运行发现抱错,错误如下: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(par' at line 4 请问是什么问题导致呢,谢谢~[/quote] 第四行 少了一个 from 抱歉啊
penguinhzf 2019-07-10
  • 打赏
  • 举报
回复
引用 4 楼 Edmond1023 的回复:
select t.fd_Code,t.fd_Name,t.fd_NowPrice, t1.avg_price, t.fd_NowPrice/t1.avg_price e_value (select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(partition by fd_Code order by fd_UpdateDate desc) rn from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' ) t where t.rn=1 ) t -------当前价格 left join ( select fd_Code,fd_Name, sum(fd_NowPrice)/count(1) avg_price --平均价格 from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' group by fd_Code,fd_Name ) t1 on t.fd_Code=t1.fd_Code order by t.fd_NowPrice/t1.avg_price desc limit 10; 逻辑是 t表获取最新的股票价格,t1获取统计平均值,然后关联计算
非常感谢你的回复,但我运行发现抱错,错误如下: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(par' at line 4 请问是什么问题导致呢,谢谢~
文修 2019-07-10
  • 打赏
  • 举报
回复
楼主你好,出现 #1270 - Illegal mix of collations (latin1_swedish_ci,IMPLICIT), (utf8_general_ci,COERCIBLE), (utf8_general_ci,COERCIBLE) for operation 'between'的问题,是你数据库的编码问题,参考下面文档改一下即可 https://blog.csdn.net/Yetmoon/article/details/79941866 另外多说几句 1、mysql安装玩之后进来更改配置文件里面的编码方式为utf8,建数据库时也尽量选用uft8,除非你的数据库没有一点中文,否则多少会出 一点和编码相关的问题 2、又是或过于长的sql语句并不是高效率的,向楼上有位仁兄的建议可以适当采纳,先查出部分数据,通过程序得到想要的结果,因为sql的连接速度是慢于程序处理的,尤其是表数据比较多,差距就更加明显。
Edmond1023 2019-07-10
  • 打赏
  • 举报
回复
那你改成大于小于好了
penguinhzf 2019-07-10
  • 打赏
  • 举报
回复
引用 8 楼 Edmond1023 的回复:
select t.fd_Code,t.fd_Name,t.fd_NowPrice, t1.avg_price, t.fd_NowPrice/t1.avg_price e_value from (select fd_Code, fd_Name, fd_NowPrice from (select fd_Code, fd_Name, fd_NowPrice from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' order by fd_UpdateDate desc) t group by fd_Code,fd_Name ) t left join (select fd_Code, fd_Name, sum(fd_NowPrice)/count(1) avg_price from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' group by fd_Code,fd_Name ) t1 on t.fd_Code=t1.fd_Code order by t.fd_NowPrice/t1.avg_price desc limit 10;
还是出现问题,是不是版本不支持呢? #1270 - Illegal mix of collations (latin1_swedish_ci,IMPLICIT), (utf8_general_ci,COERCIBLE), (utf8_general_ci,COERCIBLE) for operation 'between' 我是用phpnow 1.56安装的,版本是 Apache/2.2.16 (Win32) PHP/5.2.14 MySQL 支持 Yes / client lib version 5.1.50
Edmond1023 2019-07-10
  • 打赏
  • 举报
回复
原因是 mysql 没有开窗函数
Edmond1023 2019-07-10
  • 打赏
  • 举报
回复
select t.fd_Code,t.fd_Name,t.fd_NowPrice, t1.avg_price, t.fd_NowPrice/t1.avg_price e_value from (select fd_Code, fd_Name, fd_NowPrice from (select fd_Code, fd_Name, fd_NowPrice from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' order by fd_UpdateDate desc) t group by fd_Code,fd_Name ) t left join (select fd_Code, fd_Name, sum(fd_NowPrice)/count(1) avg_price from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' group by fd_Code,fd_Name ) t1 on t.fd_Code=t1.fd_Code order by t.fd_NowPrice/t1.avg_price desc limit 10;
Edmond1023 2019-07-09
  • 打赏
  • 举报
回复
select t.fd_Code,t.fd_Name,t.fd_NowPrice, t1.avg_price, t.fd_NowPrice/t1.avg_price e_value (select t.* from (select fd_Code,fd_Name,fd_NowPrice , row_number() over(partition by fd_Code order by fd_UpdateDate desc) rn from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' ) t where t.rn=1 ) t -------当前价格 left join ( select fd_Code,fd_Name, sum(fd_NowPrice)/count(1) avg_price --平均价格 from tb_shareinfo where fd_UpdateDate between '起始时间' and '结束时间' group by fd_Code,fd_Name ) t1 on t.fd_Code=t1.fd_Code order by t.fd_NowPrice/t1.avg_price desc limit 10; 逻辑是 t表获取最新的股票价格,t1获取统计平均值,然后关联计算
心怀啊 2019-07-08
  • 打赏
  • 举报
回复
根据日期排序然后查询出前一百,然后根据股票价格排序查询出前10就行了
penguinhzf 2019-07-08
  • 打赏
  • 举报
回复
引用 1 楼 心怀啊 的回复:
根据日期排序然后查询出前一百,然后根据股票价格排序查询出前10就行了
估计还没有明白我意思了,我意思是首先要按照每一个股票来计算它的平均当前价格,然后再用“当前价格”除以求出的平均当前价格,然后再按照这个值,谁最高的,排在第一位,然后再找出前10位,分开的代码我会写,但希望找到更简便更高效的代码,谢谢!
penguinhzf 2019-07-08
  • 打赏
  • 举报
回复
估计还没有明白我意思了,我意思是首先要按照每一个股票来计算它的平均当前价格,然后再用“当前价格”除以求出的平均当前价格,然后再按照这个值,谁最高的,排在第一位,然后再找出前10位,分开的代码我会写,但希望找到更简便更高效的代码,谢谢!

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧