Reputation: 1
I have a SQL view created already & that view gives certain columns from a database table Data
. Now, I want to modify that view in such a manner that it does not need to add any additional columns to the result & should match exactly with the same result as this view generated before. The only modification I want here is: There is a column called PayDate
that the view takes from Data
table. I need that column to have value depending on other column called OrderType
. The Data
table looks like :
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
Currently, the view gives OrderType
& Paydate
as a result. Twist here is, for OrderType = 'CA' OR 'Dvidend'
we have PayDate
. For OrderType = 'CO' OR 'IO'
we have ValuDate
. Each time for 'CO' or 'IO' the value got is NULL. So here I want a CASE that will check if OrderType
is 'CO' OR 'IO' it'll return ValuDate
in PayDate column. And at places where OrderType
is 'CA' or 'Dividend' give value of PayDate
from Data
table in PayDate
column of the view.
PS : Do not want to add a ValuDate
column here. All we need is just put the value of ValueDate
in PayDate
column of view if OrderType
is 'CO' or 'IO'`
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
Upvotes: 0
Views: 44
Reputation: 1
This is something which I have done. But I want to use 'UNION' & get the view modified. Can we use UNION in anyways to modify this view??
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
Upvotes: 0
Reputation: 2203
Trying to follow what you are asking. Is this what you mean?
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
EDIT: I suspect the first part needs to be
SELECT ISNULL(Book, '') + '_' + ISNULL(OrderType,'') + '_'
+ ISNULL(CONVERT(VARCHAR(8), CASE WHEN OrderType in ('CO','IO') THEN t.ValuDate ELSE t.PayDate END, 112), '') + '_'
Upvotes: 1