SELECT DATEPART(YEAR, t_kqlist23.RQCHAR) AS [Year], DATEPART(MONTH, t_kqlist23.RQCHAR) AS [Month], t_kqlist23.BSIN AS BSIN, t_kqlist23.ESOUT AS ESOUT, T_JKDX.YDJBH AS card_number, T_BM_BM.BMMC AS department, T_BM_ZW.ZWMC AS duties, T_JKDX.XM AS name, T_BM_GZ.GZMC AS work_type_name, COUNT(*) AS [Count] FROM [cxrydw].[guoxw].[t_kqlist23] AS t_kqlist23 LEFT JOIN [cxrydw].[guoxw].[T_JKDX] AS T_JKDX ON T_JKDX.XTBH = t_kqlist23.XTBH LEFT JOIN [cxrydw].[guoxw].[T_BM_BM] AS T_BM_BM ON T_BM_BM.BMBM = T_JKDX.BMBM LEFT JOIN [cxrydw].[guoxw].[T_BM_ZW] AS T_BM_ZW ON T_BM_ZW.ZWBM = T_JKDX.ZWBM LEFT JOIN [cxrydw].[guoxw].[T_BM_GZ] AS T_BM_GZ ON T_BM_GZ.GZBM = T_JKDX.GZBM WHERE DATEPART(YEAR, t_kqlist23.RQCHAR) = 2023 AND t_kqlist23.XTBH = '17' GROUP BY DATEPART(YEAR, t_kqlist23.RQCHAR), DATEPART(MONTH, t_kqlist23.RQCHAR), t_kqlist23.BSIN, t_kqlist23.ESOUT, T_JKDX.YDJBH, T_BM_BM.BMMC, T_BM_ZW.ZWMC, T_JKDX.XM, T_BM_GZ.GZMC ORDER BY [Year], [Month] 查询出每月的数据条数
时间: 2024-04-13 19:25:00 浏览: 271
这个SQL查询语句是用于查询指定年份和XTBH的每个月的数据条数。它从多个表中选择了一些列,并进行了一系列的JOIN操作。然后,使用DATEPART函数提取出年份和月份,并对这些列进行分组。最后,使用COUNT函数计算每个组的数据条数,并按年份和月份进行排序。
注意:这个查询语句是基于SQL Server的语法,如果你使用的是其他数据库,可能需要进行适当的调整。
相关问题
SELECT DATEPART(YEAR, t_kqlist23.RQCHAR) AS [Year], DATEPART(MONTH, t_kqlist23.RQCHAR) AS [Month], t_kqlist23.BSIN AS BSIN, t_kqlist23.ESOUT AS ESOUT, T_JKDX.YDJBH AS card_number, T_BM_BM.BMMC AS department, T_BM_ZW.ZWMC AS duties, T_JKDX.XM AS name, T_BM_GZ.GZMC AS work_type_name, COUNT(*) AS [Count] FROM [cxrydw].[guoxw].[t_kqlist23] AS t_kqlist23 LEFT JOIN [cxrydw].[guoxw].[T_JKDX] AS T_JKDX ON T_JKDX.XTBH = t_kqlist23.XTBH LEFT JOIN [cxrydw].[guoxw].[T_BM_BM] AS T_BM_BM ON T_BM_BM.BMBM = T_JKDX.BMBM LEFT JOIN [cxrydw].[guoxw].[T_BM_ZW] AS T_BM_ZW ON T_BM_ZW.ZWBM = T_JKDX.ZWBM LEFT JOIN [cxrydw].[guoxw].[T_BM_GZ] AS T_BM_GZ ON T_BM_GZ.GZBM = T_JKDX.GZBM WHERE DATEPART(YEAR, t_kqlist23.RQCHAR) = 2023 AND t_kqlist23.XTBH = '17' GROUP BY DATEPART(YEAR, t_kqlist23.RQCHAR), DATEPART(MONTH, t_kqlist23.RQCHAR), t_kqlist23.BSIN, t_kqlist23.ESOUT, T_JKDX.YDJBH, T_BM_BM.BMMC, T_BM_ZW.ZWMC, T_JKDX.XM, T_BM_GZ.GZMC ORDER BY [Year], [Month] 改成查询出每月的数据条数
这个查询已经是用于查询每月的数据条数了。它在SELECT子句中使用了COUNT(*)函数来统计每个月的数据条数,并将结果命名为[Count]。在GROUP BY子句中,按照年份、月份、BSIN、ESOUT、card_number、department、duties、name和work_type_name进行分组。最后使用ORDER BY子句按照年份和月份进行排序。所以这个查询已经可以得到每个月的数据条数了。
优化这一段sql:SELECT user_worker.Title AS worker, COUNT(CASE WHEN (CASE WHEN bl.Checker = 0 THEN '' WHEN bl.Checker > 0 AND bl.Audited = -1 THEN 'NG' WHEN bl.Checker > 0 AND bl.Audited = 1 THEN 'OK' END) ='' THEN 1 END) as nullstr, COUNT(CASE WHEN (CASE WHEN bl.Checker = 0 THEN '' WHEN bl.Checker > 0 AND bl.Audited = -1 THEN 'NG' WHEN bl.Checker > 0 AND bl.Audited = 1 THEN 'OK' END) = 'OK' THEN 1 END) as okstr, COUNT(CASE WHEN (CASE WHEN bl.Checker = 0 THEN '' WHEN bl.Checker > 0 AND bl.Audited = -1 THEN 'NG' WHEN bl.Checker > 0 AND bl.Audited = 1 THEN 'OK' END) = 'NG' THEN 1 END) as NGstr, (COUNT(CASE WHEN (CASE WHEN bl.Checker = 0 THEN '' WHEN bl.Checker > 0 AND bl.Audited = -1 THEN 'NG' WHEN bl.Checker > 0 AND bl.Audited = 1 THEN 'OK' END) = 'OK' THEN 1 END) +COUNT(CASE WHEN (CASE WHEN bl.Checker = 0 THEN '' WHEN bl.Checker > 0 AND bl.Audited = -1 THEN 'NG' WHEN bl.Checker > 0 AND bl.Audited = 1 THEN 'OK' END) ='' THEN 1 END) )as totalOKStr FROM [bdWorkOrderList] bl LEFT JOIN [bdWorkOrder] bd ON bl.OrderID = bd.ID LEFT JOIN [reportMJWXJL] rwx ON bl.OrderID = rwx.OrderID LEFT JOIN [reportMJQHLHJL] rqh ON bl.OrderID = rqh.OrderID LEFT JOIN user_worker ON user_worker.ID = bl.Worker WHERE bd.IsDeleted = 0 AND bl.StepName LIKE 'Maker%' AND bl.StepName IN ('Maker修模', 'maker切换料号', 'Maker模具保养') AND DATEPART(MONTH, bl.StartTime) = DATEPART(MONTH, GETDATE()) and DATEPART(YEAR, bl.StartTime) = DATEPART(YEAR, GETDATE()) GROUP BY user_worker.Title)
你可以尝试优化以上SQL查询的性能:
```sql
WITH CTE_Counts AS (
SELECT
user_worker.Title AS worker,
COUNT(CASE WHEN bl.Checker = 0 THEN 1 END) AS nullstr,
COUNT(CASE WHEN bl.Audited = -1 THEN 1 END) AS NGstr,
COUNT(CASE WHEN bl.Audited = 1 THEN 1 END) AS okstr
FROM [bdWorkOrderList] bl
LEFT JOIN [bdWorkOrder] bd ON bl.OrderID = bd.ID
LEFT JOIN [reportMJWXJL] rwx ON bl.OrderID = rwx.OrderID
LEFT JOIN [reportMJQHLHJL] rqh ON bl.OrderID = rqh.OrderID
LEFT JOIN user_worker ON user_worker.ID = bl.Worker
WHERE bd.IsDeleted = 0
AND bl.StepName LIKE 'Maker%'
AND bl.StepName IN ('Maker修模', 'maker切换料号', 'Maker模具保养')
AND DATEPART(MONTH, bl.StartTime) = DATEPART(MONTH, GETDATE())
AND DATEPART(YEAR, bl.StartTime) = DATEPART(YEAR, GETDATE())
GROUP BY user_worker.Title
)
SELECT
worker,
nullstr,
NGstr,
okstr,
(nullstr + okstr) AS totalOKStr
FROM CTE_Counts;
```
在上述优化中,我使用了一个公共表表达式(CTE)来计算每个分类的数量。通过将计算逻辑放在CTE中,可以提高查询的可读性和性能。然后,从CTE中选择所需的列,并计算总的OK数量。
请注意,为了进一步优化查询性能,你可能需要创建适当的索引来支持查询中使用的过滤和连接操作。具体索引的创建需要根据表结构和查询需求进行评估和调整。
阅读全文