SQL User Defined Functions for performing statistical calculations
Hi!
I hope you can help. I just wasn’t sure where to go with this question, so I’m hoping you can at least point me in the right direction.
I’m writing a SQL Server stored procedure that returns information for a facility-wide scorecard-type report. The row and columns are going to be displayed in a SQL Server Reporting Services report.
Each row of information contains “Current Month” and “Previous Month” numbers and a variance column. Some rows may compare percentages, others whole numbers, others ratios, depending on the metric they’re measuring. For each row/metric the company has specified whether they want to see a t-test or a chi-squared statistical test to determine whether or not there was a statistically significant difference between the current month and the previous month.
My question is this: Do you know where I can find a set of already-written user defined functions to perform statistical calculations beyond the basic ones provided in SQL Server 2005? I’m not using Analysis Services, so what I’m looking for are real SQL User Defined Functions where I can just pass my data to the function and have it return the result within a stored procedure.
I’m aware that there may be some third-party statistical packages out there we could purchase, but that’s not what I’m looking for. And I’m not able to do anything like call Excel’s analysis pack functions from within my stored procedure. I’ve asked. They won’t let me do that. I just need to perform the calculation within the stored procedure and return the result.
Any suggestions? Is there a site where people are posting their SQL Server UDF’s to perform statistical functions? Or are you perhaps aware of something like a free add-in for SQL that will add statistical functions to those available in SQL? I just don’t want to have to write my own t-test function or my own chi-squared function if someone has already done it.
Thanks for your help in advance! Oh, and please let me know if this should have been posted in the TSQL forum instead. I wasn't entirely sure.
Karen Grube
STATS_T_TEST_
docs.oracle.com/cd/B19306_01/server.102/b14200/functions157.htm
STATS_T_TEST_ONE: A one-sample t-test
STATS_T_TEST_PAIRED: A two-sample, paired t-test (also known as a crossed t-test)
STATS_T_TEST_INDEP: A t-test of two independent groups with the same variance (pooled variances)
STATS_T_TEST_INDEPU: A t-test of two independent groups with unequal variance (unpooled variances)
Similar Messages
-
? Regarding 'SQL user-defined function' execution performance ?
Hi All,
I have an user-defined function in SQL which am using in a SQL select statement and i found that the execution time taken by the user-defined function is 9-10 minutes as a result of which my whole query execution time is affected.
How to i improvise the execution time. Should i do some sort of scheduling for executing the user-defined functions only. Am new to the database world. Your early response with a good suggestion would really be appreciated.
Thanks in Advance.Try to read SQL tuning documents along with TkProf. Then only you will be able to detect the problem.
There is no instant method by which you can solve this problem.
But, you can get idea from this samples.
Tuning:
http://www.dba-oracle.com/art_sql_tune.htm
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::NO::P11_QUESTION_ID:8764517459743
Reading TkProf:
http://www.dbspecialists.com/presentations/use_explain.html
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:920429344869
Regards.
Satyaki De. -
Calling PL/SQL user defined functions from ODI Constraints
Hi All,
We are trying to call user defined PL/SQL functions from ODI. We are able to call them from ODI's User functions. But when we are trying to call them from ODI Constraints under Models, it is throwing an error 'ORA-00920 invalid relational operator'. Kindly let me know if anyone has faced the same issue and got the resolution for the same. Thanks in Advance.
Regards,
Abhishek SharmaHi Ace,
Thanks for the response, the same error was coming in operator also.
I am able to call PL?SQL user defined functions from ODI Constraints. We have to first call ODI User functions from the ODI constraints as we cant call PL/SQL function (compiled in database) directly.
From the ODI User functions, we can then call the PL/SQL functions.
Please reach out to me if you need further details reg this. -
How to call Microsoft SQL User defined function??
I am trying to call Miscrosoft SQL user defined function (not stored procedure) and experience problems. The function -- func_profile_history takes 3 inputs and returns resultSet.
Please let me know if there is any examples some where. All the examples I found are for stored procedure.
Thanks....
Siu
************ source *************
public Vector callSqlFunction(String eidTradcom, String idSku, String endDate, String idUserPost)
throws TradcomException, RemoteException
System.out.println("--------------in callSqlFunction--------------1");
Connection conn = null;
java.sql.CallableStatement cstmt = null;
ResultSet rs = null;
ForecastInfo info = null;
Vector v = new Vector();
try{
conn = TradcomUtils.getConnection();
String sql = "{call func_profile_history(?,?,?)}";
System.out.println("--------------in callSqlFunction--------sql="+sql);
cstmt = conn.prepareCall(sql);
System.out.println("--------------in callSqlFunction-------2");
cstmt.setString(1, eidTradcom);
cstmt.setString(2, idSku);
cstmt.setString(3, endDate);
System.out.println("--------------in callSqlFunction-------3");
rs = cstmt.executeQuery();
System.out.println("--------------in callSqlFunction-------4");
while (rs.next()){
info = new ForecastInfo ();
info.setEidTradcom(rs.getString("eid_tradcom"));
info.setIdSku(rs.getString("id_sku"));
info.setQtOnHand(rs.getDouble("qt_on_hand"));
info.setQtOnHold(rs.getDouble("qt_hold"));
info.setQtNetOnHand(rs.getDouble("qt_net_on_hand"));
info.setQtAlloc(rs.getDouble("qt_alloc"));
info.setQtAvailUnalloc(rs.getDouble("qt_avail_unalloc"));
info.setQtPoShip(rs.getDouble("qt_po_ship"));
info.setQtPoRcvd(rs.getDouble("qt_po_rcvd"));
info.setQtTransit(rs.getDouble("qt_transit"));
info.setQtAsn(rs.getDouble("qt_asn"));
info.setQtPo(rs.getDouble("qt_po"));
info.setQtPoBalance(rs.getDouble("qt_po_balance"));
info.setQtSoShip(rs.getDouble("qt_so_ship"));
info.setQt4Cast(rs.getDouble("qt_4cast"));
v.addElement(info);
System.out.println("--------------in callSqlFunction-------5");
System.out.println("--------------in callSqlFunction-------v="+v);
return v;
}catch(Exception ex){
System.out.println("Error in callSqlFunction:"+ ex.getMessage());
throw CachingManager.getTradcomException(ex, "Error in callSqlFunction");
}finally{
try{ if (cstmt != null) cstmt.close(); } catch(Exception ex){}
try{ if (conn != null) conn.close(); } catch(Exception ex){}
********************* error output ***************
********************* bef calling callSqlFunction
--------------in callSqlFunction--------------1
--------------in callSqlFunction--------sql={call func_profile_history(?,?,?)}
--------------in callSqlFunction-------2
--------------in callSqlFunction-------3
Error in callSqlFunction:The request for procedure 'func_profile_history' failed
because 'func_profile_history' is a function object. Severity 18, State 1, Proc
edure 'LAP_DUAL null', Line 4
ERROR=Error in callSqlFunction...The request for procedure 'func_profile_history
' failed because 'func_profile_history' is a function object. Severity 18, State
1, Procedure 'LAP_DUAL null', Line 4well, I tried the preparedStatemnet and it worked. Case closed.
-
User defined function for getting last string in the line
Hi Experts,
I am not java expert, can anyone give me user defined function for getting last string in the line.
for example if the source field is "NEW ARBOUR SQUARE" i want to pass to target field only last string that is "SQUARE"
please help me out of this.
Kind Regards.
Praveen.You don't even need a UDF for this. In the graphical editor look for the standard functions and once you do a scroll over on 'text functions; you will find what you are looking for.
Just a piece of advice, try keeping UDF's to minimum unless really required or it is complicated without it.
regards -
User - defined functions for calculating the taxes of state and country
hi expects,
how can write user-defined function in message mapping in which i want to calculate the states taxes (2500) and country taxes (5000) and give the result as grand total in output.please help me?Hi,
Activities
1. To create a new user-defined function, in the data-flow editor, choose Create New Function (This
graphic is explained in the accompanying text), which is located on the lower left-hand side of the
screen. In the menu, choose Simple Function or Advanced Function.
2. In the window that appears, specify the attributes of the new function:
Name
Technical name of the function. The name is displayed in the function chooser and on the data-flow
object.
Description
Description of how the function is used.
Cache
Function type (see above)
Argument Count
In this table, you specify the number of input values the function can process, and name them. All
functions are of type String.
3. In the window that appears, you can create Java source code:
a. You can import Java packages to your methods from the Imports input field, by specifying them
separated by a comma or semi-colon:
You do not need to import the packages java.lang., java.util., java.io., and java.lang.reflect. since
all message mappings require these packages and therefore import them. You should be able to
access standard JDK and J2EE packages of the SAP Web Application Server by simply specifying the
package under Import. In other words, you do not have to import it as an archive into the Integration
Repository. You can also access classes of the SAP XML Toolkit, the SAP Java Connector, and the
SAP Logging Service (see also: Runtime Environment (Java-Mappings)).
In addition to the standard packages, you can also specify Java packages that you have imported as
archives and that are located in the same, or in an underlying software component version as the
message mapping.
b. Create your Java source text in the editor window or copy source text from another editor.
4. Confirm with Save and Close.
5. User-defined functions are limited to the message mapping in which you created the function. To
save the new function, save the message mapping.
6. To test the function, use the test environment.
The new function is now visible in the User-Defined function category. When you select this category,
a corresponding button is displayed in the function chooser pushbutton bar. To edit, delete, or add the
function to the data-flow editor, choose the arrow next to the button and select from the list box
displayed.
Regards
Seshagiri -
Using a SQL user-defined function in Crystal Reports XI
Post Author: JoannKarp
CA Forum: Formula
Is it possible to use a user defined function in SQL and use this in multiple Crystal reports?
JoannKarpSELECT COALESCE(ufn_GetAddressByBusinessEntityIDandAddressTypeID(table1.BusinessEntityID,712),ufn_GetAddressByBusinessEntityIDandAddressTypeID(table1.BusinessEntityID,712)) AS Zipcode
FROM table1
Nope. This is two function calls. coalesce(a, b, c, ...) is just syntatic sugar for
CASE WHEN a IS NOT NULL THEN a
WHEN b IS NOT NULL THEN b
WHEN c IS NOT NULL THEN c
END
But if you use isnull it's a different matter. (But isnull() permits two arguments.)
Erland Sommarskog, SQL Server MVP, [email protected] -
Error in User defined function for dynamic file naming
Hi,
While creating User Defined function with this following code for dynamic fieldname
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
String ourSourceFileName = conf.get(key);
return ourSourceFileName;
which options should i select for cache Value, Context, Queue
for Augument , what name shd i mention.
Regards,
VarunHi Varun,
I guess I have answered a similar question just a few minutes before on very similar post from you. Just pasting the same here .................
Are you trying to access the ASMA values from the SOAP header of the XI message for the source file name?
First of all you need to Set the Sender File Adapter for Set ASMA and then file name. So it will work fine when you actually run the scenario end to end.
But in the mapping tool when you test the mapping - there is not message header updated with the actual source filename - and whenever you try to read the FileName attribute in the message header from the UDF - it cannot find the object and returns a NullPointerException.
I would suuggest for your mapping tool testing to be successful, have a check in the java code for null values,
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
String ourSourceFileName = conf.get(key);
If (ourSourceFileName != null)
Return ourSourceFileName;
Return "NoFileName.txt";
Let us know if this works.
Regards,
Suddha -
Making a User Defined Function for Mapping in XI
Hi folks..
I knw how to make User defined Function in XI,
i was wondering about can we have any room for making a function for Initializing Purpose, which intializes few fields in source Message to some Global variables, and later we can use these Global Variables to set the the Target field
My Doubt is this...
i have Source & target Structure like this Structure like this....
SOURCE TEST DATA IS...
Parentsrc
child1src a
child2src b
child3src c
child4src hi
child4src hello
child4src hey
(On the basis of instances of child4src the istance of the Parentdst will be generated)
TARGET DATA SHUD BE LIKE THIS....
Parentdst
child1dst a
child2dst b
child3dst c
child4dst hi
Parentdst
child1dst a
child2dst b
child3dst c
child4dst hello
Parentdst
child1dst a
child2dst b
child3dst c
child4dst heyHi
You have to modify your target structure:
<b>Source</b>
Parentsrc
child1src a
child2src b
child3src c
child4src hi
child4src hello
child4src hey
<b>Target</b>
<b>root</b>
Parentdst
child1dst a
child2dst b
child3dst c
child4dst hi
Parentdst
child1dst a
child2dst b
child3dst c
child4dst hello
Parentdst
child1dst a
child2dst b
child3dst c
child4dst hey
<b></root></b>
Now map like this:
child4src -
Parentdst
child1src--|
child4src--| UseOneAsMany -- Splitbyvalue --- child1dst
child4src--|
child2src--|
child4src--|UseOneAsMany --Splitbyvalue --- child2dst
child4src--|
child3src--|
child4src--| UseOneAsMany -- Splitbyvalue --- child3dst
child4src--|
child4src -- SplitbyValue -- child4src.
Regards
Suraj -
Java user-defined function for mapping a complex structure
All,
Hope one of you can help me with this. I have a structure with over 15 fields and would like to concatenate all the fields into one target field and while I do this, I need to ensure that each field is padded with blanks as defined the data type. Can one of tell me if this is possible with a java user-defined function and if so, what type of logic is needed.
Input_MT
Field_1 string len=10 "need"
Field_2 string len=6 "java"
Field_3 string len=7 "help"
Field_4 string len=8 "asap"
etc,
Output_MT
DataOut string "need java help asap "
(for some reason the exact spaces in between the words disappear in my Preview message)
I have several fields in the input mt and therefore I find graphical mapping using concatenate and my own user defined function padWithSpace too messy.
Thank you for you help.Hi,
If your final req is to write all these fields next each other in a file, you can configure this in receiver file adapter by specifying the fixed length for each field.
If you want the padded string as your MM o/p, you can create a simple user defined funtion with 15 fields as input and 15 constants for their lengths, and find out the length of the each string and pad it with required no of spaces.
int max_len = 10;
int actual_len;
actual_len = a.lengh();
for(int i=0; i < (max_len-actual_len; i++)
a = a.append(" ");
return a;
praveen -
Need user define function for the message mapping structure
Hi All
I am new to XI.I need the java code for Udf for the following structure.
i have two input and one out put as error.
order_nos----
user define function-------error message
order_details----
num of orders---- i.e. 10
order_details------ i.e. whole order records details
1.i have to compare the 1st input with 2nd one means if the ist input contain 10 means there are 10 orders in 2nd input.if not then i have to give error message .
2.then i have to check the fields in 2nd input has no null values.if no values the output will be error message.
Edited by: Rohit Kumar on Dec 18, 2008 4:43 PMHi Sudhir
thanks for your continues help.i need some help so that i can fulfill my requirmrnt
this is my message mapping xml.
<?xml version="1.0" encoding="UTF-8" ?>
- <ns0:methodCall_MT xmlns:ns0="http://www.dfdsf">
<num_orders>10</num_orders>
<test_mode />
- <order_x> ( which is order details)
<order_id>t78tyu8t</order_id>
<order_date />
<mfg_id />
<catalog_id />
<first_name />
<last_name />
<recipient />
<message />
<address1 />
<address2 />
<address3 />
<city />
<state />
<zip />
<country />
<country_code />
<phone />
<subtotal />
<tax />
<shipping />
<total />
<shopatron_total />
<fulfiller_total />
<shipment_id />
- <additional_info>
<in_store_pickup />
- <express_shipping>
<express_shipping_flag />
<express_shipping_text />
<express_shipping_arrival />
</express_shipping>
</additional_info>
- <discount>
<discount_description />
<discount_percentage />
<discount_total />
</discount>
<lang_id />
<currency_id />
<packing_list />
<num_items />
- <items>
<item_id />
<quantity />
<price />
<part_number />
<fulfiller_total />
<shopatron_total />
- <options>
<option_x />
</options>
</items>
</order_x>
</ns0:methodCall_MT>
this my mapping where under one element number elements are there.when i am excuting the code to check the null value its always giving the there is null val but i have filled evrything.its due to first its checking the order_x and which will be always null.uder this all data will be filled.please suggest what to change in the udf.the code given by u is working fine but its failing in this scenario because it getting the parent filed is null. -
UDF(user defined function) for standard date transformate function,
Hi All,
Hope you are doing good !!!
I have a requirement where i need to write an UDF(user defined function). Please help me in writing the code for below logic-
I am getting my Input as 111213 where 11 denotes HH 12 denotes mm 13 denotes ss. I need ti insert : between hh:mm:ss
Please help me achieving a code for below logic
I have used standard date transformate function, there i am getting spaces as separator in my input resulting an errors i am sending without spaces.
Regards,
Vijay Kumar.Hello,
I am getting my Input as 111213 where 11 denotes HH 12 denotes mm 13 denotes ss. I need ti insert : between hh:mm:ss
Please help me achieving a code for below logic
I have used standard date transformate function, there i am getting spaces as separator in my input resulting an errors i am sending without spaces.
What do you mean by spaces? Can you try the mapping below?
inputDate -> replaceString -> dateTrans(inputDateFormat: HHmmss outputDateFormat: HH:mm:ss) -> target
Constant: -> /
Constant:-> /
Hope this helps,
Mark -
User define function for SQL Server
Hi all,
it's possible to write an UDF for SQL Server in Java?
I know that it is possible with DB2 and Oracle.
My main goal is to write an UDF that works well with all of these DB.
Thank'sHi all,
it's possible to write an UDF for SQL Server in
Java?Okay, JDBC can talk to any database (given you have a driver implementation). Also, JDBC is like a conduit. So the real question is likely, will your database let you do this?
I know that it is possible with DB2 and Oracle.
My main goal is to write an UDF that works well with
all of these DB.I also don't know what a UDF is, but if you ARE talking about stored procedures, then no, it's not possible. All databases are different. On that note, I typically recommend NEVER using stored procedures unless absolutely necessary.
But it also sounds like you want to do this at runtime. I've seen systems that do this (bind stored procs at runtime dynamically) and it's a freakin nightmare! So be careful. -
User defined function for removing wrong data ... !!!
Hi Experts,
I'm having a source data like this
1001 545 6895895 1584 654 5478
1002 525 6895895 5084 654 5472
1003 535 6895895 0584 654 5478
1004 545 6895895 5184 654 5478 4XXX &^%% 1004 545
1005 555 6895895 5824 654 5478 8547 0000 522# ##00
1006 565 6895895 5844 654 5478
1007 575 6895895 5845 654 5478
1008 585 6895895 5846 654 5478
Some times I get a wrong data in my source file (above 4 and 5 th rows) .
My requirement is to remove these kind of wrong lines(entire lines to be removed) whenever XI reads the data. For this what is the standard function i can use in mapping. I thought I can create my own function to remove such kind of false data lines. It would be helpful for me if some body provide me the java code.
Thanks very much.Hi Swarna,
1001 545 6895895 1584 654 5478
1002 525 6895895 5084 654 5472
1003 535 6895895 0584 654 5478
1004 545 6895895 5184 654 5478 4XXX &^%% 1004 545
1005 555 6895895 5824 654 5478 8547 0000 522# ##00
1006 565 6895895 5844 654 5478
1007 575 6895895 5845 654 5478
1008 585 6895895 5846 654 5478
is it the .txt file you are keeping. Or the input in your mapping.
You just Tell your requirement properly.
If it is input in Mapping. Map like this
input ----->subtring( starting position 0 andNumber of characters 30) ---->Output
substring is a standard Text function.
Thanks. -
Error in User defined function for jdbc lookup
I am getting th error as follows "Method TestJDBCAPI$ with 2 arguments not found in class com.sap.xi.tf._JLU_MM_" when i a m testing in message mapping
//write your code here
String Query = " ";
Channel channel = null;
DataBaseAccessor accessor = null;
DataBaseResult resultSet = null;
//Build the Query String
Query = "Select role from login where username = " + username[0] + " and password = " + password[0] ;
try{
//Determine a channel as created in the configuration
channel = LookupService.getChannel("JLU_BUSS","JLU_chan");
// Get a system accessor for a channel.As the call is being made to the database an database accessor is obtained.
accessor = LookupService.getDataBaseAccessor(channel);
// Execute the Query and get the values in the resultSet.
resultSet = accessor.execute(Query);
for(Iterator rows = resultSet.getRows();rows.hasNext();)
Map rowMap = (Map)rows.next();
result.addValue((String)rowMap.get("role"));
catch(Exception ex){
result.addValue(ex.getMessage());
Above is my udf.My logic is that for the given username and password it should select the corresponding role from the database.
Maybe you are looking for
-
Hello, I've posted a project at some commercial sites to find someone to help me doing it, but couldn't find any. I thought that it might be impossible doing this type of projects. The following is what was posted expecting someone could help/lead me
-
FTI_TR_POSITIONS with LDB_PROCESS
Hello everybody I am trying to get data ouf of the LDB FTI_TR_POSITIONS but all I get is a runtime error: LDB_SELECTIONS_NOT_ACCEPTED what am I doing wrong? DATA callback LIKE ldbcb OCCURS 0 WITH HEADER LINE. DATA seltab LIKE rsparams OCCURS 0
-
Bug Report, spamassasin score is not set properly
through server.app . it is shifted by 4 points. If I set 36, the actual setting is 4.
-
Updating IOS Failed and stuck on recovery mode
i'd tried to update my iphone 4 ios 7.0.4 to 7.1, but it gave error and stuck on recovery mode i tried to restore it to previous ios but failed again, anyone have idea what i should do to restore iphone to previous ios or ios 7.1 ...
-
I purchased a 20" iMac in the fall of 2005. The computer would turn off at random times without warning. At first it would only happen once a week or so, but it started happening more and more frequently until it finally got to where the computer wou