SELECT (
SELECT avg(KW)
FROM genset
WHERE (GenSetName like 'GEA3519')
and GenDate >= '1 jan 2003 00:00:00'
and GenDate < '1 feb 2003 00:00:00'
) AS avg_GEA3519,
(
SELECT avg(KW)
FROM genset
WHERE (GenSetName like 'GEA3520')
and GenDate >= '1 jan 2003 00:00:00'
and GenDate < '1 feb 2003 00:00:00'
) AS avg_GEA3520,
(
SELECT avg(KW)
FROM genset
WHERE (GenSetName like 'GEA3521')
and GenDate >= '1 jan 2003 00:00:00'
and GenDate < '1 feb 2003 00:00:00'
) AS avg_GEA3521,
(
SELECT avg(KW)
FROM genset
WHERE (GenSetName like 'GEA3522')
and GenDate >= '1 jan 2003 00:00:00'
and GenDate < '1 feb 2003 00:00:00'
) AS avg_GEA3522
или SQL Server 2005+
, это:
SELECT [GEA3519], [GEA3520], [GEA3521], [GEA3522]
FROM (
SELECT GenSetName, KW
FROM genset
WHERE GenDate >= '1 Jan 2003 00:00:00'
AND GenDate < '1 Feb 2003 00:00:00'
) AS q
PIVOT
(
AVG(KW)
FOR GenSetName IN (['GEA3519'], ['GEA3520'], ['GEA3521'], ['GEA3522']
)