Solved

SQL Select create column

Posted on 2014-03-12
4
638 Views
Last Modified: 2014-03-12
Hi Experts

I need to create a SQL Select that will pull all the fields as per the code below and have an additional field based on the field Pay or Receive.

If field is Pay new field says 'Pay by Position'
If field says receive, new field says pay by customer

can you please help

select p.PORTFOLIOMNEMONIC, td.TRADEDIRECTION, cf.CUSTOMERID, cf.CUSTOMERMNEMONIC, TP.TRADECCY1AMOUNT
from CUSTOMERFAMILY cf 
left join CUSTOMER c on c.CUSTOMERID = cf.CUSTOMERID 
left join PORTFOLIO p on p.VERIFICATIONCODE = c.VERIFICATIONCODE
left join TRADEDETAIL td on td.CUSTOMERID = c.CUSTOMERID
left join TRADEPOSITION tp on td.TRADEDIRECTION = tp.TRADEDIRECTION 
where 
--p.PORTFOLIOMNEMONIC in( 'MAIN') and 
cf.CUSTOMERID like '%IFE%' and
td.tradedirection in( 'PAY', 'RECEIVE')

Open in new window

0
Comment
Question by:SimonPrice33
  • 2
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39923843
select 
   p.PORTFOLIOMNEMONIC, 
   td.TRADEDIRECTION, 
   cf.CUSTOMERID, 
   cf.CUSTOMERMNEMONIC, 
   TP.TRADECCY1AMOUNT, 
   CASE td.tradedirection     -- < -- Looky here
      WHEN 'PAY' THEN 'Pay by Position' 
      WHEN 'RECEIVE' THEN 'Pay by Customer' END as column_name_goes_here
from CUSTOMERFAMILY cf 
   left join CUSTOMER c on c.CUSTOMERID = cf.CUSTOMERID 
   left join PORTFOLIO p on p.VERIFICATIONCODE = c.VERIFICATIONCODE
   left join TRADEDETAIL td on td.CUSTOMERID = c.CUSTOMERID
   left join TRADEPOSITION tp on td.TRADEDIRECTION = tp.TRADEDIRECTION 
where cf.CUSTOMERID like '%IFE%' and td.tradedirection in( 'PAY', 'RECEIVE')

Open in new window

For more info on CASE blocks there's an article out there on SQL Server CASE Solutions if that helps.
0
 

Author Comment

by:SimonPrice33
ID: 39923871
awesome... i thought it would be a case but wasnt quite sure how i would write it

will letyou konw how it works.
0
 

Author Closing Comment

by:SimonPrice33
ID: 39923900
works a charm!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39923987
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question