Finding 5 most recent records for each customer with abnormal order amounts
The database consists of the following tables:
create table dbo.customer (
customer_id int identity primary key clustered,
customer_name nvarchar(256) not null
create table dbo.purchase_order (
purchase_order_id int identity primary key clustered
customer_id int not null,
amount money not null,
order_date date not null
Implement a query for the report that will provide the following information: for each customer output at most 5 different dates which contain
abnormally high or low amounts (bigger or less than 3 times SDTDEV from AVG), for each of these dates output minimum and maximum amounts as well.
Possible result: http://triontp.pop3.ru/333.gif
amount Not Between stdev_amt - 3 * avg_amt And stdev_amt + 3 * avg_amt
vice versa.
amount Not Between avg_amt - 3 * stdev_amt And avg_amt + 3 * stdev_amt)
Similar Messages
-
Select most recent DATE for an ID.
Hi All,
I need to SELECT the records with the most recent DATE for each ID. A DATE range is part of the selection criteria.
My data.
MY_ID MY_DATE
1684662 26-JAN-09
1424097 27-JAN-09
1684663 27-JAN-09
1684664 27-JAN-09
1684672 28-JAN-09
0689073 28-JAN-09
1052476 21-JAN-09
1052476 21-JAN-09
1360828 23-JAN-09
1684661 23-JAN-09
1052476 30-JAN-09
1052476 30-JAN-09
1052476 30-JAN-09
1052476 30-JAN-09
The code below works fine when selecting 1 ID in the SUBSELECT, but with multiple ID it still selects rownum=1 (of course). This as far as my thinking takes me.
SELECT my_id,
my_date
FROM
(SELECT my_id,
my_date
FROM my_table
ORDER BY my_date DESC
WHERE rownum = 1
AND *{color:#ff0000}my_id = 1052476{color}*
AND TO_CHAR(my_date,'YYYY/MM/DD') BETWEEN '2009/01/01' AND '2009/01/31';
If I could somehow pass the SELECT ID into the SUBSELECT WHERE clause I should have this done.
Any suggestions?
Thank You in Advance for Your help,
LouOne of many possible soultions.
ME_XE?with data as
2 (
3 select 1684662 as id, to_date('26-JAN-09','dd-mon-yyyy') as the_date from dual union all
4 select 1424097 as id, to_date('27-JAN-09','dd-mon-yyyy') as the_date from dual union all
5 select 1684663 as id, to_date('27-JAN-09','dd-mon-yyyy') as the_date from dual union all
6 select 1684664 as id, to_date('27-JAN-09','dd-mon-yyyy') as the_date from dual union all
7 select 1684672 as id, to_date('28-JAN-09','dd-mon-yyyy') as the_date from dual union all
8 select 0689073 as id, to_date('28-JAN-09','dd-mon-yyyy') as the_date from dual union all
9 select 1052476 as id, to_date('21-JAN-09','dd-mon-yyyy') as the_date from dual union all
10 select 1052476 as id, to_date('21-JAN-09','dd-mon-yyyy') as the_date from dual union all
11 select 1360828 as id, to_date('23-JAN-09','dd-mon-yyyy') as the_date from dual union all
12 select 1684661 as id, to_date('23-JAN-09','dd-mon-yyyy') as the_date from dual union all
13 select 1052476 as id, to_date('30-JAN-09','dd-mon-yyyy') as the_date from dual union all
14 select 1052476 as id, to_date('30-JAN-09','dd-mon-yyyy') as the_date from dual union all
15 select 1052476 as id, to_date('30-JAN-09','dd-mon-yyyy') as the_date from dual union all
16 select 1052476 as id, to_date('30-JAN-09','dd-mon-yyyy') as the_date from dual
17 )
18 select id, the_date
19 from
20 (
21 select id, the_date, max(the_date) over (partition by id) as max_the_date
22 from data
23 )
24 where the_date = max_the_date;
ID THE_DATE
689073 28-JAN-0009 12 00:00
1052476 30-JAN-0009 12 00:00
1052476 30-JAN-0009 12 00:00
1052476 30-JAN-0009 12 00:00
1052476 30-JAN-0009 12 00:00
1360828 23-JAN-0009 12 00:00
1424097 27-JAN-0009 12 00:00
1684661 23-JAN-0009 12 00:00
1684662 26-JAN-0009 12 00:00
1684663 27-JAN-0009 12 00:00
1684664 27-JAN-0009 12 00:00
ID THE_DATE
1684672 28-JAN-0009 12 00:00
12 rows selected.
Elapsed: 00:00:00.03 -
Dear Expert,
I have one table called Tab1 & have a lot columns. For this schenario, i mentioned few of the columns.
The columns are
product_type varchar2(100),
curr_date date (storing with time stamp),
other1_cloumn varchar2(10),
other2_cloumn varchar2(10)
Now I want to display all the records in ascending order except most recent record for every product_type || Union all || Most recent records in ascending order in single query.
For example...
product_type curr_date date other1_cloumn other2_cloumn
1, "10-May-2005 10:10:10", 10, 10
1, "10-May-2005 10:10:11", 10, 10
1, "10-May-2005 10:10:12", 10, 10
1, "10-May-2005 10:10:09", 10, 10
2, "10-May-2005 10:10:10", 10, 10
2, "10-May-2005 10:10:11", 10, 10
2, "10-May-2005 10:10:12", 10, 10
2, "10-May-2005 10:10:09", 10, 10
3, "10-May-2005 10:10:10", 10, 10
3, "10-May-2005 10:10:11", 10, 10
3, "10-May-2005 10:10:12", 10, 10
3, "10-May-2005 10:10:09", 10, 10
Now I want to display (OUTPUT) like...
1, "10-May-2005 10:10:10", 10, 10
1, "10-May-2005 10:10:11", 10, 10
1, "10-May-2005 10:10:09", 10, 10
2, "10-May-2005 10:10:10", 10, 10
2, "10-May-2005 10:10:11", 10, 10
2, "10-May-2005 10:10:09", 10, 10
3, "10-May-2005 10:10:10", 10, 10
3, "10-May-2005 10:10:11", 10, 10
3, "10-May-2005 10:10:09", 10, 10
1, "10-May-2005 10:10:12", 10, 10
2, "10-May-2005 10:10:12", 10, 10
3, "10-May-2005 10:10:12", 10, 10
Plz, provide a solution....I want to display like...
select * from tab1 where (product_type,curr_date) in
(select product_type,max(curr_date) from tab1 group by product_type)
order by product_type
Union All
(select * from tab1 where (product_type,curr_date) NOT IN
(select product_type,max(curr_date) from tab1 group by product_type)
order by product_type)
But, In the above case, The ORDER BY Clause will not work...How to get this one...?
I mean.. I want to display all the records in ascending order except Most recent record base on Product type and next I want to display Most recent record in ascending order. I want to display both cases in a single query...? -
SQL help: return number of records for each day of last month.
Hi: I have records in the database with a field in the table which contains the Unix epoch time for each record. Letz say the Table name is ED and the field utime contains the Unix epoch time.
Is there a way to get a count of number of records for each day of the last one month? Essentially I want a query which returns a list of count (number of records for each day) with the utime field containing the Unix epoch time. If a particular day does not have any records I want the query to return 0 for that day. I have no clue where to start. Would I need another table which has the list of days?
Thanks
RayPeter: thanks. That helps but not completely.
When I run the query to include only records for July using a statement such as following
============
SELECT /*+ FIRST_ROWS */ COUNT(ED.UTIMESTAMP), TO_CHAR((TO_DATE('01/01/1970','MM/DD/YYYY') + (ED.UTIMESTAMP/86400)), 'MM/DD') AS DATA
FROM EVENT_DATA ED
WHERE AGENT_ID = 160
AND (TO_CHAR((TO_DATE('01/01/1970','MM/DD/YYYY')+(ED.UTIMESTAMP/86400)), 'MM/YYYY') = TO_CHAR(SYSDATE-15, 'MM/YYYY'))
GROUP BY TO_CHAR((TO_DATE('01/01/1970','MM/DD/YYYY') + (ED.UTIMESTAMP/86400)), 'MM/DD')
ORDER BY TO_CHAR((TO_DATE('01/01/1970','MM/DD/YYYY') + (ED.UTIMESTAMP/86400)), 'MM/DD');
=============
I get the following
COUNT(ED.UTIMESTAMP) DATA
1 07/20
1 07/21
1 07/24
2 07/25
2 07/27
2 07/28
2 07/29
1 07/30
2 07/31
Some dates donot have any records and so no output. Is there a way to show the missing dates with a COUNT value = 0?
Thanks
Ray -
Need to retrieve the most recent record per deposit number
Can someone please show me a way to solve my problem or at least point me in the right direction.
I have a simple bibliography table called mdd_biblio that has 3 fields:
bib_mdd_no this is a deposit number
bib_upd_by person who updated the record
bib_upd_date date the record was updated (timestamp)
A deposit might have more than one bibliographic entry with either the same date or a newer date and, the deposit might have one or more person updating the bibliography
The table has 4974 records but only 1867 are unique deposit numbers
All I want to do is select a single record that indicates when the deposit was last edited and and who did the editing (as indicated by the *). I don't care which record it is as long as it is the most recent date.
The following query retrieves 1944 records (67 duplicates) and has eliminated 3000 duplicates but hasn't eliminated them all. Why?
select unique(bib_mdd_no), bib_upd_by, bib_upd_date from mdd_biblio
order by bib_mdd_no
M64C/16-001 MDD 13-SEP-07 *
M64C/16-001 MDD 13-SEP-07
M64C/16-002 DPROUXE 30-NOV-07 *
M64C/16-002 MDD 13-SEP-07
M64C/16-002 MDD 13-SEP-07
M64C/16-003 DPROUXE 29-NOV-07 *
M64C/16-003 MDD 13-SEP-07
M64C/16-003 MDD 13-SEP-07
M64C/16-004 MDD 13-SEP-07 *
M64C/16-004 MDD 13-SEP-07
M64C/16-005 MDD 13-SEP-07 *
M64C/16-005 MDD 13-SEP-07
M64C/16-006 DPROUXE 03-DEC-07 *
M64C/16-006 MDD 13-SEP-07
M64C/16-007 MDD 02-OCT-07 *
M64C/16-008 MDD 02-OCT-07 *
M64C/16-009 MDD 02-OCT-07 *
M64C/16-010 MDD 02-OCT-07 *
I don't understand why of these duplicates still show up here since 3000 have been eliminated..
To Reiterate: What I really need is a simple query to retrieve the most recently edited record for each deposit number regardless of the updater's name. I need this in order to build a summary table from this and 15 other tables.
Thanks for any help you can give
GlennUsually it is done somehow like this
select bib_mdd_no,
bib_upd_by,
bib_upd_date
from (select bib_mdd_no,
bib_upd_by,
bib_upd_date,
row_number() over (partition by bib_mdd_no order by bib_upd_date desc) r
from mdd_biblio
where r = 1
order by bib_mdd_noRegards
Etbin -
How to get Sales rep information for each customer
Hi
For each record in ra_customers, I want to find associated Sales rep for each customer.
Can some one please tell me what table stores this information?
Thanks
AaliTry
SELECT rc.customer_name, rc.customer_number, jrs.NAME
FROM jtf_rs_salesreps jrs, ra_customers rc
WHERE rc.primary_salesrep_id = jrs.salesrep_idHope this helps,
Sandeep Gandhi -
Select "most recent" records in abap
Hi
how can I select the most recent records in a table by using the abap select statement.
Example: The 100 rows with the "highest" (most recent) sequence-numbers in a table with documents.
somehow like this:
SELECT "most recent" * FROM draw
INTO TABLE gt_doklist
UP TO 100 ROWS
where ...
Has anybody an idea?
Thanks in advance,
WilliActually I believe that all the answers are wrong.
I believe that there will never be a single statement. If you need to determine the last 100 records for a special user you first need to determine the highest document number.
this can be done by
select max( document_number ) into document_number from table where username = username.
Any descending sorting order or group by etc. will never make sure that you get the last one. For a simple reason What should the select statement look like that makes sure (in combination of any cursor applied)? Its impossible!
If you now need the latest 100 records for a single user its the same problem like buffered number ranges. There is no way to perform that task because there is no database routine or sql statement to do so. And 1.5 million records are too much to try out or select everything.
You could do an assumption that the last 100 for that user have been posted during the last 1000 or last 10.000 records, select them and filter out.
Alternative you can perform the following select statement for 100 times. Using an index on document number and user might not be such a performance killer if its only done for one user during his online dynpro process:
data: max_number type char10.
select max( documentnumber ) into max_number from table
where username = username into [structure].
max_number = max_number + 1.
do 100 times
select max( documentnumber ) from table intomax_number
where username = username and docnumber lt max_docnumber.
select * from [db_table] into [structure] where docnumber = max_number.
append [structure] to [table].
enddo.
Of course that just draft coding... apply if statements and so on...
Even though its pretty poor, its the only way to do. Any select statement will never garantee what records you will get if you do not restrict accordingly and if the restriction has to be made on document number, but if there is no way to get the max_number - [100 last records of this user], there is no solution using one statement.
Thats it.
Edited by: Rob Burbank on Feb 25, 2010 8:52 AM -
Select most recent record prior to a target date
Let's say I have a set of unique records with the following dates:
11/15/08
11/30/08
Is it possible to select only the most recent record prior to a user-entered target date (e.g., 12/1/08)? So in this case, only the record dated 11/30/08 would be selected for the report.Use the record selection formula like this
{Date field}<={?Date Parameter}
And also wrtite the group selection formula like this
{Date field}=maximum({Date field})
This returns the recent value nearer to the date entered in the prompt.
Regards,
Raghavendra -
Aperture 3.1.2 When exporting files to Finder is creating a folder for each file, that means export 200 files is creating 200 folders for each files. Does anyone knows how to fix this. Thanks
No. This is NOT true. I've been battling with this for a couple hours.
I choose Export>Versions
choose
Subfolder Format: Project Name
no custom subfolder
Name Format: Current Version Name.
Aperture creates project folders for each image and names them Projects1, Projects2, Projects3 etc.
To be sure, I have no project named Projects
x
This particular Aperture library has 16 projects. you can see in the screen grab it's created more than 30 folders
If I uncheck the Subfolder, that is say NONE, I just get images and images and images, not in their project folders (of course, because I've said NONE, so why would it put them in project folders?)
I just want to export the entire library and have the versions placed in folders named by the projects from which they came.... ez enough, no? -
How to print the out put for each customer in table
Dear SDN Team Members!
I've created a report and the output is displayed on the smartform for the same.
In this report, I've selected a list of customers and the report has to print the list of transactions being made by each customer in Smartform.
These details must be printed seperately for each customer.
I'm thinking of control statement AT NEW & AT END. But i don't know how to use them exactly so that the output is displayed seperately for each customer in the Smartform.
Please provide syntax and points will be awarded for.
Best Regards!Hi Krsihna,
Its simple.
call function 'SSF_FUNCTION_MODULE_NAME'
exporting formname = '<form name>'
importing FM_NAME = lv_func_name.
loop at itab.
at new customer. "customer must be the fisrt field in the itab and itab mus be sorted by customer.
call function lv_func_name
endat.
endloop.
Regards,
Ravi -
My phone will not let me download the most recent update for iOS 7.1. It says I need a wifi connection and will not allow me to push the 'Download & Install' button, even when I am at home and my wifi is on. How do I fix this?
Unfortunately yes, though technology would never move on if it kept to older standards and did not strive to do more.
The app store always gives you the option to use a previous version of the app IF it is available for your device.
Sorry but you are stuck without new hardware.
PJRS -
Select one record for each member of the group
Hi,
am having a table where in i will be having data for so many group members. i need to fetch data for a particular group members whose number of rows of data may be more in numbers (but i want only one row of data for each member of the group)
here is the query for fetching all rows of data
select RI.RESOURCE_NAME,TR.MSISDN,TR.ADDRESS1_GOOGLE, TR.MSG_DATE_INFO, FROM TRACKING_REPORT TR, RESOURCE_INFO RI
WHERE TR.MSISDN IN (SELECT MSISDN FROM RESOURCE_INFO WHERE GROUP_ID ='1' AND COM_ID=2 ) AND RI.MSISDN=TR.MSISDN
order by MSG_DATE_INFOoutput of this query is...
>
ddd 12345 13-Mar-10 19:43:03
eee 54321 Tamil Nadu, India 13-Mar-10 19:39:48
ddd 12345 13-Mar-10 19:32:58
eee 54321 Tamil Nadu, India 13-Mar-10 19:30:07
ddd 12345 13-Mar-10 19:23:08
eee 54321 Tamil Nadu, India 13-Mar-10 19:20:14
fff 98765 13-Mar-10 19:19:22
ddd 12345 13-Mar-10 19:13:01
eee 54321 Tamil Nadu, India 13-Mar-10 19:09:50
ddd 12345 13-Mar-10 19:02:56
eee 54321 tn,ind 13-Mar-10 18:59:49
ddd 12345 13-Mar-10 18:53:08
eee 54321 tn,ind 13-Mar-10 18:49:50
ddd 12345 13-Mar-10 18:42:56
eee 54321 tn,ind 13-Mar-10 18:39:50
ddd 12345 13-Mar-10 18:33:00
eee 54321 tn,ind 13-Mar-10 18:29:50
ddd 12345 13-Mar-10 18:22:54
eee 54321 tn,ind 13-Mar-10 18:19:50
ddd 12345 13-Mar-10 18:12:56
eee 54321 tn,ind 13-Mar-10 18:09:50
ddd 12345 13-Mar-10 18:02:54
eee 54321 tn,ind 13-Mar-10 18:00:02
fff 98765 Tamil Nadu, India 13-Mar-10 17:59:26
fff 98765 Tamil Nadu, India 13-Mar-10 17:54:26
ddd 12345 13-Mar-10 17:52:56
eee 54321 tn,ind 13-Mar-10 17:49:50
fff 98765 Tamil Nadu, India 13-Mar-10 17:49:25
fff 98765 Tamil Nadu, India 13-Mar-10 17:44:26
ddd 12345 13-Mar-10 17:42:56
>
from this output i want only one latest record for each member(ddd,eee,fff). i.e
>
ddd 12345 13-Mar-10 19:43:03
eee 54321 Tamil Nadu, India 13-Mar-10 19:39:48
fff 98765 13-Mar-10 19:19:22
>
how to modify the query to achieve this...?Hi,
This is not giving the result which i want...
table is
CREATE TABLE TRACKING_REPORT
ID NUMBER,
MSISDN NUMBER(12) NOT NULL,
X NUMBER(15,8) NOT NULL,
Y NUMBER(15,8) NOT NULL,
TIME_STAMP DATE,
MSG_DATE_INFO DATE DEFAULT sysdate,
ADDRESS1_GOOGLE VARCHAR2(400 BYTE),
ADDRESS2_GOOGLE VARCHAR2(400 BYTE),
ADDRESS_MLINFO VARCHAR2(400 BYTE),
REQ_ID VARCHAR2(30 BYTE)
CREATE TABLE RESOURCE_INFO
RESOURCE_ID NUMBER,
MSISDN NUMBER,
RESOURCE_NAME VARCHAR2(25 BYTE),
ADDRESS VARCHAR2(100 BYTE),
COM_ID VARCHAR2(20 BYTE),
ADMIN_ID NUMBER,
TIME_STAMP DATE DEFAULT SYSDATE,
GROUP_ID NUMBER
) -
When we drilldown at document level it displays one record for each cube
Dear Experts .
The users want to see the report by status for the order detail report at document level it displays one record for each cube .
They want to see the single line for the order quantity , Amount, Billing quantity delivery and amount along with the Sales order number , billing document number billing date , delvert date ..et in signgle line. ( Sale order DeliveryBilling)
When we are doing on Multiprovider recording displaying from each cube.
We have tried with Constant selections and it is not giving the required results m since these want to see by status. Due to huge data performance reasons we re unable to use it .
Please provide your valuable inputs.
Thanks,
Mahesh.Mahesh,
If you want to get detail data with respect to Sales order - delivery - Billing, on top of Multiprovider you can come up with Constant selection.
But I am not clear that why constant selection is not working properly, can I have some light on that.
Probably, you are struggling with multiple records .
I suggest you to go with end routine and map all fields to one IC and do reporting on that single IC, then your requirement will fulfill.
But here we need to take care of performance issues and process chain scheduling wile loading deltas.
If Multiprovider is not working properly , then try with Infoset(If you have more than 2 ICs it not possible).
Regards,
rvc -
A report which displaies Bank Incoming payments for each Customer
Hi All,
could anyone show me a report which displaies the Bank Incoming payments for each Customer?
Thanks
GandalfYes u can very well see the Off setting account ( Bank account number ) in the line item report.
For that small customization is requiered so as to activate the off setting account display in all line item report.
Just implement the below changes
NodeFinancial Accounting
PathIMG u2192 Financial Accounting u2192 Financial Accounting Global Settings u2192 Business Transaction Events
1 Active Settings
Setttings u2192 Products u2192 u2026 of a customer
Following new product added and activated:
Product - OFF
Text - Offsetting account
Active - Yes
Setttings u2192 P/S Function Modules u2192 u2026 of a customer
Following new event added:
Event - 00001650
Product - OFF
Ctr - (Leave Blank)
Appl. - (Leave Blank)
Function Module - LINE_ITEMS_GET_GKONT
The above settings enable the function module for displaying the offsetting account in line item display.
Ritesh -
Loading the cube from 3 datasources and getting 3 records for each keyfield
Hi All,
I am loading an InfoCube from 3 separated datasources. These 3 datasources are UD Datasources and their unique source system is UD Connect.
Each of the datasource contains a unique key field 'Incident Number' (same as we use have in Datasources for DSO).
The problem is, when I am loading data with these 3 datasources to the cube, for each 'Incident number' there becomes 3 records.
We have reports on this Infocube and the report also displays 3 records for each incident number.
If I remove Incident Number key field from 2 of the Datasources, the data from these datasources do not reach to the Cube.
For many of you, this may be a minor problem ( or may not be a problem at all !!! ) , but as a New Joinee in SAP field, this has become a showstopper issue for me.
Please suggest.
Thanks in Advance.Hi Pravender,
Thanks for your interest.
The scenario is, I have 3 datasources form the same source system, All the 3 datasources have different fields except 'Incident Number'. So, each and every field has only one value in the report. But due to 3 separate datasources, it creates 3 records displahying values of each datasource in a separate record.
There is no field in the query output which is having different values for the different source systems. Due to 3 records in the cube, one record will contain the value for a particular field and the other two records will show a Blank for that field.
Regards.
Maybe you are looking for
-
Dynamic Stamps with Text Input Options
You guys were so helpful I thought I'd give this another go. I'm trying to create a stamp like the one used in this tutorial (http://blogs.adobe.com/acrolaw/2009/05/add_dynamic_exhibit_stamps_in_ac.html), but based off of the attached image. The tuto
-
Add Data driven subscription option missing in sharepoint 2013
i have a sharepoint 2013 enterprise version with sql server 2012 standard version. And i want to add Data- driven subscriptions for a document library by selecting manage subscriptions. But in the manage subscriptions Add Data- Driven subscription op
-
CFMAIL -- How to send an e-mail to a network folder
Guys, I am using Cold Fusion 7.0 and I have the functionality in the website where an e-mail is generated (i.e based on some changes or actions) and that e-mail goes to a folder in the Outlook right now. Instead of sending the email (which is generat
-
2611xm Terminal Server + ACS + reauthentication when selecting menu options
Hi, I've managed to setup ACS Authentication on my 2611xm router, after you login to the router I have a autocommand setup to run a menu. My problem is when you select the option on the menu, You are then re prompted to reauthenicated against the
-
How to automate DDL statements in batch or stored procedure?
I'd like to write a start file or stored procedure to create synonyms for all tables owned by the current user as well as any to which the user has been granted access. However, no DDL statement like create synonyms is allowed in stored procedures. H