我试图让所有学者分类。我使用以下查询。
SELECT DISTINCT
LOGINID,
ISNULL(SUM(PHD_INTERNAL),0) AS PHD_INTERNAL,
ISNULL(SUM(MS_INTERNAL),0) AS MS_INTERNAL,
ISNULL(SUM(PHD_EXTERNAL),0) AS PHD_EXTERNAL,
ISNULL(SUM(MS_EXTERNAL),0) AS MS_EXTERNAL,
ISNULL((ISNULL(SUM(PHD_INTERNAL), 0)) + (ISNULL(SUM(MS_INTERNAL), 0)) + (ISNULL(SUM(PHD_EXTERNAL), 0)) + (ISNULL(SUM(MS_EXTERNAL),0)),0) AS Total
FROM
(SELECT DISTINCT
LOGINID,
STYPE, CTYPE,
CASE
WHEN STYPE = 'Internal' AND CTYPE = 'Ph.D'
THEN CNT
END PHD_INTERNAL,
CASE
WHEN STYPE = 'Internal' AND CTYPE = 'M.S'
THEN CNT
END MS_INTERNAL,
CASE
WHEN STYPE = 'External' AND CTYPE = 'Ph.D'
THEN CNT
END PHD_ExTERNAL,
CASE
WHEN STYPE = 'External' AND CTYPE = 'M.S'
THEN CNT
END MS_EXTERNAL
FROM
(SELECT DISTINCT
LOGINID,
STYPE, CTYPE,
COUNT (STYPE) AS CNT
FROM
SCHDET
GROUP BY
LOGINID, STYPE, CTYPE) T
) TT
GROUP BY
LOGINID
UNION ALL
SELECT
'Total',
ISNULL(SUM(IP), 0) AS IP,
ISNULL(SUM(IMS), 0) AS IMS,
ISNULL(SUM(EP), 0) AS EP,
ISNULL(SUM(EMS), 0) AS EMS,
ISNULL((ISNULL(SUM(IP), 0) + ISNULL(SUM(IMS), 0) + ISNULL(SUM(EP), 0) + ISNULL(SUM(EMS), 0)), 0) AS TOTAL
FROM
(SELECT
'TOTAL',
CASE
WHEN STYPE = 'Internal' AND CTYPE = 'Ph.D'
THEN CNT
END IP,
CASE
WHEN STYPE = 'External' AND CTYPE = 'Ph.D'
THEN CNT
END EP,
CASE
WHEN STYPE = 'Internal' AND CTYPE = 'M.S'
THEN CNT
END IMS,
CASE
WHEN STYPE = 'External' AND CTYPE = 'M.S'
THEN CNT
END EMS
FROM
(SELECT
STYPE, CTYPE, COUNT (STYPE) CNT
FROM
SCHDET
GROUP BY
STYPE, CTYPE) B
) BB
GROUP BY
'Total';
但是,我收到了错误:
没有为' BB'。
的第1列指定列名
请帮帮我。
答案 0 :(得分:0)
在视图'BB'中,您需要定义值为'TOTAL'的列的名称。
.....
FROM
(
SELECT 'TOTAL' AS TOTAL,
......
答案 1 :(得分:0)
始终内部查询应该有一个别名,以便在外部查询中使用它。
select 'Total' as Total,