Solved

SQL Select create column

Posted on 2014-03-12
4
665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 66

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 66

Expert Comment

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

Featured Post

Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

632 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