我需要計算所有工資總額的絕對值可以得到選定年份的每個部門。
SELECT D.dept_name AS 'Department', SUM(S.salary) AS 'Total', CONCAT(ROUND(SUM(SUM(S.salary)), 2), '%') AS '%'
FROM departments D INNER JOIN dept_emp DE ON D.dept_no=DE.dept_no INNER JOIN salaries S ON DE.emp_no=S.emp_no
WHERE (S.from_date<='2000-01-01') AND (S.to_date>='2000-12-31')
GROUP BY D.dept_name
我試過,但它不工作;它說: 錯誤代碼1111,SQL狀態HY000:無效的使用組功能
SUM(SUM())聽起來對我很可疑,但我沒有想法。
下面是桌子,如果你需要:
REATE TABLE employees (
emp_no INT NOT NULL,
birth_date DATE NOT NULL,
first_name VARCHAR(14) NOT NULL,
last_name VARCHAR(16) NOT NULL,
gender ENUM ('M','F') NOT NULL,
hire_date DATE NOT NULL,
PRIMARY KEY (emp_no)
);
CREATE TABLE departments (
dept_no CHAR(4) NOT NULL,
dept_name VARCHAR(40) NOT NULL,
PRIMARY KEY (dept_no),
UNIQUE KEY (dept_name)
);
CREATE TABLE dept_emp (
emp_no INT NOT NULL,
dept_no CHAR(4) NOT NULL,
from_date DATE NOT NULL,
to_date DATE NOT NULL,
KEY (emp_no),
KEY (dept_no),
FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
FOREIGN KEY (dept_no) REFERENCES departments (dept_no) ON DELETE CASCADE,
PRIMARY KEY (emp_no,dept_no)
);
CREATE TABLE salaries (
emp_no INT NOT NULL,
salary INT NOT NULL,
from_date DATE NOT NULL,
to_date DATE NOT NULL,
KEY (emp_no),
FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
PRIMARY KEY (emp_no, from_date)
);
你是正確可疑。給我們適當的DDL和期望的結果 – Strawberry