Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Select create column

Posted on 2014-03-12
4
Medium Priority
?
683 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 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

670 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