Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Derived Table from the existing view data

$
0
0

From the following View can someone please suggest what changes can I make to build a derived table using all the customer and column for gender in the query logic please..

 

Create View dbo.vwGetCustomers

With CTE AS

(

Select EC.IDCustomer_SACustomer_HubRowID

,EC.CTV3Code_SACtv3_HubRowID

,EC.CTV3Code

,EC.DateEvent_Date_HubRowID,

EC.DateEvent_Time_HubRowID

,ROW_NUMBER() OVER (PARTITION BY EC.IDCustomer_SACustomer_HubRowID ORDER BY EC.DateEvent_Date_HubRowID DESC

,EC. DateEvent_Time_HubRowID DESC)

AS Tmptble

FROM dbo.SACode_Details AS EC

WHERE EC.CTV3Code

IN(‘abc’,’def’,’bac’,’aaa’,’ddd’)

)

SELECT CH.IDCustomer_SACustomer_HubRowID AS SACustomer_HubRowID

, CH.CTV3Code_SACtv3_HubRowID AS Gender_SACTv3_HubRowID

,CH.CTV3Code AS Gender_CTV3Code ,C.CTV3Text AS Gender_CTV3Text

,CASE WHEN C.CTV3Code  =  ‘abc’ then ‘A

           WHEN C.CTV3Code  =  ‘def’ then ‘B’

            WHEN C.CTV3Code  =  ‘bac’ then ‘C’

           WHEN C.CTV3Code  =  ‘aaa’ then ‘D’

               WHEN C.CTV3Code  =  ‘ddd’ then ‘E’

END AS Gender_Code

,CH.DateEvent_Date_HubRowID

,CH.DateEvent_Time_HubRowID

FROM CTE AS CH

INNER JOIN dbo.SACTV3_Details AS C

                               ON CH.CTV3Code_SACtv3_HubRowID = C.HubRowID

WHERE CH.Tmptble = 1;


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>