有一张基础数据表tab1,里面的字段很多,只列举有用的字段,
设备编号(dev_num),
统计码(statistical_code),
管理员号(admin_num),
统计日期(statistical_date),以天计算
交易金额(trade_money)
一天同一个设备号、统计码、管理员,发生交易很多次,交易金额不同,想要把同一个设备号、统计码号、管理员,一天发生的次数和金额总和记录到另一张表中tab2,
设备编号(dev_num),
统计码(statistical_code),
管理员号(admin_num),
统计日期(statistical_date),以天计算
交易金额总和(trade_sum)
交易次数(trade_times),
假设设备号有1000个,统计码有100个,请问怎么做呢,用一个sql语句很难实现,用存储过程怎么做或其他的方法,请高手指点。
设备编号(dev_num),
统计码(statistical_code),
管理员号(admin_num),
统计日期(statistical_date),以天计算
交易金额(trade_money)
一天同一个设备号、统计码、管理员,发生交易很多次,交易金额不同,想要把同一个设备号、统计码号、管理员,一天发生的次数和金额总和记录到另一张表中tab2,
设备编号(dev_num),
统计码(statistical_code),
管理员号(admin_num),
统计日期(statistical_date),以天计算
交易金额总和(trade_sum)
交易次数(trade_times),
假设设备号有1000个,统计码有100个,请问怎么做呢,用一个sql语句很难实现,用存储过程怎么做或其他的方法,请高手指点。
解决方案 »
- 一个数据库名字是: my_database,我想在本地复制一个my_database1,命令是什么,,
- 启动的时候显示Timeout error occurred trying to start MySQL Daemon.
- 请问如何安装与配置MySQL server 5.0
- 大家都用那种可视化工具建立MYSQL存储过程,触发器.phpmyAdmin好像不行.
- MYSQL数据库文件怎样移动?
- 求一条语句,如何将一列中的数据按字母顺序排列?
- 求数据库联查技巧
- 请教一条sql语句
- 安装mysql后的root密码设置问题
- 求SQL语句(修改时间)
- SQL同表中的字段数据复制代码
- MySQL的my.ini中的log会记录哪些内容?
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
还有你给我的答案,对我启发很大,很有用,已经满足我一半的要求了,就是交易次数怎么计算呢
我把表的一些约束去掉了,只贴上基本字段。
CREATE TABLE if not exists business_monitor_table(
dev_num varchar(15) not null, // 设备编号
statistical_code varchar(10) not null, // 统计码
response_code varchar(8) not null, // 应答码
out_account varchar(25), // 出款方账户
transit_account varchar(25), // 中转账户
in_account varchar(25), // 入款方账户
trade_money decimal(7,2), // 交易金额
admin_account varchar(25), // 管理员账号
trade_time datetime not null, // 交易时间
statistical_time datetime not null, // 统计时间
currency_code varchar(3) not null, // 货币代码
accept_bank_code varchar(20), // 受理行机构代码
accept_bank_inf varchar(20), // 受理行信息
sendout_bank_code varchar(20), // 发卡行机构代码
admin_num varchar(8) not null, // 管理员号
card_type varchar(3)); // 卡种类型CREATE TABLE day_settlement_table(
settlement_id int key primary key auto_increment, // 无意义主键
dev_num varchar(15) not null, // 设备编号
admin_num varchar(8) not null, // 管理员号
statistical_code varchar(10) not null, // 统计码
trade_times smallint not null, // 统计时间
trade_sum decimal(8,2), // 交易金额总和
statistical_date date not null); // 统计日期把business_monitor_table表中的数据进行计算后插入到day_settlement_table表中,主要是计算金额总和和交易次数。
e_times,trade_sum,statistical_date) select dev_num,admin_num,statistical_code,co
unt(*),sum(trade_money),statistical_time from business_monitor_table group by de
v_num,statistical_code,admin_num,statistical_time;我要加上统计时间 where DATE(statistical_time)='2010-09-07',应该加在什么地方
insert into day_settlement_table (dev_num,admin_num,statistical_code,trad
e_times,trade_sum,statistical_date) select dev_num,admin_num,statistical_code,co
unt(*),sum(trade_money),statistical_time from business_monitor_table where DATE(statistical_time)='2010-09-07' group by de
v_num,statistical_code,admin_num,statistical_time;问题已解决
CREATE TABLE day_settlement_table(
settlement_id int key primary key auto_increment, // 无意义主键
dev_num varchar(15) not null, // 设备编号
admin_num varchar(8) not null, // 管理员号
statistical_code varchar(10) not null, // 统计码
trade_times smallint not null, // 统计时间
trade_sum decimal(8,2), // 交易金额总和
statistical_date date not null); // 统计日期CREATE TABLE day_settlement_table(
dev_num varchar(15) not null, // 设备编号
admin_num varchar(8) not null, // 管理员号
statistical_code varchar(10) not null, // 统计码
trade_times smallint not null, // 统计时间
trade_sum decimal(8,2), // 交易金额总和
statistical_date date not null); // 统计日期这两个表的区别是第一个表比第二个表多了无意义的主键,这两个表哪一个更好,请指教