analytic-functions

avg sale of quarter with previous quarter avg sale

微笑、不失礼 提交于 2019-12-19 11:42:11
问题 I have a table one in which there are various attribute like region product,year,qtr,month,sale. I have to calculate the avg_qtr sale of each product having same region and show their previous avg_qtr sale.I have read about lag but here it is not possible to use as it is not fixed after how many rows it will be repeated. My table structure is like this Region Product Year Qtr Month Sales NORTH P1 2015 1 JAN 1000 NORTH P1 2015 1 FEB 2000 NORTH P1 2015 1 MAR 3000 NORTH P1 2015 2 APR 4000 NORTH

Taking the record with the max date

扶醉桌前 提交于 2019-12-17 18:04:08
问题 Let's assume I extract some set of data. i.e. SELECT A, date FROM table I want just the record with the max date (for each value of A). I could write SELECT A, col_date FROM TABLENAME t_ext WHERE col_date = (SELECT MAX (col_date) FROM TABLENAME t_in WHERE t_in.A = t_ext.A) But my query is really long... is there a more compact way using ANALYTIC FUNCTION to do the same? 回答1: The analytic function approach would look something like SELECT a, some_date_column FROM (SELECT a, some_date_column,

Oracle Analytics - partitions and ordering of sql queries

淺唱寂寞╮ 提交于 2019-12-13 15:12:48
问题 This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY COL1 COL2 COL3 0 0.1 100 <NULL> 12 <NULL> <NULL> 3 24 0.2 <NULL> <NULL> and the following query: select 'B' METRIC, ROW_PRIORITY, last_value(col1 ignore nulls) over (ORDER BY ROW_PRIORITY) col1, last_value(col2 ignore nulls) over (ORDER BY ROW_PRIORITY) col2, last_value(col3 ignore nulls) over (ORDER BY ROW_PRIORITY) col3 from (SELECT * FROM ZTEST); I get these results: METRIC ROW

How to best calculate n-level aggregation data based on (n-1)-level data (Oracle)

淺唱寂寞╮ 提交于 2019-12-13 09:10:29
问题 In this answer (with all executable simplified and documented sample code + helpful comments) I did a kind of a trick there to calculate the last two rows of the following table: DESCR SUM ---------------------------------- ---------- money available in 2013 33233235.3 money spent in 2013 4253235.3 money bound to contracts in 2013 34333500 money spent 2013 in % of available 12 money bound 2013 in % of available 103 Does anybody know of a better (performance, amount of code, understandability)

How to get new and returned YTD,MTD,WTD users in a user traffic table?

北城余情 提交于 2019-12-12 01:41:42
问题 I would like to get all the new and returned users from a user_traffic table. Just wondering what would be the approach in solving this problem. Any thoughts/inputs would be appreciated. I am not expecting a ready made solution for this problem but any kind of directional inputs would help me. Thank you. user_traffic : Session_ID, session_day, glance_view_id, user_id, product_id. Sample Input :- create table user_traffic (session_id number(6), session_day date, user_id number(6), product_id

MySQL get row position with ORDER BY incorrectly numbered

╄→尐↘猪︶ㄣ 提交于 2019-12-11 21:27:33
问题 I have the following mySQL query which I want to give the rows sorted on a calculated field with the corresponding row position. I've used OMG Ponies code from here as a template but the rows are coming back incorrectly numbered (they are being numbered in key (gemid) order without the sort). I know it has to do with the JOIN on a GROUP BY but I know know how to fix it. Thanks in advance. SELECT g.gemid, sum_rating, @rownum := @rownum + 1 AS row_num FROM gems g LEFT JOIN (SELECT gemid, SUM

How do I compress this Oracle resultset into values according to row priority, ignoring nulls?

删除回忆录丶 提交于 2019-12-11 12:58:08
问题 I'll simplify the problem as much as possible: I have an oracle table: row_priority, col1, col2, col3 0, .1, 100, {null} 12, {null}, {null}, 3 24, .2, {null}, {null} Desired result: col1, col2, col3 .2, 100, 3 So according to the priority of the row, it overrides previous row values, if given. I'm attempting to work out a solution using analytical functions over the table, but it just isn't behaving... I try: select last_value(col1 ignore nulls) over () col1, last_value(col2 ignore nulls)

Can I use Oracle analytical function here?

冷暖自知 提交于 2019-12-11 12:08:39
问题 I've got this query: SELECT year, month, week, C.cpg_pk CPG, C.dep_pk DEPT, T.cust_id CUST_ID, D1.r_id R_ID, Decode(d2.at_code, '3', func1.Get_att(d2.at_code, D2.val_code)) AS P1, Decode(d2.at_code, '2', func1.Get_att(d2.at_code, D2.val_code)) AS IC, Decode(d2.at_code, '1', func1.Get_att(d2.at_code, D2.val_code)) AS B1, Decode(func1.Get_att(d2.at_code, D2.val_code), 2, d2.at_code) AS P2, Decode(func1.Get_att(d2.at_code, D2.val_code), 5, d2.at_code) AS B2, Count(DISTINCT A.cust_id) TOTAL_ACC ,

Analytic functions for “the attribute from the row with the max date”

家住魔仙堡 提交于 2019-12-11 06:41:50
问题 I'm refactoring a colleague's code, and I have several cases where he's using a cursor to get "the latest row that matches some predicate": His technique is to write the join as a cursor, order it by the date field descending, open the cursor, get the first row, and close the cursor. This requires calling a cursor for each row of the result set that drives this, which is costly for many rows. I'd prefer to be able to join, but what something cheaper than a correlated subquery: select a.id

Use TOP or Rank when finding the first few or the most observations

痞子三分冷 提交于 2019-12-11 05:35:42
问题 I have searched others' code and tried to customize in my case, but it seemed when the question was the "max", it worked. When it comes to find the top 100, it was not working. I am trying to get the first 100 people hired in the firm. I first tried TOP(100) and then RANK(), I guess they would both work. I was not super familiar with the syntax though. I wonder would anyone could kindly provide me with any suggestion on my code? Thank you very much! SELECT d.department_name, d.department_id,