Sum of 2 aggregated rows
Hi,
I have the following data as a result of my SQL. I have used GROUP BY CUBE
Category Status Count
A S 5
A F 10
A R 5
A Tot 20
B S 15
B F 10
B R 5
B Tot 30
I need to get the new row ST which is Sum of S and F status records for each Category. For Example.
Category Status Count
A S 5
A F 10
A ST 15
A R 5
A Tot 20
B S 15
B F 10
B ST 25
B R 5
B Tot 30
Can someone please help me solve this issue.
Thank you very much.
Edited by: 959026 on Sep 13, 2012 12:09 PM
Hi,
Interesting problem!
959026 wrote:
Hi,
I have the following data as a result of my SQL. I have used GROUP BY CUBEAre you sure? If you used GROUP BY CUBE, wouldn't there be super-aggregate rows where category was NULL? If you're using CUBE, then I suspect you're doing some additional manipulation after you get the CUBE results. Without seeing exactly what, I can't tell how to incorporate it into the solution.
Category Status Count
A S 5
A F 10
A R 5
A Tot 20
B S 15
B F 10
B R 5
B Tot 30
I need to get the new row ST which is Sum of S and F status records for each Category. For Example.
Category Status Count
A S 5
A F 10
A ST 15
A R 5
A Tot 20
B S 15
B F 10
B ST 25
B R 5
B Tot 30
Can someone please help me solve this issue.It would have been very helpful if you'd posted CREATE TABLE and INSERT statements for your sample data, and your existing GROUP BY query.
Since I don't have your table, I'll use scott.emp to illustrate.
Here's a query that's similar to yours. The 4 values of job in scott.emp are kind of like the 3 values of status in your table.
SELECT deptno
, job
, COUNT (*) AS cnt
FROM scott.emp
WHERE job IN ( 'ANALYST', 'CLERK', 'MANAGER', 'PRESIDENT')
GROUP BY CUBE (deptno, job)
ORDER BY deptno, job
;Output:
` DEPTNO JOB CNT
10 CLERK 1
10 MANAGER 1
10 PRESIDENT 1
10 3
20 ANALYST 2
20 CLERK 2
20 MANAGER 1
20 5
30 CLERK 1
30 MANAGER 1
30 2
ANALYST 2
CLERK 4
MANAGER 3
PRESIDENT 1
10Say we want to get a sub-total of just two jobs, MANAGER and PRESIDENT, within each department, like this:
` DEPTNO JOB CNT
10 MANAGER 1
10 PRESIDENT 1
10 Management Subtotal 2
10 CLERK 1
10 Total 3
20 MANAGER 1
20 Management Subtotal 1
20 ANALYST 2
20 CLERK 2
20 Total 5
30 MANAGER 1
30 Management Subtotal 1
30 CLERK 1
30 Total 2One way to do that is to derive a new column, job category, which can have either of 2 values: ' Management Subtotal' or 'Others'. We can then include that new column in the GROUP BY clause. That would give us a sub-total for all the rows in the Management group, but it would also produce a sub-total row for the Others group, which we don't want to see. The query below uses a HAVING clause to eliminate the super-aggregate rows for 'Others'.
WITH got_job_grp AS
SELECT deptno, job
, CASE
WHEN job IN ('MANAGER', 'PRESIDENT')
THEN ' Management Subtotal'
ELSE 'Others'
END AS job_grp
FROM scott.emp
WHERE job IN ( 'ANALYST', 'CLERK', 'MANAGER', 'PRESIDENT')
SELECT deptno
, COALESCE (job, job_grp, ' Total') AS job
, COUNT (*) AS cnt
FROM got_job_grp g
GROUP BY GROUPING SETS ( (deptno, job_grp, job)
, (deptno, job_grp )
, (deptno)
HAVING COALESCE (job, job_grp, '?') != 'Others'
ORDER BY deptno
, job_grp -- add DESC NULLS LAST, if neessary
, g.job
;I used GROUPING SETS instead of CUBE, because we don't want all possible rollups; we only want a few, easily defined rollups.
Notice that the management group is listed first in the output for each department. ORDER BY ... job_grp does that, but only because the label I used for the management group (' Management Subtotal') happens to come before the label for the other group ('Others'). If you want to use other labels, which are more meaningful to you, then you may need to ORDER BY job_grp in DESCending order, in which case you would also need to specify NULLS LAST, to get the output in the right order.
Similar Messages
-
Download Interactive Reports to Spreadsheet - Aggregation rows?
Is there any way to use the Interactive Report download formats (either XLS or CSV) so that the spreadsheet includes the aggregation rows (SUM, AVERAGE) for each control break displayed on the screen.
I am on 3.1.0.00.32 (soon we will be at 4.1).
When we create a customized report using the interactive tools, it only downloads the data, and not the sum rows.bhenderson wrote:
Is there any way to use the Interactive Report download formats (either XLS or CSV) so that the spreadsheet includes the aggregation rows (SUM, AVERAGE) for each control break displayed on the screen.
I am on 3.1.0.00.32 (soon we will be at 4.1).
When we create a customized report using the interactive tools, it only downloads the data, and not the sum rows.A workaround would be to use SQL to provide the aggregation rows. The ROLLUP, CUBE and GROUPING SETS (my favourite) group functions are especially build for such a task.
Here is an example in the docs: http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10002.htm#i2091446 -
How to Calculate sum for the selected rows in ALV gird using custom button
Hi I am trying to implement an ALV Grid display where the user can select few rows in the output and if he click on some custom button he should get the sum for only those rows.
Can anyone give me some idea regarding this.
Thanks in advanceWhile building the fieldcatalog for amount field use gt_fieldcat-do_sum = 'X'. And use the below for doc type sorting and subtotal.
* ALV data declarations
data: it_sortcat type slis_sortinfo_alv occurs 1,
wa_sort like line of it_sortcat.
perform build_sortcat.
*& Form build_sortcat
* Build Sort catalog
FORM build_sortcat .
wa_sort-spos = 1.
wa_sort-fieldname = 'EBELN'.
wa_sort-SUBTOT = 'X'. "subtotals any totals column by this field
* gd_sortcat-tabname
APPEND wa_sort TO it_sortcat.
wa_sort-spos = 2.
wa_sort-fieldname = 'EBELP'.
* gd_sortcat-tabname
APPEND wa_sort TO it_sortcat.
ENDFORM. " build_sortcat
call function 'REUSE_ALV_LIST_DISPLAY'
exporting
i_callback_program = gd_repid
i_callback_top_of_page = 'TOP-OF-PAGE'
is_layout = gd_layout
it_fieldcat = fieldcatalog[]
it_sort = it_sortcat
i_save = 'X'
tables
t_outtab = it_ekko
exceptions
program_error = 1
others = 2.
Regards,
Chandru -
MDX query with DMV to get all cubes and aggregation row count on SSAS engine
Hi All,
How can I get all cube names on a SSAS engine server and count of number of aggregation rows in each cube ?
I got a DMV where it shows all catalogs names and description but where can I found aggregation row count of each cube.
Please let me know, thanks in advance.
Maruthi...Hi Maruthi,
Please check below link, hope this will help you.
SSAS 2008 CTP6 – new DMV $SYSTEM. DISCOVER_ OBJECT_ ACTIVITY
lists the memory usage and CPU time for each object i.e. cube, dimension, cache, measure, partition, etc. They also show which aggregations were hit or missed, how many times these objects were read, and how many rows were returned by them:
Discover_object_memory_usage and discover_object_activity
select * from $system.discover_object_memory_usage
select * from $system.discover_object_activity
Thanks
Suhas
Mark as Answer if this resolves your problem or "Vote as Helpful" if you find it helpful.
My Blog
Follow @SuhasKudekar -
Interactive report showing only the aggregation rows
I have an interactive report with three columns that can be selected for aggregation. When I select one of the aggregation columns, I get the break logic that summarizes the columns I selected. My question: Is there a way to select the aggregation column and see the summed values only?
Example:
Fields: Company, Division, Location, Employee Name, Employee Salary
Aggregation on: Company, Division, Location.
Company Division Location Emp Name Emp Salary
Able 1 East Joe 100
Able 1 East Tony 200
Summary for Company Able, Division 1, Location East 300
I want to be able to see everything, but when I select aggregation at the location level, I want to just see the Summary line.
Thanks for any suggestions!Create a Row Count Measure, as the value being 1, in your Universe for the report. Add that Row Count into the query for the report, re-execute the query, highlight the entire report and then set a filter for that by dragging and dropping the Row Count Measure and filtering where the value > 1.
Edited by: Dennis Scoville on Jul 27, 2009 11:29 AM -
Hi All,
i have an report with a few columns an i sumerized them by clicking on the sum checkbox. My report result into data with a nice sum row at the bottom of the report.
The question is, is it possible to get this summary line to the beginning of the report as the first row of the report ?
Regards,
MarcoYes, but you got to do that yourself like in this example using analytic functions:
http://apex.oracle.com/pls/otn/f?p=31517:86
You may put your totals wherever you want.
Denes Kubicek
http://deneskubicek.blogspot.com/
http://www.opal-consulting.de/training
http://apex.oracle.com/pls/otn/f?p=31517:1
------------------------------------------------------------------- -
How do I get a sum of cells for rows that contain a text in a drop down menu?
I am trying to track individual sales with each of my vendors. I have a column of drop down menu's that list each of my vendors. I am trying to associate a dollar amount for a single transaction in a row that is associated with a vendor and get a sum for all of my single trasactions with that vendor for the year. I have used =countif to build a pie chart of % of transactions per vendor now I am trying to get a dollar amount as well
Thanks in advance
DonHI Don,
COUNTIF will count, SUMIF will sum.
The main difference between them is that COUNTIF works with data in a single column—in this case the column containing the vendor names—while SUMIF uses two columns—the vendor names to decide which rows to include in the sums and the amount column to determine the amount to incude.
Example:
Main: Vendor names in column A, descriptions in columns B and C, and dollar amounts in column D
Summary: Vendor names in column A, Number of transactions in column B, Dollar totals in column C
Formulas inn Summary (both are entered in row 2 and filled doen to the last row):
B2: =COUNTIF(Main :: $A,A2)
C2: =SUMIF(Main :: $A,A2,Main :: $D)
Regards,
Barry -
Sum values in last row for date format "hh24:mi"
Hi,
I have a big problem creating a sum row with date data.
I created a sql view where the result looks like that:
SELECT category_name,
user_name,
b_mmyyyy,
b_1, ..., b_31
FROM view_category_user_booking
category_name, user_name, b_1, ..., b_31
category1, user1, 122008, 01:00, ..., 03:30
category2, user1, 122008, 02:00, ..., 01:00
category3, user1, 122008, 00:00, ..., 00:15My goal is to integrate a sum row at the end for the columns b_1 ... b_31
category_name, user_name, b_1, ..., b_31
category1, user1, 122008, 01:00, ..., 03:30
category2, user1, 122008, 02:00, ..., 01:00
category3, user1, 122008, 00:00, ..., 00:15
Sum, user1, 122008, 03:00, ..., 04:45I tried it like that:
select decode(grouping(category_name),1,'Summe',category_name),
sum(to_number(replace(b_1,':',','))) as b_1,
sum(to_number(replace(b_31,':',','))) as b_31
from category_user_booking
where user_name = 'user1'
and b_mmyyyy = '122008'
group by rollup(category_name)But the result isn't really successful
category_name, b_1, b_31
category1, 1, 3,3
category2, 2, 1
category3, 0, 0,15
Sum, 3, 4,45Is there somebody with an idea?
Thanks ahead,
TobiasHi Avinash,
the problem is not the grouping it is the date format.
Instead of a value like 5,4 > I want 05:40 or 1,75 > I want 02:15 in the sum value.
In my table all time values are saved in the format hh24:mi.
I don't know how to get that via a sql statement.
Output I want:
category_name, user_name, b_1, ..., b_31
category1, user1, 122008, 01:00, ..., 03:30
category2, user1, 122008, 02:00, ..., 01:00
category3, user1, 122008, 00:00, ..., 00:15
Sum, user1, 122008, 03:00, ..., 04:45Regards,
Tobias -
Hi all,
I have a datagrid and I want to sum each row values and display it to the last column.
How can I do that?
Thanks in advanceHi christomanos,
Check out the below links ...there are sample source files also included which display the sum at the bottom of the DataGrid.
http://blogs.adobe.com/aharui/2007/04/datagrid_footers.html
http://blogs.adobe.com/aharui/2008/03/flex_3_datagrid_footers.html
If this post answers your question or helps, please kindly mark it as such.
Thanks,
Bhasker Chari -
Column is summing up all the rows and displaying same value in all rows.
Hi,
Kindly see the below query. I have a problem in this query to fetch the abs_qty when the parameter P:item ie)msib.segment1 is not given as the input parameter. Its just summing up the entire quantity for all the items.When I pass P:item ie)msib.segment1 as input parameter, It fetches the exact value for abs_qty. Kindly help me to resolve this.
Note only : Manufacturing plant ,Start date and End Date are Mandatory(required) parameters. Others like Item,Base Model and Planner code are optional.
SELECT mmt.transaction_date date_produced, msib.segment1 item,
cic.cost_type_id, msib.description item_description,
mc.segment6 base_model, mc.segment7 trade_brand,
mmt.subinventory_code subinventory,
mil.segment1 || '.' || mil.segment2 || '.' || mil.segment3 LOCATOR,
ood.organization_code
|| '-'
|| ood.organization_name manufacturing_plant,
mmt.transaction_quantity quantity_produced,
mtt.transaction_type_name transaction_type, msib.inventory_item_id,
msib.organization_id, cic.material_cost COST,
cic.material_overhead_cost freight, csc.standard_cost,
*(select (NVL((SELECT SUM (mmt.transaction_quantity) total_NET_qty*
FROM mtl_material_transactions mmt,
mtl_system_items_b msib,
mtl_transaction_types mtt,
org_organization_definitions ood,
mtl_txn_source_types mts
WHERE mmt.organization_id = msib.organization_id
AND msib.inventory_item_id = mmt.inventory_item_id
AND mmt.transaction_type_id = mtt.transaction_type_id
AND mmt.organization_id = ood.organization_id
AND mtt.transaction_type_name = 'WIP Completion'
AND mts.transaction_source_type_name = 'Job or Schedule'
AND msib.segment1 = NVL (:p_item, msib.segment1)
AND ood.organization_code = :p_manufacturing_plant
AND TRUNC (mmt.transaction_date) BETWEEN :p_start_date
AND :p_end_date ),0)
*(NVL((SELECT SUM (mmt.transaction_quantity) total_NET_qty*
FROM mtl_material_transactions mmt,
mtl_system_items_b msib,
mtl_transaction_types mtt,
org_organization_definitions ood,
mtl_txn_source_types mts
WHERE mmt.organization_id = msib.organization_id
AND msib.inventory_item_id = mmt.inventory_item_id
AND mmt.transaction_type_id = mtt.transaction_type_id
AND mmt.organization_id = ood.organization_id
AND mtt.transaction_type_name = 'WIP Issue'
AND mts.transaction_source_type_name = 'Job or Schedule'
AND msib.segment1 = NVL (:p_item, msib.segment1)
AND ood.organization_code = :p_manufacturing_plant
AND TRUNC (mmt.transaction_date) BETWEEN :p_start_date
AND :p_end_date
*),0))) abs_qty from dual) AS ABS_qtyy,*(select (NVL((SELECT SUM (mmt.transaction_quantity) total_NET_qty
FROM mtl_material_transactions mmt,
mtl_system_items_b msib,
mtl_transaction_types mtt,
org_organization_definitions ood,
mtl_txn_source_types mts
WHERE mmt.organization_id = msib.organization_id
AND msib.inventory_item_id = mmt.inventory_item_id
AND mmt.transaction_type_id = mtt.transaction_type_id
AND mmt.organization_id = ood.organization_id
AND mtt.transaction_type_name = 'WIP Completion'
AND mts.transaction_source_type_name = 'Job or Schedule'
AND msib.segment1 = NVL (:p_item, msib.segment1)
AND ood.organization_code = :p_manufacturing_plant
AND TRUNC (mmt.transaction_date) BETWEEN :p_start_date
AND :p_end_date ),0)
(NVL((SELECT SUM (mmt.transaction_quantity) total_NET_qty
FROM mtl_material_transactions mmt,
mtl_system_items_b msib,
mtl_transaction_types mtt,
org_organization_definitions ood,
mtl_txn_source_types mts
WHERE mmt.organization_id = msib.organization_id
AND msib.inventory_item_id = mmt.inventory_item_id
AND mmt.transaction_type_id = mtt.transaction_type_id
AND mmt.organization_id = ood.organization_id
AND mtt.transaction_type_name = 'WIP Issue'
AND mts.transaction_source_type_name = 'Job or Schedule'
AND msib.segment1 = NVL (:p_item, msib.segment1)
AND ood.organization_code = :p_manufacturing_plant
AND TRUNC (mmt.transaction_date) BETWEEN :p_start_date
AND :p_end_date
),0))) abs_qty from dual)*csc.standard_cost AS abs_val
FROM mtl_material_transactions mmt,
mtl_system_items_b msib,
mtl_item_locations mil,
mtl_transaction_types mtt,
org_organization_definitions ood,
mtl_txn_source_types mts,
mtl_categories mc,
mtl_item_categories mic,
mtl_category_sets mcs,
cst_item_costs cic,
cst_standard_costs csc,
cst_cost_types ct,
mfg_lookups mlo
WHERE mmt.organization_id = msib.organization_id
AND msib.inventory_item_id = mmt.inventory_item_id
AND mmt.locator_id = mil.inventory_location_id
AND mil.organization_id = msib.organization_id
AND mil.subinventory_code = mmt.subinventory_code
AND mmt.transaction_type_id = mtt.transaction_type_id
AND msib.organization_id = ood.organization_id
AND mic.category_set_id = mcs.category_set_id
AND mc.category_id = mic.category_id
AND mc.structure_id = mcs.structure_id
AND mic.inventory_item_id = msib.inventory_item_id
AND mic.organization_id = msib.organization_id
AND mcs.category_set_id = 52487965
AND msib.inventory_item_id = cic.inventory_item_id
AND msib.organization_id = cic.organization_id
AND ct.cost_type_id = cic.cost_type_id
AND cic.organization_id = ood.organization_id
AND mil.inventory_location_id = mmt.locator_id
AND mmt.transaction_source_type_id = mtt.transaction_source_type_id
AND mmt.transaction_action_id = mtt.transaction_action_id
AND mmt.transaction_source_type_id = mts.transaction_source_type_id
AND mmt.transaction_action_id = mlo.lookup_code
AND mmt.inventory_item_id = csc.inventory_item_id
AND mmt.organization_id = csc.organization_id
AND csc.last_update_date >=
(SELECT MAX (csc1.last_update_date)
FROM cst_standard_costs csc1
WHERE csc1.inventory_item_id = mmt.inventory_item_id
AND csc1.organization_id = mmt.organization_id)
AND msib.segment1 = NVL (:p_item, msib.segment1)
AND NVL (mc.segment6, 'X') = NVL (:p_base_model, NVL (mc.segment6, 'X'))
AND NVL (msib.planner_code, 'Y') =
NVL (:p_planner_code, NVL (msib.planner_code, 'Y'))
AND UPPER (mlo.meaning) = 'ASSEMBLY COMPLETION'
AND mtt.transaction_type_name = 'WIP Completion'
AND ct.cost_type = 'Frozen'
AND mts.transaction_source_type_name = 'Job or Schedule'
AND ood.organization_code = :p_manufacturing_plant
AND TRUNC (mmt.transaction_date) BETWEEN :p_start_date AND :p_end_dateIf <tt>abs_qty</tt> and consequently <tt>abs_val</tt> should not be the same for all rows returned, you'll have to rewrite your query so that the <tt>abs_qty</tt> won't be computed in isolation (separately) with no interaction with data from the main from table list.
Your query is treated the same as the one below (note the changed table aliases by appending numbers to the originals of yours)
SELECT mmt.transaction_date date_produced,
msib.segment1 item,
cic.cost_type_id,
msib.description item_description,
mc.segment6 base_model,
mc.segment7 trade_brand,
mmt.subinventory_code subinventory,
mil.segment1 || '.' || mil.segment2 || '.' || mil.segment3 LOCATOR,
ood.organization_code || '-' || ood.organization_name manufacturing_plant,
mmt.transaction_quantity quantity_produced,
mtt.transaction_type_name transaction_type,
msib.inventory_item_id,
msib.organization_id,
cic.material_cost COST,
cic.material_overhead_cost freight,
csc.standard_cost,
(select (NVL(
(SELECT SUM (mmt1.transaction_quantity) total_NET_qty
FROM mtl_material_transactions mmt1,
mtl_system_items_b msib1,
mtl_transaction_types mtt1,
org_organization_definitions ood1,
mtl_txn_source_types mts1
WHERE mmt1.organization_id = msib1.organization_id
AND msib1.inventory_item_id = mmt1.inventory_item_id
AND mmt1.transaction_type_id = mtt1.transaction_type_id
AND mmt1.organization_id = ood1.organization_id
AND mtt1.transaction_type_name = 'WIP Completion'
AND mts1.transaction_source_type_name = 'Job or Schedule'
AND msib1.segment1 = NVL (:p_item, msib1.segment1)
AND ood1.organization_code = :p_manufacturing_plant
AND TRUNC (mmt1.transaction_date) BETWEEN :p_start_date AND :p_end_date
),0
) - NVL(
(SELECT SUM (mmt2.transaction_quantity) total_NET_qty
FROM mtl_material_transactions mmt2,
mtl_system_items_b msib2,
mtl_transaction_types mtt2,
org_organization_definitions ood2,
mtl_txn_source_types mts2
WHERE mmt2.organization_id = msib2.organization_id
AND msib2.inventory_item_id = mmt2.inventory_item_id
AND mmt2.transaction_type_id = mtt2.transaction_type_id
AND mmt2.organization_id = ood.2organization_id
AND mtt2.transaction_type_name = 'WIP Issue'
AND mts2.transaction_source_type_name = 'Job or Schedule'
AND msib2.segment1 = NVL (:p_item, msib2.segment1)
AND ood2.organization_code = :p_manufacturing_plant
AND TRUNC (mmt.2transaction_date) BETWEEN :p_start_date AND :p_end_date
),0
) abs_qty
from dual
) AS ABS_qtyy,
(select (NVL(
(SELECT SUM (mmt3.transaction_quantity) total_NET_qty
FROM mtl_material_transactions mmt3,
mtl_system_items_b msib3,
mtl_transaction_types mtt3,
org_organization_definitions ood3,
mtl_txn_source_types mts3
WHERE mmt3.organization_id = msib3.organization_id
AND msib3.inventory_item_id = mmt3.inventory_item_id
AND mmt3.transaction_type_id = mtt3.transaction_type_id
AND mmt3.organization_id = ood3.organization_id
AND mtt3.transaction_type_name = 'WIP Completion'
AND mts3.transaction_source_type_name = 'Job or Schedule'
AND msib3.segment1 = NVL (:p_item, msib3.segment1)
AND ood3.organization_code = :p_manufacturing_plant
AND TRUNC (mmt3.transaction_date) BETWEEN :p_start_date AND :p_end_date
),0
) - NVL(
(SELECT SUM (mmt4.transaction_quantity) total_NET_qty
FROM mtl_material_transactions mmt4,
mtl_system_items_b msib4,
mtl_transaction_types mtt4,
org_organization_definitions ood4,
mtl_txn_source_types mts4
WHERE mmt4.organization_id = msib4.organization_id
AND msib4.inventory_item_id = mmt4.inventory_item_id
AND mmt4.transaction_type_id = mtt4.transaction_type_id
AND mmt4.organization_id = ood4.organization_id
AND mtt4.transaction_type_name = 'WIP Issue'
AND mts4.transaction_source_type_name = 'Job or Schedule'
AND msib4.segment1 = NVL (:p_item, msib4.segment1)
AND ood4.organization_code = :p_manufacturing_plant
AND TRUNC (mmt4.transaction_date) BETWEEN :p_start_date AND :p_end_date
),0
) abs_qty
from dual
) * csc.standard_cost AS abs_val
FROM mtl_material_transactions mmt,
mtl_system_items_b msib,
mtl_item_locations mil,
mtl_transaction_types mtt,
org_organization_definitions ood,
mtl_txn_source_types mts,
mtl_categories mc,
mtl_item_categories mic,
mtl_category_sets mcs,
cst_item_costs cic,
cst_standard_costs csc,
cst_cost_types ct,
mfg_lookups mlo
WHERE mmt.organization_id = msib.organization_id
AND msib.inventory_item_id = mmt.inventory_item_id
AND mmt.locator_id = mil.inventory_location_id
AND mil.organization_id = msib.organization_id
AND mil.subinventory_code = mmt.subinventory_code
AND mmt.transaction_type_id = mtt.transaction_type_id
AND msib.organization_id = ood.organization_id
AND mic.category_set_id = mcs.category_set_id
AND mc.category_id = mic.category_id
AND mc.structure_id = mcs.structure_id
AND mic.inventory_item_id = msib.inventory_item_id
AND mic.organization_id = msib.organization_id
AND mcs.category_set_id = 52487965
AND msib.inventory_item_id = cic.inventory_item_id
AND msib.organization_id = cic.organization_id
AND ct.cost_type_id = cic.cost_type_id
AND cic.organization_id = ood.organization_id
AND mil.inventory_location_id = mmt.locator_id
AND mmt.transaction_source_type_id = mtt.transaction_source_type_id
AND mmt.transaction_action_id = mtt.transaction_action_id
AND mmt.transaction_source_type_id = mts.transaction_source_type_id
AND mmt.transaction_action_id = mlo.lookup_code
AND mmt.inventory_item_id = csc.inventory_item_id
AND mmt.organization_id = csc.organization_id
AND csc.last_update_date >= (SELECT MAX (csc1.last_update_date)
FROM cst_standard_costs csc1
WHERE csc1.inventory_item_id = mmt.inventory_item_id
AND csc1.organization_id = mmt.organization_id
AND msib.segment1 = NVL (:p_item, msib.segment1)
AND NVL (mc.segment6, 'X') = NVL (:p_base_model, NVL (mc.segment6, 'X'))
AND NVL (msib.planner_code, 'Y') = NVL (:p_planner_code, NVL (msib.planner_code, 'Y'))
AND UPPER (mlo.meaning) = 'ASSEMBLY COMPLETION'
AND mtt.transaction_type_name = 'WIP Completion'
AND ct.cost_type = 'Frozen'
AND mts.transaction_source_type_name = 'Job or Schedule'
AND ood.organization_code = :p_manufacturing_plant
AND TRUNC (mmt.transaction_date) BETWEEN :p_start_date AND :p_end_dateRegards
Etbin -
Hi all,
I am facing this problem, where in i have to find the Max of a set of rows based on paritition. Then sum all those max values and show it in the OBIEE. Is there any way we can do that in Criteria expressions of OBIEE Presentation Layer? Basically i am trying to find the header total at line level. Therefore for a given set of records the total amount will always be the same.
For e.g.
Header id Amount
1234 -> 100
1234 -> 100 MAX value - 100 Total = 100 + 200 = 300
1234 -> 100
1235 -> 200
1235 -> 200 MAX value - 200
1235 -> 200
This is how i want to show. Is it possible?
Thanks,
UpendraHi,
Yes tried this once.
First Solution:
Use three columns
1. Dimension
2. Measure column
3. Rcount(Measure_column)
Third column should have the column formula as-
case when Rcount(Measure_column)=2 then 'Max'||MAX(Measure_column) end
Second Solution:
Use the column formula in the second measure column itself as
cast(Measure_column as char)||case when Rcount(Measure_column)=2 then 'Max'||MAX(Measure_column) end
NOTE- 2 above refers the position where the message has to be displayed.
Award if this is helpful.
Regards
MuRam -
Displaying different sums in the same row based on date ranges
Say I had a table like this
DECLARE @t1 TABLE (RowId INT PRIMARY KEY IDENTITY(1,1), BusinessType VARCHAR(100), SalesPerson VARCHAR(100), Category VARCHAR(100), OrderAmount DECIMAL(10,2), OrderDate DATETIME )
Now I want to get a result that is grouped first by BusinessType, then SalesPerson and include derived columns that are sums based on different date ranges.
Month-to-date = from the start of the current month to today
Month-to-date previous year = from the start of the current month to today 1 year ago
DECLARE @today DATETIME
DECLARE @today_prev_year DATETIME
DECLARE @year_start DATETIME
DECLARE @year_prev_start DATETIME
DECLARE @month_start DATETIME
DECLARE @month_prev_start DATETIME
SET @today = GETDATE();
SET @today_prev_year = DATEADD(YEAR,-1,@today)
SET @year_start = CAST('01/01/' + CAST(DATEPART(YEAR,GETDATE()) AS VARCHAR(10)) AS DATETIME);
SET @year_prev_start = DATEADD(YEAR,-1,@year_start)
SET @month_start = DATEADD(month, DATEDIFF(month, 0, @today), 0); --gets the first of the current month
SET @month_prev_start = DATEADD(YEAR,-1,@month_start); --gets the first of the current month
SELECT t.BusinessType,t.SalesPerson,t.Category
SUM(CASE WHEN o.OrderDate > @month_start AND ac.DocumentDate < @today THEN o.OrderAmount ELSE 0 END) AS MTDActualAmount
SUM(CASE WHEN o.OrderDate > @month_prev_start AND ac.DocumentDate < @today_prev_year THEN o.OrderAmount ELSE 0 END) AS MTDPrevActualAmount
FROM @t1 t
Hope this is clear as to what Im trying to do. Im ultimately going to pull this into an SSRS report, so im wondering if I can do this different SUM values in the report and handle the grouping there.
Thoughts?Based on your example (with a group by added) it should work.
However, if you incorperated a calendar table (http://social.technet.microsoft.com/wiki/contents/articles/29260.tsql-calendar-functions-and-tables.aspx)
you could lose all those manually set parameters:
DECLARE @t1 TABLE (RowId INT PRIMARY KEY IDENTITY(1,1), BusinessType VARCHAR(100), SalesPerson VARCHAR(100), Category VARCHAR(100), OrderAmount DECIMAL(10,2), OrderDate DATETIME, DocumentDate DATETIME)
INSERT INTO @t1 (BusinessType, SalesPerson, Category, OrderAmount, OrderDate, DocumentDate) VALUES
('A','Joe','CatA',10,'2015-01-01','2015-01-01'),('A','Joe','CatB',10,'2015-01-02','2015-01-01'),('A','Joe','CatA',30,'2015-01-03','2015-01-04'),('A','Joe','CatB',40,'2015-01-04','2015-01-01'),('A','Joe','CatA',100,'2015-02-01','2015-02-01'),
('A','Joe','CatB',100,'2015-02-02','2015-02-01'),('A','Joe','CatA',300,'2015-02-03','2015-02-04'),('A','Joe','CatB',400,'2015-02-04','2015-02-01'),('A','Bob','CatA',1,'2015-01-01','2015-01-01'),('A','Bob','CatB',1,'2015-01-02','2015-01-01'),
('A','Bob','CatA',3,'2015-01-03','2015-01-04'),('A','Bob','CatB',4,'2015-01-04','2015-01-01'),('A','Bob','CatA',10,'2015-02-01','2015-02-01'),('A','Bob','CatB',10,'2015-02-02','2015-02-01'),('A','Bob','CatA',30,'2015-02-03','2015-02-04'),
('A','Bob','CatB',40,'2015-02-04','2015-02-01'),('B','Joe','CatA',10,'2015-01-01','2015-01-01'),('B','Joe','CatB',10,'2015-01-02','2015-01-01'),('B','Joe','CatB',40,'2015-01-04','2015-01-01'),
('B','Joe','CatA',100,'2015-02-01','2015-02-01'),('B','Joe','CatB',100,'2015-02-02','2015-02-01'),('B','Joe','CatA',300,'2015-02-03','2015-02-04')
SELECT t.BusinessType,t.SalesPerson,t.Category,
SUM(CASE WHEN OrderDate >= monthStart AND DocumentDate < today THEN OrderAmount ELSE 0 END) AS MTDActualAmount,
SUM(CASE WHEN OrderDate BETWEEN prevMonthStart AND prevMonthENd AND DocumentDate >= yearStart THEN OrderAmount ELSE 0 END) AS MTDPrevActualAmount
FROM @t1 t
INNER JOIN calendar c
ON today = CAST(CURRENT_TIMESTAMP AS DATE)
Group by t.BusinessType,t.SalesPerson,t.Category -
SQL Query : sum only numbers in row - need help
Pls see data below. I would like to display only 8 rows and BRH CCN rows should be merged together and displayed as - BRH 98 2 instead of 2 rows. The rest of the rows should remain as is. Please help.
CCN SERVICES MANUFACTURING
ASL 138 NA
BRH 98 NA
BRH NA 2
C10000 NA 0
C40000 NA 0
DAO NA 10
E10000 NA 0
I10000 NA 0
M10000 NA 0
Edited by: auxilia on Nov 7, 2009 6:25 AMwith t as (
select 'ASL' ccn,'138' services,'NA' manufacturing from dual union all
select 'BRH','98','NA' from dual union all
select 'BRH','NA','2' from dual union all
select 'C10000','NA','0' from dual union all
select 'C40000','NA','0' from dual union all
select 'DAO','NA','10' from dual union all
select 'E10000','NA','0' from dual union all
select 'I10000','NA','0' from dual union all
select 'M10000','NA','0' from dual
select ccn,
nvl(max(case services when 'NA' then null else services end),'NA') services,
nvl(max(case manufacturing when 'NA' then null else manufacturing end),'NA') manufacturing
from t
group by ccn
CCN SERVICES MANUFACTURING
E10000 NA 0
C10000 NA 0
BRH 98 2
M10000 NA 0
C40000 NA 0
ASL 138 NA
I10000 NA 0
DAO NA 10
8 rows selected.
SQL> SY.
Edited by: Solomon Yakobson on Nov 7, 2009 6:46 AM -
Help! Trying to Sum Quantity with Duplicate Rows
I have a database that have the following columns barcode, quantity, etc. The barcode colum has barcode duplicates and the corresponding quantity differs. I have been working hours trying to solve how to sum each different barcode and replace the table with 1 barcode each with corresponding total quantity. So, instead of:
97138644756
3
97138644756
2
97138644756
1
97138660565
5
97138660565
4
97138661777
1
97138662224
1
97138670403
1
97138679239
1
97138714978
1
97138716262
1
97138739667
6
97138739667
7
97138739667
8
I would like it to read:
97138644756
6
97138660565
9
97138661777
1
97138662224
1
97138670403
1
97138679239
1
97138714978
1
97138716262
1
97138739667
21
I was using the following code, which only picks out the duplicates.
<cfquery name="CountBarcodesCart1" datasource='inventory'>
SELECT *
FROM cart1
WHERE barcode IN (
SELECT barcode
FROM cart1
GROUP BY barcode
HAVING (COUNT(barcode ) > 1))
</cfquery>
Thanks for anyone that can help with my business.I know there is a lot better way to do this, but I will share the following code and hope it will help someone else. What I did was change from CART1 table to a TEMP table, get the dupliates added up and then output the corrected data (with no duplicates) to a new table. Then, I delete the TEMP table.
<!--- FIND DUPLICATES AND SUM QUANTITY --->
<cfquery name="CountBarcodesTemp" datasource='inventory'>
SELECT barcode, sum(quantity) as quantitysum
FROM temp
GROUP BY barcode
</cfquery>
<!--- OUTPUT RESULTS AND INSERT INTO NEW TABLE--->
<cfoutput query="CountBarcodesTemp">
<cfquery name="InsertIntoCart" datasource="inventory">
INSERT INTO Cart1 (barcode, quantity)
VALUES (#CountBarcodesTemp.barcode#, #CountBarcodesTemp.quantitysum#)
</cfquery>
#barcode# -- #QuantitySum#<br /><br/>
</cfoutput>
<!--- DELETE THE TEMP TABLE --->
<cfquery name="DeleteBarcodesTemp" datasource='inventory'>
DELETE *
FROM Temp
</cfquery> -
Difference between SUM values in adjoining rows
Hi I've written following query:
DECLARE @Decision_Group nvarchar(10) = 'All',
@Entry_Month INT = 09,
@In_Use CHAR(1) = 'Y',
@Scheme nvarchar(05) = 'UGS',
@Subject_Area nvarchar(10) = 'ART',
@Location nvarchar(10) = 'UOB',
@Home_Overseas nvarchar(05) = 'H'
SELECT SUM(a.CONVERSION_EARLY) AS Early, SUM(a.CONVERSION_MIDDLE) AS Middle, SUM(a.CONVERSION_CLEARING) AS Clearing, a.AcademicYear
FROM
SELECT
srs_cap.cap_ayrc AS AcademicYear
,CONVERSION_EARLY =
CASE
WHEN (srs_cap.cap_crtd < cast('02/10/' + left(srs_cap.cap_ayrc,4) AS dateTime)) THEN 1
END
,CONVERSION_MIDDLE =
CASE
WHEN (
srs_cap.cap_crtd < cast('08/01/' + left(srs_cap.cap_ayrc,4) AS dateTime) AND
srs_cap.cap_crtd >= cast('02/10/' + left(srs_cap.cap_ayrc,4) AS dateTime)) THEN 1
END
,CONVERSION_CLEARING =
CASE WHEN (srs_cap.cap_crtd >= cast('08/01/' + left(srs_cap.cap_ayrc,4) AS dateTime)) THEN 1
END
FROM srs_cap
LEFT JOIN srs_mcr ON srs_cap.cap_mcrc = srs_mcr.mcr_code
LEFT JOIN srs_dsc ON srs_cap.cap_dscc = srs_dsc.dsc_code
LEFT JOIN srs_mth ON srs_cap.cap_mthc = srs_mth.mth_code
LEFT JOIN ins_lca ON srs_cap.cap_cgpc = ins_lca.lca_code
LEFT JOIN men_xon ON men_xon.xon_oldv = srs_cap.cap_dptc
LEFT JOIN ins_dpt ON srs_cap.cap_dptc=ins_dpt.dpt_code
LEFT JOIN srs_sce ON srs_cap.cap_scjc = srs_sce.sce_scjc
AND srs_cap.cap_ayrc = srs_sce.sce_ayrc
WHERE cap_ayrc BETWEEN dbo.[GetAcademicYear-2] (getdate()) AND dbo.getnextacademicyear(getdate())
AND (@Decision_Group='All' OR (@Decision_Group<>'A' AND RIGHT(srs_cap.cap_dscc,1) IN (left(@Decision_Group,1)) ))
AND cap_mthc IN (@Entry_Month)
AND srs_mcr.mcr_iuse IN (@In_Use)
AND srs_cap.cap_schc IN (@Scheme)
AND srs_cap.cap_dptc IN (@Subject_Area)
AND srs_cap.cap_cgpc IN (@Location)
AND srs_cap.cap_apcc IN (@Home_Overseas)
AND (men_xon.xon_tabl='Academic Group' or men_xon.xon_tabl IS NULL)
GROUP BY
srs_cap.cap_ayrc,
srs_cap.cap_crtd
AS a
GROUP BY
AcademicYear
This returns following data.
Early
Middle
Clearing
AcademicYear
79
58
27
2012/13
69
58
37
2013/14
75
55
30
2014/15
67
6
NULL
2015/16
Is there a simple way using windows functions to get the difference between Early 2012/13 and Early 2013/14, then Middle 2012/13 and Middle 2013/14 etc?
Cheers, AndySugsPlease follow basic Netiquette and post the DDL we need to answer t His. Follow industry and ANSI/ISO standards in your data. You should follow ISO-11179 rules for naming data elements. You are not even close.
You should follow ISO-8601 rules for displaying temporal data. We need to know the data types, keys and constraints on the table. You failed again and did it in the worst way! Is your '08/01/' supposed to be “yyyy-08-01” or “yyyy-01-08” ? Who knows? This
is why ANSI/ISO Standard SQL allows only the “yyyy-mm-dd” display format.
Avoid dialect in favor of ANSI/ISO Standard SQL.
Why did you treat temporal data like 1960's COBOL strings? And then you picked the worst possible display format!
Why do you write with assembly language flags?
Why did you write more left outer joins in ONE query than I have written in the last three years? Your DDL (which you did not post) would have to be total mess.
Why do you think that table names of less than 8 characters are maintainable? I am old and remember when file names had to be less than 8 characters but that was in the 1970's on magnetic tape file systems.
Why do you think that “cap_ayrc” is better than the ISO-1179 compliant “academic_year” that you had to alias it to? In RDBMS, a data element has one and only one name. This name does not contain meta data. The general syntax is “<attribute>_<attribute
property>”
The old Sybase/UNIX getdate() should be CURRENT_TIMESTAMP today.
Competent SQL programmers do not use UDF. They do not port, the overhead is too great and they cannot be optimized. But VB, COBOL and programmers in other procedural languages use them to avoid learning declarative programming. This means your silly [Get_Academicyear-2]
() and Get_Next_Academicyear() would be in a report period look-up table. SQL is a data base language so we use tables instead of computations.
Why did you write code like:
S.cap_schc IN (@scheme_name) ?
The IN() predicate implies that you are passing a list. But First Normal Form (1NF) uses scalars. That would be sooo wrong!
My guess is that when you fix the schema, this query would look more like this skeleton:
SELECT A.academic-year,
SUM (CASE WHEN (A.foobar_date < R.something_start_date)
THEN 1 ELSE 0 END)) AS early_conversion_cnt,
SUM (CASE WHEN (A.foobar_date BETWEEN R.something_start_date
AND R.something_end_date) AS middle_conversion_date
THEN 1 ELSE 0 END)) AS early_conversion_cnt,
SUM (CASE WHEN (A.foobar_date > R.something_end_date)
THEN 1 ELSE 0 END)) AS AS clearing_conversion_cnt
FROM Report_Periods AS R, .. AS A
WHERE .. ;
Do you want to follow the forum rules and try again with DDL and specs? Having taught SQL and having other credentials, I would say that you are 3-5 years from being a productive SQL programmer.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL
Maybe you are looking for
-
Drivers for Lenovo 3000 N500 4233-5FG notebook
Hi everybody! yesterday I became my brandnew N500 4233-5FG notebook. Here are the specifications: http://www5.pc.ibm.com/europe/products.nsf/$wwwPartNumLookup/_NS75Fxx?open&OpenDocument&epi=web_express A friend of mine and I tried to install Win XP,
-
Create document from template without launching Adobe Bridge (InDesign CS4)
When I select "New Document from Template... while using InDesign CS4, Adobe Bridge launches instead of a search menu. It's much harder for me to navigate through Bridge than it is with a standard New menu. How can I get InDesign NOT to launch Bridg
-
Hi, I have a backup of a database in a tar ball. I need to create a clone of the database on another server. The old database was in a location say : /usr/test01 on another server And I have extracted the contents of the tar ball on the location /usr
-
IPhone sync issue with iTunes after Snow Leopard upgrade
I upgraded to Snow Leopard and since then iTunes does not see my iPhone 3GS and I am unable to sync. I've read the troubleshooting guide to no avail. What am I missing?
-
How to communicate via RS232?
I am trying to program an sbRIO-9606 to communicate with a Matrix Orbital LCD display via RS232. I have NI-VISA 5.4.1 and NI-VISA Server 5.4.1 software installed on the sbRIO. But I am not seeing any COM ports to use to communicate. Is there other