我已经创建了一个SQL视图&该视图提供了数据库表Data
中的某些列。现在,我想以这样一种方式修改该视图,即它不需要在结果中添加任何其他列。应该与之前生成的视图完全匹配。我想要的唯一修改是:视图从PayDate
表中获取了一个名为Data
的列。我需要该列具有值,具体取决于名为OrderType
的其他列。 Data
表格如下:
Book OrderType PayDate ValuDate
-------------------------------------------
FFF CA 21/05/2015 NULL
BBB CO NULL 2/02/2014
EEE IO NULL 3/2/2014
QQQ Dividend 02/02/2014 NULL
目前,该视图提供了OrderType
&结果Paydate
。这里有麻烦,OrderType = 'CA' OR 'Dvidend'
我们有PayDate
。对于OrderType = 'CO' OR 'IO'
,我们有ValuDate
。每次'CO'或'IO'得到的值都是NULL。所以我在这里想要一个CASE来检查OrderType
是'CO'还是'IO'它将在PayDate列中返回ValuDate
。在OrderType
为'CA'或'Dividend'的地方,在PayDate
列的Data
列中为PayDate
表提供ValuDate
的值。
PS:不想在此处添加ValueDate
列。如果PayDate
是'CO'或'IO',那么我们只需将OrderType
的值放在IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[ABC]'))
BEGIN
DROP VIEW [dbo].[ABC]
PRINT '<<< DROPPED VIEW [dbo].[ABC] >>>'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[ABC]
AS
SELECT ISNULL(Book, '') + '_' + ISNULL(OrderType,'') + '_'
+ ISNULL(CONVERT(VARCHAR(8), PayDate, 112), '') + '_'
MAX(t.[InstrumentKey]) [InstrumentKey] ,
MAX(t.[BookKey]) [BookKey] ,
t.[OrderType],
MAX(t.[InstrumentID]) [InstrumentID] ,
t.PayDate ,
FROM dbo.Data t (NOLOCK)
LEFT OUTER JOIN dbo.ExtInstrument i ON t.InstrumentKey = i.InstrumentKey
WHERE [STATUS] = 'LIVE'
GROUP BY Book ,
OrderType ,
PayDate ,
BackToBackBook
GO
PRINT '<<< CREATED VIEW [dbo].[ABC] >>>'
GRANT SELECT ON dbo.ABC TO INVENTORY_READERS
GO
PRINT '<<< GRANT SELECT ON VIEW [dbo].[ABC] TO INVENTORY_READERS >>>'
GO
列的视图中
{{1}}
答案 0 :(得分:1)
试着按照你的要求去做。这是你的意思吗?
CREATE VIEW [dbo].[ABC]
AS
SELECT ISNULL(Book, '') + '_' + ISNULL(OrderType,'') + '_'
+ ISNULL(CONVERT(VARCHAR(8), PayDate, 112), '') + '_'
MAX(t.[InstrumentKey]) [InstrumentKey] ,
MAX(t.[BookKey]) [BookKey] ,
t.[OrderType],
MAX(t.[InstrumentID]) [InstrumentID] ,
CASE WHEN OrderType in ('CO','IO') THEN t.ValuDate ELSE t.PayDate END AS PayDate,
FROM dbo.Data t (NOLOCK)
LEFT OUTER JOIN dbo.ExtInstrument i ON t.InstrumentKey = i.InstrumentKey
WHERE [STATUS] = 'LIVE'
GROUP BY Book ,
OrderType ,
CASE WHEN OrderType in ('CO','IO') THEN t.ValuDate ELSE t.PayDate END ,
BackToBackBook
编辑:我怀疑第一部分需要
SELECT ISNULL(Book, '') + '_' + ISNULL(OrderType,'') + '_'
+ ISNULL(CONVERT(VARCHAR(8), CASE WHEN OrderType in ('CO','IO') THEN t.ValuDate ELSE t.PayDate END, 112), '') + '_'
答案 1 :(得分:0)
这是我所做的。但我想使用'UNION'&amp;修改视图。我们可以使用UNION来修改这个视图吗?
IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[ABC]'))
BEGIN
DROP VIEW [dbo].[ABC]
PRINT '<<< DROPPED VIEW [dbo].[ABC] >>>'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[ABC]
AS
SELECT ISNULL(Book, '') + '_' + ISNULL(OrderType,'') + '_'
+ ISNULL(t.RIC, '') + '_'
+ CASE OrderType
WHEN OrderType = 'PNoteCorporateAction' OR 'PNoteDividendDraft' OR 'PNoteDividend' THEN ISNULL(CONVERT(VARCHAR(8), PayDate, 112), '')
ELSE ISNULL(CONVERT(VARCHAR(8), ValuDate, 112), '')
END AS PayDate + '_'
MAX(t.[InstrumentKey]) [InstrumentKey] ,
MAX(t.[BookKey]) [BookKey] ,
t.[OrderType],
MAX(t.[InstrumentID]) [InstrumentID] ,
t.PayDate ,
FROM dbo.Data t (NOLOCK)
LEFT OUTER JOIN dbo.ExtInstrument i ON t.InstrumentKey = i.InstrumentKey
WHERE [STATUS] = 'LIVE'
GROUP BY Book ,
OrderType ,
CASE WHEN OrderType = 'PNoteCorporateAction' OR 'PNoteDividendDraft' OR 'PNoteDividend' THEN PayDate ELSE ValueDate END AS PayDate,
BackToBackBook
GO
PRINT '<<< CREATED VIEW [dbo].[ABC] >>>'
GRANT SELECT ON dbo.ABC TO INVENTORY_READERS
GO
PRINT '<<< GRANT SELECT ON VIEW [dbo].[ABC] TO INVENTORY_READERS >>>'
GO