该 SQL 查询语句用于统计当前月份排队记录表中状态为 2 的所有排队记录,并按照科室名称进行分组统计,最后按照排队记录数量的降序进行排序。

select u_zndt_hbv2.v_dai_dept.dept_name, count(distinct u_zndt_hbv2.v_queue_number_record.queue_id)
        from u_zndt_hbv2.v_queue_number_record
                 join u_zndt_hbv2.v_dai_dept  on u_zndt_hbv2.v_queue_number_record.dept_code = u_zndt_hbv2.v_dai_dept.dept_code
        where u_zndt_hbv2.v_queue_number_record.state = 2
          and replace(substr(u_zndt_hbv2.v_queue_number_record.queue_time, 1, 7), '-', '') = to_char(sysdate, 'yyyyMM')
        group by u_zndt_hbv2.v_dai_dept.dept_name
        order by count(distinct u_zndt_hbv2.v_queue_number_record.queue_id) desc

解释:

  • select u_zndt_hbv2.v_dai_dept.dept_name, count(distinct u_zndt_hbv2.v_queue_number_record.queue_id):查询科室名称和排队记录数量。使用 count(distinct u_zndt_hbv2.v_queue_number_record.queue_id) 统计排队记录数量,确保每个排队记录只计算一次。
  • from u_zndt_hbv2.v_queue_number_record:从排队记录表中获取数据。
  • join u_zndt_hbv2.v_dai_dept on u_zndt_hbv2.v_queue_number_record.dept_code = u_zndt_hbv2.v_dai_dept.dept_code:连接排队记录表和科室表,根据 dept_code 字段进行连接。
  • where u_zndt_hbv2.v_queue_number_record.state = 2:过滤状态为 2 的排队记录。
  • and replace(substr(u_zndt_hbv2.v_queue_number_record.queue_time, 1, 7), '-', '') = to_char(sysdate, 'yyyyMM'):筛选当前月份的排队记录。substr(u_zndt_hbv2.v_queue_number_record.queue_time, 1, 7) 获取排队时间的前 7 个字符(即年份和月份),replace 函数移除 - 号,to_char(sysdate, 'yyyyMM') 获取当前时间的年份和月份,通过比较筛选出当前月份的排队记录。
  • group by u_zndt_hbv2.v_dai_dept.dept_name:按照科室名称进行分组统计。
  • order by count(distinct u_zndt_hbv2.v_queue_number_record.queue_id) desc:按照排队记录数量的降序进行排序。

动态传递参数:

动态传递参数可以通过将 SQL 语句中的固定值替换为变量,然后在执行 SQL 语句时通过赋值给变量的方式传递参数。比如,可以使用变量来代替当前月份,使得查询的时间范围可以动态指定。

具体的实现方式取决于使用的数据库和编程语言。例如,使用 Python 和 MySQL 可以使用以下代码动态传递参数:

import datetime
import mysql.connector

# 获取当前月份
current_month = datetime.datetime.now().strftime('%Y%m')

# 连接数据库
conn = mysql.connector.connect(host='localhost', database='your_database', user='your_username', password='your_password')
cursor = conn.cursor()

# 定义 SQL 语句
sql = "SELECT u_zndt_hbv2.v_dai_dept.dept_name, COUNT(DISTINCT u_zndt_hbv2.v_queue_number_record.queue_id) " \
      "FROM u_zndt_hbv2.v_queue_number_record " \
      "JOIN u_zndt_hbv2.v_dai_dept ON u_zndt_hbv2.v_queue_number_record.dept_code = u_zndt_hbv2.v_dai_dept.dept_code " \
      "WHERE u_zndt_hbv2.v_queue_number_record.state = 2 " \
      "AND REPLACE(SUBSTR(u_zndt_hbv2.v_queue_number_record.queue_time, 1, 7), '-', '') = %s " \
      "GROUP BY u_zndt_hbv2.v_dai_dept.dept_name " \
      "ORDER BY COUNT(DISTINCT u_zndt_hbv2.v_queue_number_record.queue_id) DESC"

# 执行 SQL 语句
cursor.execute(sql, (current_month,))

# 获取结果
results = cursor.fetchall()
for row in results:
    dept_name = row[0]
    queue_count = row[1]
    print(f"科室名称:{dept_name},排队记录数量:{queue_count}")

# 关闭数据库连接
cursor.close()
conn.close()

在上述代码中,通过使用 datetime 模块获取当前月份,并将其作为参数传递给 SQL 查询语句中的动态参数 %s。然后使用 MySQL Connector/Python 库连接数据库,并执行 SQL 语句。最后,通过遍历结果集获取查询结果并输出。


原文地址: https://www.cveoy.top/t/topic/qwV9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录