Analytical function sum() ...for Till-date reporting
Hi,
I need help in forming an SQL with analytical function.
Here is my scenario:
create table a (name varchar2(10), qty_sold number,on_date date);
insert into a values ('abc',10,'10-JAN-2007 00:01:00');
insert into a values ('abc',01,'10-JUL-2007 00:01:00');
insert into a values ('abc',05,'10-JUL-2007 08:11:00');
insert into a values ('abc',17,'10-JUL-2007 09:11:00');
insert into a values ('def',10,'10-JAN-2006 08:01:00');
insert into a values ('def',01,'10-JUN-2006 10:01:00');
insert into a values ('def',05,'10-JUL-2006 08:10:00');
insert into a values ('pqr',17,'10-JUL-2006 09:11:00');
Now I want to have a sql which displays the following:
NAME--TOTAL_QTY_SOLD_IN_LAST_10_DAYS, TOTAL_QTY_SOLD_IN_LAST_20_DAYS...etc
I know we can do it using sum(qty_sold) over (order on_date range interval '10' days and preceding) .... but I get too many rows for each "NAME" ....for each of the date in the database table a ... I want just one row for each "Name"...and sum() should be till SYSDATE ....
Any help is highly appreciated.
Thanks.
SQL> select name
2 , sum(case when sysdate - on_date <= 10 then qty_sold end) total_qty_last_10_days
3 , sum(case when sysdate - on_date <= 100 then qty_sold end) total_qty_last_100_days
4 , sum(case when sysdate - on_date <= 500 then qty_sold end) total_qty_last_500_days
5 from a
6 group by name
7 /
NAME TOTAL_QTY_LAST_10_DAYS TOTAL_QTY_LAST_100_DAYS TOTAL_QTY_LAST_500_DAYS
abc 23 33
def 6
pqr 17
3 rijen zijn geselecteerd.Regards,
Rob.
Similar Messages
-
Analytical function SUM() OVER (PARTITION BY ) in Crosstab
I am trying to resolve this from a very long time. I have an amount column that has to be grouped on Year, but all the other columns grouped by month. I am trying to achieve this using analytic function SUM(Case when (Condition1 and Condition2) then Sum(Amount) else 0 end) OVER ( PARTITION BY Account, Year), Where Account, Sub Account are the left axis columns. Now, column displays the values correctly, but at different rows. This is confusing.............
For Ex: For Account 00001, there are 3 sub accounts 1000,2000,3000. For Sub account 3000, conditions 1 and 2 are satisfied, so it should display the Amount in the row corresponding to Sub account 3000, and 0 for remaining Sub Accounts. And the Total amount of all the sub accounts, which will be the same as amount for SubAccount 3000 should be displayed in the row corresponding to Account 00001.
But I get blank rows for 1000 and 3000 Sub accounts and Amount displayed in 2000 Sub account, and blank for Account 00001 also.
When I created the same workbook in Tabular form, the same amount is displayed for all the SubAccounts of a single Account.
When I used this CASE statement in TOAD, I figured that this is due to the Analytic function. When I use a group by clause as shown below instead of partition by, I get the results I need.
SELECT (Case when (Condition1 and Condition2) then Sum(Amount) else 0 end), Account, Sub Account FROM tables WHERE conditions GROUP BY Year, Account, Sub Account
But I cannot use groupby for whole SQL of the workbook as I need the other columns with page item 'MONTH' not 'Year'.
Could somebody please help me with this?Hi,
In your tabular form do you get the correct total display against all you subaccounts and account? If this correct then you can use case to ensure that the total is displayed only for the single account.
Once you have the correct totals working in a tabular form it is easier to re-produce what you want in a cross-tab.
Rod West -
How you will see reports last year and till date reports and compare?
How you will see reports last year and till date reports and how you will compare?
sridharHi.
This you can achieve at query level.
I have few question.. First let me know how many KFs you have in the query and based on what characteristic you want to compare the KFs.
Let me assume that you want to compare "SALES" KF for the last and current year.
1) Add the "SALES" KF to the Query.
2) Rename it as "SALES - Prior Year"
3) Copy the above KF and rename the new KF as "SALES - Current Year"
4) In the "SALES - Prior Year", add the date (say, Calendar Date, Fiscal Period, etc.) as per yr requirement. Keep offset as "-1"
5) Do the same for "SALES - Current Year" . No offset.
6) Now create a new formula to find the difference between "SALES - Prior Year" and "SALES - Current Year" .
Hope you got some idea!!
Let me know if anything more is required.
Regards,
Balaji V -
Please update the functional specifications for Vendor Balance Report
Dear All,
Please update the functional specifications for Vendor Balance Report with
proper descriptions.
Please include
1. How your selection screen should look like..
2. Output format ( in an excel sheet)
3. Logic for every field in the output format (viz. .. how do link the
vendors from the selection screen to the BKPf and BSEG tables, how do you
find open and closed items in BSEG , etc.)
Please avoid statements like 'The total value of all the break-up should
tally with the balance as shown in FBL1N for a given vendor for the given
day.'
You need to give the logic for the FBL1N, in that case.
Regards
Rama MohanHello again,
Regarding having them update the specs, one can dream. Dreams are free. LOL. Hopefully the memory makers will update their specs as more makers start supporting a large memory configuration.
The 4GB sticks I am using are http://shop.kingston.com/partsinfo.asp?ktcpartno=KTL-TP667/4G
I did not do an extended test of the sticks in my MBP. Their home is in the ThinkPad I have because it is running Windows Server 2008 with Hyper-V.
As you can see from the link above, these little suckers are pricey. But consider the price of 2GB SoDIMMs were $950 when they entered the market a couple of years ago. The price will drop.
I asked the Kingston rep that sent these to me if online sites like newegg.com would have them soon. She indicated no, but the corporate buying sites like CDW and Zone would. So check around. They just started shipped a couple of weeks ago so be patient.
If you have any other questions, please stop by http://blogs.technet.com/keithcombs/archive/2008/07/05/testing-my-apple-macbook- pro-with-8gb-of-ram.aspx and ask there. I'll see them much more quickly.
Cheers,
Keith Combs
Microsoft -
Case Statement in Analytic Function SUM(n) OVER(PARTITION BY x)
Hi Guys,
I have the following SQL that doesn't seem to consider the When clause I am using in the case staement inside the analytic function(SUM). Could somebody let me know why? and suggest the solution?
Select SUM(Case When (A.Flag = 'B' and B.Status != 'C') Then (NVL(A.Amount_Cr, 0) - (NVL(A.Amount_Dr,0))) Else 0 End) OVER (PARTITION BY A.Period_Year) Annual_amount
, A.period_year
, B.status
, A.Flag
from A, B, C
where A.period_year = 2006
and C.Account = '301010'
--and B.STATUS != 'C'
--and A.Flag = 'B'
and A.Col_x = B.Col_x
and A.Col_y = C.Col_y
When I use this SQL, I get
Annual_Amount Period_Year Status Flag
5721017.5 --------- 2006 ---------- C -------- B
5721017.5 --------- 2006 ---------- O -------- B
5721017.5 --------- 2006 ---------- NULL ----- A
And when I put the conditions in the where clause, I get
Annual_Amount Period_Year Status Flag
5721017.5 ---------- 2006 ---------- O -------- BHere are some scripts,
create table testtable1 ( ColxID number(10), ColyID number(10) , Periodname varchar2(15), Flag varchar2(1), Periodyear number(15), debit number, credit number)
insert into testtable1 values(1, 1000, 'JAN-06', 'A', 2006, 7555523.71, 7647668)
insert into testtable1 values(2, 1001, 'FEB-06', 'B', 2006, 112710, 156047)
insert into testtable1 values(3, 1002, 'MAR-06', 'A', 2006, 200.57, 22376.43)
insert into testtable1 values(4, 1003, 'APR-06', 'B', 2006, 0, 53846)
insert into testtable1 values(5, 1004, 'MAY-06', 'A', 2006, 6349227.19, 6650278.03)
create table testtable2 ( ColxID number(10), Account number(10))
insert into testtable2 values(1, 300100)
insert into testtable2 values(2, 300200)
insert into testtable2 values(3, 300300)
insert into testtable2 values(4, 300400)
insert into testtable2 values(5, 300500)
create table apps.testtable3 ( ColyID number(10), Status varchar2(1))
insert into testtable3 values(1000, 'C')
insert into testtable3 values(1001, 'O')
insert into testtable3 values(1002, 'C')
My SQL:
select t1.periodyear
, SUM(Case When (t1.Flag = 'B' and t3.Status != 'C') Then (NVL(t1.credit, 0) - (NVL(t1.debit,0))) Else 0 End) OVER (PARTITION BY t1.PeriodYear)
Annual_amount
, t1.flag
, t3.status
, t2.account
from testtable1 t1, testtable2 t2, testtable3 t3
where t1.colxid = t2.colxid
and t1.colyid = t3.colyid(+)
--and t1.Flag = 'B' and t3.Status != 'C'
Result:
PeriodYear ----- AnnualAmount ----- Flag ----- Status ----- Account
2006 ------------------ 43337 --------------- A ----------- C ---------- 300100
2006 ------------------ 43337 --------------- B ----------- O ---------- 300200
2006 ------------------ 43337 --------------- A ----------- C ---------- 300300
2006 ------------------ 43337 --------------- B ------------ ----------- 300400
2006 ------------------ 43337 --------------- A ------------ ----------- 300500
With condition "t1.Flag = 'B' and t3.Status != 'C'" in where clause instead of in Case statement, Result is (which is desired)
PeriodYear ----- AnnualAmount ----- Flag ----- Status ----- Account
2006 ------------------ 43337 --------------- B ----------- O ---------- 300200 -
ALV FUNCTION MODLE FOR GETTING DATA IN EXCEL SHEET
HI ,
CAN I HAVE ALV FUNCTION MODLE FOR GETTING DATA IN EXCEL SHEET
Regards,
ArunaStandard ALV grid uses the FM <b>ALV_XXL_CALL</b> for exporting data into Excel. You can put a break-point in the FM and check it for urself.
for ur info - the above FM takes the internal table name, internal table content and the field catalog as input. -
Analytical functions approach for this scenario?
Here is my data:
SQL*Plus: Release 11.2.0.2.0 Production on Tue Feb 26 17:03:17 2013
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select * from batch_parameters;
LOW HI MIN_ORDERS MAX_ORDERS
51 100 6 8
121 200 1 5
201 1000 1 1
SQL> select * from orders;
ORDER_NUMBER LINE_COUNT BATCH_NO
4905 154
4899 143
4925 123
4900 110
4936 106
4901 103
4911 101
4902 91
4903 91
4887 90
4904 85
4926 81
4930 75
4934 73
4935 71
4906 68
4907 66
4896 57
4909 57
4908 56
4894 55
4910 51
4912 49
4914 49
4915 48
4893 48
4916 48
4913 48
2894 47
4917 47
4920 46
4918 46
4919 46
4886 45
2882 45
2876 44
2901 44
4921 44
4891 43
4922 43
4923 42
4884 41
4924 40
4927 39
4895 38
2853 38
4890 37
2852 37
4929 37
2885 37
4931 37
4928 37
2850 36
4932 36
4897 36
2905 36
4933 36
2843 36
2833 35
4937 35
2880 34
4938 34
2836 34
2872 34
2841 33
4889 33
2865 31
2889 30
2813 29
2902 28
2818 28
2820 27
2839 27
2884 27
4892 27
2827 26
2837 22
2883 20
2866 18
2849 17
2857 17
2871 17
4898 16
2840 15
4874 13
2856 8
2846 7
2847 7
2870 7
4885 6
1938 6
2893 6
4888 2
4880 1
4875 1
4881 1
4883 1
ORDER_NUMBER LINE_COUNT BATCH_NO
4879 1
2899 1
2898 1
4882 1
4877 1
4876 1
2891 1
2890 1
2892 1
4878 1
107 rows selected.
SQL>The batch_parameters:
hi - high count of lines in the batch.
low - low count of lines in the batch.
min_orders - number of minimum orders in the batch
max_orders - number of maximum orders in the batch.
The issue is to create an optimal size of batches for us to pick the orders. Usually, you have stick within a given low - hi count but, there is a leeway of around let's say 5%percent on the batch size (for the number of lines in the batch).
But, for the number of orders in a batch, the leeway is zero.
So, I have to assign these 'orders' into the optimal mix of batches. Now, for every run, if I don't find the mix I am looking for, then, the last batch could be as small as a one line one order too. But, every Order HAS to be batched in that run. No exceptions.
I have a procedure that does 'sort of' this, but, it leaves non - optimal orders alone. There is a potential of the orders not getting batched, because they didn't fall in the optimal mix potentially missing our required dates. (I can write another procedure that can clean up after).
I was thinking (maybe just a general direction would be enough), with what analytical functions can do these days, if somebody can come up with the 'sql' that gets us the batch number (think of it as a sequence starting at 1).
Also, the batch_parameters limits are not hard and fast. Those numbers can change but, give you a general idea.
Any ideas?Ok, sorry about that. That was just guesstimates. I ran the program and here are the results.
SQL> SELECT SUM(line_count) no_of_lines_in_batch,
2 COUNT(*) no_of_orders_in_batch,
3 batch_no
4 FROM orders o
5 GROUP BY o.batch_no;
NO_OF_LINES_IN_BATCH NO_OF_ORDERS_IN_BATCH BATCH_NO
199 4 241140
99 6 241143
199 5 241150
197 6 241156
196 5 241148
199 6 241152
164 6 241160
216 2 241128
194 6 241159
297 2 241123
199 3 241124
192 2 241132
199 6 241136
199 5 241142
94 7 241161
199 6 241129
154 2 241135
193 6 241154
199 5 241133
199 4 241138
199 6 241146
191 6 241158
22 rows selected.
SQL> select * from orders;
ORDER_NUMBER LINE_COUNT BATCH_NO
4905 154 241123
4899 143 241123
4925 123 241124
4900 110 241128
4936 106 241128
4901 103 241129
4911 101 241132
4903 91 241132
4902 91 241129
4887 90 241133
4904 85 241133
4926 81 241135
4930 75 241124
4934 73 241135
4935 71 241136
4906 68 241136
4907 66 241138
4896 57 241136
4909 57 241138
4908 56 241138
4894 55 241140
4910 51 241140
4914 49 241142
4912 49 241140
4915 48 241142
4916 48 241142
4913 48 241142
4893 48 241143
2894 47 241143
4917 47 241146
4919 46 241146
4918 46 241146
4920 46 241146
2882 45 241148
4886 45 241148
2901 44 241148
2876 44 241148
4921 44 241140
4891 43 241150
4922 43 241150
4923 42 241150
4884 41 241150
4924 40 241152
4927 39 241152
2853 38 241152
4895 38 241152
4931 37 241154
2885 37 241152
4929 37 241154
4890 37 241154
4928 37 241154
2852 37 241154
2843 36 241156
2850 36 241156
4932 36 241156
4897 36 241156
4933 36 241158
2905 36 241156
2833 35 241158
4937 35 241158
4938 34 241158
2880 34 241159
2872 34 241159
2836 34 241158
2841 33 241159
4889 33 241159
2865 31 241159
2889 30 241150
2813 29 241159
2902 28 241160
2818 28 241160
4892 27 241160
2884 27 241160
2820 27 241160
2839 27 241160
2827 26 241161
2837 22 241133
2883 20 241138
2866 18 241148
2849 17 241161
2871 17 241156
2857 17 241158
4898 16 241161
2840 15 241161
4874 13 241146
2856 8 241154
2847 7 241161
2846 7 241161
2870 7 241152
2893 6 241142
1938 6 241161
4888 2 241129
2890 1 241133
2899 1 241136
4877 1 241143
4875 1 241143
2892 1 241136
ORDER_NUMBER LINE_COUNT BATCH_NO
4878 1 241146
4876 1 241136
2891 1 241133
4880 1 241129
4883 1 241143
4879 1 241143
2898 1 241129
4882 1 241129
4881 1 241124
106 rows selected.As you can see, my code is a little buggy in that it may not have followed the strict batch_parameters. But, this is acceptable to be in the general area. -
Function Module for selecting date by day
Is there any function module to choose date by day. for example if I give today's date the FM should give me next thursday's date.
Thanks
Tharanithis works good........
*& Report YCHATEST *
REPORT YCHATEST
data:day like DTRESR-WEEKDAY.
parameters:curr_dat like sy-datum default sy-datum.
CALL FUNCTION 'DATE_TO_DAY'
EXPORTING
DATE = CURR_DAT
IMPORTING
WEEKDAY = day.
write : / day.
case day.
when 'Monday'.
CURR_DAT = CURR_DAT + 3.
when 'Tuesday'.
CURR_DAT = CURR_DAT + 2.
when 'Wed.'.
CURR_DAT = CURR_DAT + 1.
when 'Thursday'.
CURR_DAT = CURR_DAT + 7.
when 'Friday'.
CURR_DAT = CURR_DAT + 6.
when 'Sat.'.
CURR_DAT = CURR_DAT + 5.
when 'Sunday'.
CURR_DAT = CURR_DAT + 4.
endcase.
write :/ CURR_DAT. -
Remote enabled Function module for a simple report program
Hi Experts ,
I have to convert a Simple list report program to a remote enabled function module, so that it can be used from the online applications.
One option that comes to my mind is to create some import parameters for all the selection screen fields and then the result will be fetched in the export tables in function module. and call that report program using call transaction or submit program.
I Dont know whether this is a best solution or not, can you please help me with your suggestions?
Regards,
Ashish ShahHi,
You can use the SUBMIT statment in the Function module , then in the report program you need to EXPORT the Result to the list then use the IMPORT option in the function moduel then pass the data to the TABLE parameters so that the data will be avilable in the table parameters
Regards
Sudheer -
Functional specs for Open BI reports
Hi
Has anyone written functional specs BI report. Can you help me out with an example?
Thanks
TarangHi Tarang,
I never got an oppertunity to developed a Functional Spec for BI to my knowledge it is not a big deal to prepare it.
Here we go you must be having atleast what you want from R/3 or the Functional Spec from Functionl req.
That will contain the R/3 Fields and Table names, so based on that you have to find which app area does it comes.
Then from there by seeing the fields and cross checking the Datasourcess under that application you can decide the the data source.
Once the data source is decide you can prepare the Gap analisys to proceed further.
By above details you can prepare a Functional Come Tech Spec.We made all these kind of specification(Functional Come Technical Spec).
I believe there is no saparate Functiuonal Spec for BW activities.
Best Regards,
VNK. -
Functiona Spec For VENDOR PERFORMANCE REPORT
Hi experts
I need to develope a VENDOR PERFORMANCE REPORT. But due to some reason i cant be able to get the Functional Specification for that object. So could you please help me by sending the above mention functional Specification.
thnaks and regadrs
PratikPratik,
Bad question in ABAP forum.
can you please close this thread and put it into MM forum for quick reply.
Amit. -
Best Practice for Master Data Reporting
Dear SAP-Experts,
We face a challenge at the moment and we are still trying to find the right approach to it:
Business requirement is to analyze SAP Material-related Master Data with the BEx Analyzer (Master Data Reporting)
Questions they want to answer here are for example:
- How many active Materials/SKUs do we have?
- Which country/Sales Org has adopted certain Materials?
- How many Series do we have?
- How many SKUs below to a specific season
- How many SKUs are in a certain product lifecycle
- etc.
The challenge is, that the Master Data is stored in tables with different keys in the R/3.
The keys in these tables are on various levels (a selection below):
- Material
- Material / Sales Org / Distribution Channel
- Material / Grid Value
- Material / Grid Value / Sales Org / Distribution Channel
- Material / Grid Value / Sales Org / Distribution Channel / Season
- Material / Plant
- Material / Plant / Category
- Material / Sales Org / Category
etc.
So even though the information is available on different detail levels, the business requirement is to have one query/report that combines all the information. We are currently struggeling a bit on deciding, what would be the best approach for this requirement. Did anyone face such a requirement before - and what would be the best practice. We already tried to find any information online, but it seems Master data reporting is not very well documented. Thanks a lot for your valuable contribution to this discussion.
Best regards
LukasPass a reference to the parent into the modal popup. Then you
can reference anything in the parent scope.
I haven't done this i 2.0 yet so I can't give you code. I'll
post if I do.
Oh, also, you can reference the parent using parentDocument.
So in the popup you could do:
parentDocument.myPublicVariable = "whatever";
Tracy -
Function Modules for Format Date, Month Name and Quarter value.
Hi All,
1. I am getting the date field from the Flatfile, so i need to format the date to YYYYMMDD. Is there any Function module for that in BW.
2. Based on the above Formated Date i have to find out the Name of the Month like JANUARY, FEBUARY etc.
3. Based on the same above Formated Date i need to find out the Quarter like 1, 2 etc.
Could you please let me know if any function modules are there for the above questions in BW not in ABAP, why i mention is some Function modules are there in ABAP but not in BW.
Thanks in advance.
Regards,
srinivasHi ,
1.If your input date format is MMDDYYYY then Use SDATE as conversion routine in trans strucutre to convert into YYYYMMDD.
2.By passing year as input parameter for the following FM you would get all the months with text.Using READ statement in routine you can get month name
MONTH_NAMES_GET
3.For Quarters you can use the following FM:
TSTR_PERIODS_QUARTERS
hope it helps...
regards,
Raju -
Function Module for Discount Date
Hi,
Can somebody tell me whether there is any function module for determining the Discount Date (DATS) and Payment Due Date(DATS) based on Payment Terms (ZTERM)?
Thanks,
John.FI_FIND_PAYMENT_CONDITIONS
DETERMINE_DUE_DATE -
CO03 function module for cost analysis report
Hi
Can anybody pls let me know the function module for which gives the cost analysis data in CO03 transaction.
RegardsHi
I'm sorry; BAPI_INTERNALORDER_GETDETAIL, this BAPI did serve my purpose
Pls let me know if any other FM.
Thanks for your help.
Regards
Maybe you are looking for
-
How to display Attachments on OAF page for different entities
Hi, I have a requirement where I have to create an attachment page to display the attachments dynamically at run time depending upon the values passed such as Entity Name, Primary Key values, Category Id values. I have a query to give me the attachme
-
Getting Error when trying to connect to the Primavera database. PPM V7 SP3
Hi Guys, Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailService' defined in class path resource [com/primavera/bre//com/primavera/bre/integration/intgconf.xml]: Invalid destruction signature; ne
-
Need to know schema name and table name associated with a column-URGENT
Hi folks, I need to know the schema name and the table name associated with a column. Though jdbc has the api to getTableName and getSchemaName, some database vendor like oracle does return empty upon call of mentioned methods. I found that oracle dr
-
I need the iPod Updater 2005-10-12. I installed the new one (1-10) and it screwed up my ipod. if anybody has it or has a link to it (ive tried google) please send it to me via AIM or leave it here, ill be here for a while. AIM= PimpinElf4819 any help
-
Hi all; I have a 10g database on suse linux 10 on VMware; I have an instance on this server called by ABCD. Then I have deleted this instance and I have a new instance with using RMAN from another instance. Then I want to have Enterprise Manager for