0

select devicename, name, value, timestamp from table1给了我以下,

DEVICENAME     NAME     VALUE    TIMESTAMP
123            CURRENT  130      2012-10-16 13:00:00.000
123            AVG      140      2012-10-16 13:00:00.000
123            MIN      100      2012-10-16 13:00:00.000
123            MAX      160      2012-10-16 13:00:00.000

我如何在 sql 查询中像这样得到它们,

123, 130, 140, 100, 160 2012-10-16 13:00:00.000
4

5 回答 5

3

PIVOT如果你想在单独的列中使用它,你可以使用它:

select devicename, [Current], [Avg], [Min], [Max], timestamp
from
(
  select devicename, name, value, timestamp
  from table1
) x
pivot
(
  max(value) 
  for name in ([Current], [Avg], [Min], [Max])
) p

请参阅带有演示的 SQL Fiddle

如果您希望同一列中的所有值,那么您可以使用以下内容:

select devicename,
  STUFF((SELECT distinct ', ' + cast(value as varchar(10))
              from table1 t2
              where t1.DEVICENAME = t2.DEVICENAME
            FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'') name,
  timestamp
from table1 t1
group by devicename, timestamp

请参阅带有演示的 SQL Fiddle

于 2012-10-16T19:28:54.540 回答
2
select 
  devicename, 
  MAX(CASE WHEN Name = 'CURRENT' THEN value ELSE NULL END), 
  MAX(CASE WHEN Name = 'AVG' THEN value ELSE NULL END), 
  MAX(CASE WHEN Name = 'MIN' THEN value ELSE NULL END), 
  MAX(CASE WHEN Name = 'MAX' THEN value ELSE NULL END), 
  timestamp 
from table1
GROUP BY devicename, timestamp
于 2012-10-16T19:20:11.423 回答
0

您可以将它们连接成一列

select a + ' '+ b as Column1 from yourtable

或将它们作为输出变量返回

于 2012-10-16T19:16:47.920 回答
0
SELECT x.devicename, *
FROM 
    (SELECT devicename, value as 'current', timestamp 
    FROM table1 WHERE name = 'current') x 
INNER JOIN 
    (SELECT devicename, value as [avg] FROM table1 WHERE name = 'avg') y 
    ON x.devicename = y.devicename
INNER JOIN (SELECT devicename, value as [min] FROM table1 WHERE name = 'min') z 
    ON x.devicename = z.devicename
INNER JOIN (SELECT devicename, value as [max] FROM table1 WHERE name = 'max') q 
    ON x.devicename = q.devicename
于 2012-10-16T19:25:53.483 回答
-1
create table table1 (devicename varchar(10)
              ,name varchar(100)
              ,value varchar(10)
              ,TimeSTamp datetime)

INSERT INTO table1 
SELECT '123','CURRENT','130','2012-10-16 13:00:00.000'
UNION
 SELECT '123','AVG','140','2012-10-16 13:00:00.000'
UNION
 SELECT '123','MIN','100','2012-10-16 13:00:00.000'
UNION
 SELECT '123','MAX','160','2012-10-16 13:00:00.000'


select devicename, name, value, timestamp from table1 


SELECT DISTINCT t1.devicename
     , value_list
     , t1.timestamp
  FROM table1 t1
 INNER JOIN (SELECT t2.devicename
                  , t2.timestamp
                  , value_list = SUBSTRING((SELECT t3.value
                                                 + ', '
                                              FROM table1 t3
                                             WHERE t3.devicename = t2.devicename
                                               AND t3.timestamp  = t2.timestamp
                                             ORDER BY t3.value
                                               FOR XML PATH('')),1,LEN((SELECT t3.value
                                                                             + ', '
                                                                          FROM table1 t3
                                                                         WHERE t3.devicename = t2.devicename
                                                                           AND t3.timestamp  = t2.timestamp
                                                                         ORDER BY t3.value
                                                                           FOR XML PATH(''))) -1)
               FROM table1 t2
              GROUP BY t2.devicename
                     , t2.timestamp) t4 on t4.devicename  = t1.devicename
                                       AND t4.timestamp   = t1.timestamp


drop table1
于 2012-10-17T20:04:55.153 回答