Join rows from 2 tables with non-matching numeric intervals
Hi,
Could you please help me with the following:
I have 2 tables - [Coal_Samples] and [Coal Type] Which I have pasted below. I want to create a single result set that matches as closley as possible [Coal Type].Coal_Type to the interval between [Coal_Samples].mfrom and [Coal_Samples].mto. I would have
[Coal_Samples] as the primary table using a left outer join.
The problem is that the intervals [Coal_Samples].mfrom do not match [Coal Type].mfrom and [Coal_Samples].mto does not match [Coal Type].mto. I want to match the [Coal_Samples] intervals ([Coal_Samples].mfrom-[Coal_Samples].mto) as closely as possible to
the [Coal_Type].Coal_Type field.
I have been able to acheive this in excel using a VLOOKUP statement - but I want to know how to do this with and SQL Script without duplicating the rows based in [Coal_Samples].mfrom-[Coal_Samples].mto interval.
COAL SAMPLES TABLE
ID
mFrom
mTo
SAMPLE NUMBER
AD261
57
57.5
SAMPLE_001
AD261
57.5
58
SAMPLE_002
AD261
58
59
SAMPLE_003
AD261
59
60
SAMPLE_004
AD261
60
61
SAMPLE_005
AD261
61
62
SAMPLE_006
AD261
62
63
SAMPLE_007
AD261
63
64
SAMPLE_008
AD261
64
65
SAMPLE_009
AD261
65
66
SAMPLE_010
AD261
66
67
SAMPLE_011
AD261
67
68
SAMPLE_012
AD261
68
69
SAMPLE_013
AD261
69
70
SAMPLE_014
AD261
70
71
SAMPLE_015
AD261
71
72
SAMPLE_016
AD261
72
73
SAMPLE_017
AD261
73
74
SAMPLE_018
AD261
74
75
SAMPLE_019
AD261
75
76
SAMPLE_020
AD261
76
77
SAMPLE_021
AD261
77
78
SAMPLE_022
AD261
78
79
SAMPLE_023
COAL TYPE TABLE
ID
Type_mFrom
Type__mTo
COAL TYPE
AD261
57
68.6
BROWN COAL
AD261
68.6
75
GREY COAL
AD261
75
78.2
BLACK COAL
AD261
78.2
79.2
BLACK COAL
Example RESULT SET
ID
mFrom
mTo
DOMINANT_COAL TYPE
AD261
57
57.5
BROWN COAL
AD261
57.5
58
BROWN COAL
AD261
58
59
BROWN COAL
AD261
59
60
BROWN COAL
AD261
60
61
BROWN COAL
AD261
61
62
BROWN COAL
AD261
62
63
BROWN COAL
AD261
63
64
BROWN COAL
AD261
64
65
BROWN COAL
AD261
65
66
BROWN COAL
AD261
66
67
BROWN COAL
AD261
67
68
BROWN COAL
AD261
68
69
BROWN COAL
AD261
69
70
GREY COAL
AD261
70
71
GREY COAL
AD261
71
72
GREY COAL
AD261
72
73
GREY COAL
AD261
73
74
GREY COAL
AD261
74
75
GREY COAL
AD261
75
76
BLACK COAL
AD261
75
76
BLACK COAL
AD261
76
77
BLACK COAL
AD261
77
78
BLACK COAL
AD261
78
79
BLACK COAL
Can you please help with the logic of this query?
Thanks very much.
Well, here is the beginning of the solution:
;with cte as (select S.*, T.[Coal Type] as [Dominant_Coal],
case when
T.m_from between S.m_from and S.m_to
AND T.m_to between S.m_from and S.m_to then 0
when T.m_from between S.m_from and S.m_to then t.m_from - S.m_from
else S.m_to - T.m_to end as [Diff]
from Coal_Samples S LEFT JOIN Coal_Type T
ON T.m_from between S.m_from and S.m_to
OR T.m_to between S.m_from and S.m_to),
cte1 as (select *, row_number() over (partition by cte.ID order by Diff) as Rn
from cte)
select * from cte1 where Rn = 1 -- attempt to get best match
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles
Similar Messages
-
Cartesian of data from two tables with no matching columns
Hello,
I was wondering – what’s the best way to create a Cartesian of data from two tables with no matching columns in such a way, so that there will be only a single SQL query generated?
I am thinking about something like:
for $COUNTRY in ns0: COUNTRY ()
for $PROD in ns1:PROD()
return <Results>
<COUNTRY> {fn:data($COUNTRY/COUNTRY_NAME)} </COUNTRY>
<PROD> {fn:data($PROD/PROD_NAME)} </PROD>
</Results>
And the expected result is combination of all COUNTRY_NAMEs with all PROD_NAMEs.
What I’ve noticed when checking query plan is that DSP will execute two queries to have the results – one for COUNTRY_NAME and another one for PROD_NAME. Which in general results in not the best performance ;-)
What I’ve noticed also is that when I add something like:
where COUNTRY_NAME != PROD_NAME
everything is ok and there is only one query created (it's red in the Query plan, but still it's ok from my pov). Still it looks to me more like a workaround, not a real best approach. I may be wrong though...
So the question is – what’s the suggested approach for such queries?
Thanks,
Leszek
Edited by xnts at 11/19/2007 10:54 AMWhich in general results in not the best performanceI disagree. Only for two tables with very few rows, would a single sql statement give better performance.
Suppose there are 10,000 rows in each table - the cross-product will result in 100 million rows. Sounds like a bad idea. For this reason, DSP will not push a cross-product to a database. It will get the rows from each table in separate sql statements (retrieving only 20,000 rows) and then produce the cross-product itself.
If you want to execute sql with cross-products, you can create a sql-statement based dataservice. I recommend against doing so. -
How to compare two rows from two table with different data
how to compare two rows from two table with different data
e.g.
Table 1
ID DESC
1 aaa
2 bbb
3 ccc
Table 2
ID DESC
1 aaa
2 xxx
3 ccc
Result
2Create
table tab1(ID
int ,DE char(10))
Create
table tab2(ID
int ,DE char(10))
Insert
into tab1 Values
(1,'aaa')
Insert
into tab1 Values
(2,'bbb')
Insert
into tab1 Values(3,'ccc')
Insert
into tab1 Values(4,'dfe')
Insert
into tab2 Values
(1,'aaa')
Insert
into tab2 Values
(2,'xx')
Insert
into tab2 Values(3,'ccc')
Insert
into tab2 Values(6,'wdr')
SELECT
tab1.ID,tab2.ID
As T2 from tab1
FULL
join tab2 on tab1.ID
= tab2.ID
WHERE
BINARY_CHECKSUM(tab1.ID,tab1.DE)
<> BINARY_CHECKSUM(tab2.ID,tab2.DE)
OR tab1.ID
IS NULL
OR
tab2.ID IS
NULL
ID column considered as a primary Key
Apart from different record,Above query populate missing record in both tables.
Result Set
ID ID
2 2
4 NULL
NULL 6
ganeshk -
Find rows from a table with no Activities
Hi,
I have a ‘Data’ table in relation with a ‘Note’ table with a date and I would like to find all the rows in the ‘Data’ table that do not have any ‘Note’ taken in the the last 3 months (including no ‘Note’ at all)?
THanks,Hi,
934281 wrote:
Thanks for the great and quick answers!
Could someone tell me what is the difference between the 2 methodes (Not Exist vs Not In)?They can usually be used to get the same results. Sometimes, it's easier to understand the logic of one rather than the other. Use whichever one you prefer. I can't guarantee that the optimizer won't do exactly the same thing, regardless of which one you use.
Also, I would I add in the Select the Date of the most recent Note and sort ASC so that I could have the Data that had not activity in the last 3 months and sorted by the one that had no activity the longest time ago first?In that case, I suggest a join, rather than either EXISTS or IN:
WITH got_r_num AS
SELECT d.*
, n.notedate
, ROW_NUMBER () OVER ( PARTITION BY d.id
ORDER BY n.notedate DESC NULLS LAST
) AS r_num
FROM data d
LEFT OUTER JOIN note n ON n.dataid = d.id
SELECT * -- Or list all couumns except r_num
FROM got_r_num
WHERE r_num = 1
AND NVL ( notedate
, SYSDATE - 99 -- or any date more than 3 months ago
) < ADD_MONTHS ( SYSDATE
, -3
ORDER BY notedate
;If you'd care to post some sample data (CREATE TABLE and INSERT statements), and the results you want from that data, then I could test this.
When you say you don't want dates within the last 3 months, I assume you're including dates in the future. (Future dates may be impossible in your table, anyway.)
3 months can be as short as 89 days or as long as 92 days. I used ADD_MONTHS (sysdate, -3) to get exactly 3 months; you can use SYSDATE - 90 if that suits your needs. -
Reading data from BSEG table with Non-key fields in where clause
Hi All,
I have to read data from BSEG table based on WBS element field (PROJK). As I'm not passing key fields to WHERE clause system couldnt run the select statement. Since BSEG is a cluster table I cant even create secondary index on PROJK field.
Could you please tell me, how to improve its performance.
Regards
Jaker.SELECT bukrs
belnr
gjahr
shkzg
dmbtr
hkont
lifnr
matnr
werks
menge
meins
ebeln
FROM bseg
INTO TABLE it_bseg
PACKAGE SIZE 10
FOR ALL ENTRIES IN it_final
WHERE bukrs EQ it_final-bukrs
AND belnr EQ it_final-belnr
AND gjahr EQ it_final-gjahr
AND buzei EQ it_final-buzei
AND hkont EQ it_final-hkont
AND werks IN s_werks.
By using package and fetch from BSEG table. gathering all other information to a final internal table.This will reduce the hit to database.And also try to put that data in hashed internal table which is it_bseg....then definetly improve the performance.
<REMOVED BY MODERATOR>
Dara.
Edited by: Alvaro Tejada Galindo on Apr 21, 2008 12:47 PM -
SQL - alternate rows from 2 tables
Hi, not sure how to best put this, but I have 2 tables for a Sales database. One is for our Sales Agents Actual sales (Agent_Sales, Product, Jul, Aug, Sep, Oct....) showing the actual sales by agent. PK is composite Agent_Sales and Product. The other table is similar but for the Targets (Agent_Target , Product, Jul, Aug, Sep, Oct....) with the PK again being composite Agent_Target and Product.
What I would like to do is get a listing for each agent in a table listing the actuals on the first line, then Targets on the second. Furthermore the 3rd line will be the variance between actuals and target and 4th line a percentage to target.
I would like the output to look like this:
Jul Aug Sep Oct
Agent_Sales 8 9 10 8
Agent_Target 9 8 8 9
Variance -1 1 2 -1
Percent 0.89 1.12 1.25 0.89
Thanks in advance.Hi,
Another way:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <=4
SELECT a.agent_sales
, a.product
, DECODE (c.n, 1, a.jul, 2, t.jul, 3, t.jul-a.jul, 4, a.jul/t.jul) AS jul
, DECODE (c.n, 1, a.aug, 2, t.aug, 3, t.aug-a.aug, 4, a.aug/t.aug) AS aug
, DECODE (c.n, 1, a.sep, 2, t.sep, 3, t.sep-a.sep, 4, a.sep/t.sep) AS sep
, DECODE (c.n, 1, a.oct, 2, t.oct, 3, t.oct-a.oct, 4, a.oct/t.oct) AS oct
FROM actual_sales a
JOIN target_sales t ON a.agent_sales = t.agent_sales
AND a.product = t.product
CROSS JOIN cntr c
ORDER BY a.agent_sales
, a.product
, c.n
;It sounds like each row in each table has a matching row in the other table. If that's not the case, then you might want to make the join between actural_sales and target_sales an outer join. All the computations might be messier, if they need NVL or NULLIF to deal with missing items.
I wonder if this is the best table design. Perhaps something with a separate row for each month would be better.
Edited by: Frank Kulash on Aug 2, 2009 4:09 PM -
Issue while deleting a row from a table
Dear friends,
i am getting an issue while deleting a row from a table, pls check screen shots , the first screen shot is my table contents
when i delete 2 row , the second row is deleting properly like below screen shot
but i want like below screen shot , Col1 contents should be like pic 1 . could any one pls let me know how to solve this issue.
Thanks
VijayaHi vijaya,
please try this code, it will help you.
DATA : it_rows TYPE wdr_context_element_set,
wa_rows LIKE LINE OF it_rows.
DATA lo_nd_table TYPE REF TO if_wd_context_node.
DATA lt_table TYPE wd_this->elements_table.
DATA lo_el_table TYPE REF TO if_wd_context_element.
DATA ls_vbap TYPE wd_this->element_table.
DATA: ld_index TYPE i.
data value TYPE sy-index.
* navigate from <CONTEXT> to <table> via lead selection
lo_nd_table= wd_context->get_child_node( name = wd_this->wdctx_table ).
* @TODO handle non existant child
* IF lo_nd_table IS INITIAL.
* ENDIF.
* get element via lead selection
* alternative access via index
* lo_el_table = lo_nd_table->get_element( index = 1 ).
* @TODO handle not set lead selection
IF lo_el_table IS INITIAL.
ENDIF.
* navigate from <CONTEXT> to <table> via lead selection
lo_nd_table = wd_context->get_child_node( name = wd_this->wdctx_table ).
* @TODO handle non existant child
* IF lo_nd_table IS INITIAL.
* ENDIF.
lo_nd_table->get_static_attributes_table( IMPORTING table = lt_table ).
* @TODO handle non existant child
* IF lo_nd_table IS INITIAL.
* ENDIF.
** @TODO compute values
** e.g. call a model function
* navigate from <CONTEXT> to <table> via lead selection
lo_nd_table = wd_context->get_child_node( name = wd_this->wdctx_table ).
* @TODO handle non existant child
* IF lo_nd_table IS INITIAL.
* ENDIF.
** @TODO compute values
** e.g. call a model function
it_rows = lo_nd_table>get_selected_elements( ).
CALL METHOD lo_nd_table->GET_LEAD_SELECTION_INDEX
RECEIVING
INDEX = value .
LOOP AT it_rows INTO wa_rows.
CALL METHOD wa_rows->get_static_attributes
IMPORTING
static_attributes = ls_table.
READ TABLE lt_table INTO ls_table WITH KEY col1 = ls_table-col1.
ld_index = value.
ENDLOOP.
CLEAR : ls_table-col2,
ls_table-col2.
MODIFY lt_table INDEX ld_index FROM ls_table.
lo_nd_table->bind_table( new_items = lt_table set_initial_elements = abap_true ). -
How NOT to restrict no of rows from two tables
I have two identical tables Invoice and Payment. The only difference is Invoice_id,Invoice_Amt and Payment_id,Payment_Amt columns that shows different ids and amounts. The bank_ids, names, account_types are same. Invoice table has 3 rows and Payment has 2. Simply meaning that there were 3 invoices generated but the bank received 2 payments. I want to show Invoice_Amt and Payment_Amt using sql query. But its giving me total 6 rows. Whereas, I want 3 from Invoice and 2 rows from Payment table to show side-by-side.
CREATE TABLE invoice
( invoice_id NUMBER
bank_id NUMBER,
bank_name VARCHAR2(256),
invoice_amount NUMBER);
----Invoice table has 3 rows showing 3 Invoice Amts
CREATE TABLE payment
( payment_id NUMBER
bank_id NUMBER,
bank_name VARCHAR2(256),
payment_amount NUMBER);
----Payment table has 2 rows showing 2 Payments
After executing this sql statement below, I get 6 rows:
select inv.invoice_amount,pymt.payment_amount from invoice inv,payment pymt where inv.bank_id=pymt.bank_id;
How can I show 3 rows for Invoice and 2 for Payment..?
Thank you.Hi,
So you want
the 1st invoice to appear side-by-side with the 1st payment,
the 2nd invoice to appear side-by-side with the 2nd payment,
the nth invoice to appear side-by-side with the nth payment.
But, if there are an uneqaul numner of payments and invoices, all the payments and all the invoices must still be shown, alone on a row if necessary.
That sounds like a job for FULL OUTER JOIN.
Use the analytic ROW_NUMBER fucntion to determine which is the 1st, 2nd, ..., nth row in each table, for each bank.
WITH invoice_plus_r_num AS
SELECT bank_id, bank_name
, invoice_amount
, ROW_NUMBER () OVER ( PARTITION BY bank_id, bank_name
ORDER BY invoice_id
) AS r_num
FROM invoice
, payment_plus_r_num AS
SELECT bank_id, bank_name
, payment_amount
, ROW_NUMBER () OVER ( PARTITION BY bank_id, bank_name
ORDER BY payment_id
) AS r_num
FROM payment
SELECT NVL (i.bank_id, p.bank_id) AS bank_id
, NVL (i.bank_name, p.bank_name) AS bank_name
, i.invoice_amount
, p.payment_amount
FROM invoice_plus_r_num i
FULL OUTER JOIN payment_plus_r_num p ON i.bank_id = p.bank_id
AND i.bank_name = p.bank_name
AND i.r_num = p.r_num
ORDER BY bank_id -- you can use column aliases here
, bank_name
, NVL (i.r_num, p.r_num)
;You mentioned something about accounts, but didn't include that in the CREATE TABLE statements. You'll probably want to add that wherever I used bank_id and bank_name, above.
Are invoce and payment actually views, rather than tables? If not, you should have a separate bank table, and only include the bank_id in the other tables.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
Edited by: Frank Kulash on Feb 3, 2011 12:32 PM -
Select unique rows from two tables...
Hi,
I have two tables, replies1 and replies2.
SQL> desc replies
Name Null? Type
URN VARCHAR2(36)
ADDRESS VARCHAR2(18)
FILESIZE NUMBER
AS_NUM VARCHAR2(6)
SQL> desc replies2
Name Null? Type
URN VARCHAR2(36)
ADDRESS VARCHAR2(18)
AS_NUM VARCHAR2(6)
Both of the tables have no primary keys, but I have indixes on (urn, addrss) combination on both....
I am trying to select the unique rows with (urn, address) from replies2, and then find the matching size from replies...
I am using the following query:
select distinct replies2.urn, replies2.address, replies.filesize from replies2, replies where replies2.AS_NUM like 'XYZ' and replies.urn = replies2.urn;
I cannot figure out why it won't work. the way I understand it is that, distinct will give all distinct combination of all column names that follow, which is what I want...
I know it is wrong, because the query:
select count(*) from replies2 where AS_NUM like 'XYZ' returns less number of rows than the above query.
Any help would be greatly appreciated.
Thank you
Oz.Thanks a lot Mohan for your reply.
urn is not a unique key. Several rows could have the same (urn, address) pair in both tables. What I want is retrieve all (urn, address) rows from one table, and find the size from the other table to make a (urn, address, size). I want all unique combinations of (urn, address) to appear in the output.
AS_NUM is an empty column in replies... It would've been a lot easier if it wasn't, since then I'll just say: select distinct urn, address, filesize from replies where AS_NUM like 'XYZ';
I will try your query though and let u know how it goes. It takes quite a while to run since my tables are huge. -
Delete many rows from one table at once URGENT
Assume we have table called emp.
table desription:
SQL> desc emp
Name Null? Type
EMPNO NOT NULL NUMBER(6)
DIVISION NOT NULL NUMBER(3)
JOB_NO NOT NULL NUMBER(4)
START_DATE NOT NULL DATE
select * from emp;
EMPNO--------DIVISION--------JOB_NO------------START_DATE
1111------------------011-------------8181--------------04/10/1999
1111------------------011-------------8181--------------04/10/2004
2222------------------011-------------3131--------------05/11/2005
3333-----------------022-------------8181--------------06/09/2001
3333-----------------044-------------8181--------------06/08/1988
5555-----------------011-------------8066--------------01/01/2001
6666-----------------033-------------9600--------------01/01/1999
7777-----------------044-------------8181--------------06/24/1996
7777-----------------033-------------8181--------------12/02/1991
7777-----------------033-------------8181--------------03/01/2002
9999-----------------044-------------9191--------------03/05/1980
9999-----------------055-------------9191--------------03/06/1989
My goal is to delete employee records for those employee which contains multiple values for JOB_NO
equal to 8181, (JOB_NO= 8181) with new start_dateS.
We need to keep only one record for JOB_NO 8181 which contains oldest start_date.
Here is the delete statement for the single record.
delete from emp
where empno = 7777 and job_no = 8181 and start_date in ('03/01/2002','06/24/1996);
So how could I delete thousands records from the table with this logic?
After deleting multiple records table should be as below:
select * from emp;
EMPNO--------DIVISION--------JOB_NO---------START_DATE
1111-----------------011-------------8181--------------04/10/1999
2222-----------------011-------------3131--------------05/11/2005
3333-----------------044-------------8181--------------06/08/1988
5555-----------------011-------------8066--------------01/01/2001
6666-----------------033-------------9600--------------01/01/1999
7777-----------------033-------------8181--------------12/02/1991
9999-----------------044-------------9191--------------03/05/1980
9999-----------------055-------------9191--------------03/06/1989Here's one way to do it. I think this fits your business rules. At least it matches the output for your first example. It uses an analytic. I just like using them.
SQL> select * from employees;
EMPNO DIVISION JOB_NO START_DATE
1111 11 8181 10-APR-1999
1111 11 8181 10-APR-2004
2222 11 3131 11-MAY-2005
3333 22 8181 09-JUN-2001
3333 44 8181 08-JUN-1988
5555 11 8066 01-JAN-2001
6666 33 9600 01-JAN-1999
7777 44 8181 24-JUN-1996
7777 33 8181 02-DEC-1991
7777 33 8181 01-MAR-2002
9999 44 9191 05-MAR-1980
9999 55 9191 06-MAR-1989
12 rows selected.
SQL> delete employees
2 where rowid in
3 (select rowid
4 from
5 (
6 select empno
7 ,job_no
8 ,start_date
9 ,row_number() over (partition by empno, job_no order by start_date) rn
10 from employees
11 where job_no = 8181
12 )
13 where rn > 1
14 )
15 ;
4 rows deleted.
SQL> select * from employees;
EMPNO DIVISION JOB_NO START_DATE
1111 11 8181 10-APR-1999
2222 11 3131 11-MAY-2005
3333 44 8181 08-JUN-1988
5555 11 8066 01-JAN-2001
6666 33 9600 01-JAN-1999
7777 33 8181 02-DEC-1991
9999 44 9191 05-MAR-1980
9999 55 9191 06-MAR-1989
8 rows selected. -
Deleting rows from one table while filter condition lies into another table
Hi All,
I'm facing a problem deleting a row from a table where the condition lies in other table. To ealaborate the scenario further let's say I have two table T1 (transaction table) and T2 (Master Table). I want to delete a record from table T1 by checking some conditions lies in table T2.
I tried with the below query but this query deleting records from both the table.
DELETE FROM ( SELECT * FROM T1 top INNER JOIN T2 tp
ON top.TID = tp.TID
WHERE top.DEAL_SITE_ID = inputparameter1
AND (TP.SEGMENT <>inputparameter2 OR tp.segment is not null));
Here the record is getting deleted from both the tables but I don't want to delete data from my master table i.e table T2.
Any help would be highly appreciated. Thanks in advance.
Regards,
SubhadeepSQL> select *
2 from the_table;
X Y
AA a
BB a
CC a
AA b
DD b
SQL> select *
2 from the_table_2;
X Y
AA a
BB a
CC a
SQL> delete from
2 (
3 select *
4 from the_table t1 inner join the_table_2 t2
5 on t1.x = t2.x
6 and t1.y = t2.y);
3 rows deleted.
SQL> select *
2 from the_table;
X Y
AA b
DD b
SQL> select *
2 from the_table_2;
X Y
AA a
BB a
CC a -
Insert missing rows from another table
Hi
I need to insert rows from another table if there is gaps in my transaction table. For each ärendenr there should be 5 Id status rows (1-5). Except for the fact that the inserted rows never should have higher IdStatus number then the highest value from
the transaction for each Ärendenr. See my example below that illustrates my transaction table, Stages master and the desired result.
Is there some t sql code that could handle this problem?
Best regards
Arne
So far i have tried to solve this with the code below but it doesnt work.
I have copied the transaction file information to the TempLogTransTimes
and then insert into LogTransTimes
IF OBJECT_ID('dbo.TempLogTransTimes', 'U') IS NOT NULL --Här raderas den tillfälliga tabellen
DROP TABLE dbo.TempLogTransTimes;
SELECT *
INTO TempLogTransTimes
FROM LogTransTimes
DECLARE @TopÄrende INT
WHILE EXISTS(SELECT * FROM TempLogTransTimes)
BEGIN
SET @TopÄrende = (SELECT TOP(1) Ärendenr FROM TempLogTransTimes ORDER BY Ärendenr)
INSERT INTO LogTransTimes(Ärendenr, IdStatus, StatusNamn, SourceId)
SELECT @TopÄrende, SM.IdStatus, SM.StatusNamn, 'Beräkning'
FROM Statusmall SM
FULL JOIN TempLogTransTimes LT ON SM.IdStatus = LT.IdStatus
WHERE LT.IdStatus IS NULL
DELETE TempLogTransTimes WHERE Ärendenr = @TopÄrende
END
Arne OlssonHi Arne Olsson, try this..
DECLARE @Transactions TABLE
Id1 INT,
IdStatus INT,
StatusName VARCHAR(10)
DECLARE @Stages TABLE
IdStatus INT,
StatusName VARCHAR(10)
INSERT INTO @Transactions VALUES(1,1,'AA'), (1,3,'CC'), (1,5,'EE'), (2,1,'AA'), (2,3,'CC')
INSERT INTO @Stages VALUES (1,'AA'), (2,'BB'), (3,'CC'), (4,'DD'), (5,'EE')
;WITH CTE AS(SELECT DISTINCT Id1 FROM @Transactions)
SELECT Id1, IdStatus, StatusName FROM CTE, @Stages
EXCEPT
SELECT Id1, IdStatus, StatusName FROM @Transactions
Please mark as answer, if this has helped you solve the issue.
Good Luck :) .. visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles. -
Hi ,
I have a table with 20 records and 10 columns.I want to add columns from another table with out cross join.As others have said, you need to have some sort of join condition otherwise it is a cross join.
SQL> ed
Wrote file afiedt.buf
1 with T1 as (select 'e' as c1, 2 as c2 from dual union all
2 select 'd', 3 from dual)
3 ,T2 as (select 'x' as c3, 5 as c4 from dual union all
4 select 'y', 6 from dual union all
5 select 'z', 2 from dual)
6 --
7 select T2.c3, T2.c4, T1.c1, T1.c2
8 FROM (select c1, c2, row_number() over (order by c1) as rn from T1) T1
9 FULL OUTER JOIN
10 (select c3, c4, row_number() over (order by c3) as rn from T2) T2
11* ON (T1.rn = T2.rn)
SQL> /
C C4 C C2
x 5 d 3
y 6 e 2
z 2
SQL>This example assigns a row number to each row within each table and then joins using that row number.
(I've assumed the row number should be generated based on the order of the first column of each table, but you can change that as required).
What is the point of your requirement? -
Listing foreign keys with non matching column definitions
Dear All,
I seen an entry in DBA weeky activities s that "Listing foreign keys with non matching column definitions" . What is meant by that?. How to pick up that?
ShijuI seen an entry in DBA weeky activities s that
"Listing foreign keys with non matching column
definitions" . What is meant by that?. How to pick up
that?Most probably find columns which are part of foreign keys, but their respective primary key column have different length (AFAIK data type cannot be different).
Data dictionary can help you in that, I'll give you hints where to look at:
user/all/dba_constraints - foreign and primary keys
user/all/dba_cons_columns - columns which are part of constraints
user/all/dba_tab_columns - column definitions of tables
However creating query from these data dictionary views could be a nice task for you :)
Gints Plivna
http://www.gplivna.eu -
How to select all the colomns_names from a table, with their datatypes ..
hi :)
i would like to know, how to select in SQL all the columns names from a table with their datatypes so that i get something like this :
Table 1 : table_name
the column ID has the Datatype NUMBER
the column name has the Datatype Varchar2
Table 2 : table_name
the column check has the Datatype NUMBER
the column air has the Datatype Varchar2
and that has to be for all the tables that i own ! ..
P. S : i m trying to do this with java, so it s would be enough if you just tell me how to select all the tables_names with all their colums_names and with all their datatypes ! ..
thank you :)
i ve heard it can be done with USER_TABLES .. but i have no idea how :( ..
Edited by: user8865125 on 17.05.2011 12:22Hi,
The data dictionary view USER_TAB_COLUMNS has one row for every column in every table in your schema. The columns TABLE_NAME, COLUMN_NAME and DATA_TYPE have all the information you need.
Another data dictionary view, USER_TABLES, may be useful, too. It has one row pre table.
Maybe you are looking for
-
BexGetData - Limitation or bug/problem ?
Hello, We are currently running BI 7 NW04s on Service Pack 9 (but with many notes applied to fix known issues). I am attempting to determine the level of flexibility available with the BExGetData and "convert to fomula" functionality in the Bex Anal
-
Revision: 7175 Author: [email protected] Date: 2009-05-21 07:48:34 -0700 (Thu, 21 May 2009) Log Message: Changing some namespace usage for "mx_internal::" to "use namespace mx_internal" Maybe we'll get some slight performance improvements. QE N
-
MYOB AccountEdge v8 (Australia version) install failure
Hi, I downloaded the free test drive version of MYOB AccountEdge v8 from myob.com.au. I tried to install it on my Mac (Al iMac, 10.5.6, 2.4GHz, 4GB RAM) but the installation process failed prematurely with the following message whilst still in the In
-
After updating pc not starting
My macbook is not starting. I see a circle with a diameter inside the circle
-
Writer : what is web url via http option
writer has a "web url via http" option . Can you please explain this ? Where dose it write to ? A blog storage ? if yes , what is the path to the storage ?