Can a JOIN be used instead of a UNION to obtain the Total Sales from B1?

Hi Everyone,
I am in the midst of writing a report which shows the 'Total Sales', 'Gross Profit', and 'Gross Profit %' by Sales Person. Traditionally I have used a UNION between two result sets to achieve this task. However I am wondering it if is possible to achieve the same outcome with a JOIN as shown below?
SELECT
    T0.SlpCode
    , T0.SalesPerson
    , T0.Branch
    , (T0.TotalSales - T1.TotalSales) AS 'Total Sales'
    , ((T0.TotalSales - T1.TotalSales) - (T0.StockValue + T1.StockValue)) AS 'Gross Profit'
    , CAST(((T0.TotalSales - T1.TotalSales) - (T0.StockValue + T1.StockValue)) / NULLIF((T0.StockValue + T1.StockValue), 0) * 100 AS decimal(15,2)) AS 'Gross Profit %'
    FROM
        /*** SAP - Invoices by Sales Person ***/
        SELECT
        T1.SlpCode
        , T2.SlpName AS 'SalesPerson'
        , T2.U_INE_Branch AS 'Branch'
        , CAST(SUM(T0.LineTotal) AS decimal(15,2)) AS 'TotalSales'
        , CAST(SUM(T0.StockValue) AS decimal(15,2)) AS 'StockValue'
        FROM AU.dbo.INV1 T0
        INNER JOIN AU.dbo.OINV T1 ON T1.DocEntry = T0.DocEntry
        LEFT JOIN AU.dbo.OSLP T2 ON T2.SlpCode = T1.SlpCode
        WHERE T1.DocDate >= '2014-08-01' AND T1.DocDate <= '2014-09-01'
        GROUP BY T1.SlpCode, T2.SlpName, T2.U_INE_Branch
    ) AS T0
    FULL JOIN
        /*** SAP - Credits by Sales Person ***/
        SELECT
        T1.SlpCode
        , T2.SlpName AS 'SalesPerson'
        , T2.U_INE_Branch AS 'Branch'
        , ISNULL(CAST(SUM(T0.LineTotal) AS decimal(15,2)), 0) AS 'TotalSales'
        , ISNULL(CAST(SUM(T0.StockValue) AS decimal(15,2)), 0) AS 'StockValue'
        FROM AU.dbo.RIN1 T0
        INNER JOIN AU.dbo.ORIN T1 ON T1.DocEntry = T0.DocEntry
        LEFT JOIN AU.dbo.OSLP T2 ON T2.SlpCode = T1.SlpCode
        WHERE T1.DocDate >= '2014-08-01' AND T1.DocDate <= '2014-09-01'
        GROUP BY T1.SlpCode, T2.SlpName, T2.U_INE_Branch
    ) AS T1
ON T1.SalesPerson = T0.SalesPerson
Currently I am not getting correct results (which I can do through a UNION), but am instead seeing NULLs scattered throughout, as shown below...
(I have blacked out certain details to keep the company I work for anonymous)
My question is: are NULLs unavoidable in this scenario simply because of the nature of JOINs? I have tried a 'JOIN' rather than a 'FULL JOIN' but instead of seeing all of the Sales People I only see Sales People who do not have NULLs, which looks nice but is not particularly accurate.
Any help and suggestions here will be greatly appreciated.
Kind Regards,
David

Hi David,
You can try something like this:
to avoid null
Type in each field the keyword isnull, IsNull((T0.TotalSales - T1.TotalSales), 0) AS 'Total Sales' 
or something like this
SELECT T0.[SlpCode], T0.[SlpName], (Select Sum(T1.doctotal) from oinv t1 where t1.docdate between '???' and '???' and T0.slpcode = t1.slpcode)  - (Select Sum(T2.doctotal) from orin t2 where t2.docdate between '???' and '???' and T0.slpcode = t2.slpcode) as Sales FROM OSLP T0
Hope it helps
Kind regards,
Augusto

Similar Messages

  • Can a JOIN be used here instead of a UNION?

    Hi Everyone,
    I am in the midst of writing a report which shows the 'Total Sales', 'Gross Profit', and 'Gross Profit %' by Sales Person. Traditionally I have used a UNION between two result sets to achieve this task. However I am wondering it if is possible to achieve the
    same outcome with a JOIN as shown below?
    SELECT
    T0.SlpCode
    , T0.SalesPerson
    , T0.Branch
    , (T0.TotalSales - T1.TotalSales) AS 'Total Sales'
    , ((T0.TotalSales - T1.TotalSales) - (T0.StockValue + T1.StockValue)) AS 'Gross Profit'
    , CAST(((T0.TotalSales - T1.TotalSales) - (T0.StockValue + T1.StockValue)) / NULLIF((T0.StockValue + T1.StockValue), 0) * 100 AS decimal(15,2)) AS 'Gross Profit %'
    FROM
    /*** SAP - Invoices by Sales Person ***/
    SELECT
    T1.SlpCode
    , T2.SlpName AS 'SalesPerson'
    , T2.U_INE_Branch AS 'Branch'
    , CAST(SUM(T0.LineTotal) AS decimal(15,2)) AS 'TotalSales'
    , CAST(SUM(T0.StockValue) AS decimal(15,2)) AS 'StockValue'
    FROM AU.dbo.INV1 T0
    INNER JOIN AU.dbo.OINV T1 ON T1.DocEntry = T0.DocEntry
    LEFT JOIN AU.dbo.OSLP T2 ON T2.SlpCode = T1.SlpCode
    WHERE T1.DocDate >= '2014-08-01' AND T1.DocDate <= '2014-09-01'
    GROUP BY T1.SlpCode, T2.SlpName, T2.U_INE_Branch
    ) AS T0
    FULL JOIN
    /*** SAP - Credits by Sales Person ***/
    SELECT
    T1.SlpCode
    , T2.SlpName AS 'SalesPerson'
    , T2.U_INE_Branch AS 'Branch'
    , ISNULL(CAST(SUM(T0.LineTotal) AS decimal(15,2)), 0) AS 'TotalSales'
    , ISNULL(CAST(SUM(T0.StockValue) AS decimal(15,2)), 0) AS 'StockValue'
    FROM AU.dbo.RIN1 T0
    INNER JOIN AU.dbo.ORIN T1 ON T1.DocEntry = T0.DocEntry
    LEFT JOIN AU.dbo.OSLP T2 ON T2.SlpCode = T1.SlpCode
    WHERE T1.DocDate >= '2014-08-01' AND T1.DocDate <= '2014-09-01'
    GROUP BY T1.SlpCode, T2.SlpName, T2.U_INE_Branch
    ) AS T1
    ON T1.SalesPerson = T0.SalesPerson
    Currently I am not getting correct results (which I can do through a UNION), but am instead seeing NULLs scattered throughout, as shown below...
    (I have blacked out certain details to keep the company I work for anonymous)
    My question is: are NULLs unavoidable in this scenario simply because of the nature of JOINs? I have tried a 'JOIN' rather than a 'FULL JOIN' but instead of seeing all of the Sales People I only see Sales People who do not have NULLs, which looks nice but is
    not particularly accurate.
    Any help and suggestions here will be greatly appreciated.
    Kind Regards,
    David

    Hi Darts75
    IS
    this still an issue or you can close the thread by marking
    SaravanaC's answer?
    [Personal Site] [Blog] [Facebook]

  • What can i use instead of microsoft word that has the same capabilities for school work etc

    what can i use instead of microsft word that has the same capabilities so i can do school work etc.

    You could use LibreOffice which is a free open source Office clone. The only downside is that by default it ddoesn't save in Office formats, you have to specifically choose that option. GoogleDocs is another free Office like product.

  • I'm getting prompted to update adobe acrobat 9.5.5 which I have deleted. I am currently using 10.1.13. It is still asking me to update 9.5.5 (can't find anything using it) I have tried to cancel the updates for 9.5.5 but it only recognizes the 10.1.13 ver

    I'm getting prompted to update adobe acrobat 9.5.5 which I have deleted. I am currently using 10.1.13. It is still asking me to update 9.5.5 (can't find anything using it) I have tried to cancel the update prompts for 9.5.5, but it only recognizes the 10.1.13 version. Suggestions? Am I missing something? It's driving me crazy!

    Hi ncm72,
    Could you please let me know what version of OS are you using.
    Also, go to Control Panel/System Preferences in Windows/MAC and check whether Acrobat 9 is shown under installed programs list.
    You might also try running Acrobat cleaner tool to remove all traces of previous versions of Acrobat:
    Use the CC Cleaner Tool to solve installation problems | CC, CS3-CS6
    Then, install Acrobat X again from the link mentioned below:
    Download Acrobat products | Standard, Pro | XI, X
    Let me know if this works.
    Regards,
    Anubha

  • I can only open one Firefox browser window. When I click the firefox icon from desktop or taskbar to open up another browser window it does not open. I am ablt to open up new tabs within the one window.

    I can only open one Firefox browser window. When I click the firefox icon from desktop or taskbar to open up another browser window it does not open. I am ablt to open up new tabs within the one window.

    Have you tried: click Firefox button > New Tab > New Window '''''OR''''' CTRL+N '''''OR''''' File > New Window (if using the Menu Bar)
    Once open, Firefox locks the Profile that is in use and you can not open another window with that Firefox version and Profile combination using the Windows Desktop icon or the Windows Programs list.

  • Can iTunes credit be used instead of credit card for Family Sharing Organizer?

    I'm aware that a member in a Family Sharing group uses their iTunes credit (gift cards) before the Organizer's credit card is charged.
    But what happens if the Organizer has iTunes credit? Will the members' purchases get deducted from that credit prior to the credit card?
    Haven't found any real info on this yet :/
    side question: if all of the members have iCloud Drive monthly fees, will that also get deducted from the Organizer's account? (either by credit card or iTunes credit)

    Sad but true, I've tried to pay for iTunes Match with my credit and couldn't because it's a subscription service, regardless the credit amount I have. The system ask me to pay with my credit card too.
    How are Family Sharing purchases billed?
    When you initiate a new purchase, it will be billed directly to the family organizer's account, unless you have gift or store credit. First, your store credit will be used to pay the partial or total bill. The remainder will bill to the family organizer. iTunes Store credit on your account isn't shared with other family members. So if the family organizer has a $50 credit on their account and another family member initiates a purchase, it won't deduct from the organizer's iTunes Store credit, but will bill the organizer's credit or debit card.
    How iTunes Store purchases are billed

  • How can i pass a variable instead of a table name in the Select statement.

    Dear all
    how can i pass a variable instead of a table name in a select statement ?
    Example :-
    Begin
    P_get_procedure_tname (aap_name,otable_name);--It will take an application name and will return a table name
    Select col1 into ocol1
    from  ---- here i want to pass the variable OTABLE_NAME
    End;How can i pass this ?

    Hi,
    You can use dynamic sql.
    EXECUTE IMMEDIATE 'SELECT COL1 INTO ' || OCOL1 || ' FROM " || OTABLE_NAME;
    {code}
    cheers
    VT                                                                                                                                                                                                                                                                                                   

  • Can two accounts just use one macbook.error occurred talking to the iTunes store

    I have two accounts(A and B), of course I just use one iphone and one macbook.
    but now ,the problem is,
    the account A can use xcode organizer to upload App,
    and the account B can't upload in the same MAC and Xcode,
    the error message is: error occurred talking to the iTunes store
    and the console print:  *** Error: An error occurred while deserializing the JSON request.  Error Message - Invalid JSON character read at index 0
    (of course if I change from account B to A, upload success)
    it cotinue for three days, of course there are many people have the same problem.
    must I buy another iphone ,another macbook,and install another xcode
    I am so sad.
    thank you

    the problem is ,  .ipa can't  upload by(in) account B
    I think the bug can be two possible list blow
          1.  the .ipa  is wrong
          2.  the account(B) is wrong.
    if   the .ipa is wrong  
    the .ipa can't upload  by any account (account A  or  account B,  just change the identifier and the sigh identity)
    but if the .ipa can upload success by  account A,
    so , the .ipa(program) is right.
    then, there is exist only one possible,   the account (B) is wrong,
    K T , do you thine my logic for this problem is right

  • I need to make a copy of an entire email, including the header w/the sender's info and time, etc. to insert into a letter, etc. How can I do this w/out cutting and paste and doing the header separately from the text.

    I need to make a copy of an entire email, including the header w/the sender's info and time, etc. to insert into a letter, etc. How can I do this w/out cutting and pasting   the header separately from the text. I know there is a way besides a screen shot but I've spend hours trying to find it.

    Smurfslayer wrote:
    For the particularly persnickety types you might want to expose the full headers in the email message as well.  It's easy enough to do, from mail's 'menu' select "view"; then "message"; then all headers.
    Another option you could use is a screen capture using Grab.
    Activate Grab, then shift + command + w (for a window screen shot).  Then confirm the window selection and click the mail message. 
    Dave
    Why are you addressing this to me...?
    I am not the OP...

  • HT204053 I have changed my apple ID and I changed it via computer.  When I just went to update my apps, my phone is still showing the old apple ID so I can't log in to update.  How do I change the apple ID from my iphone?

    I have successfully changed my apple id via computer.  Now, how do I change it on my actual phone?  I have the 5.1.1 iphone 3G.

    I recently changed my email address and want to change my Apple ID account to match. I have created a new Apple ID to match the new account but now I can't sign into iCloud on our phones or computer with the old Apple ID. I can't make any changes on iCloud on our phones because it directs me to the old account, which I can't sign into. I also can't sign me in on my computer to make any changes. Help!!! Now I have two addresses and can't access the one that directly affects my devices. 

  • Can I parse an ASCII charater variable that is included in the data string from my device?

    Here is the RS232 data from the device.
    “R,2.306 MPa a,0.011 MPa/s,97.000 kPa a”
                                   OR
    “NR,2.306 MPa a,0.011 MPa/s,97.000 kPa a”
    Channels 0,1, and 2 take care of the 3 numerical values and write them to a file no prob..
    The R and NR represent the Ready Status of the device, is there a way to write these characters into the same file?
    Any help is appreciated, I'm fairly green when it comes to DASYLab.

    Here are a couple screen shots of trying to read just the one channel using the a\x2c measurement data format.
    As shown in the RS232 monitor, it looks like its ignoring the text characters i need, and picking up the values that are already being written to a file in my original worksheet.
    I also messed around with your suggestion to store it as a global string and use the action module to write it to my txt file.  I'm not too sure I was doing this correctly, how are the RS232 Input, action, and write modules linked to each other in order to get this accomplished?  
    I was able to write the string to the header of my file, but thats just a one-time write..
    Thanks for your reply cj 

  • HT3917 not apple keyboard, can a logitech be used instead?

    can a wireless keyboard and mouse logitech be used on a Imac?

    I've heard of many people using the logitech mouse, so If you're referring to the Apple keyboard and Logitech mouse, I'd say yes.
    Captfred

  • Can cross join be used with other joins

    hi,
    Q1) is this syntactically correct , cross join with other joins like inner outer. like following.
    select * from t1 cross join t2
    inner join t3 on cast(t3.c1 as varchar) =  (cast(t1.id as varchar) +  cast(t2.t2 as varchar))
    I have seen it works but wanted to know is it syntactically correct.
    yours sincerely

    Hi
    First, Sorry, I wanted to VOTE (as I wrote) and not to Propose as answer :-)
    Join are not necessarily executing in the order we write them. I can show simple example with tables that include the amount of rows is very different.
    /**************************************************** DDL - Create tables */
    CREATE TABLE T1 (
    ID INT IDENTITY CONSTRAINT PK_T1 PRIMARY KEY,
    MyValue UNIQUEIDENTIFIER DEFAULT NEWID()
    CREATE TABLE T2 (
    ID INT IDENTITY CONSTRAINT PK_T2 PRIMARY KEY,
    MyValue UNIQUEIDENTIFIER DEFAULT NEWID()
    CREATE TABLE T3 (
    ID INT IDENTITY CONSTRAINT PK_T3 PRIMARY KEY,
    MyValue UNIQUEIDENTIFIER DEFAULT NEWID()
    GO
    /**************************************************** DML - Populate Tables*/
    SET NOCOUNT ON;
    insert T1 (MyValue)
    select top 100 null
    from _ArielyAccessoriesDB.dbo.ArielyNumbers
    GO
    insert T2 (MyValue)
    select top 500 null
    from _ArielyAccessoriesDB.dbo.ArielyNumbers
    GO
    insert T3 (MyValue)
    select top 10000 null
    from _ArielyAccessoriesDB.dbo.ArielyNumbers
    GO
    /**************************************************** Play Time */
    select T1.MyValue, T2.MyValue, T3.MyValue
    from T3
    join T2 on T2.ID = T3.ID
    join T1 on T1.ID = T2.ID
    GO
    -- Check Execution Plan [EP]
    -- Notice that SQL Server has changed the join order from T3-T2-T1 to T1-T2-T3 because it’s better that way.
    -- You can notice that the order was by the number of rowns in tables from the smallest to the bigest SET
     hope this is helpful :-)
    [Personal Site] [Blog] [Facebook]

  • Can a JViewPort be used instead of a JPanel?

    My map graphics applications display the graphics in a JPanel. The only trouble is that when I have to redraw the whole graphics window whenever any event that requires a window update ocurs. This slows down my applications badly.
    I found out that a JViewPort has features equivalent to a backing store. I wonder if it is advisable to try to display my graphics in a JViewPort instead of a JPanel. If this is not possible, is there a way to speedup my applications during window update events?
    Thanks for your help.

    Why does it slow down painting? Are you recalculating your data model each time?

  • External Lync clients can't join meetings - get kicked out with "You have left the call"

    My users connecting remotely (ie. from home) from their Lync clients installed on their PC are unable to join any meetings.  It tries to connect, but then gives the status "You have left the call." 
    Internally (ie. in the office) it works fine. Additionally, the Lync Web App clients also works fine, internally and externally.
    Some details on my enviroment:
    Edge - Lync 2013
    Front End Pools - one 2010 Enterprise (albeit one server), one 2013 Enterprise (two servers)
    A standalone Lync 2010 A/V server exists as well.
    Meet and dialup records internally point to 2010 pool. Externally, they point to 2013 access edge.
    SRV records on external DNS are _sip._tls 443 and _sipfederationtls._tcp 5061, both point to access edge.
    SRV records on internal DNS are _sipinternaltls._tcp 5061 to sip (which is an "A" record with IP for 2013 FE pool), and _sip._tls 5061 to access (which is an "A" record with the internal IP of the edge server).
    Any thoughts on how to troubleshoot this would be most appreciated. Thanks.
    Ron Piecyk
    [email protected]

    Can external Lync user make an audio call to an
    external user?
    Your problem is mostly related with Lync Edge Server.
    Check Lync Server Audio/Video Edge service is started.
    Please use Remote Connectivity Analyzer to perform Audio/Video server connectivity test:
    https://testconnectivity.microsoft.com/
    Please check if you can find error in event log on Lync Edge Server.
     Check the required ports are opened, for details, check
    http://technet.microsoft.com/en-us/library/gg425882.aspx
    Lisa Zheng
    TechNet Community Support

Maybe you are looking for

  • How to temporarily turn OFF Spotlight Indexing?

    It was suggested to me that it is best to turn off spotlight indexing for the duration of an initial clone backup of your system drive. How can this be done? Thanks.

  • Program logic for Camparison

    Hi Guys,    I am trying to write a logic for comparison....Can you plz help me on this.... I have data like below.. I need to compare value which is comming from file with the data in SAP, If it fall under data maintained in sap 'Accept record' other

  • XI System Authorisation Issue

    Dear XI Experts , I am not able to test my scenarios in the Dev box.They are failing when I check them at the adapter engine. The error which I am getting is as follows : File_http://sap.com/xi/XI/System failed, due to: com.sap.aii.af.ra.ms.api.Recov

  • Win8/Metro Support

    Is Adobe planning support for Win8/Metro environment in FB 4.7? Win Mobile?

  • What will happen if I delete my husband's iCloud account from my iPad?

    What will happen if I delete my husband's iCloud account from my iPad?