Solved

SQL Select create column

Posted on 2014-03-12
4
624 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
Comment Utility
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
Comment Utility
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
Comment Utility
works a charm!
0
 
LVL 65

Expert Comment

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

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Backup 24 67
Powershell SMO script not working. 18 97
Trouble connecting to SqlServer database 4 29
Report Builder 9 22
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now