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]

Similar Messages

  • 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

  • How can I make Outlook use Firefox instead of Internet Explorer and can I turn IE off without hurting anything else on my machine?

    I would like to use Firefox for Outlook Express. I can't find an option in Outlook or Firefox to make Firefox the default to access the web for email through the mail client.
    Also, is there a way to stop IE so that it isn't used at all by any program in my machine?
    Thanks
    CajunReb

    Matt,
    I've tried that. When I go to outlook express, then to tools, options, then connections tab, it says "Outlook shares your connections with Internet Explorer, click the change button to modify these settings", but I don't see a way to do that. It just offers the option of changing settings for connecting to the internet, not which browser.
    I'm stumped

  • 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]

  • I have a macbook pro 7, can I upgrade to use iCloud instead of mobile me

    I recently started using an iPad and now have iCloud but my Macbook pro 7 has mobile me. What can I do.

    You have got a Mid 2010 13-inch MacBook Pro, and it supports up to 16 GB of RAM, so you can install 8 GB of RAM. You can buy more memory at OWC or Crucial

  • Can we perform Join operation using SQLCall with Datatabae Query

    Hi,
    I am working on Toplink SQLCall query. I am performing join operation but, it is giving error.
    so please, any can tell me . we can perform join operation using SQLCall with Database Query
    Thanking You.

    You can use joining with SQLCall queries in TopLink, provided your SQL returns all of the required fields.
    What is the query you are executing and what error are you getting?

  • How do I make KDE use smartdimmer instead of ... ?

    Hi!
    I have KDE 4.3 installed on my laptop and the power management options don't work when it comes to dimming my screen. I have just installed smartdimmer, which does work for my screen and I'm wondering how I can tell KDE to use that instead.
    Thanks
    Last edited by steven (2009-10-24 02:10:50)

    There are two different settings involved:
    '''Address Bar Autofill'''
    If you have used HTTPS on a site before, Firefox's address bar autofill feature will prefer the HTTPS address. If you don't mind selecting from the drop-down instead, or simply typing the whole address, you can turn off the address bar autofill. Here's how:
    (1) In a new tab, type or paste '''about:config''' in the address bar and press Enter. Click the button promising to be careful.
    (2) In the filter box, type or paste '''autofill''' and pause while the list is filtered
    (3) Double-click '''browser.urlbar.autoFill''' to toggle it from true to false.
    '''Address Bar Search'''
    Regarding address bar search, you can change the URL used by the "keyword" service. Please see this thread for links: [https://support.mozilla.org/en-US/questions/955363 Unable to connect to proxy due to automatic https].
    Does that solve it?

  • My daughter sold me this iPad now when I update instead of it using my appleid and password it always tries to use hers! I can't update or do anything important with my own appleid what should I do! And she can't remember the password she

    MY daughter sold me this iPad! When I go to update it always tries to use her appleid instead of mine!! She has forgot the password to this account what should I do! Because it says it's hooked up to my Id but when I try to update itsail ways says her I'd information

    Is this when updating apps or the iPad's iOS version ? If apps then any apps that your daughter downloaded onto the iPad will be tied to her account, so only her account can download updates to those apps (you would need to delete the apps and buy/download them with your own account to be able to download future updates to them with your account) then she could try getting her password reset via http://iforgot.apple.com
    If you are trying to update the iOS version on it and she's left it tied to her iCloud account then with iOS 7 and activation lock only her account will be able to re-activate the iPad. She can try getting her password reset via the above iforgot link.

  • HT4623 My daughter update my iphone using her itunes acct., which caused all her apps to download onto my iphone. Now when it asks for my password it has her email address instead of mine. How can I fix the problem?

    My daughter was updating her iphone to the iOS6 and everything went great. So, she was going to update mine. She used her itunes acct. instead of mine and it downloaded all her music and apps onto my iphone. Now when it asks for me to enter my password, it has her email address. How can I fix this???

    plug your iphone into your computer with your itunes account signed in to update it, (if you want your own apps ect.) if all you want to do is change accounts signed in on the iphone than simply open settings and scroll down until you see itunes and app stores touch the apple id tip top and signout, sign yourself in. any apps she has "purschased" that you have not.. i imagine will just cease to work.
    if you both share the same contacts, it won't be a big deal, however if you have work and her shcools numbers ect, and she just has mom/dad/buddies you may have double numbers and apps and what not by doing this, so if it does become a mess.. just get on itunes log into your account by clicking the store menu in itunes and going down to the account id on the bottom of that menu and just restore your phone.

  • My account is locked, i cant remember my security question, and I accidentally entered my itunes card in here instead of my other account, so can someone from apple help me out and fix my account

    my account is locked, i cant remember my security question, and I accidentally entered my itunes card in here instead of my other account, so can someone from apple help me out and fix my account (and no, i dont have any rescue email address or whatsoever)
    please help me because i dont to waste money for nothing

    Alternatives for Help Resetting Security Questions and/or Rescue Mail
         1. If you have a valid rescue email address, then use this procedure:
             Rescue email address and how to reset Apple ID security questions.
         2. Fill out and submit this form. Select the topic, Account Security. You must
             have a Rescue Email to use this option.
         3. This is the only option if you do not already have a valid Rescue Email.
             These are telephone numbers for contacting Apple Support in your country.
             Apple ID- Contacting Apple for help with Apple ID account security. Select
             the appropriate country and call. Ask to speak to the Account Security Team.
         4. Account security issues almost always require you to speak directly to an
             Apple representative to securely establish your identity as the account holder.
             You can set it up so that Apple calls you, either immediately or at a time
             convenient to you.
                1. Go to www.apple.com/support.
                2. Choose Contact Support and click Contact Us.
                3. Choose Other Apple ID Topics and choose the appropriate topic for
                    your issue.
                4. Follow the onscreen instructions.
             Note: If you have already forgotten your security questions, then you cannot
             set up a rescue email address in order to reset them. You must set up
             the rescue email address beforehand.
    Your Apple ID: Manage My Apple ID.
                            Apple ID- All about Apple ID security questions.

  • I have a IPad and I tried to send a message to a friends IPad mini and it didn't go through.  She has a different carrier than I do.  Can't all Apple products work together no matter the carrier?  I used her apple IPad mini designated phone number.

    How can I message a friend on her IPad mini from my IPad?

    If both have Apple devices AND both have data plans, why don't you message each other using iMessages instead of regular SMS?
    iMessages get sent thru the TCP/IP Internet rather than the cellular network so they are essentially free (other than any data cap you may have, irrellevant if the iGadget is connecting via Wi-Fi).
    http://support.apple.com/kb/HT3529

  • Can ANT be used here ???

    i want to invoke the default email client (DrClap , i know there's no concept, as such, of a default email client...hehe , but i guess u do understand what i want to say) . through a java program ....
    what i want is that , the "TO:" , "Cc:", "Bcc:" , "Subject:" fields shud be set by the program itself ..... what essentially the user wud be left with wud be to press the send button ( i can do away with that later)... the emailclient shud come up with a compose window with the "TO:" , "Cc:", "Bcc:" , "Subject:" fields set .....
    i also want to attach a file ( actually this is more important ) , the file shud be attached when the compose window comes up
    I want to know if ANT can be used here ( although i have been asked to use it , but i m new to ANT , so i dont know) .. also give me a few hints on how to go abt it
    do care to help .....

    Hi,
    An email client is meant to be used for sending emails to multiple number of people and as such would not provide a mechanism to set the To, From and Cc fields pre-populated. Not unless it is something you have written and have provided an api to set the fields.
    If you don't want to launch the email client but instead just use the API to set the fields to the desired values and send an email, then you can try Java mail API.
    Ant is a build tool that can also run other tasks like run a external command etc. Ant has no means to populate the fields of your favourite email client be it outlook, eudora or netscape mail.
    Hope this helps.
    Cheers,
    vidyut

  • HT202667 Hi - My daughter was using her Apple TV until recently when she got a Smart TV.  Now the Apple TV is not needed so she passed it on to me.  How do I get it transferred over from her Home Sharing account to mine so that I can use it with my PC?

    Hi - My daughter was using her Apple TV until recently when she got a Smart TV.  Now the Apple TV is not needed so she passed it on to me.  How do I get her Home Sharing account transferred over to mine so that I can use it with my PC? Thanks!

    As  Winston Churchill wrote, however you might want to do a factory reset on the Apple TV.
    This way all information pertaining to your daughter will be removed from the device. Specifically if see was sign into any of the streaming service, Netflix, Hulu, etc. her credentials will still be associated with this Apple TV, doing  a factory reset will remove all that and make it as if you purchased the device and just plugged it in.
    regards

  • How do you transfer Apple IDs from one ipad to another iPad?  I got the new ipad and gave my ipad 2 to my wife who had the first version and now I can't get my Apple ID off so she can use her Apple ID.

    how do you transfer Apple IDs from one ipad to another iPad?  I got the new ipad and gave my ipad 2 to my wife who had the first version and now I can't get my Apple ID off so she can use her Apple ID.

    You don't transfer Apple ID's from one device to another one. You sign out in the settings, )the App Store and iTunes if necessary) and then sign in with the other ID.
    Settings>iTunes & App Stores>Apple ID>Sign out. Them sign in with the other ID. You can sign out of the app store and iTunes as well by going to the featured Atab in the App Store and the music tab in iTunes, swipe to the bottom and access the Apple ID in there.
    You should have erased the iOad before you gave it to her in Settings>General>Reset>Erase all content and settings. That way she could set up the iPad as new with her own Apple ID.
    Be aware of the fact that if you use each others ID's in order to download past purchased content to your own iPads, you will lock yourself out of your own ID as you will have associated your iPad with the other person's Apple ID.
    In other words, if you sign into your wife's ID on your iPad so that you can download an app or an album so that you don't have to pay for it again, you will lock yourself out of your Apple ID for 90 days.

  • How can my daughter use her gift cards without it charging my credit card first?

    With Family Sharing, how can gift card be used first before the owner is billed?
    My daughter has several gift cards, when she tried to make a purchase it would not allow her to input her gift card information. It automatically wants to bill me?

    Hi lindafromharvest,
    Your daughter and other members of your family group will want to enter any gift cards into their account before making purchases that they want to put on the gift card. To redeem the card see this article -
    Redeem iTunes Gift Cards and content codes
    Purchases made by accounts with gift balances will be billed before the family sharing credit card. See this article -
    How iTunes Store purchases are billed
    Thanks for using Apple Support Communities.
    Best,
    Brett L 

Maybe you are looking for