如何根据数据库中其他来源的可用性来切换查看真实来源的记录(如果可用或不可用)

问题描述 投票:0回答:1

在SSMS中, 我们有 4 个不同的来源,每个数字记录都在一个事实表中。

业务希望我切换可用性记录以确定优先级

我们有实际记录或预测吗?如果是预测的话;

首先,sourcedId = 4 中是否有可用记录,然后将记录带入 如果我们有 sourceId = 4,我想查看最新的记录。 其次,如果我们在 sourceId = 4 中没有任何可用记录,则转到 sourceId = 1 并把记录拿来给我

如果不是投影,那么它就是实际值,所以使用 sourceId = 2 并将记录带给我。

我想了解我可以使用什么方法,我以前从未做过这样的事情。 任何想法,任何例子都非常感谢!

谢谢!

CREATE TABLE mytable(
   ID          INTEGER  NOT NULL PRIMARY KEY 
  ,SCENARIO_ID VARCHAR(15) NOT NULL
  ,GUID_ID     VARCHAR(12) NOT NULL
  ,RECORD_TYPE VARCHAR(11) NOT NULL
  ,VALUE       NUMERIC(4,2) NOT NULL
  ,APPROVED_ON DATE 
  ,SOURCE_ID   INTEGER  NOT NULL
  ,IS_APPROVED VARCHAR(8)
);
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (1,'sdfgsdfgsdfg','sdfsd1231','actuals',7.64,NULL,2,NULL);
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (2,'fncvbdfsgdfg','sdfgsd213','actuals',6.78,NULL,2,NULL);
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (3,'fghcvbfncvb','sdfgsdfg4234','actuals',5.92,NULL,2,NULL);
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (4,'fdsgsdhsdf','sdfgf234','actuals',5.06,NULL,2,NULL);
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (5,'vbncvbncvbn','dfghdf4534','actuals',4.2,NULL,2,NULL);
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (6,'dfghgfdhfgdh','ncbnc2342','actuals',3.34,NULL,2,NULL);
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (7,'bvnmvbnmv','fbhdfg4567','projections',2.34,NULL,4,'DENIED');
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (8,'srejtsskdjfhgsk','fbhdfg4567','projections',2.56,'9/5/2023',4,'APPROVED');
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (9,'skdjfhgskjdfhg','fghdfgh3453','projections',2.78,NULL,4,'DENIED');
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (10,'oswiertsdfjg','fghdfgh3453','projections',3,NULL,1,NULL);
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (11,'erthskljdjg','cfghfg3453','projections',3.22,'9/3/2023',4,'APPROVED');
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (12,'dfghdfgh','cfghfg3453','projections',3.44,'9/2/2023',4,'APPROVED');
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (13,'cxvbxcvbxcvb','cfghfg3453','projections',3.66,NULL,4,'DENIED');
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (14,'dfsgdfgxdbx','dfghdf345','projections',3.88,NULL,1,NULL);
INSERT INTO mytable(ID,SCENARIO_ID,GUID_ID,RECORD_TYPE,VALUE,APPROVED_ON,SOURCE_ID,IS_APPROVED) VALUES (15,'erstdfgdcbx','fdghdfghd454','projections',4.1,NULL,4,'DENIED');
tsql switch-statement datasource
1个回答
0
投票

我会尝试一下。但由于期望不明确,不确定这是否是一个好的:

SELECT 
        M.*
FROM mytable AS M
INNER JOIN  (
                SELECT MAX(approved_on) AS APPROVED_ON
                FROM #mytable
                WHERE RECORD_TYPE = 'PROJECTIONS' AND SOURCE_ID = 4
            ) AS T
    ON (M.RECORD_TYPE = 'PROJECTIONS' AND M.SOURCE_ID = 4 AND M.APPROVED_ON = T.APPROVED_ON)
    OR (M.RECORD_TYPE = 'PROJECTIONS' AND M.SOURCE_ID = 1)
    OR (M.RECORD_TYPE = 'ACTUALS' AND M.SOURCE_ID = 2);
© www.soinside.com 2019 - 2024. All rights reserved.