SQL select query help
I know this is a SQL question but I can't seem to figure this
out. The below SQL query will run without error however it will
output the company_name for every row which I dont want.
So basicly it currently outputs like this now:
company_name - apple
company_name - dell
BUT want it to output like this:
company_name - apple,dell
MY CURRENT QUERY:
SELECT d.disputeid, t.company_name, t.tlid, t.type,
l.description
FROM member_disputes d, member_tradeline t, letters l
WHERE d.fk_tlid = t.tlid
AND d.fk_letterid = l.letterid
AND d.fk_memberid = #GetAuthUser()#
Any help much appreciated
quote:
Originally posted by:
LionelR
I know this is a SQL question but I can't seem to figure this
out. The below SQL query will run without error however it will
output the company_name for every row which I dont want.
So basicly it currently outputs like this now:
company_name - apple
company_name - dell
BUT want it to output like this:
company_name - apple,dell
MY CURRENT QUERY:
SELECT d.disputeid, t.company_name, t.tlid, t.type,
l.description
FROM member_disputes d, member_tradeline t, letters l
WHERE d.fk_tlid = t.tlid
AND d.fk_letterid = l.letterid
AND d.fk_memberid = #GetAuthUser()#
Any help much appreciated
Suddenly a light bulb illuminated over my head.
<cfouput>
company_name - #ValueList(yourquery.company_name)#
</cfoutput>
If that doesn't work, either change this, "t.company_name" to
"company_name" or "t.company_name as company_name".
I can't believe none of us thought of this earlier.
Similar Messages
-
SQL SELECT Query Help ..Please its very Urgent!!
Hi All,
I am having Oracle Database whice is storing 1000's of records daily.
I need to select some information based on date and time.
I am having two coloumns for Date and time. The first column(testDate) of type Date stores date as MM/DD/YY format and the second column(testTime)of type Numeric stores the time in seconds.
The Example data is :
testDate ------=-- testTime
11/12/2002 --- 35000
11/12/2002 --- 43000
11/12/2002 --- 45000
11/12/2002 --- 75000
11/13/2002 --- 2000
11/13/2002 --- 3500
11/13/2002 --- 4300
11/13/2002 --- 9800
11/13/2002 --- 23000
11/14/2002 --- 5000
11/14/2002 --- 10000
11/14/2002 --- 15000
How can i write a SELECT Query to get the records of specific date and seconds to next day specific date and seconds.I mean i want all the records between 11/12/2002 --- 43000 seconds to 11/14/2002 --- 1000 seconds.
If any one helps me in this regard iam very thank full to them.Its very urgent for me.
ThanksHi m7nra,
I used the query as
SELECT * FROM table
WHERE testDate + (testTime/(24*60*60)) BETWEEN TO_DATE('MM/DD/YYYY','12.11.2002') AND TO_DATE('MM/DD/YYYY','14.11.2002')
its giving DATE FORMAT NOT RECOGNIZED error.
The Example data is :
testDate ------=-- testTime
11/12/2002 --- 35000
11/12/2002 --- 43000
11/12/2002 --- 45000
11/12/2002 --- 75000
11/13/2002 --- 2000
11/13/2002 --- 3500
11/13/2002 --- 4300
11/13/2002 --- 9800
11/13/2002 --- 23000
11/14/2002 --- 5000
11/14/2002 --- 10000
11/14/2002 --- 15000
infact i need all the records between 11/12/2002 --- 43000 seconds to 11/14/2002 --- 1000 seconds.
Please help me to find a full query beacuse iam very new to Oracle.
Thanks,
S R Mannava -
hi friends,
i've a view called "risk_efforts" with fields user_id,user_name,wknd_dt,week_day,prod_efforts,unprod_efforts.
Name Type
ROW_ID NUMBER
USER_ID VARCHAR2(14)
USER_NAME VARCHAR2(50)
WKND_DT VARCHAR2(8)
WEEK_DAY VARCHAR2(250)
PROD_EFFORTS NUMBER
UNPROD_EFFORTS NUMBER
data is like this:
when there is some data in prod_efforts, unprod_efforts will be null
when there is some data in unprod_efforts, prod_efforts will be null
for example:
USER_ID USER_NAME WKND_DT WEEK_DAY PROD_EFFORTS UNPROD_EFFORTS
G666999 GTest 20100403 TUE null 3
G666999 GTest 20100403 TUE 14 null
now i want to combine these 2 rows into 1 row i.e o/p should be like this
USER_ID USER_NAME WKND_DT WEEK_DAY PROD_EFFORTS UNPROD_EFFORTS
G666999 GTest 20100403 TUE 14 3
i've tried all combinations but couldn't get the query. Please help me with the exact SQL select query.
thanks,
GirishWelcome to the forum.
First read this:
Urgency in online postings
Secondly, it's always helpful to provide the following:
1. Oracle version (SELECT * FROM V$VERSION)
2. Sample data in the form of CREATE / INSERT statements.
3. Expected output
4. Explanation of expected output (A.K.A. "business logic")
5. Use \ tags for #2 and #3. See FAQ (Link on top right side) for details.
You have provided #3 and #4. However with no usable form of sample data forum members will often not respond as quickly as they could if you provided #2.
I'm just wagering a guess here but what about this:SELECT ROW_ID
, USER_ID
, WKND_DT
, WEEK_DAY
, MAX(PROD_EFFORTS) AS PROD_EFFORTS
, MAX(UNPROD_EFFORTS) AS UNPROD_EFFORTS
FROM RISK_EFFORTS
GROUP BY ROW_ID
, USER_ID
, WKND_DT
, WEEK_DAY -
How to capture all the rows returned from a sql select query in CPO
Hi,
I am executing an sql select query which returns multiple rows. I need to capture the values of each row to specific variables. How do I proceed.
Thanks,
SwatiThe select activities ("Select from Oracle," Select from SQL Server," etc.) against database already return tables. Use one of the database adapters to do your select, and it will already be in a table form. Just put your query in the select and identify the columns in your result table. The online help or the database adapter guides in the product documentation can help.
-
Oracle SQL Select query takes long time than expected.
Hi,
I am facing a problem in SQL select query statement. There is a long time taken in select query from the Database.
The query is as follows.
select /*+rule */ f1.id,f1.fdn,p1.attr_name,p1.attr_value from fdnmappingtable f1,parametertable p1 where p1.id = f1.id and ((f1.object_type ='ne_sub_type.780' )) and ( (f1.id in(select id from fdnmappingtable where fdn like '0=#1#/14=#S0058-3#/17=#S0058-3#/18=#1#/780=#5#%')))order by f1.id asc
This query is taking more than 4 seconds to get the results in a system where the DB is running for more than 1 month.
The same query is taking very few milliseconds (50-100ms) in a system where the DB is freshly installed and the data in the tables are same in both the systems.
Kindly advice what is going wrong??
Regards,
PurushothamSQL> @/alcatel/omc1/data/query.sql
2 ;
9 rows selected.
Execution Plan
Plan hash value: 3745571015
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | SORT ORDER BY | |
| 2 | NESTED LOOPS | |
| 3 | NESTED LOOPS | |
| 4 | TABLE ACCESS FULL | PARAMETERTABLE |
|* 5 | TABLE ACCESS BY INDEX ROWID| FDNMAPPINGTABLE |
|* 6 | INDEX UNIQUE SCAN | PRIMARY_KY_FDNMAPPINGTABLE |
|* 7 | TABLE ACCESS BY INDEX ROWID | FDNMAPPINGTABLE |
|* 8 | INDEX UNIQUE SCAN | PRIMARY_KY_FDNMAPPINGTABLE |
Predicate Information (identified by operation id):
5 - filter("F1"."OBJECT_TYPE"='ne_sub_type.780')
6 - access("P1"."ID"="F1"."ID")
7 - filter("FDN" LIKE '0=#1#/14=#S0058-3#/17=#S0058-3#/18=#1#/780=#5#
8 - access("F1"."ID"="ID")
Note
- rule based optimizer used (consider using cbo)
Statistics
0 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
0 bytes sent via SQL*Net to client
0 bytes received via SQL*Net from client
0 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
9 rows processed
SQL> -
Reg: SQL select Query in BPEL process flow
<p>
Hi,
I am suppose to execute a SQL select query (in BPEL Process flow) as mention below in JDeveloper using Database adapter.
</p>
<p>
SELECT LENGTH, WIDTH, HEIGHT, WEIGHT,
</p>
<p>
LENGTH*WIDTH* HEIGHT AS ITEM_CUBE
</p>
<p>
FROM CUBE
</p>
<p>
WHERE ITEM= <xyz>
</p>
<p>
AND OBJECT= (SELECT CASE_NAME FROM CUBE_SUPPLIER WHERE ITEM=<xyz> AND SUPP_IND = ‘Y')
<strong>Now my question is:
1.</strong> What does this "*" refer to in the query and how can I retrieve the value of LENGTH*WIDTH* HEIGHT from the query where LENGTH,WIDTH and HEIGHT are the individual field in the table.
2.What does this " AS" refer to? If " ITEM_CUBE " is the alies for the table name "ITEM" to retrieve the value, then query shoud be evaluated as
</p>
<p>
SELECT LENGTH, WIDTH, HEIGHT, WEIGHT,
</p>
<p>
LENGTH*WIDTH* HEIGHT AS ITEM_CUBE
</p>
<p>
FROM CUBE
</p>
<p>
WHERE <strong>ITEM_CUBE.ITEM</strong>= <xyz>
</p>
<p>
AND <strong>ITEM_CUBE.OBJECT</strong>= (SELECT CASE_NAME FROM CUBE_SUPPLIER WHERE ITEM=<xyz> AND SUPP_IND = ‘Y')
Is my assumption correct?
Please suggest asap.
Thanks...
</p>
<p>
</p>Hi
Thank for your reply!
I have a nested select query which performs on two different table as shown below:
<p>
SELECT LENGTH, WIDTH, HEIGHT, WEIGHT,
</p>
<p>
LENGTH*WIDTH* HEIGHT AS ITEM_CUBE
</p>
<p>
FROM CUBE
</p>
<p>
WHERE ITEM= <abc>
</p>
<p>
AND OBJECT= (SELECT NAME FROM SUPPLIER WHERE ITEM=<Item> AND SUPP_IND = ‘Y')
I am using DB adapter of Oracle JDeveloper in BPEL process flow, where I can able to select only one master table in DB adapter say SUPPLIER and its attributes at a time.But as per my requirment I need to select both the table (CUBE and SUPPLIER) in a single adapter to execute my query.
It can be achievable by using two DB adapter , One to execute the nested query and another to execute the main qyery considering value of nested query as a parameter.But I want to achieve it by using a single one.
Am I correct with my concept?
Please suggest how to get it ?
</p>
Edited by: user10259700 on Oct 23, 2008 12:17 AM -
Need help for SQL SELECT query to fetch XML records from Oracle tables having CLOB field
Hello,
I have a scenario wherein i need to fetch records from several oracle tables having CLOB fields(which is holding XML) and then merge them logically to form a hierarchy XML. All these tables are related with PK-FK relationship. This XML hierarchy is having 'OP' as top-most root node and ‘DE’ as it’s bottom-most node with One-To-Many relationship. Hence, Each OP can have multiple GM, Each GM can have multiple DM and so on.
Table structures are mentioned below:
OP:
Name Null Type
OP_NBR NOT NULL NUMBER(4) (Primary Key)
OP_DESC VARCHAR2(50)
OP_PAYLOD_XML CLOB
GM:
Name Null Type
GM_NBR NOT NULL NUMBER(4) (Primary Key)
GM_DESC VARCHAR2(40)
OP_NBR NOT NULL NUMBER(4) (Foreign Key)
GM_PAYLOD_XML CLOB
DM:
Name Null Type
DM_NBR NOT NULL NUMBER(4) (Primary Key)
DM_DESC VARCHAR2(40)
GM_NBR NOT NULL NUMBER(4) (Foreign Key)
DM_PAYLOD_XML CLOB
DE:
Name Null Type
DE_NBR NOT NULL NUMBER(4) (Primary Key)
DE_DESC NOT NULL VARCHAR2(40)
DM_NBR NOT NULL NUMBER(4) (Foreign Key)
DE_PAYLOD_XML CLOB
+++++++++++++++++++++++++++++++++++++++++++++++++++++
SELECT
j.op_nbr||'||'||j.op_desc||'||'||j.op_paylod_xml AS op_paylod_xml,
i.gm_nbr||'||'||i.gm_desc||'||'||i.gm_paylod_xml AS gm_paylod_xml,
h.dm_nbr||'||'||h.dm_desc||'||'||h.dm_paylod_xml AS dm_paylod_xml,
g.de_nbr||'||'||g.de_desc||'||'||g.de_paylod_xml AS de_paylod_xml,
FROM
DE g, DM h, GM i, OP j
WHERE
h.dm_nbr = g.dm_nbr(+) and
i.gm_nbr = h.gm_nbr(+) and
j.op_nbr = i.op_nbr(+)
+++++++++++++++++++++++++++++++++++++++++++++++++++++
I am using above SQL select statement for fetching the XML records and this gives me all related xmls for each entity in a single record(OP, GM, DM. DE). Output of this SQL query is as below:
Current O/P:
<resultSet>
<Record1>
<OP_PAYLOD_XML1>
<GM_PAYLOD_XML1>
<DM_PAYLOD_XML1>
<DE_PAYLOD_XML1>
</Record1>
<Record2>
<OP_PAYLOD_XML2>
<GM_PAYLOD_XML2>
<DM_PAYLOD_XML2>
<DE_PAYLOD_XML2>
</Record2>
<RecordN>
<OP_PAYLOD_XMLN>
<GM_PAYLOD_XMLN>
<DM_PAYLOD_XMLN>
<DE_PAYLOD_XMLN>
</RecordN>
</resultSet>
Now i want to change my SQL query so that i get following output structure:
<resultSet>
<Record>
<OP_PAYLOD_XML1>
<GM_PAYLOD_XML1>
<GM_PAYLOD_XML2> .......
<GM_PAYLOD_XMLN>
<DM_PAYLOD_XML1>
<DM_PAYLOD_XML2> .......
<DM_PAYLOD_XMLN>
<DE_PAYLOD_XML1>
<DE_PAYLOD_XML2> .......
<DE_PAYLOD_XMLN>
</Record>
<Record>
<OP_PAYLOD_XML2>
<GM_PAYLOD_XML1'>
<GM_PAYLOD_XML2'> .......
<GM_PAYLOD_XMLN'>
<DM_PAYLOD_XML1'>
<DM_PAYLOD_XML2'> .......
<DM_PAYLOD_XMLN'>
<DE_PAYLOD_XML1'>
<DE_PAYLOD_XML2'> .......
<DE_PAYLOD_XMLN'>
</Record>
<resultSet>
Appreciate your help in this regard!Hi,
A few questions :
How's your first query supposed to give you an XML output like you show ?
Is there something you're not telling us?
What's the content of, for example, <OP_PAYLOD_XML1> ?
I don't think it's a good idea to embed the node level in the tag name, it would make much sense to expose that as an attribute.
What's the db version BTW? -
I need help with a SELECT query - help!
Hello, I need help with a select statement.
I have a table with 2 fields as shown below
Name | Type
John | 1
John | 2
John | 3
Paul | 1
Paul | 2
Paul | 3
Mark | 1
Mark | 2
I need a query that returns everything where the name has type 1 or 2 but not type 3. So in the example above the qery should bring back all the "Mark" records.
Thanks,
IanOr, if the types are sequential from 1 upwards you could simply do:-
SQL> create table t as
2 select 'John' as name, 1 as type from dual union
3 select 'John',2 from dual union
4 select 'John',3 from dual union
5 select 'Paul',1 from dual union
6 select 'Paul',2 from dual union
7 select 'Paul',3 from dual union
8 select 'Paul',4 from dual union
9 select 'Mark',1 from dual union
10 select 'Mark',2 from dual;
Table created.
SQL> select name
2 from t
3 group by name
4 having count(*) <= 2;
NAME
Mark
SQL>Or another alternative if they aren't sequential:
SQL> ed
Wrote file afiedt.buf
1 select name from (
2 select name, max(type) t
3 from t
4 group by name
5 )
6* where t < 3
SQL> /
NAME
Mark
SQL>Message was edited by:
blushadow -
Splitting complex SQL select query in to independent sequence of small queries.
=====================
Problem Description
=====================
Consider I have the following table structures with approx no. of records in each.
Employee Table
emp_id:
name :
dept_id:
#Approx no. of rows in the table: 1 million
Department Table
dept_id:
dept_name:
#Approx no. of rows in the table: 1 Hundred
=====================
Target Result: is to get the employee name who's id is '100' and the department name he belongs to.
=====================
=====================
Ideal Query : Consider it as COMPLEX QUERY
=====================
select emp.name,dept.name
from employee emp, department dept
where
emp.dept_id = dept.dept_id
and
emp.emp_id = '100';
=====================
Optimized Independent Queries : Consider it as SIMPLE QUERIES
=====================
Now i want to split the above query in to two independent queries.
Query #1
select emp.name
from employee emp
where emp.id = '100';
:: Returns one row as emp.id is unique.
Query #2
select dept.dept_id, dept.name
from department dept
:: Returns 100 rows as there are only 100 rows.
=====================
Need Your Suggestions Here
=====================
If you compare the COMPLEX QUERY against SIMPLE QUERIES, i prefer to use SIMPLE QUERIES, get the results
and compare department id against two results programmatically and then get the final result. I know the database
query engines will do similar query optimization like dividing COMPLEX QUERY to SIMPLE QUERIES and run them
in some specific order to get results faster.
Now is it possible to get the exact sub queries which the database query engine (ANY database query engine) generates
out of complex select query? I mean i don't want to do the splitting manually but i still need those sub-queries.
Can any one help me out please?Why on earth would you want to keep the queries separate? As Tom Kyte would say, databases are born to join!
Providing emp_id has an index on it (and it should, assuming it's the table's primary key!) your "complex" (or, what I would call simple*), it'll most likely be faster than doing it separately. You've not got the context switches for one thing.
If you want to see what the optimizer does with your query, you should run a trace on the query. An explain plan would give you an idea of what it's going to do, too.
In general, doing something like this procedurally is going to almost certainly be worse than doing it in SQL (and by worse, I mean slower).
* I'd hate to see what you'd call some of the monster queries I work with, if you think that query is complex! -
Select query help for Sales order
Hi Experts,
I have to write a select query to fetch sales orders which are open along with the quantities which are open ( not delivered). What would the best approach for this?
Any help is appreciated. Expecting code samples....Thanks
Thanks
Rickyhi,
do like this,
write a select query for vbak and vbuk as follows.
delivery status field is <b>lfstk</b> from <b>vbuk</b>,
and relation field is <b>vbeln</b> from the both the tables.
reward points if useful,
regards,
seshu. -
SQL select query having more than 1000 values in 'IN' clause of predicate.
Hi,
We are executing a select query from a table and showing it through a front end screen. When the count of values given in the 'IN' clause of predicate are exceeding 1000 , it is throwing error.
eg. select * from Employees where emp.Id. in('111',123','121','3232',........1001 Ids)
We are using Oracle version 10.2.0.
Please suggest how to tackle such issue.
Regards,
Naveen Kumar.C.
Edited by: Naveen Kumar C on Aug 30, 2008 10:01 PMUse a nested table:
create or replace type numbertype
as object
(nr number(20,10) )
create or replace type number_table
as table of numbertype
create or replace procedure tableselect
( p_numbers in number_table
, p_ref_result out sys_refcursor)
is
begin
open p_ref_result for
select *
{noformat} from employees
, (select /*+ cardinality(tab 10) */ tab.nr
from table(p_numbers) tab) tbnrs
where id = tbnrs.nr;
end;
/{noformat}
Using nested tables will reduce the amount of parsing because the sql statement uses binded variables! The cardinality hint causes Oracle to use the index on employees.id. -
Hi friends ,
i have a table like
table ABC(
essay_id number(2),
line_id number(3),
line_t varchar2(100)
and here my table data goes..
essay_id line_id line_t
1 1 abc
1 2 def
1 3 ghi
2 1 klm
2 2 nop
here a single essay is stored as multiple lines in diff rows with same essay id and diff line id..
here i want to concatenate all the lines associated to same essay id..
for essay_id 1 ..output should be abcdefghi
for essay_id 2.. output should be klmnop
i did this with the help of cursor..
bt my question is ..
can we achieve this with a single select query...?
is it possible..
Please help me on this..
Thanks in advance
Regards,
JeyanthiWhat version of Oracle are you using?
Tim Hall has a page on the various string aggregation techniques that are available. The approaches that are available will depend heavily on the Oracle version.
Justin -
Adding columns in a open sql select query
Hi Everyone,
I would like to add the values of n columns of a table and place them in one column, how can i achieve this in open sql.
In oracle native sql the query will look like this:
Select col1 + col2 + col3 AS col
from tab1;
On a similar grounds, i would like to concatenate the contents of n columns into one column. The oracle native sql will look like this:
select col1 || col2 || col3
from tab1;
How can I do this in Open SQL.
Thanks in advance.
Prabhu.Hi Prabhu,
I'm afraid what you're trying to do is not possible using the Open SQL. You will have to get the data from all the three columns into an internal table and then write your own logic to accomplish the same.
data : begin of itab occurs 0,
col1 type i,
col2 type i,
col3 type i,
col4 type i,
end of itab.
field-symbols: <fs_itab> like line of itab.
SELECT COL1
COL2
COL3
INTO TABLE ITAB.
LOOP AT ITAB assigning <fs_itab>.
<fs_itab>-col4 = <fs_itab>-col1 + <fs_itab>-col2 + <fs_itab>-col3.
ENDLOOP.
Regards,
Anand Mandalika. -
How to execute SQL SELECT QUERY using VB in FDMEE
Hi Experts,
We are struggling with converting some of the FDM VB Scripts to FDMEE. While in FDM it was possible to simply create a RS Object, it doesnt seem to work in FDMEE. Can you please help me with some code samples that will help me execute SELECT Query and taking the output of the Query to Appropriate Variables.
Also it seems that the Accelerators in erstwhile FDM are no longer available in FDMEE. So we have to re-write our entire code all over again
ThanksThere are plenty of code examples in the FDMEE guide and al of the API methods are documented for both Visual Basic and Jython. Personally, I think, if you are having to re-factor your code because you have migrated from FDM to FDMEE I would look at doing it in Jython not the VB API. Jython is the scripting language that will be the mainstream choice going forward and athough the VB API is present to ease the move from FDM to FDMEE I wouldn't be sure how long it will be supported and you therefore may have to perform another code migration not too far down the line. Jython is a very powerful and easy to use if you spend a bit of time familiarizing yourself with the syntax and also opens up the opportunity to leverage the myriad of Java classes out there. Also all of your import scripts have to be in Jython with no VB alternative.
One other piece of advice is do not try and replicate allyour old code line for line. Look at what you were trying to achieve, assess whether it is still relevant or can it be faciliated by the new extended out of the box constructs and functionality FDMEE offers over classic FDM. -
Hi All,
I have below table
IT_Terms_First_Date
IT_Terms_Last_Date
DI_Debt_Num
IT_Terms_Seq_Num
200501
201101
1000
131
200512
203412
1001
131
200503
204209
1003
131
200507
201001
1004
131
200510
202710
1005
131
200506
202412
10020
131
197910
198310
257000
101
198009
202909
298000
101
198101
202908
298000
103
198105
202910
298000
104
199109
201309
578000
101
199204
201110
600000
101
198009
201010
298010
101
198105
204010
298010
104
201011
202909
298010
103
I need to check whether my DI_Debt_Num having Ovelaping or not for each DI_Debt_Num,
at this moment we are checking each row in loop and usnig function
exec @Overlap1=[DffMonths] @ITtermsLD,@ITtermsNextFD
exec @Overlap2=[DffMonths] @ITtermsFD,@ITtermsNextFD
exec @Overlap3=[DffMonths] @ITtermsFD,@ITtermsNextLD
if(@Overlap1>0 and (@Overlap2<=0 OR @Overlap3<0))
BEGIN
SET @CheckOverlap=1
END
here @ITtermsLD is the IT_Terms_Last_Date of the current DI_Debt_Num,@ITtermsNextFD is the IT_Terms_First_Date of the next row. @ITtermsFD is the IT_Terms_First_Date of the current month
if we consider the 298000 DI_Debt_Num we have 3 IT_Terms_Seq_Num 101,103,104
in this senario we need to check only the first 2 rows from that itself we can identify it is overlapped ,but when we consider the 298010 we need to check all 3 IT_Terms_Seq_Num 101,103,104 if we consider first two rows 101 & 103 it is not overlapped.Then
we have to check first row with 3rd row ie 104 and it is overlapped.We are checking the overlap senario for DI_Debt_Num having multipple IT_Terms_Seq_Num rows
Some situation first row may not be overlapped with other rows .Then we have to check the 2nd row with the next rows in the same way we are doing for first row
My aim is to covert this looping method to a select query to improve my query performance
Thanks in advance
RoshanPlease post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. You have no idea,
do you? Temporal data should use ISO-8601 formats. You failed again! I will guess that your dates are months. Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior on SQL forums.
>> I have below table <<
This is not a table! Where is the DDL? This picture has no name. Not even a name!! There is no “seq_nbr” in RDBMS; it has to be a “<something in particular>_seq” and there are no duplicates in a sequence.
My guess is that each di_debt_nbr has a sequence within its range. I will call it the “foobar_seq” for lack of a name.
My next guess is that your dates are really months and you do not know about using a report period table. This idiom gives a name to a range of dates that is common to the entire enterprise.
CREATE TABLE Something_Report_Periods
(something_report_name CHAR(10) NOT NULL PRIMARY KEY
CHECK (something_report_name LIKE <pattern>),
something_report_start_date DATE NOT NULL,
something_report_end_date DATE NOT NULL,
CONSTRAINT date_ordering
CHECK (something_report_start_date <= something_report_end_date),
etc);
These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantages are that it will sort with the ISO-8601
data format required by Standard SQL and it is language independent. The pattern for validation is '[12][0-9][0-9][0-9]-00-00' and '[12][0-9][0-9][0-9]-[01][0-9]-00'
Here is another guess at what you want, if you knew what a table is:
CREATE TABLE DI_Debts
(it_terms_first_date CHAR(10) NOT NULL
REFERENCES Report_Period (month_name),
it_terms_last_date CHAR(10) NOT NULL
REFERENCES Report_Period (month_name),
CHECK (it_terms_first_date <= it_terms_last_date),
di_debt_nbr INTEGER NOT NULL,
foobar_seq INTEGER NOT NULL,
PRIMARY KEY (di_debt_nbr, foobar_seq));
INSERT INTO DI_Debts
VALUES
('2005-01-00', '2011-01-00', 1000, 1),
('2005-12-00', '2034-12-00', 1001, 1),
('2005-03-00', '2042-09-00', 1003, 1),
('2005-07-00', '2010-01-00', 1004, 1),
('2005-10-00', '2027-10-00', 1005, 1),
('2005-06-00', '2024-12-00', 100201, 1),
('1979-10-00', '1983-10-00', 257000, 1),
('1980-09-00', '2029-09-00', 2980001, 1),
('1981-01-00', '2029-08-00', 298000, 1),
('1981-05-00', '2029-10-00', 298000, 2),
('1991-09-00', '2013-09-00', 578000, 1),
('1992-04-00', '2011-10-00', 600000, 1),
('1980-09-00', '2010-10-00', 298010, 1),
('1981-05-00', '2040-10-00', 298010, 2),
('2010-11-00', '2029-09-00', 298010, 3);
I need to check whether my DI_Debt_nbr are overlapping or not for each DI_Debt_nbr,
>> at this moment we are checking each row in loop and using function
exec @Overlap1=[DffMonths] @IttermsLD, @ITtermsNextFD;
exec @Overlap2=[DffMonths] @IttermsFD, @ITtermsNextFD;
exec @Overlap3=[DffMonths] @IttermsFD, @ITtermsNextLD; <<
And you were too rude to post the code for these functions! You write SQL with assembly language flags! We do not do that! We also would use a CASE expression, and not IF-THEN control flow in SQL.
Did you know that ANSI/ISO Standard SQL has a temporal <overlaps predicate>? Notice the code to handle NULLs and the ISO half-open interval model.
(start_date_1 > start_date_2
AND NOT (start_date_1 >= end_date_2
AND end_date_1 >= end_date_2))
OR (start_date_2 > start_date_1
AND NOT (start_date_2 >= end_date_1
AND end_date_2 >= end_date_1))
OR (start_date_1 = start_date_2
AND (end_date_1 <> end_date_2 OR end_date_1 = end_date_2))
I tend to prefer the use of a calendar table. NULLs return an empty set, as above.
EXISTS
((SELECT cal_date FROM Calendar
WHERE cal_date BETWEEN start_date_1 AND end_date_1)
INTERSECT
(SELECT cal_date FROM Calendar
WHERE cal_date BETWEEN start_date_2 AND end_date_2))
--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
-
Java EE tutorial, problem with ANT populating database
Hi! I have followed the instructions in the Java EE tutorial to set up the enviroment but when I execute the command "ant create tables" I get this output: C:\javaeetutorial5\examples\web\bookstore1>ant create-tables Buildfile: build.xml -pre-init: i
-
Can I watch movies without wifi on my iPod touch.
Why can't I watch movies on my iPod touch?
-
Photoshop (and other suite programs) freeze up continually (OSX)
Hey people, I thought I'd ask here before contacting support. I've bought the Adobe Web Premium CS4 package a while back (when they just released CS5) and I'm having huge issues now. Basically all my Adobe applications freeze up directly or do so whe
-
DPM2012SP1 RU3, Symantec SEP 12.1RU3 and IBM x3550M4 BSOD
Hi! Not directly a question, and not even directly a MS/DPM issue, I'm just sharing info if someone happens to run into a similar situation. We had a case where our DPM2012 SP1 RU3, installed on Server 2012 and running on IBM x3550 M4 with all the l
-
HT1918 cannot purchase anything on my iphone
My payment information is correct in the i tunes store on my computer but i cannot access them on my iphone to check or change them. my apple id password is accepted but then does nothing when trying to download or purchase anything??? help