Linking two table..it is look like unpivot but it is not

Hi guys, I got a situation like this:
Table A
create table #fly (company varchar (50), period varchar(6), adcost (int), ppcost (int))
insert into #fly values ('PanAm', '201401', 100, 200) ,  ('PanAm', '201402', 200, 200) , ('PanAm', '201403', 700, 1000), ('EGAm', '201401', 1100, 1200), ('EGAm', '201402', 7100, 7200)                                                                                                                 
Table 2 #Cost (firm varchar (100), client varchar (100), period varchar(6), element (varchar 10), tot int) insert into #firm ('Zuck', ''Panam', '201401', ''adcost', 40), ('Zuck', ''Eagam', '201401', ''adcost', 40), ('Zuck', ''Panam', '201402', ''ppcost', 240)                
As you can see I got in fly the field adcost that is a value in the table #cost. I need to link the two table and I'm wondering whitch is the best way. Another issue is the adcost (#fly) has in #cost two relatives adcost and fixadcost element. I should consider
both the elements as adcost.  Advices? Thanks

create table #fly (company varchar (50), period varchar(6), adcost int, ppcost int)
insert into #fly values ('PanAm', '201401', 100, 200) , ('PanAm', '201402', 200, 200) , ('PanAm', '201403', 700, 1000), ('EGAm', '201401', 1100, 1200), ('EGAm', '201402', 7100, 7200)
create table #Cost (firm varchar (100), client varchar (100), period varchar(6), element varchar (20), tot int)
insert into #cost values ('Zuck', 'Panam', '201401', 'adcost', 40), ('Zuck', 'Eagam', '201401', 'adcost', 40)
, ('Zuck', 'Panam', '201401', 'fixedadcost', 40)
, ('Ford', 'Panam', '201401','ppcost', 240)
select * from #Cost
select * from #Fly
Select company,f.period,adcost, (c1.tot+c2.tot) as [adcost + fixedadcost] , ppcost, c3.tot as [ppcost from Cost] from (select company,period,adcost,ppcost from #Fly
WHERE company='PanAM' AND period='201401') f
left join #Cost c1 on c1.period = f.period AND c1.client=f.company AND c1.element ='adcost'
left join #Cost c2 on c2.period = f.period AND c2.client=f.company AND c2.element ='fixedadcost'
left join #Cost c3 on c2.period = f.period AND c3.client=f.company AND c3.element ='ppcost'
drop table #Cost, #Fly

Similar Messages

  • Query for Linking Two Tables without Reference

    Hi Folks,
    Good Day!
    I have this dilemma on how to link two tables without a reference. In the Business Partner Master Data, there is a field for the Territory which is from the table OTER. This OTER focuses on its description. Once you have added the particular Territory for that Business Partner, it is being stored in the Territory field of the BP right? Our client wants to have an automated way of searching for the Business Partner's Profit Center in creating a Sales Order wherein it is the same as the Business Partner's Territory. The Profit Center is from the table OPRC.
    When I create a Sales Order, after I have entered the Business Partner/Customer Code, the Profit Center should have the value of the Profit Center Code wherein it is the Business Partner's Territory. My query will go like this:
    Enter the BP/Customer Code automatically the Profit Center code where Profit Name = BP's Territory.
    OTER and OCRD don't have connections. How can it be?
    I'm thinking of retrieving the Territory first before the Profit Center but can you suggest of any other way?
    Thank you much!
    Regards,
    Fringe

    Hi Fringe,
    Okay, I already understood your case here. I presume your configuration as follows:
    OTER (Territory)
    territryID - descript
    1 - Philippine
    2 - Indonesia
    3 - Germany
    OPRC (Cost Centre)
    PrcCode - PrcName
    25 - Philippine
    33 - Indonesia
    47 - Germany
    Was above illustration correct? Then, you could use this formatted search in Cost Centre / Dimension field in Sales Order rows
    SELECT PrcCode FROM OPRC WHERE PrcName=(
    SELECT TOP 1 descript FROM OTER T0
    INNER JOIN OCRD T1 ON T0.territryID=T1.Territory
    WHERE T1.CardCode=$[OCRD.CardCode.0])
    However, I suggest you use a little bit different approach here. Since you can't define Territory Code / Territory ID (you could only type the Territory name, Philippine etc.), why don't you standardize Territory name with Cost Centre code? Let say, you define Philippine as PHI, Indonesia as INA, Germany as DEU and so on. Therefore, user can read the Cost Centre or more precisely Distribution Rules in Sales Order form in more familiar way.
    OTER (Territory)
    territryID - descript
    1 - PHI
    2 - INA
    3 - DEU
    OPRC (Cost Centre)
    PrcCode - PrcName
    PHI - Philippine
    INA - Indonesia
    DEU - Germany
    When FMS worked in Sales Order document, your user will read PHI instead of 25, should be more familiar to them. With that being said, you could use simpler FMS
    SELECT descript FROM OTER T0
    INNER JOIN OCRD T1 ON T0.territryID=T1.Territory
    WHERE T1.CardCode=$[OCRD.CardCode.0]
    You only need to maintain consistency between Territory Name and Cost Centre code. Just my two cents. Hope this help.
    Best Regards,
    Hendry Wijaya

  • In ER DIagram using toad( how can i link two tables)

    Hi,
    Any Toad expert can guide me that how can i link two tables in Toad in ER Diagram to show the relationship between two tables.
    Thanks

    Hi,
    I hope there is foreign key relationship in tables, if so then you can click on "Find table dependencies" icon which will draw lines between them.
    ~Vinod

  • How to link two tables?

    Hello,
    I'm trying to create invoices with Pages, but my question is how can I link two tables in one Pages document?
    Or having two Header Rows with some 'normal' rows between would also be great. (even better)
    I'm trying to calculate my subtotal etc. below my overview of articles.
    Thanks in advance!
    Andy

    (1) no way to link two tables in Pages.
    (2) you may have several header rows, several footer rows but you can't have standard rows between header (or footer) ones.
    You may achieve what you describe without the asked feature.
    Use correct ranges and you will be able to calculate subtotals.
    Yvan KOENIG (VALLAURIS, France) lundi 28 décembre 2009 19:02:38

  • Since itunes 2.0.4 has been downloaded. My ipod doesn't synchronize properly/ at all.The LED screen shows  "copy 1 or 2 from 101" and it will remain like that. It looks like sync but there's nothing to happen. It doesn't stop by it self. How to solve?

    Since itunes 2.0.4 has been downloaded my ipod doesn't synchronize properly/ at all. The LED screen shows çopying 1 or 2 from 101' and itt will remain like that.
    It looks like sync but nothing is happening. The sync doesn't stop by itself. Is there anybody to solve this problem? In previous versions never a problem.

    I have exactly the same issues with my new 80G iPOD. I have found that when connecting to my PC, the iPOD does not start iTUNES 7. This can be resolved by stopping the iPOD Service in Computer Management, connecting the iPOD and then starting it again. Then when you start iTUNES it finds the iPOD. - http://docs.info.apple.com/article.html?artnum=93716
    Seems like a stupid long-winded way to do things, but at least you can get some stuff on to your iPOD.
    As far as using Restore goes - I just get the restoring iPOD message you described, which it blatantly isn't :((
    DELL   Windows XP Pro  

  • Is possible to have a script that makes a text randomly have caps and lower caps... i would love to create a kind of effect so it would look like manuscript - but it cannot be only the same letter in caps or lower caps - I wOuld NeEd oF SOmeTHing LikE Thi

    Is possible to have a script that makes a text randomly have caps and lower caps... i would love to create a kind of effect so it would look like manuscript - but it cannot be only the same letter in caps or lower caps - I wOuld NeEd oF SOmeTHing LikE This (random) - is it possibel?

    Hi,
    Sample with 2 regex:
    Launching the 1rst regex:
    Launching the 2nd regex:

  • I downloaded from iTunes a hd movie for the first time. What a disaster !                                                Keep stopping and looks like after 2 days has not been downloaded. What am I suppose to do? Can I get my money back? How?

    I downloaded from iTunes a hd movie for the first time. What a disaster ! Keep stopping and looks like after 2 days has not been downloaded. What am I suppose to do? Can I get my money back? How?

    Contact iTunes suport... http://www.apple.com/support/itunes/contact/
    It sounds like you might have a slow wifi internet connection?  How fast is it with a speed test from your iPad.

  • Who can do a jsp page look like applet,but  no applet in it?

    Who can do a jsp page look like applet , but no applet in it?
    It means can use swing , panel or others show . Same as WebControlLibrary in .net.

    Try to use jato.... in Sun Java Studio it is Sun One Application Framework folder

  • Hi, I need help for my notebook Macbook Pro - I turned on the notebook and the screen look like start but did not start nothing, the screen froze and continuing load, load and never stop.

    Hi, I need help for my notebook Macbook Pro - I turned on the notebook and the screen look like start but did not start nothing, the screen froze and continuing load, load and never stop

    screen look on

  • Hi All, I have new iphone 4s and want to import my contacts from my old Orange SIM (the contacts have all been copied to SIM). I enter the SIM and select "Import from SIM" in the settings -- Calendar, Contacts etc it look like Ok but nothing copied,helpp

    Hi All, I have new iphone 4s and want to import my contacts from my old Orange SIM (the contacts have all been copied to SIM). I enter the SIM and select "Import from SIM" in the settings --> Calendar, Contacts etc it looks like Ok but nothing copied,I tried hundreds of time and waited 2 day but still nothing and I again made sure that I do have the Contacts in the Microsim and it is there but cant import? dunno why???

    I have never used it, but have heard good things...
    http://www.littlemachines.com/

  • HT4993 I changed my "invert colors" to negative and i love the way the apps look. Is there anyway that i can keep the apps looking like that but keep all my pictures the normal colors??

    I changed my "invert colors" to negative and i love the way the apps look. But is the anyway i can keep all the apps looking like that but keep all my pictures the normal colors?

    no

  • I purchased two tv shows on my new ATV2, but they do not show up in iTunes on my computer. Was hoping to download to ipad2 for travel. Help!?.

    I purchased two tv shows on my new ATV2, but they do not show up in iTunes on my computer. Was hoping to download to ipad2 for travel. Help!?.

    Welcome to the Apple Community.
    Have you tried the 'Purchased' option in the top right corner of the Store window in iTunes

  • Linking two tables with no duplicated records!

    Hi,
    I have two tables now with both tables is having the common filed matnr.
    now using this field MATNR from  first table ,i need to fetch projectno from the other table which has to be unique.if there are more than one entry found for MATNR in question then that must not be updated to the first table!
    any ideas?
    Raj

    hi raj,
    try this code.
    data:
      ld_count type p,
      ld_tabix type sy-tabix.
    loop at itab1.
      move sy-tabix to ld_tabix.
      ld_count = 0.
      loop at itab2 where matnr eq itab1-matnr.
        add 1 to ld_count.
      endloop.
      if ld_count = 1.
        read table itab2 with key matnr = itab1-matnr.
        move itab2-project to itab1-project.
        modify itab1 index ld_tabix.
      endif.
    endloop.

  • To link two tables in ap

    hi
    could someone pls guide me to link ap_invoices_all table with gl_code_combinationsthanking in advance
    Edited by: makdutakdu on Oct 14, 2010 12:15 PM

    Hello.
    Use the following query (R11i):
    SELECT i.invoice_num "Invoice Nr.",
    c.segment1 "Segment1",
    l.ae_line_type_code "Line Type",
    l.accounted_dr "Debit",
    l.accounted_cr "Credit"
    FROM ap_ae_lines_all l, ap_ae_headers_all h, ap_invoices_all i, ap_accounting_events_all e, gl_code_combinations c
    WHERE e.source_table = 'AP_INVOICES'
    AND i.invoice_num = '<your invoice nymber>'
    AND l.ae_header_id = h.ae_header_id
    AND e.accounting_event_id = h.accounting_event_id
    AND e.source_id = i.invoice_id
    AND l.code_combination_id = c.code_combination_id
    AND i.org_id = <your org_id>
    As you may have more than one invoice with the same number, the query may return more lines then expected. To restrict it, enter the vendor_id from table AP_INVOICES_ALL.
    Hope this helps.
    Octavio
    Edited by: Octopus Rex on Oct 14, 2010 4:10 AM

  • Linking two lists to create a Data Source but cannot add web part to page in SharePoint designer

    All;
    I created a new data source by linking two lists which created a linked Data Source. I now want to access that linked data source via insertion of a Data Source control on a web part page.
    What I find is, both DATA SOURCE and DATA VIEW are inactive and unselectable.
    Is there any way to get this functionality going?
    Thx
    Larry Pexter

    Hi Larry,
    Since it is a coding UI, we need to put cursor into appropriate place so we can insert data view web part, i.e. the coding which represents the web part in to the whole coding part.
    As I tested in my envrionment, if you put cursor between <ZoneTemplate> and </ZoneTemplate>, I cannot get the DATA view, even before </ZoneTemplate>. Please put it exactly before <ZoneTemplate>.
    If you have modify the coding, please click save and test the issue again.
    Regards,
    Rebecca Tu
    TechNet Community Support

Maybe you are looking for

  • Multipanel and/or Multiple GUI DLL

    Hello, I am in the process of writing an application that will use some CVI GUI DLL's   The thought is each DLL is associated with an instrument and will have an instrument specific user interface that can be accessed through an exported function (RU

  • Russian Translations problem with Translation Tool Package

    Hello Everybody. I'm doing some translations and whenever I try to paste a Russian text into one of the messages in TTP, the app transforms the Russian characters into question marks. For example: Предупреждение: Контактное лицо X не имеет связи с Би

  • Password-protected Numbers spreadsheet only opens in computer that created it

    I recently password-protected a spreadsheet file on my iMac.  It works just fine when I open it on my iMac.  However, if I try to open that same file from my MacBook Air, it rejects the password.  It says that the password is incorrect.  When I try t

  • Changing from Active to Standby in a DHCP Failover scenario (on the fly) without deconfiguring operation...

    Hi all; Suppose I have set up a scope in SRV01 to be in Active mode. Is it possible to change its state to Standby mode (on the fly) without deconfigurig it and then configuring it again with my desired configuration? Thanks Please VOTE as HELPFUL if

  • Installing adobe reader x

    i have not been able to install adobe reader x for a week now. when downloading i get to about 96 percent and then it stops and says: Window Installer Service could not be accessed. It says, This can occur if the Window Installer is not correctly ins