我想在同一查询中根据类型显示数据

时间:2014-09-11 05:27:32

标签: sql sql-server sql-server-2008

我有以下表格" vehicle_data" :

ID    ALERT_TYPE    VALUE
58      2             1
58      1             1
104     1             1
104     2             1

此处alert_type = 2表示GPS值,alert_type = 1表示engine_value。

so if alert_type=2 and its value is =1 then it means its value is correct.
when alert_type=2 and its value is =0 then it means its value is wrong.

same for alert_type=1 

所以现在我想要以下输出:

ID    gps    engine_value  
58     1      1
104    1      1

我该如何执行此查询??

8 个答案:

答案 0 :(得分:1)

你可以这样做。

SELECT ID
   ,CASE WHEN [ALERT_TYPE]=2 and [value ]=1 THEN 1 ELSE 0 END as gps 
    ,CASE WHEN [ALERT_TYPE]=1 and [value ]=1  THEN 1 ELSE 0 END as engine

FROM vehicle_data

答案 1 :(得分:0)

SELECT ID, alert_type=2 AS gps, alert_type=1 AS [engine] FROM vehicle_data WHERE value=1;

已编辑以说明您对VALUE的解释。

答案 2 :(得分:0)

根据您的问题,我相信您想要列中的数据并满足您的要求我已经制作了一个SQL提示工作 - 下面还提到了代码 -

这是你带着工作的好消息 -

WORKING DEMO

REFERNECE的SQL代码 -

CREATE TABLE ALERTS (ID INT, ALERT_TYPE INT, VALUE INT)

INSERT INTO ALERTS VALUES (58,2,1)
INSERT INTO ALERTS VALUES (58,1,0)
INSERT INTO ALERTS VALUES (104,1,1)
INSERT INTO ALERTS VALUES (104,2,0)

CREATE TABLE ALERTSVALUE (ID INT, gps INT,engine INT)

INSERT INTO ALERTSVALUE VALUES (58,1,0)
INSERT INTO ALERTSVALUE VALUES (104,0,1)



SELECT A.ID, 
CASE A.ALERT_TYPE WHEN 2 THEN 1 ELSE 0 END AS GPS, 
CASE A.ALERT_TYPE WHEN 1 THEN 1 ELSE 0 END AS ENGINE_VALUE, 
A.VALUE FROM ALERTS A WHERE A.VALUE = 1

基于评论编辑 - 合并GPS和ENGINE_VALUE的行:

SELECT X.ID,X.ALERT_TYPE as GPS,X.VALUE as ENGINE_VALUE
FROM (
    SELECT ID,ALERT_TYPE ,VALUE ,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY alert_type ) AS [Rank] FROM ALERTS
 )X
 WHERE [Rank]=1

SQL FIDDLE DEMO

答案 3 :(得分:0)

模式

CREATE TABLE table3 (id int, ALERT_TYPE int)
INSERT table3 VALUES (58, 1), (58, 2), (104, 1), (104, 2)

查询

SELECT *
FROM (
    SELECT ID
        ,ROW_NUMBER() OVER (
            PARTITION BY id ORDER BY id
            ) AS row_num
        ,gps = CASE 
            WHEN ALERT_TYPE = 2
                THEN 1
            ELSE 0
            END
        ,engine = CASE 
            WHEN ALERT_TYPE = 1
                THEN 1
            ELSE 0
            END
    FROM table3
    ) a
WHERE a.row_num = 1

输出

ID    gps    engine  
58     1      0
104    0      1

答案 4 :(得分:0)

使用子查询的一种可能方式:

select 
    Ids.ID
    , gps.VALUE 'gps'
    , engine_value.VALUE 'engine_value'
from (select distinct ID from vehicle_data) Ids
    left join 
        (select ID, VALUE from vehicle_data where ALERT_TYPE = 2) gps 
    on gps.ID = Ids.ID
    left join 
        (select ID, VALUE from vehicle_data where ALERT_TYPE = 1) engine_value 
    on engine_value.ID = Ids.ID

<强> [SQL Fiddle demo]

答案 5 :(得分:0)

我希望这对你有用,

Select ID,sum(gps) as gps ,sum(engine) as engine from 
(SELECT ID
   ,CASE WHEN [ALERT_TYPE]=2 and [value ]=1 THEN 1 ELSE 0 END as gps 
    ,CASE WHEN [ALERT_TYPE]=1 and [value ]=1  THEN 1 ELSE 0 END as engine

FROM vehicle_data
)a
group by id

答案 6 :(得分:0)

 select x.id,x.alert_type as GPS,x.value as engine_value   from (
 select ID,alert_type,value,ROW_NUMBER() over (partition by id order by alert_type ) as Rnk from mytable
 )x
 where Rnk=1

答案 7 :(得分:0)

请在SQL中检查此查询:

create table mytable (id int, alert_type int, value int);

insert into mytable (id, alert_type, value)
values (58, 2, 1),
(58, 1, 1),
(104, 1, 1),
(104, 2, 1);


SELECT distinct ID
   ,(select count (id) from mytable mt where mt.id=mytable.id and mt.[ALERT_TYPE]=2 and mt.[value ]=1) as gps 
    ,(select count (id) from mytable mt where mt.id=mytable.id and mt.[ALERT_TYPE]=1 and mt.[value ]=1) as engine

FROM mytable