SQL 服务器:Return 数据不存在时为空白值
SQL Server: Return blank value when data not exists
我遇到了问题,正在寻求您的帮助。我有一个名为 freefieldassignment
的 table,其中包含如下所示的一些数据。
+-----------------+-------------------+--------+--------+
| Carrierobjectid | freefieldobjectid | value |objectid|
+-----------------+-------------------+--------+--------+
| 90465009 | 75509 | D-AA |90453004|
| 90465009 | 76378 | Yellow |90453062|
| 90465009 | 87395964 | ESCORT |90453072|
+-----------------+-------------------+--------+--------+
最后一个值为 ESCORT
的事件有时可能会从 table 中删除,因为用户是从他正在使用的应用程序中选择的。
我想做的是当ESCORT
被删除时,return一个空白值(或空值)
我尝试使用 NOT EXISTS
编写查询,但它不起作用。
事情是这样的
select carrierobjectid,
case
when [freefieldassignment].[value] = 'Escort' then 'Escort'
when NOT EXISTS (SELECT [freefieldassignment].[value] FROM [freefieldassignment]
WHERE [freefielddefinition].[objectid]= 87409935
AND [freefieldassignment].[carrierobjectid] = 90465009) then 'NULL'
有人可以帮我吗?非常感谢,谢谢。
我想你想要像下面这样
select carrierobjectid,
case
when t.value = 'Escort' then t.value
else null end as value
FROM freefieldassignment t
WHERE t.objectid= 87409935
AND t.carrierobjectid = 90465009
这是我的测试table:
CREATE TABLE dbo.freefieldassignment (
Carrierobjectid NVARCHAR(255),
freefieldobjectid NVARCHAR(255),
value NVARCHAR(255),
objectid NVARCHAR(255)
)
INSERT INTO freefieldassignment (Carrierobjectid, freefieldobjectid, value, objectid)
VALUES
('1','2','N','2'),
('1','3','N','2'),
('1','3','N','3'),
('1','3','ESCORT','3')
这是我的查询。如果没有为 group by 子句中使用的值找到 ESCORT 行,则第二个查询将生成行:
SELECT Carrierobjectid, freefieldobjectid, value, objectid FROM freefieldassignment
WHERE Carrierobjectid = 1 AND objectid = 2
UNION ALL
SELECT Carrierobjectid, null, null, objectid
FROM freefieldassignment
WHERE Carrierobjectid = 1 AND objectid = 2
GROUP BY Carrierobjectid, objectid
HAVING MAX(CASE WHEN value = 'ESCORT' THEN 1 ELSE 0 END) = 0
结果:
Carrierobjectid | freefieldobjectid | value | objectid
1 | 2 | N | 2
1 | 3 | N | 2
1 | NULL | NULL | 2
当行 ESCORT 存在时,来自 union 的第二个查询没有 return 数据:
SELECT Carrierobjectid, freefieldobjectid, value, objectid FROM freefieldassignment
WHERE Carrierobjectid = 1 AND objectid = 3
UNION ALL
SELECT Carrierobjectid, null, null, objectid
FROM freefieldassignment
WHERE Carrierobjectid = 1 AND objectid = 3
GROUP BY Carrierobjectid, objectid
HAVING MAX(CASE WHEN value = 'ESCORT' THEN 1 ELSE 0 END) = 0
结果
Carrierobjectid | freefieldobjectid | value | objectid
1 | 3 | N | 3
1 | 3 | ESCORT | 3
我遇到了问题,正在寻求您的帮助。我有一个名为 freefieldassignment
的 table,其中包含如下所示的一些数据。
+-----------------+-------------------+--------+--------+
| Carrierobjectid | freefieldobjectid | value |objectid|
+-----------------+-------------------+--------+--------+
| 90465009 | 75509 | D-AA |90453004|
| 90465009 | 76378 | Yellow |90453062|
| 90465009 | 87395964 | ESCORT |90453072|
+-----------------+-------------------+--------+--------+
最后一个值为 ESCORT
的事件有时可能会从 table 中删除,因为用户是从他正在使用的应用程序中选择的。
我想做的是当ESCORT
被删除时,return一个空白值(或空值)
我尝试使用 NOT EXISTS
编写查询,但它不起作用。
事情是这样的
select carrierobjectid,
case
when [freefieldassignment].[value] = 'Escort' then 'Escort'
when NOT EXISTS (SELECT [freefieldassignment].[value] FROM [freefieldassignment]
WHERE [freefielddefinition].[objectid]= 87409935
AND [freefieldassignment].[carrierobjectid] = 90465009) then 'NULL'
有人可以帮我吗?非常感谢,谢谢。
我想你想要像下面这样
select carrierobjectid,
case
when t.value = 'Escort' then t.value
else null end as value
FROM freefieldassignment t
WHERE t.objectid= 87409935
AND t.carrierobjectid = 90465009
这是我的测试table:
CREATE TABLE dbo.freefieldassignment (
Carrierobjectid NVARCHAR(255),
freefieldobjectid NVARCHAR(255),
value NVARCHAR(255),
objectid NVARCHAR(255)
)
INSERT INTO freefieldassignment (Carrierobjectid, freefieldobjectid, value, objectid)
VALUES
('1','2','N','2'),
('1','3','N','2'),
('1','3','N','3'),
('1','3','ESCORT','3')
这是我的查询。如果没有为 group by 子句中使用的值找到 ESCORT 行,则第二个查询将生成行:
SELECT Carrierobjectid, freefieldobjectid, value, objectid FROM freefieldassignment
WHERE Carrierobjectid = 1 AND objectid = 2
UNION ALL
SELECT Carrierobjectid, null, null, objectid
FROM freefieldassignment
WHERE Carrierobjectid = 1 AND objectid = 2
GROUP BY Carrierobjectid, objectid
HAVING MAX(CASE WHEN value = 'ESCORT' THEN 1 ELSE 0 END) = 0
结果:
Carrierobjectid | freefieldobjectid | value | objectid
1 | 2 | N | 2
1 | 3 | N | 2
1 | NULL | NULL | 2
当行 ESCORT 存在时,来自 union 的第二个查询没有 return 数据:
SELECT Carrierobjectid, freefieldobjectid, value, objectid FROM freefieldassignment
WHERE Carrierobjectid = 1 AND objectid = 3
UNION ALL
SELECT Carrierobjectid, null, null, objectid
FROM freefieldassignment
WHERE Carrierobjectid = 1 AND objectid = 3
GROUP BY Carrierobjectid, objectid
HAVING MAX(CASE WHEN value = 'ESCORT' THEN 1 ELSE 0 END) = 0
结果
Carrierobjectid | freefieldobjectid | value | objectid
1 | 3 | N | 3
1 | 3 | ESCORT | 3