SQL to find logical replication
Hi all
with t as
(
Select 'FRANCE' as source, 'USA' as dest, 1000 as double price
Union of all the
Select "USA" as a source, 'FRANCE' as dest, 1000 as double price
Union of all the
Select "USA" as a source, 'INDIA' as dest, 2000 as double price
Union of all the
Select 'INDIA' as a source, 'USA' as dest, 2000 as double price
Union of all the
Select 'CHINA' as source, "JAPAN" as dest, 3000 as price of the double
Union of all the
Select "JAPAN" as a source, 'CHINA' as dest, 3000 as double price
Union of all the
Select "AUSTRALIA" as a source, "NEW ZEALAND" as dest, 500 as the price of double
)
Select * from t
Here
SOURCE | DEST | PRICE |
---|---|---|
France | U.S.. | 1000 |
U.S.. | France | 1000 |
are logically duplicates.
Power required:
SOURCE | DEST | PRICE |
---|---|---|
Australia | NEW ZEALAND | 500 |
CHINA | Japan | 3000 |
France | U.S.. | 1000 |
U.S.. | India | 2000 |
I tried the sql below and got the result. Want to know is there a better option.
with t as ( Select 'FRANCE' as source, 'USA' as dest, 1000 as price from dual union all Select 'USA' as source, 'FRANCE' as dest, 1000 as price from dual union all Select 'USA' as source, 'INDIA' as dest, 2000 as price from dual union all Select 'INDIA' as source, 'USA' as dest, 2000 as price from dual union all Select 'CHINA' as source, 'JAPAN' as dest, 3000 as price from dual union all Select 'JAPAN' as source, 'CHINA' as dest, 3000 as price from dual union all Select 'AUSTRALIA' as source, 'NEW ZELAND' as dest, 500 as price from dual ) select * from ( select t.source,t.dest, t.price from t where t.source >= t.dest union select t.dest,t.source, t.price from t where t.source <= t.dest )
Version:
Oracle 10 g Release 2
Thank you
Here's one way:
with t as
(
Select 'FRANCE' as source, 'USA' as dest, 1000 as double price
Union of all the
Select "USA" as a source, 'FRANCE' as dest, 1000 as double price
Union of all the
Select "USA" as a source, 'INDIA' as dest, 2000 as double price
Union of all the
Select 'INDIA' as a source, 'USA' as dest, 2000 as double price
Union of all the
Select 'CHINA' as source, "JAPAN" as dest, 3000 as price of the double
Union of all the
Select "JAPAN" as a source, 'CHINA' as dest, 3000 as double price
Union of all the
Select "AUSTRALIA" as a source, "NEW ZEALAND" as dest, 500 as the price of double
)
Select *.
de)
Select t.*, row_number() over (partition by the greatest (source, dest), less (source, dest) price range) rnum
t)
where rnum = 1
Scanning of a single table. I ordered by price, you can order by descending price or include prizes in the partition clause
SOURCE | DEST | PRICE | RNUM |
---|---|---|---|
CHINA | Japan | 3000 | 1 |
Australia | NEW ZEALAND | 500 | 1 |
France | U.S.. | 1000 | 1 |
U.S.. | India | 2000 | 1 |
4 selected lines.
Tags: Database
Similar Questions
-
There is no sql to find no matching rows
Hi all
I want to optimize the sql where I find the column (junction of two columns) presents in a table (tab1) but not in other (tab2). I tried something like that, but it is showing me no line.
SELECT COL1 | COL2
OF e TAB1
where there is NO
(
Select 1 in TAB2 rd
Join e TAB1 on e.COL1 | e.COL2 = RD. COL1 | RD. COL2
)
When I use the simple sql (which takes a lot of time hell) I get good output: 0
SELECT COL1 | COL2
OF e TAB1
where COL1 | Col2 not in
(
Select COL1 | Col2 from TAB2 rd
)
What is wrong in the query "EXISTS NOT", or suggest me best way to achieve the same thing. Thank you!
Few things
1. do not concatenate columns, and then compare. They can cause an incorrect result. Consider this
tab1.col1 = 'KAR' and tab1.col2 = 'THICK' and tab2.col1 = 'KARTHI' and tab2.col2 = 'CK'
Concatenation and join them will result in the return line. But in fact they do not condition.
2. NO EXISTS and NOT IN are not the same. The two manage NULL value very differently. If there is a NULL value in the sub operator NOT IN the entire State query fails.
https://asktom.Oracle.com/pls/asktom/f?p=100:11:0:no:P11_QUESTION_ID:442029737684
Says, try something like this
select t1.* from tab1 t1 where not exists ( select null from tab2 t2 where t1.col1 = t2.col1 and t1.col2 = t2.col2 )
-
Modeler data SQL cannot find the java sdk
Hello
I want to install 4.0.3 - 853 on windows xp 32-bit.
In the first inning, message "could not find a java sdk SE way...» »
I had already installed first jre 1.7.0_45 - as indicated on the installation notes page that produces this error message.
After reading other peoples problems I replaced it with the jdk 1.7.0_45 because the request for enforcement is the path to the java JDK home.
Still get the same message as above.
I tried to change the SetJavaHome in datamodeler.conf, but that didn't work or the other.
I tried pointing the Launcher to d:\oracle\java\jdk1.7.0_45 as d:\oracle\java\jdk1.7.0_45\bin and d:\oracle\java\jdk1.7.0_45\bin\java.exe all to nothing does not.
On a light off the coast of the chance - like cleaning the windshield when the car won't start - I installed SQL Developer 4.0.3.16.84 in case the treatment was slightly different.
Same result
Very frustrating every previous version of the developer and grouped data with java maker has just installed in a few minutes.
All of the inspiring thoughts / solutions would be extremely appreciated
Thank you
Stem
Ok
It seems that - maybe - jdk1.7.0_71 high is needed and jdk1.7.0_45 is not acceptable whatever the installation notes which stipulate that jre 1.7 is suitable. Mind the same version of SQLDeveloper says he wants to 1.8, but seems to work very well with 7u71
I had stayed in 7u45 to minimize disruptions to the installation of 10g of shapes on the same machine which of course do not now
It's looking no doubt easier to get this working with 7u71 than the other way around.
Stem
-
The shortcomings of SQL to find missing persons
Hi gurus,
I have a simple SQL question on how to find the gaps. I have a Table: GLHDR_M with ACUTE flange as key field with an ID of account from 'A1000001' to 'A3999999', but with gaps inbetween. I want to reassign the missing IDS to those newly created. How can I find the missing ID with a simple SQL statement?
Thank you
Otherwise, you can simply add the condition "AND PRIOR DBMS_RANDOM. VALUE don't IS NOT NULL' with the connect by clause
SELECT 'A ' | LPAD (TO_NUMBER (LTrim (Replace ('A0904000', 'A'), '0')) +(rowNum-1), 7, '0') accide
OF THE DOUBLE
CONNECT BY LEVEL<= to_number(ltrim(replace('a0904999','a'),'0'))-to_number(ltrim(replace('a0904000','a'),'0'))="" +="">=>
AND PRIOR DBMS_RANDOM. VALUE IS NOT NULL
LESS
SELECT acid glhdr_m acid WHERE BETWEEN 'A0904000' AND 'A0904999 ';
I don't have a 9i environment to test this. Then you can try it, and if you get the error, you can try with the function table in pipeline I gave already.
-
Bluetooth cannot find logical error code turn 10
Acer aspire 6920 with bluetooth. try to only synchronize sound logic itower, acer vcm no external audio device and the bluetooth connection Center disabled
Hello
Method 1:
Check the link and see if it helps:
Description of errors related to the 10 Code that generates the Device Manager in Windows on computers
http://support.Microsoft.com/kb/943104
Method 2:
Check the link and try to run the Microsoft Fix it:
Hardware devices do not work or are not detected in Windows
http://support.Microsoft.com/mats/hardware_device_problems/en-us
Method 3:
I would say as update you the drivers for the device and check if the problem persists:
Updated a hardware driver that is not working properly
Let us know if it helps.
-
SQL to find different files in path
Hi Experts,
Please help me this sql
I have a table
file tracking template
P1 m1 f1
P1 m1 f2
P1 f3 m2
P2 f11 m11
P2 f12 m12
------------------
I need output of
P1 f3 m2 (m2 model in path p1 is LESS THAN m1)
P2 f11 m11 (case model m11 in road p2 is equal to m12)
OR
P1 f3 m2 (m2 model presence in path p1 is more than m1)
P2 f12 m12 (case model m12 in road p2 is equal to m11)
---------------------
Thanks in advance
Thanks to user12222981 for providing the scripts of creation and insertion. These scripts using my request could be simplified as below.
() AS T2
SELECT THE PATH,
FILES,
MODELS,
COUNT (*) IN THE CNT (PARTITION PATH, MODELS)
OF TEST_PATH)
SELECT THE PATH,
FILES,
MODELS
DE)
SELECT THE PATH,
MODELS,
FILES,
NTC,
MIN (MODELS) MORE MIN_MODEL (PATH PARTITION)
THE T2
WHERE NTC IN (SELECT MIN (CNT) OF THE T2
GROUP BY PATH))
WHERE MODELS = MIN_MODEL;
OUTPUT:
PATH OF THE TEMPLATES FILES
-------------------- -------------------- -----------
p1 f3 m2
p2 f11 m11
-
How to force a return, even if the sql cannot find a record
Hello
I ' l try explan, what I'm looking for:
I have a SQL query (Oracle 10 g) as
Select system, A, B T where system = 0
In the table T is no record with system = 0, so I don't get anything back from the SQL. It's OK, but I need to get something like * 0, "," * as a return. I tried with NVL(SysT,0)... but it seems not to be the solution.
Does anyone has an idea how to solve this problem?
Best regards
CarstenHi, Carsten,
Another way to do this is to use an outer join:
SELECT 0 AS SysT, a, b FROM dual LEFT OUTER JOIN t ON t.SysT = 0 ;
UNION, Saurabh suggested, is a way to do it. If the combination (System, a, b) is not unique in table t, then you need to make UNION ALL , which is more efficient anyway, to avoid losing lines.
If the original query is longer than what you have posted, you will not want to repeat. Write it only once, in a WITH clause, like this:WITH original_query AS ( SELECT SysT, a, b FROM t WHERE SysT = 0 ) SELECT * FROM origianl_query UNION ALL SELECT 0 AS SysT, a, b FROM dual WHERE NOT EXISTS ( SELECT 1 FROM original_query ) ;
Regardless of how complicated the original query, the EXISTS subquery is just 'SELECT 1 FROM original_query '.
Published by: Frank Kulash, December 19, 2011 06:21
-
How to build a SQL for this logic.
Hello
I have a requirement like, I need to derive a column with a calculation.
Here is the psedocode,
number of occurrence (deptno = 10) / count (deptno = 10)
Expected result:
If the deptno = 10 occurs 3 times in the table, then at the time of fist should be the calcuation
second 1/3, 2/3 of the time, the third time 3/3 and so on.
How can I build in a sql query. Help, please.
Any help will be appreciated with points.
Thank you.Try this.
SELECT LEVEL / CNT CALC FROM (SELECT COUNT (*) CNT FROM EMP WHERE DEPTNO = 10) CONNECT BY LEVEL <= CNT
G.
-
Is it possible to use SQL queries to search for names of entries which are contained in an URL parameter?
The situation is the following:
In the #1 database, I have several entries (A, B, C, D, etc.). I want to know if I can use WHERE and LIKE clause to filter the entries that appear in a URL parameter.
The normal code: WHERE nom_entree AS % whateverTheURLparameterIs % displays only entries that have the URL containsparameter. I need the code to display the entries contained in the URL parameter.
P.S. does not have the code WHERE the whateverTheURLparameterIs AS argument entryName % .
Consider using the full text for this research. You could probably achieve what you want to use the like with a predicate subquery, but a full text search will be faster. You can also control the respect of the case.
> PS another problem that I am having with what corresponds to the URL
> settings, because they include all of the
> article ("Hi. My name is... (''), tend to be very long.
No need to include the item in the query string. The article is stored in the table. Just pass the id of the item in the query string.
-
SQL to find the same DATE?
I wanted to do a check based on a Table of 10g.
customer_id, DATE
Basically, I wanted to do a check, make sure that each customer_id has a SEPARATE DATE, therefore, for the returned data.
the DATE must be unique and not repeat, I know its probably something super simple, but I wanted to analyze the entire
table and do a count of these instances:
It would be nice:
12345 20101001
25487 20101002
This would not be permitted:
12345 20101001
12345 20101001 - double date
25487 20101002
a varchar2 column, it is in format YYYYMMDD, not not using type DATE, just FYI. Ago, I know, I have not said that upward.
I would use a SELF JOIN here, not a lot of experience in this.Hello
Here's one way:
WITH got_cnt AS ( SELECT customer_id , dt -- DATE is not a good column name , COUNT (*) OVER ( PARTITION BY customer_id , dt ) AS cnt FROM table_x ) SELECT * FROM cot_cnt WHERE cnt > 1 ORDER BY customer_id , dt ;
A self-join anouther way to find duplicates, but may not be as effective. In addition, what would you like if it was be stored 3 (or more) with the same customer_id and dt?
Published by: Frank Kulash, October 21, 2010 16:44
Oh!
If you want to display the columns are duplicated, you can do something even more fast and simlpler:SELECT customer_id , dt , COUNT (*) AS cnt FROM table_x GROUP BY customer_id , dt HAVING COUNT (*) > 1 ;
-
SQL to find the grouped rows that have changed their values?
Hello
I wonder if it is a pure SQL way do this...?
I have a table that has a number of columns:
InsertionDate
IDENTITY
Col - has to pass - z - some data.
the pair (InsertionDate, identity) is unique.
The lines represent the same values as they change over time, for example:
Select * from < mytable > by its identity, InsertionDate;
Balance InsertionDate identity
10 February 01-1001 - 100
11 February 01-1001-1
1001 12 February 01 50
10 February 01-1002-10
11 February 01-1002-10
1002 12 February 01 76
I want to do the following:
Select < myTable > from where InsertionDate and the next InsertionDate have a different value for the balance "grouped by" identity and InsertionDate = "10 February 01»;»»
[I use "grouped by" with great caution - I can't explain in 'Nick sql' without using the misleading grouped by term]
To produce the output:
Balance InsertionDate identity
10 February 01-1001 - 100
11 February 01-1001-1
Select < myTable > from where InsertionDate and the next InsertionDate have a different value for the balance "grouped by" identity and InsertionDate = ' 11 February 01»;»»
Balance InsertionDate identity
11 February 01-1001-1
1001 12 February 01 50
11 February 01-1002-10
1002 12 February 01 76
There may be several columns in addition to the balance that should also be checked for changes.
See you soon,.
Karl.A little misread your reqs:
with t as ( select to_date('10-Feb-01','dd-mon-yy') InsertionDate,1001 Identity,-100 Balance from dual union all select to_date('11-Feb-01','dd-mon-yy'),1001,1 from dual union all select to_date('12-Feb-01','dd-mon-yy'),1001,50 from dual union all select to_date('10-Feb-01','dd-mon-yy'),1002,10 from dual union all select to_date('11-Feb-01','dd-mon-yy'),1002,10 from dual union all select to_date('12-Feb-01','dd-mon-yy'),1002,76 from dual ) select InsertionDate, Identity, Balance from ( select InsertionDate, Identity, Balance, row_number() over(partition by Identity order by InsertionDate) rn, lead(Balance,1,Balance - 1) over(partition by Identity order by InsertionDate) next_balance from t ) where balance != next_balance and rn != 1 order by Identity, InsertionDate / INSERTION IDENTITY BALANCE --------- ---------- ---------- 11-FEB-01 1001 1 12-FEB-01 1001 50 11-FEB-01 1002 10 12-FEB-01 1002 76 SQL>
SY.
-
SQL to find the counts based on the date
Hello
I have a table called APP_TXN and there application_id, application_status, and created_dt columns. My job is to find number of applications created in the last 0-4, 5-9 days and 10 + days, application_status group. Help, please.
Thank you and best regards,
Christopher.Something like that
select application_status, sum(case when created_date between sysdate-4 and sysdate then 1 else 0 end) CNT0TO4DAYS, sum(case when created_date between sysdate-9 and sysdate-5 then 1 else 0 end) CNT5TO9DAYS, sum(case when created_date <= sysdate-10 then 1 else 0 end) CNT10PLUSDAYS from app_txn group by application_status
-
SQL to find identical domains in e-mail addresses
Hi, guys can anyone help me design a query that will be guve me the names and e-mail addresses of all people who wear the same registered domain name
Published by: user12258774 on November 22, 2009 13:35
Published by: user12258774 on November 28, 2009 13:09
Published by: user12258774 on November 28, 2009 13:11Add consultantid column to table T and use < (or=""> ) instead of (! =) when to comparating consultantid:
WITH T AS ( select firstname, lastname, email, expertiseid, c.consultantid from consultants c, skills s where s.consultantid=c.consultantid) Select t1.firstname, t1.lastname, t1.email, t2.firstname, t2.lastname, t2.email from t t1, t t2 where t1.consultantid
Max
-
Query or logic to find several days through each month.
Dear friends,
I need your help to solve this problem.
We have a now where we have to pay compensation to the employees based on their number of working days.
Say, for example, if an employee has worked since March 3, 2012-April 5, 2012.
We have a fixed value for a month 300 dirhams. But the number of days on s March 31 and number of days in the month of April are 30.
So by compensation daily for March day would be 300/31 and April would be 300/30.
We are looking for a logical opr query that calcluates first right number of days in each month (per month) and then calculate as below
Number of working days in the month of March is 31-3 + 1 = 29
Compensation of A1 = (300 * 29) / 31
Number of working days in the month of April is 5 (must also find logical I guess)
Allowance for A2 = (300 * 5) / 30
Then A1 + A2.
(E) would be the total quota when the number of months in all.
Please guys any help would be greatly appricated.
RJust a follow-up ;-)
Note that you can almost get what you want with standard MONTHS_BETWEEN function [url http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions102.htm#i78039].
But notice this quote from the documentation:>
If date1 and date2 are the same days of the month or the last two days of the month, the result is always an integer. Otherwise the Oracle database calculates the fractional part of the result based on a month of 31 days and consider the time difference components date1 and date2.
>It will always use one even if 31 days a month has fewer days.
Then you could also implement the logic of my previous answer as your own function months_between implementation:
create or replace function my_months_between ( p_todate date , p_fromdate date ) return number is begin return ((extract(day from last_day(p_fromdate))-extract(day from p_fromdate)+1) / extract(day from last_day(p_fromdate))) + (months_between(trunc(p_todate-1,'MM'),trunc(p_fromdate,'MM'))-1) + (extract(day from p_todate-1) / extract(day from last_day(p_todate-1))); end my_months_between; /
I changed it to make it "compatible" with the standard function that it calculates up to but not including the parameter to date (which is correct when you are considering dates with time included, but I guess in your case, you have just date values.)
Then, you can compare the results of the standard months_between with the new my_months_between:
(Note that I have call the functions with the d2 + 1 to give the desired result of inclusion and to this day in the calculation).SQL> with testdata as ( 2 select date '2012-03-03' as d1, date '2012-04-05' as d2 from dual union all 3 select date '2012-02-03' as d1, date '2012-05-02' as d2 from dual union all 4 select date '2012-03-03' as d1, date '2012-03-10' as d2 from dual union all 5 select date '2012-03-01' as d1, date '2012-07-31' as d2 from dual union all 6 select date '2012-02-01' as d1, date '2012-03-01' as d2 from dual union all 7 select date '2012-01-01' as d1, date '2012-03-25' as d2 from dual 8 ) 9 -- 10 -- end of test data -- 11 -- 12 select months_between(d2+1, d1) std_months 13 , 300 * months_between(d2+1, d1) std_allowance 14 , my_months_between(d2+1, d1) my_months 15 , 300 * my_months_between(d2+1, d1) my_allowance 16 from testdata 17 / STD_MONTHS STD_ALLOWANCE MY_MONTHS MY_ALLOWANCE ---------- ------------- ---------- ------------ 1.09677419 329.032258 1.10215054 330.645161 3 900 2.99555061 898.665184 .258064516 77.4193548 .258064516 77.4193548 5 1500 5 1500 1.03225806 309.677419 1.03225806 309.677419 2.80645161 841.935484 2.80645161 841.935484 6 rows selected.
For a large part of the data, both give the same result, especially when the two dates are in month of 31 days.
To work from ' 2012-03-03' to '' 2012-04-05 standard service gives too little because it uses always 31 days, while my_months_between uses 30 days of April."But note working from '' 2012-02-03 to 2012 - 05 - 02. He jumped 2 days in February and has worked 2 days in May - the standard function concludes he works precisely 3 months.
But my_months_between computes fraction 27/29 months in February, then March and April like 2 months, and then the fraction 2/31 months in May - which gives a total of 2.99555061 instead of 3 months.Just something to be aware of ;-)
-
Find missing at time interval in SQL
T3 20:00 23:00T2 09:00 18:00All theMy query returns data as shown belowNeed help with SQL to find the missing time interval.
Data1
StartTime EndTime column
======= ======= =======Data2
StartTime EndTime column
======= ======= =======T1 15:00 20:00
T3 20:00 07:00Take above output, I want to find time is not on my data within 24 hours of the first StartTime on each set of data.
Example: Data1
First StartTime: 09:00 (registration T2)
Add 24 hours, which will be updated 21:00.
Result to get missing scheduled interval for Data1
18:00 - 20:0023:00-09:00 (next day)
For expected result of data2
07:00-15:00 the next day
The basic version: 11g
Someone at - he met to calculate the missing time interval? Can I use PL/SQL for this as a function of pipeline?
Any help/directions/references that I greatly appreciate.
Thanks in advance.
Edward
One way to find missing intervals:
alter session set nls_date_format = 'DD-Mon-YYYY HH24:MI:SS'; with data as ( select to_date('28-Jun-2013 09:00', 'DD-Mon-YYYY HH24:MI') start_time, to_date('28-Jun-2013 18:00', 'DD-Mon-YYYY HH24:MI') end_time from dual union all select to_date('28-Jun-2013 20:00', 'DD-Mon-YYYY HH24:MI') start_time, to_date('28-Jun-2013 23:00', 'DD-Mon-YYYY HH24:MI') end_time from dual ) select start_time, end_time, case when lead(to_char(start_time, 'HH24'), 1, (select min(to_char(start_time, 'HH24')) from data)) over (order by to_char(start_time, 'HH24')) not between to_char(start_time, 'HH24') and to_char(end_time, 'HH24') then to_char(end_time, 'HH24:MI') || ' - ' || lead(to_char(start_time, 'HH24:MI'), 1, (select min(to_char(start_time, 'HH24:MI')) from data)) over (order by to_char(start_time, 'HH24:MI')) else null end period from data START_TIME END_TIME PERIOD ------------------------- ------------------------- ------------- 28-Jun-2013 09:00:00 28-Jun-2013 18:00:00 18:00 - 20:00 28-Jun-2013 20:00:00 28-Jun-2013 23:00:00 23:00 - 09:00
Time information should not be stored in additional fields Varchar, if you have the Date column. You can use Date fields that store the Date and time together.
Another way to approach this problem is to connect by Clause, or model. Use the search feature to find solutions using these methods too. However, in my opinion, this method is the fastest of all.
Maybe you are looking for
-
Macintosh 128 K will not be powered up, unfortunately.
7 months ago I used my Macintosh 128 K, and it still works in 2015. But then, when I left it in my closet, I watched it again since he disappeared on October 31, 2015, he's dead, and it won't turn. I tried force start, and recharging for 3 hours, it
-
Product: Laptop HP 15-g029wm Original OS: Windows 8 Currently installed: Windows 7 Recently, I've killed my Windows 8 HP 15 - g029wm Notebook for Windows 7 Ultimate. Of course because of the problems with the drivers and drivers 64 bit Windows 7 on H
-
How do I get off my desktop computer files but save in my pictures?
help please?
-
command to search for windows is genuine or not? challenge
command to search for windows is genuine or not? can someone answer this question?
-
How do I post messages on a Web site
Hey everybody, I was wondering if there is a way to display messages on the Web site using an application I created. I know it has to do with the API - but I was wondering how would the structure of code for such a task. I have a text input and a but