Will a Union handle this? Showing aggregates from different groups on the same row

So I want to display a result that includes a sum of the transaction amount for a list of orders.  Each order can have items, and each item has a transaction amount.  
Then I have a goal table that keeps track of the "goals" set for a simple forecasting method used by sales.  A goal is defined for each item type and a count.
Table structures are like this
DECLARE @OrderTran AS TABLE(RowId INT PRIMARY KEY IDENTITY(1,1) NOT NULL, OrderId INT, ItemId INT, ItemAmount DECIMAL(10,2) )
DECLARE @GoalData AS TABLE(RowId INT PRIMARY KEY IDENTITY(1,1) NOT NULL, ItemTypeKey INT, ItemGoalCount INT )
The problem with getting totals for both order transactions AND goals, is there there might not be items in some of the orders for the items that goals set for.  So If I grab the order totals first, then left join on goals, I still might miss out some
of the goal results when I do an aggregate.
Should union help here?
In other words, get a result from the order totals
Then get a similar result from the goals
Then union the 2 results?

Are you looking for something like below:
DECLARE @OrderTran AS TABLE(RowId INT PRIMARY KEY IDENTITY(1,1) NOT NULL, OrderId INT, ItemId INT, ItemAmount DECIMAL(10,2) )
DECLARE @GoalData AS TABLE(RowId INT PRIMARY KEY IDENTITY(1,1) NOT NULL, ItemTypeKey INT, ItemGoalCount INT )
INSERT INTO @OrderTran
VALUES (1, 1, 10), (2, 1, 22), (3, 2, 12), (4, 1, 50), (5, 2, 100)
INSERT INTO @GoalData
VALUES (1, 100), (3, 200)
SELECT
COALESCE(O.ItemID, G.ItemTypeKey, 0) As ItemID
,O.TotalAmount
,G.TotalGoalAmount
FROM
SELECT ItemId, SUM(ItemAmount) As TotalAmount
FROM
@OrderTran
GROUP BY
ItemID
) O
FULL OUTER JOIN
SELECT ItemTypeKey, SUM(ItemGoalCount) As TotalGoalAmount
FROM
@GoalData
GROUP BY
ItemTypeKey
) G ON O.ItemID = G.ItemTypeKey
Output
ItemID | TotalAmount
| TotalGoalAmount
1 | 82.00
| 100
2 | 112.00
| NULL
3 | NULL
| 200
Best Wishes, Arbi; Please vote if you find this posting was helpful or Mark it as answered.

Similar Messages

  • Mail sorting messages from different people in the same thread?

    I have this weird issue. It's happened before but I figured I might be able to figure this out.
    I got a response from somebody this morning and whats happening is, when I click on his response, the email shows up on the right like it should. However, ths email is from "Floyd" and shows as email "2". It shows another email below it in the "thread" from "Taryn" and has absolutely nothing to do with this email. The only similarities are the subject line which says "Poster". The "First" email from Taryn is from April. It seams as if it's sorting the emails by subject line. I don't want emails from different people in the same thread. Any thoughts on how to fix this issue? Image attached.
    OSX 10.8.4

    This question is about the same as mine.  I migrated all my data from old mac mini to new mac mini.  System screwed up and permission and owners were not setup.  Now I'm trying to salvage this mess.  I'm also trying to import mail, which I think should be on the computer but I don't know where.
    Same question where does the computer store Mail mail?
    Chuck

  • Hi can someone tell me if it is possible to have two accounts (from different countries) in the same laptop?

    Hi can someone tell me if it is possible to have two accounts (from different countries) in the same laptop?

    Hi...
    The issue is that your credit or debit card credentials must be associated with the same country where you reside to make purchases.
    "Although you can browse the iTunes Store in any country without being signed in, you can only purchase content from the iTunes Store for your own country. This is enforced via the billing address associated with your credit card or other payment method that you use with the iTunes Store, rather than your actual geographic location."
    From here >  The Complete Guide to Using the iTunes Store | iLounge Article
    Billing policy is the same for both the iTunes as well as Mac App Stores.

  • Data from different databases in the same report.

    Hi Everyone,
    I am trying to build a reconciliation report in which I need to show the data from the source and target, side by side.
    Source and target are both different databases, although being oracle only
    Whenever a new data model is created, it gets attached to a data source and in the report we need to choose a specific data model.
    Can we have data from different databases in the same report ?

    Yes, it is possible.
    One way Is to use dataTemplates. There you can make queries from any number of different databases (The max I have done is 5).
    It looks something like that:
    <dataTemplate name="NameOfTemplate">
    <dataQuery>
         <sqlStatement name="Q1" dataSourceRef="Connection1">
              <![CDATA[     select * from table1]]>
         </sqlStatement>
         <sqlStatement name="Q2" dataSourceRef="Connection2">
              <![CDATA[     select * from table2]]>
         </sqlStatement>
      </dataQuery>
      <dataStructure>
         <group name="RESULT1" source="Q1">
              <element name="P_FIRST_NAME" value="P_FIRST_NAME"/>
         </group>
         <group name="RESULT2" source="Q2">
              <element name="P_DATE" value="P_DATE"/>     
         </group>
      </dataStructure>
    </dataTemplate>dataStructure is very important when you get data from different places, if you don't define those elements, then only the result from Q1 is shown.
    The second possible way is to make as two different data models, each containing their own query and then set Main Data Set as concatenated SQL Data Source.
    Best of luck,
    Evelyn

  • Data from two tables in the same row in XML transformation

    Hi,
        I am using XML transformation for generating excel file which is to besent as email attachment.
        Here  I want to display the data from two internal tables in the same row in the excel. .I am using   <tt:loop ref=".<table name>"> ...  </tt:loop> for looping through the table. Can I loop two table simultaneously ? In that case how will I specify the fields in each table . Some of the fields in two tables are of same name and type.
    Please help...
    Thanks,
    Jissa

    Hello Brian,
    Thank you for your answer. It is approach I will use, I think. However let me ask: Would it be possible to have a Version in this layout, too? I mean to see, which value comes from Version A and which comes from Version B? Something like this:
    Calendar Month Version Sales Amount
    2011.01  B  200
    2011.02  B  300
    2011.03  A  260
    2011.04  A  230
    2011.05  A  200
    A

  • How to update data in database from different region of the same page

    Hi,
    I have pepared two regions of tabular form based on the same table,each region is selecting different columns of the same table.
    I want to update the values from different regions in the database using single submit button.
    Regards
    Shashi

    I presume that the two regions are working fine with their individual buttons and your issue is how to make them both save with one button.
    Here is how
    a. You will have 2 ApplyMRU and 1 ApplyMRD processes for each of the regions. Right? Lets say you have 2 "Save" (i.e. label=Save) buttons, one has name SUBMIT and the other SAVE ( SUBMIT and SAVE being the requests that will be sent when they are clicked , respectively)
    b. Make the Display condition on one of the buttons 'Never'. Now it won't show when you run the page. Lets say you made SAVE's conditional Display 'Never'
    c. Go and change the condition on all ApplyMRU and ApplyMRD processes from "When Button Pressed" to "Request is contains in Expression1". In Expression1 enter SUBMIT,SAVERegards

  • Publish two publications from different customers with the same Adobe DPS ID

    Hello:
    I have a publication made on InDesign and I have uploaded it to the Adobe Server with my Adobe DPS account. For the first publication, I published the publication with the Folio Producer on the digitalpublishing portal, and then I created the app using the Adobe Viewer Builder. Then, I tried it locally on my iPad. It was working fine and I could see my publication.
    Now, I do need to upload a new publication for a different customer with the same Adobe DPS account and I need to create a different app with the new customer look and feel but I don't want to show the first publication in this new app, only the last one.
    In a nushell, I need to keep separated two publications (or more) when they are published if I'm using the same DPS Account... Is that possible?
    Saludos Cordiales,

    Hi Leo,
    Every title should have its own publication AdobeID (a title can contain multiple editions / folios).
    So you have to request for every title you want to work on a provisioned account with Adobe. Make sure that you have setup the corresponding emailaddress / emailalias. For example [email protected] and [email protected]
    After the accounts have been provisioned you can:
    * upload and organize multiple folios for that publication in the corresponding Folio Producer
    * Create a viewer with your Viewer Builder and on the first page supply the AdobeID for the give publication
    You can request new publication accounts any time and these are included with your DPS subscription.
    When you first account got provisioned, you should have received an excel document to request provisioning for the various publications. In this document you can list the accounts you want to use for publications, individual users of the system and viewer builder access.
    With kind regards,
    Klaasjan Tukker
    Adobe Systems Benelux

  • How do I view multiple images from different projects at the same time?

    Hello
    I've been trying to find the answer in the user manual, but no luck so far.
    I did find how to open 2 projects at once, but I can only see one image at a time in the viewer and I have to keep going back and forth between projects to compare images. (These are scans of old photos and I want to eliminate the ones that are poorer quality, so need to compare numerous images from 2 sets of scans)
    How can I view an image from one project at the same time as an image from another? I know that it can be done in the same project by apple-clicking the second, third etc image, but this doesn't work if the images are from different projects.
    Thank you
    Elizabeth

    Thank you again Tony
    I've now created an album, independent of the projects, and dragged test images into it and it does just the job I needed. Now i just need to keep my brain sorted with which ones are which while i'm working
    Elizabeth

  • WM-PP : Backflush a material from different bins in the same storage Type

    Hi Gurus,
    We have a business scenarion where we do manual staging for production , the staging area has different bins within the same storage type for the same material, I am wondering if there is a way to backflush the material from different storage bins upon prod. order confirmation (using a control cycle?)
    Thanks
    Kris T

    Look like it's not possible using the SAP standard.

  • Data from Different datasources in the same table

    Hi everyone,
    Here I am under this complex problem.
    I have two different Bapi's which are being called multiple times.
    These bapi's have 3 common export parameters. I need to display a table with these 3 common export parameters (columns), as columns and two other custom columns.
    Data in custom colums needs to be filled based on the the 3 original columns with some logic build over these 3 columns.
    All the data returned under these columns is to be accumulated and showed in the frontend in a table.
    Any ideas.. how will you handle this in the context of controllers and UI table.
    Do I need to create a dictionary element?
    Please help..
    Thanks,
    Amber

    Hi Amber,
        In your context, create a value node with 3 value attributes for the BAPI output params with the exact same name as the BAPI params. Then you can use the WDCopyService class to copy the corresponding fields.
        For the two custom columns, create 2 calculated attributes in the same value node. The WD framework will generate setters and getters for these attributes. In the setter method write the logic for calculation of these attributes.
    Regards,
    Satyajit.

  • Is is possible to open event lists from different tracks at the same time in Logic Pro 9?

    Is it possible to open event lists from two difference tracks in an arrangement at the same time in Logic Pro 9?
    When I switch between tracks the event list swiches as well. Thanks in advance.

    If you deselect the litte chain link icon in the event list window, it will no longer follow your region selection and you can openas many as you need. Also you can select multiple regions and see the inthe SAME window if you select to show multiple regions, and you can have the notes shown in their respective track colors.

  • Accessing a static varible from different object at the same time

    hi
    assume that i have calss it contains a static variable i am creating
    N numbere object of that class and i am try to access the static variable at the same time from different object.if i access like that any problem(deadlock) will occure or not? plz help me
    with regards
    suresh

    FeedFeeds : Read news and blogs a new way!
    http://www.feedfeeds.com
    Please don�t spread any kind of propaganda in this forum...;-)

  • Retrive data from different frames in the same HTML page

    Dear sirs,
    Consider a HTML page with 2 frames, frame1.html and frame2.html. Frame1 contains a textbox, and frame2 contains an "Ok" button.
    Could it be possible that, once I click "OK", and after being redirected to the proper servlet, such servlet could retrive information from Frame1?
    Or, in other words, it is possible for a servlet to get information from different html pages? (I mean INPUT elements like texboxes or radio buttons).
    Thanks in advance for any ideas provided.

    Hi,
    Try this out:
    window.parent.Frame1.document.formname.textbox1.value
    Best of luck

  • How do I add signals from different modules to the same waveform chart?

    When using build array to connect signals from different FP modules to the same waveform chart, the array type is not the same as if all the signals come from one Read Array. Only one digital indicator on the chart displays with the build array method. Why

    Hi gencell,
    There are two ways to plot multiple plots on a Waveform Chart, a 2D array and a cluster of doubles or integers. I would suggest that you use a Bundle to create a cluster.
    This information is in the Context Help of the Waveform Chart terminal (from the Block Diagram).
    Regards,
    Chad Evans

  • Spreadsheet: how can I store test results from different tests in the same central spreadsheet?

    I am looking to gather test data using LabVIEW. I then want to store all the data in the same spreadsheet, a kind of central data base. I know how to store single test data in a spreadsheet but I can I get the software to write new data in the same file without over writing the old data?

    My first recommendation is that if you need a database--use a database not a spreadsheet. A DBMS isn't that hard to use and could pay big dividends in the future.In any case, to answer your question directly, it sounds like you are writing tab-delimited data to a text file that you will then open with a spreadsheet program. On the file write vi is a boolean input that specifies whether the new data should be appended or overwrite the existing data (the default is to overwrite). Mike...
    Certified Professional Instructor
    Certified LabVIEW Architect
    LabVIEW Champion
    "... after all, He's not a tame lion..."
    Be thinking ahead and mark your dance card for NI Week 2015 now: TS 6139 - Object Oriented First Steps

Maybe you are looking for