def show_query1_result(self): # 查询数据 db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset') cursor = db.cursor() db_sql = """ SELECT *,salary + weight_reward total_salary from ( SELECT a.user_id,user_name,get_time,get_kg,efficiency,CONCAT(ROUND(ROUND(yield_rate,4) * 100,2),'%') yield_rate,ROUND(get_kg * 2 * price,1) salary,CASE WHEN yield_rate > 0.64 and get_kg < 40 THEN kg1_price WHEN yield_rate > 0.64 and get_kg < 50 THEN kg2_price WHEN yield_rate > 0.64 and get_kg >= 50 THEN kg3_price WHEN yield_rate < 0.64 THEN 0 END as weight_reward FROM (SELECT user_id, DATE_FORMAT(get_time,'%Y-%m-%d') get_time, SUM(get_kg) get_kg,round(SUM(get_kg)/(SUM(duration_time)/3600),2) efficiency,AVG(yield_rate) yield_rate FROM em_salary WHERE DATE_FORMAT(get_time,'%Y-%m-%d') = DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 0 DAY),'%Y-%m-%d') and recycle_kg IS NOT NULL GROUP BY user_id, DATE_FORMAT(get_time,'%Y-%m-%d')) a LEFT JOIN (SELECT user_id,`name` as user_name,price,kg1_price,kg2_price,kg3_price,yield_price FROM `employee_table` CROSS JOIN price_data ) d on a.user_id = d.user_id ) T """ cursor.execute(db_sql) result = cursor.fetchall() db.close() if len(result) == 0: QMessageBox.information(self, "提示", "今日无员工工资记录") return # 清空表格 self.query1_window.table_widget.setRowCount(0) self.query1_window.table_widget.setColumnCount(len(result[0])) self.query1_window.table_widget.setHorizontalHeaderLabels( ["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"]) # 添加数据到表格 for row_num, row_data in enumerate(result): self.query1_window.table_widget.insertRow(row_num) for col_num, col_data in enumerate(row_data): self.query1_window.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data))) # 显示窗口 self.query1_window.show()能不能在最后一列显示当日总工资汇总
时间: 2023-11-27 22:52:08 浏览: 89
可以在最后一列显示当日总工资汇总,你可以在查询数据时,添加一行代码来计算所有员工的当日总工资汇总,并将其添加到结果集中。然后在添加数据到表格时,将当日总工资列添加到表格中即可。以下是示例代码:
```
db_sql = """
SELECT *,
salary + weight_reward total_salary
from (
SELECT a.user_id,user_name,get_time,get_kg,efficiency,
CONCAT(ROUND(ROUND(yield_rate,4) * 100,2),'%') yield_rate,
ROUND(get_kg * 2 * price,1) salary,
CASE
WHEN yield_rate > 0.64 and get_kg < 40 THEN kg1_price
WHEN yield_rate > 0.64 and get_kg < 50 THEN kg2_price
WHEN yield_rate > 0.64 and get_kg >= 50 THEN kg3_price
WHEN yield_rate < 0.64 THEN 0
END as weight_reward
FROM (SELECT user_id, DATE_FORMAT(get_time,'%Y-%m-%d') get_time, SUM(get_kg) get_kg,
round(SUM(get_kg)/(SUM(duration_time)/3600),2) efficiency,AVG(yield_rate) yield_rate
FROM em_salary WHERE DATE_FORMAT(get_time,'%Y-%m-%d') = DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 0 DAY),'%Y-%m-%d') and recycle_kg IS NOT NULL
GROUP BY user_id, DATE_FORMAT(get_time,'%Y-%m-%d')) a
LEFT JOIN (SELECT user_id,`name` as user_name,price,kg1_price,kg2_price,kg3_price,yield_price
FROM `employee_table` CROSS JOIN price_data ) d on a.user_id = d.user_id
) T
"""
# 计算当日总工资汇总
db_sql_sum = """
SELECT SUM(salary + weight_reward) as total_salary_sum
from (
SELECT a.user_id,user_name,get_time,get_kg,efficiency,
CONCAT(ROUND(ROUND(yield_rate,4) * 100,2),'%') yield_rate,
ROUND(get_kg * 2 * price,1) salary,
CASE
WHEN yield_rate > 0.64 and get_kg < 40 THEN kg1_price
WHEN yield_rate > 0.64 and get_kg < 50 THEN kg2_price
WHEN yield_rate > 0.64 and get_kg >= 50 THEN kg3_price
WHEN yield_rate < 0.64 THEN 0
END as weight_reward
FROM (SELECT user_id, DATE_FORMAT(get_time,'%Y-%m-%d') get_time, SUM(get_kg) get_kg,
round(SUM(get_kg)/(SUM(duration_time)/3600),2) efficiency,AVG(yield_rate) yield_rate
FROM em_salary WHERE DATE_FORMAT(get_time,'%Y-%m-%d') = DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 0 DAY),'%Y-%m-%d') and recycle_kg IS NOT NULL
GROUP BY user_id, DATE_FORMAT(get_time,'%Y-%m-%d')) a
LEFT JOIN (SELECT user_id,`name` as user_name,price,kg1_price,kg2_price,kg3_price,yield_price
FROM `employee_table` CROSS JOIN price_data ) d on a.user_id = d.user_id
) T
"""
cursor.execute(db_sql)
result = cursor.fetchall()
cursor.execute(db_sql_sum)
sum_result = cursor.fetchone()
total_salary_sum = sum_result[0] # 获取当日总工资汇总
db.close()
if len(result) == 0:
QMessageBox.information(self, "提示", "今日无员工工资记录")
return
# 清空表格
self.query1_window.table_widget.setRowCount(0)
self.query1_window.table_widget.setColumnCount(len(result[0]) + 1) # 增加一列用于显示当日总工资
self.query1_window.table_widget.setHorizontalHeaderLabels(
["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"])
# 添加数据到表格
for row_num, row_data in enumerate(result):
self.query1_window.table_widget.insertRow(row_num)
for col_num, col_data in enumerate(row_data):
self.query1_window.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data)))
# 添加当日总工资到表格
total_salary_item = QTableWidgetItem(str(total_salary_sum))
self.query1_window.table_widget.setItem(row_num, len(result[0]), total_salary_item)
# 添加当日总工资行到表格底部
self.query1_window.table_widget.insertRow(len(result))
for i in range(len(result[0]) + 1):
if i == len(result[0]):
total_salary_label = QTableWidgetItem("当日总工资")
self.query1_window.table_widget.setItem(len(result), i, total_salary_label)
else:
self.query1_window.table_widget.setItem(len(result), i, QTableWidgetItem(""))
total_salary_item = QTableWidgetItem(str(total_salary_sum))
self.query1_window.table_widget.setItem(len(result), len(result[0]), total_salary_item)
# 显示窗口
self.query1_window.show()
```
阅读全文