从同一个表中的行获取值

时间:2017-02-23 07:58:58

标签: sql-server tsql

我想获得Status NEW和Type DATA,总是最新的合约金额,不是NULL。

CREATE TABLE mytable(
   Amount  VARCHAR(4) NULL
  ,Status  VARCHAR(5) NOT NULL
  ,Date    DATE  NOT NULL
  ,Type    VARCHAR(4) NOT NULL
  ,MetaKey INTEGER  NOT NULL
);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-01','DATA',6);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-02','DATA',7);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-03','DATA',8);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-04','DATA',9);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-05','DATA',10);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-06','DATA',11);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-07','DATA',12);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-08','DATA',13);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-09','DATA',14);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-10','DATA',15);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-11','DATA',16);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-12','DATA',17);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-13','DATA',18);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-14','DATA',19);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-15','DATA',20);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-16','DATA',21);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-17','DATA',22);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-18','DATA',23);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-19','DATA',24);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-20','DATA',25);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-21','DATA',26);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-22','DATA',27);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-23','DATA',28);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-24','DATA',29);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-25','DATA',30);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES ('150','TRANS','2017-01-26','DATA',31);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES (NULL,'NEW','2017-01-27','DATA',32);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES (NULL,'NEW','2017-01-28','DATA',33);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES (NULL,'NEW','2017-01-29','DATA',34);
INSERT INTO mytable(Amount,Status,Date,Type,MetaKey) VALUES (NULL,'NEW','2017-01-30','DATA',35);

预期输出为:

+--------+--------+------------+------+---------+
| Amount | Status |    Date    | Type | MetaKey |
+--------+--------+------------+------+---------+
|    150 | TRANS  | 2017-01-01 | DATA |       6 |
|    150 | TRANS  | 2017-01-02 | DATA |       7 |
|    150 | TRANS  | 2017-01-03 | DATA |       8 |
|    150 | TRANS  | 2017-01-04 | DATA |       9 |
|    150 | TRANS  | 2017-01-05 | DATA |      10 |
|    150 | TRANS  | 2017-01-06 | DATA |      11 |
|    150 | TRANS  | 2017-01-07 | DATA |      12 |
|    150 | TRANS  | 2017-01-08 | DATA |      13 |
|    150 | TRANS  | 2017-01-09 | DATA |      14 |
|    150 | TRANS  | 2017-01-10 | DATA |      15 |
|    150 | TRANS  | 2017-01-11 | DATA |      16 |
|    150 | TRANS  | 2017-01-12 | DATA |      17 |
|    150 | TRANS  | 2017-01-13 | DATA |      18 |
|    150 | TRANS  | 2017-01-14 | DATA |      19 |
|    150 | TRANS  | 2017-01-15 | DATA |      20 |
|    150 | TRANS  | 2017-01-16 | DATA |      21 |
|    150 | TRANS  | 2017-01-17 | DATA |      22 |
|    150 | TRANS  | 2017-01-18 | DATA |      23 |
|    150 | TRANS  | 2017-01-19 | DATA |      24 |
|    150 | TRANS  | 2017-01-20 | DATA |      25 |
|    150 | TRANS  | 2017-01-21 | DATA |      26 |
|    150 | TRANS  | 2017-01-22 | DATA |      27 |
|    150 | TRANS  | 2017-01-23 | DATA |      28 |
|    150 | TRANS  | 2017-01-24 | DATA |      29 |
|    150 | TRANS  | 2017-01-25 | DATA |      30 |
|    160 | TRANS  | 2017-01-26 | DATA |      31 |
|    160 | NEW    | 2017-01-27 | DATA |      32 |
|    160 | NEW    | 2017-01-28 | DATA |      33 |
|    160 | NEW    | 2017-01-29 | DATA |      34 |
|    160 | NEW    | 2017-01-30 | DATA |      35 |
+--------+--------+------------+------+---------+

我尝试使用Cross应用但很耗时。是否可以不进行交叉申请?

2 个答案:

答案 0 :(得分:1)

Declare @LatestAmount VARCHAR(4)= (select Amount FROM mytable  WHERE Date = (SELECT MAX(Date) FROM mytable WHERE Amount IS NOT NULL))

SELECT Amount,Status,Date,Type,MetaKey FROM mytable WHERE Status= 'TRANS' AND Type='DATA'
UNION
SELECT ISNULL(Amount,@LatestAmount),Status,Date,Type,MetaKey FROM mytable WHERE Status= 'NEW' AND Type='DATA'
ORDER BY Date

答案 1 :(得分:1)

你有没有试过这样的事情:

SELECT 
    COALESCE(T1.amount,t2.amount) amount
    ,T1.Status
    ,T1.DATE
    ,T1.Type
    ,T1.MetaKey
FROM mytable T1
LEFT JOIN
    (
    SELECT TOP 1 amount
    FROM mytable
    WHERE TYPE = 'DATA'
    AND amount IS NOT NULL
    ORDER BY DATE DESC
    ) T2
    ON T2.Amount IS NOT NULL
    AND T1.amount IS NULL
;