我有两个表JOB和EMP;结构和值如下:
CREATE TABLE JOB
(
JOBID SMALLINT UNIQUE NOT NULL,
JOBNAME CHAR(15)
);
CREATE TABLE EMP
(
EMPID SMALLINT,
JOBID SMALLINT,
SAL SMALLINT,
CITYID SMALLINT,
YEAR SMALLINT,
SEX CHAR(1),
STATE CHAR(2)
);
INSERT INTO JOB(JOBID, JOBNAME)
VALUES (1, 'DEVELOPMENT'),
(2, 'DEVELOPMENT'),
(3, 'TESTING'),
(4, 'TESTING'),
(7, 'TESTING'),
(8, 'HR'),
(10, 'RESEARCH');
INSERT INTO EMP (EMPID , JOBID, SAL, CITYID, YEAR, SEX,STATE)
VALUES (100, 1, 1000, 10, 2015, 'M','NY'),
(200, 2, 2000, 10, 2015, 'M','NJ'),
(300, 1, 2500, 20, 2015, 'F','NY'),
(400, 3, 1000, 10, 2015, 'M','CA'),
(500, 6, 3000, 10, 2015, 'F','NJ'),
(600, 8, 1000, 30, 2015, 'M','CA'),
(700, 8, 2000, 10, 2015, 'F','NY'),
(800, 9, 1500, 10, 2015, 'F','CA');
对于给定的输入cityid,YEAR(Emp表),获取每个工作名称(来自job表)的所有jobid,并在Emp表中进行匹配(如果存在),则按性别(男性)显示次数(Emp表中存在的Jobid数量)和女性)以纽约州,新泽西州,加利福尼亚州为水平;和总计。如果没有匹配显示为0。
输出应如下所示:
Jobname Male FEMALE TOTAL
NY NJ CA NY NJ CA
Development 1 1 0 1 0 0 3
TESTING 0 0 1 0 0 0 1
HR 0 0 1 1 0 0 2
RESEARCH 0 0 0 0 0 0 0
TOTAL 1 1 2 2 0 0 6
如何以水平方式获取结果??
答案 0 :(得分:0)
嗨,我认为这可以使用:
CREATE TABLE #JOB
(
JOBID SMALLINT UNIQUE NOT NULL,
JOBNAME CHAR(15)
);
CREATE TABLE #EMP
(
EMPID SMALLINT,
JOBID SMALLINT,
SAL SMALLINT,
CITYID SMALLINT,
YEAR SMALLINT,
SEX CHAR(1),
STATE CHAR(2)
);
INSERT INTO #JOB(JOBID, JOBNAME)
VALUES (1, 'DEVELOPMENT'),
(2, 'DEVELOPMENT'),
(3, 'TESTING'),
(4, 'TESTING'),
(7, 'TESTING'),
(8, 'HR'),
(10, 'RESEARCH');
INSERT INTO #EMP (EMPID , JOBID, SAL, CITYID, YEAR, SEX,STATE)
VALUES (100, 1, 1000, 10, 2015, 'M','NY'),
(200, 2, 2000, 10, 2015, 'M','NJ'),
(300, 1, 2500, 20, 2015, 'F','NY'),
(400, 3, 1000, 10, 2015, 'M','CA'),
(500, 6, 3000, 10, 2015, 'F','NJ'),
(600, 8, 1000, 30, 2015, 'M','CA'),
(700, 8, 2000, 10, 2015, 'F','NY'),
(800, 9, 1500, 10, 2015, 'F','CA');
SELECT * FROM #JOB;
SELECT * FROM #EMP;
SELECT J.JOBNAME,
SUM(CASE WHEN E.SEX = 'M' AND STATE = 'NY' THEN 1 ELSE 0 END) AS 'Male_NY',
SUM(CASE WHEN E.SEX = 'M' AND STATE = 'NJ' THEN 1 ELSE 0 END) AS 'Male_NJ',
SUM(CASE WHEN E.SEX = 'M' AND STATE = 'CA' THEN 1 ELSE 0 END) AS 'Male_CA',
SUM(CASE WHEN E.SEX = 'F' AND STATE = 'NY' THEN 1 ELSE 0 END) AS 'Female_NY',
SUM(CASE WHEN E.SEX = 'F' AND STATE = 'NJ' THEN 1 ELSE 0 END) AS 'Female_NJ',
SUM(CASE WHEN E.SEX = 'F' AND STATE = 'CA' THEN 1 ELSE 0 END) AS 'Female_CA',
COUNT(*) AS 'Total'
FROM #EMP E
JOIN #JOB J ON E.JOBID = J.JOBID
GROUP BY J.JOBNAME
DROP TABLE #JOB;
DROP TABLE #EMP;
但是,如果您还有其他的STATE和Gender,则它是静态模式,您必须使用动态SQL查询来构建它,并在此链接上使用EXECUTE检查。
在此链接上可以使用PIVOT检查的其他方式:
答案 1 :(得分:0)
首先,SQL查询不支持多个级别的标头。您可以具有复杂的标题字符串,但不能具有多个级别。
第二,您希望没有员工的行。
第三,您想要一个总行。
因此,您将需要一个外部联接:
SELECT COALESCE(J.JOBNAME, 'TOTAL') as JobName,
SUM(CASE WHEN E.SEX = 'M' AND E.STATE = 'NY' THEN 1 ELSE 0 END) AS Male_NY,
SUM(CASE WHEN E.SEX = 'M' AND E.STATE = 'NJ' THEN 1 ELSE 0 END) AS Male_NJ,
SUM(CASE WHEN E.SEX = 'M' AND E.STATE = 'CA' THEN 1 ELSE 0 END) AS Male_CA,
SUM(CASE WHEN E.SEX = 'F' AND E.STATE = 'NY' THEN 1 ELSE 0 END) AS Female_NY,
SUM(CASE WHEN E.SEX = 'F' AND E.STATE = 'NJ' THEN 1 ELSE 0 END) AS Female_NJ,
SUM(CASE WHEN E.SEX = 'F' AND E.STATE = 'CA' THEN 1 ELSE 0 END) AS Female_CA
FROM JOB J LEFT JOIN
EMP E
ON E.JOBID = J.JOBID AND
E.YEAR = @YEAR AND
E.CITYID = @CITYID
GROUP BY GROUPING SETS ( (J.JOBNAME), () );
如果要获取所有年份/城市的数据,请删除ON
子句中的条件。
Here是db <>小提琴。