我有這個查詢來計算我的數據庫表上的數據並將其存儲到一個文本框,然後將其保存到另一個表中存儲計數。我首先將數據存儲到我的文本框中,因爲我需要將計數放在第二個數據庫表上的特定位置。有什麼方法可以簡化這個查詢嗎? :查詢太長
query = "SELECT (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance') as [Revenue Assurance]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department Like '%Compliance, Fraud Investigation Services%' OR (Department = 'Compliance, Fraud Investigation Services')) as [Compliance, Fraud Investigation Services]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department Like '%Compliance, Solutions and Services%' OR (Department = 'Compliance, Solutions and Services')) as [Compliance, Solutions and Services]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Head (Senior)')) as [txtRAHS]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Head')) as [txtRAH]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Sr. Manager')) as [txtRASRMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Manager')) as [txtRAMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Sr. Supervisor')) as [txtRASRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Supervisor')) as [txtRASUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Jr. Supervisor')) as [txtRAJRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Sr. Staff')) as [txtRASRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Staff')) as [txtRASTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Jr. Staff')) as [txtRAJRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Revenue Assurance' AND (P_Level = 'Project Staff')) as [txtRAPS]" & _
'[CFIS]
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Head')) as [txtCFISH]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Sr. Manager')) as [txtCFISSRMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Manager')) as [txtCFISMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Sr. Supervisor')) as [txtCFISSRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Supervisor')) as [txtCFISSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Jr. Supervisor')) as [txtCFISJRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Sr. Staff')) as [txtCFISSRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Staff')) as [txtCFISSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Jr. Staff')) as [txtCFISJRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Fraud Investigation Services' AND (P_Level = 'Project Staff')) as [txtCFISPS]" & _
'[CSS]
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Head')) as [txtCSSH]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Sr. Manager')) as [txtCSSSRMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Manager')) as [txtCSSMGR]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Sr. Supervisor')) as [txtCSSSRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Supervisor')) as [txtCSSSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Jr. Supervisor')) as [txtCSSJRSUP]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Sr. Staff')) as [txtCSSSRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Staff')) as [txtCSSSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Jr. Staff')) as [txtCSSJRSTF]," & _
" (SELECT COUNT (Employee_Name) FROM tblEmployees WHERE Department = 'Compliance, Solutions and Services' AND (P_Level = 'Project Staff')) as [txtCSSPS]" & _
" From tblEmployees"
感謝
爲什麼按部門和級別的組不計及... –
同意@ ta.speot.is,如果這些別名是重要的(txtxxxxx)你在做什麼,你總是可以做一個表存儲它們並將它們與每個部門/級別組合相關聯,以便您在運行此類查詢時加入它。 –
嗨,大家好,謝謝。但我如何將他們按部門和級別分組?對不起,這樣的noob :( –