0

我有一个dbo.VBAK有列的表VKORG, VBELN and SPART

如何在下面正确编写查询

If dbo.tblVBAK.VKORG = '1001' 
BEGIN
   select dbo.tblVBAK.VKORG as 'Sales Org Approved' from dbo.tblVBAK
END
ELSE
BEGIN
   Select dbo.tblVBAK.VKORG as 'Sales Org Not Approved' from dbo.tblVBAK
END

我正在使用 SQL Server 2005

4

3 回答 3

2
SELECT CASE 
       WHEN VKORG = '1001' 
       THEN 'Sales Org Approved' 
       ELSE 'Sales Org NOT Approved' 
       END,
       VBELN,
       SPART
FROM   dbo.tblVBAK

如果您想将它们放在不同的列上,那么:

SELECT CASE 
       WHEN VKORG = '1001' 
       THEN VKORG
       END AS 'Sales Org Approved',
       CASE 
       WHEN VKORG <> '1001' 
       THEN VKORG
       END AS 'Sales Org Not Approved',
       VBELN,
       SPART
FROM   dbo.tblVBAK
于 2013-08-30T19:41:13.233 回答
0

我相信你想要这个:

SELECT 
 CASE WHEN VKORG = '1001' THEN VKORG ELSE NULL END as 'Sales Org Approved',
 CASE WHEN VKORG != '1001' THEN VKORG ELSE NULL END as 'Sales Org Not Approved'
FROM tblVBAL
于 2013-08-30T19:51:06.333 回答
0

另一种方法可以用 a 来完成UNION

SELECT VKORG AS 'Sales Org Approved', 
       null AS 'Sales Org Not Approved', 
       VBELN,
       SPART
FROM tblVBAK
WHERE VKORG = '1001'
UNION ALL
SELECT null AS 'Sales Org Approved', 
       VKORG AS 'Sales Org Not Approved', 
       VBELN,
       SPART
FROM tblVBAK
WHERE VKORG != '1001';
于 2013-08-30T20:07:11.183 回答