我希望,你可以帮助我 对于更大的SQL查询,我不是很熟练,所以我无法在大约5到6个小时内解决问题:/
在SQL Server 2008上,我收到此错误:
列'pvtMonth.1'是 在选择列表中无效,因为它不包含在任何一个中 聚合函数或GROUP BY子句。
列名称'j_sumzeit'无效。
列名称'j_sumzeit'无效。
我的查询:
SELECT
kostenstelle_nr,
kostenstelle_name,
[1] AS Jan,
[2] AS Feb,
[3] AS Mrz,
[4] AS Apr,
[5] AS Mai,
[6] AS Jun,
[7] AS Jul,
[8] AS Aug,
[9] AS Sep,
[10] AS Okt,
[11] AS Nov,
[12] AS Dez,
SUM ( j_sumzeit ) AS [2011],
SUM ( vj_sumzeit ) AS [2010],
ROUND( 100 - ( ( vj_sumzeit / j_sumzeit ) *100 ) , 0 ) AS [diff]
FROM
(
SELECT
dbo.kostenstelle.kostenstelle_nr,
dbo.kostenstelle.kostenstelle_name,
SUM ( j_sumzeitpromo ) AS j_sumzeit,
SUM ( vj_sumzeitpromo ) AS vj_sumzeit,
MONTH ( j_datum ) AS TMonth
FROM
dbo.kostenstelle
LEFT JOIN
(
SELECT
dbo.def.def_kstnr AS j_kstnr,
dbo.def.def_datum AS j_datum,
SUM ( dbo.def.def_zeit ) AS j_sumzeitpromo,
COUNT ( dbo.def.def_zeit ) AS j_anzahl
FROM
dbo.def
WHERE
YEAR ( dbo.def.def_datum ) = 2011
AND dbo.def.def_auknr > 215
GROUP BY
dbo.def.def_kstnr,
dbo.def.def_datum
) jahr
ON j_kstnr = dbo.kostenstelle.kostenstelle_nr
LEFT JOIN
(
SELECT
dbo.def.def_kstnr AS vj_kstnr,
dbo.def.def_datum AS vj_datum,
SUM ( dbo.def.def_zeit ) AS vj_sumzeitpromo,
COUNT ( dbo.def.def_zeit ) AS vj_anzahl
FROM
dbo.def
WHERE
YEAR ( dbo.def.def_datum ) = 2010
AND dbo.def.def_auknr > 215
GROUP BY
dbo.def.def_kstnr,
dbo.def.def_datum
) vorjahr
ON j_kstnr = dbo.kostenstelle.kostenstelle_nr
GROUP BY
dbo.kostenstelle.kostenstelle_nr,
dbo.kostenstelle.kostenstelle_name,
MONTH ( j_datum )
) source
PIVOT
(
SUM(j_sumzeit)
FOR TMonth
IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] )
) AS pvtMonth
GROUP BY
kostenstelle_nr,
kostenstelle_name
包含一些示例数据的表格
表结构非常糟糕,但我无法更改它们,因为它们来自外部公司。
|-----------------------------------------------|
| def_kstnr | def_zeit | def_datum | def_auknr |
|-----------------------------------------------|
| 100 | 3.2 | 2011-11-02 | 245 |
| 110 | 2.8 | 2011-02-03 | 265 |
| 120 | 5.4 | 2011-11-04 | 250 |
| 130 | 2.4 | 2011-08-05 | 280 |
| 140 | 4.9 | 2011-09-06 | 300 |
| 150 | 1.5 | 2011-10-07 | 320 |
| 160 | 2.6 | 2011-12-08 | 450 |
|-----------------------------------------------|
|-------------------------------------------------------|
| kostenstelle_id | kostenstelle_nr | kostenstelle_name |
|-------------------------------------------------------|
| 1 | 245 | KstTst1 |
| 2 | 265 | KstTst1 |
| 3 | 250 | KstTst1 |
| 4 | 280 | KstTst1 |
| 5 | 300 | KstTst1 |
| 6 | 320 | KstTst1 |
| 7 | 450 | KstTst1 |
|-------------------------------------------------------|
结果应如下所示(数字可能与上述示例数据不同)
|--------------------------------------------------------------------------------------------------------|
| def_kstnr | Jan | Feb | Mrz | Apr | Mai | Jun | Jul | Aug | Sep | Okt | Nov | Dez | 2011 | 2010 | diff |
|--------------------------------------------------------------------------------------------------------|
| 100 | 8.4 | 3.4 | 2.7 | 9.5 | 3.5 | 7.4 | 3.4 | 1.2 | 7.5 | 3.4 | 3.2 | 7.5 | 61.1 | 47.5 | 22 |
| 110 | 4.4 | 2.8 | 6.5 | 2.5 | 1.4 | 2.4 | 4.4 | 3.7 | 2.4 | 7.7 | 1.7 | 6.4 | 46.3 | 32.2 | 30 |
...
|--------------------------------------------------------------------------------------------------------|
非常感谢你。如果您需要更多信息 - 我会高兴地将它们交给您:)
答案 0 :(得分:0)
如果没有看到初始表结构,很难说。 尝试在SELECT中插入“j_sumzeit”列(刚好在第一个FROM之后)。