correct to write this query mysql syntax?
Hello
Help to write a mysql should question that I don't have the knowledge to do so.
I have 2 tables, say table1 and table2. I want to take (status) column data in table 1 and replace what is in the (state_id) column in table 2.
Notes:
The column names do not match.
I can match the user ID unique from table1 to table2, these are the same.
I want only to replace data in a few columns, not the entire table, other data must remain unchanged.
I want to m data in the tables themselves from one to the other, not only compiled as a result of a mysql query.
Wasn't sure if I should use a merger? tried several ways but without success.
any person in charge to write something that I can use? I'm sure its easy to write, I just can't write advanced queries on top of my head like that :-)
Okay, it looks like that MySQL does not support the FROM clause in the update instructions. Use a join:
http://www.electrictoolbox.com/article/MySQL/cross-table-update/
Tags: Dreamweaver
Similar Questions
-
No idea how to write this query
Hi, My Data is as below
DocNum doc_date type of amount
1154 15 November 11 232501.5 invoice
200206 4 November 11 - 243672.64 credit memo
Note flow 111 5 November 555.22 11
Output must be
DocNum doc_date amount Type AmountDR AmountCR
1154 232501.5 15 November 11 Bill 232501.5
Note credit 200206 4 November 11 - 243672.64 - 243672.64
Note flow 111 5 November 555.22 11 555.22
If the amount is > 0, then it must be displayed in the value of the amount to be AmountDR
If amount < 0 then it must be displayed in the value of the sum amount CR
Can help how to write this querywith sample_table as ( select 1154 Docnum,date '2011-11-15' doc_date,232501.5 Amount,'Invoice' type from dual union all select 200206,date '2011-11-04',-243672.64,'Credit Memo' from dual union all select 111,date '2011-11-05',555.22,'Debit Memo' from dual ) select Docnum, doc_date, Amount, type, case when Amount >= 0 then Amount end AmountDR, case when Amount < 0 then Amount end AmountCR from sample_table / DOCNUM DOC_DATE AMOUNT TYPE AMOUNTDR AMOUNTCR ---------- --------- ---------- ----------- ---------- ---------- 1154 15-NOV-11 232501.5 Invoice 232501.5 200206 04-NOV-11 -243672.64 Credit Memo -243672.64 111 05-NOV-11 555.22 Debit Memo 555.22 SQL>
SY.
-
How to write this query in the hierarchy
Hi gurus,
Really need your help on this query. Thank you very much in advance.
SELECT t1.key as root_key , (SELECT t2.unit_id AS unit_id level-1 AS level , t2.name, t2.creator FROM tab t2 START WITH t2.unit_id = t1.unit_id -----check each node as root CONNECT BY prior t2.unit_id = t2.parent_unit_id ) t1.name as parent_unit_name FROM tab t1
I'll write a query of the hierarchy as above, and that EACH line (node, totally more than 10200) is checked as root node to see how many sheets are accessible for her... It must be implemented in a single query.
I know inline query should NOT return multiple rows or multiple columns, but the inline elements are necessary and can certainly be made in a correct solution.
(env):
Database Oracle 12 c Enterprise Edition Release 12.1.0.2.0 - 64 bit Production
PL/SQL Release 12.1.0.2.0
)
Test data:
select 1 as unit_id, null as parent_organization_unit_id, 'U1' as name from dual union all select 2, 1, 'U2' FROM DUAL UNION ALL SELECT 3, NULL, 'U3' FROM DUAL UNION ALL SELECT 4, 3, 'U4' FROM DUAL UNION ALL SELECT 5, 2, 'U5' FROM DUAL UNION ALL SELECT 6, 5, 'U6' FROM DUAL UNION ALL SELECT 7, 6, 'U7' FROM DUAL UNION ALL SELECT 8, 5, 'U8' FROM DUAL UNION ALL SELECT 9, 5, 'U9' FROM DUAL;
Final result should be like this
key unit_id, level, name, parent_name 1 1 0 u1 u1 1 2 1 u2 u1 1 5 2 u5 u1 1 6 3 u6 u1 1 7 4 u7 u1 1 8 3 u8 u1 1 9 3 u9 u1 2 2 0 u2 u2 2 5 1 u5 u2 2 6 2 u6 u2 2 7 3 u7 u2 2 8 2 u8 u2 2 9 2 u9 u2
Don't know how get you your output, it does not match your data...
with tab as)
Select 1 as unit_id, null as parent_organization_unit_id 'U1' as the name of double
Union of all the
Select 2, 1, 'U2' FROM DUAL
UNION ALL
SELECT 3, NULL, 'U3' FROM DUAL
UNION ALL
SELECT 4, 3, 'U4' FROM DUAL
UNION ALL
SELECT 5, 2, 'U5' OF THE DOUBLE
UNION ALL
SELECT 6, 5, 'U6' OF THE DOUBLE
UNION ALL
SELECT 7, 6, "U7" OF THE DOUBLE
UNION ALL
SELECT 8, 5, 'U8' FROM DUAL
UNION ALL
9. SELECT, 5, 'U9' FROM DUAL
)
Select dense_rank() key (order by connect_by_root unit_id), unit_id, level - 1 as 'LEVEL', connect_by_root name root_parent_name
t tab
Start with parent_organization_unit_id is null
Connect prior unit_id = parent_organization_unit_id
KEY UNIT_ID LEVEL ROOT_PARENT_NAME 1 1 0 "U1". 1 2 1 "U1". 1 5 2 "U1". 1 6 3 "U1". 1 7 4 "U1". 1 8 3 "U1". 1 9 3 "U1". 2 3 0 "U3". 2 4 1 "U3". -
Hello
I have a problem and I realized a simplified version of it:
The id of the requirement to join these two tables based on:create table deals (id_prsn number, id_deal number, fragment number); create table deal_values (id_prsn number, id_deal number, value_ number, date_ date); insert into deals values(1,1,50); insert into deals values(2,2,40); insert into deals values(1,3,50); insert into deals values(2,4,80); insert into deals values(1,5,20); insert into deals values(2,6,80); insert into deal_values values(1,1,10 ,sysdate - 3); insert into deal_values values(2,2,208, sysdate - 3); insert into deal_values values(2,4,984, sysdate - 3); insert into deal_values values(1,null,134,sysdate - 3); insert into deal_values values(1,1,13, sysdate - 2); insert into deal_values values(2,2,118, sysdate - 2); insert into deal_values values(2,4,776, sysdate - 1); insert into deal_values values(1,null,205,sysdate - 1); insert into deal_values values(2,null,-5,sysdate - 1);
1.) ID_PRSN and ID_DEAL
2.) max DATE_ grouped per person and deal
(3.) in the case that ID_DEAL is defined in the AGREEMENTS, but not defined in the DEAL_VALUES table, I have to join this records to DEAL_VALUES based on the person where id_Deal is null.
Number 3 gives me headache. I realized the following query:
It returns the correct result of he,select *from ( select a.id_prsn, a.id_deal, a.fragment, b.value_, b.date_, max(b.date_) over (partition by b.id_prsn, b.id_deal) max_date from deals a inner join deal_values b on a.id_deal = b.id_deal or b.id_deal is null and not exists (select 1 from deal_values where id_prsn = a.id_prsn and id_deal = a.id_deal) and a.id_prsn = b.id_prsn ) where date_ = max_Date;
ID_PRSN ID_DEAL FRAGMENT VALUE_ DATE_ MAX_DATE
1 1 50 13 16.10.2012 09:59:48 16.10.2012 09:59:48
1 3 50 205 17.10.2012 09:59:48 17.10.2012 09:59:48 OK
1 5 20 205 17.10.2012 09:59:48 17.10.2012 09:59:48 OK
2 2 40 118 16.10.2012 09:59:48 16.10.2012 09:59:48
2 4 80 776 17.10.2012 09:59:48 17.10.2012 09:59:48
2 6 80-5 17.10.2012 09:59:48 17.10.2012 09:59:48 OK
but the join clause:
in fact the query much slower.on a.id_deal = b.id_deal or b.id_deal is null and not exists (select 1 from deal_values where id_prsn = a.id_prsn and id_deal = a.id_deal) and a.id_prsn = b.id_prsn
I was wondering is there a different way to write this join and manage the logic.
Thanks in advanceHere's a different approach:
select * from ( select a.id_prsn, a.id_deal, a.fragment, B.value_, b.date_, ROW_NUMBER() over( partition by a.ID_PRSN, a.ID_DEAL order by B.ID_DEAL nulls last, B.DATE_ desc ) RN from DEALS a join DEAL_VALUES B on a.ID_PRSN = B.ID_PRSN and a.ID_DEAL = NVL(B.ID_DEAL, a.ID_DEAL) ) where rn = 1 order by 1, 2;
"nulls last" is the default sort order; I just put that for clarity.
Published by: stew Ashton on October 18, 2012 12:58
-
How to write this query?
Hi people,
I need to get a query in which a set of records, I get ONLY those which previous registry has a field with a value to this topic. Other values, the field can contain are not necessary.
I know that sounds easy but... I can't get it.
So, for Oracle 10 g 2... Here's my query:
SELECT a.person_id, a.person_status, a.message_id, a.order_id
OF t_HR one
WHERE a.person_status = "rejected".
AND a.id >
(SELECT max (b.id)
OF t_HR b
WHERE b.person_id = a.person_id
and b.order_id = a.order_id
AND b.person_status! "revised =".
B.ID AND < a.id)
ORDER BY desc a.id
Let me explain:
1 - HR table is a table of people. These people has serveral STATUS.
2 - ID is a sequential (each www.voyages-sncf.com has a different identification number).
3 - the application must get THAT all people "rejected".
4. - However, (subquery) I need ONLY those that previous register (the second register) holds a status of "OK". If the person holds a "revised" status he's not, he should be the next register (the third)
5.-L' ORDER ID DESC, so is the first register must have a STATUS = "rejected" and the second a 'OK '. IF the second register = "revised", then the third register must be 'OK '. And I need this query.
HOW DO?
My problem: the subquery gives you previous register of the same guy, but... it does not give you the value of the State, I need, which is 'OK '.
I tried to add to the subquery...
SELECT max (b.id)
OF mod_human_resource b
WHERE b.person_id = a.person_id
and b.order_id = a.order_id
AND b.person_status = 'OK '.
AND b.id < a.id
... but if I have 5 records of that person, the first is "rejected", the second is "accepted", the third is 'new' and the fourth is 'OK'... the subquery gives you the 4th register and which is not correct for me, it must be only the second one (prior to the first State registry).
I need to be a query, because I need to use it on a MERGER for a DWH.
If there is another way (function, or even a procedure) to make the MERGER rather than with a request, which would be ok too. I am poor DWH knowledge.
Thanks in advance.
Hello
So, you need to know if a line is the 'first' line, and you should also know what is the 'next' status, (even the 'first' and 'next' are already defined). This sounds like a job for analytical functions. ROW_NUMBER can tell you if a line is first or not, and LEAD can tell you what a value on the next row.
Since you post CREATE TABLE and INSERT statements for your own table, I'll use the table scott.emp to illustrate.
Consider these data from scott.emp:
SELECT DeptNo
ename
work
FROM scott.emp
ORDER BY deptno
ename DESC
;
Output:
DEPTNO ENAME JOB
---------- ---------- ---------
10 MILLER CLERK
PRESIDENT OF KING 10
MANAGER 10 CLARK
20 SMITH CLERK
ANALYST SCOTT 20
20 JONES MANAGER
20 FORD ANALYST
20 ADAMS CLERK
30 WARD SALESMAN
SELLER OF 30 TURNER
30 MARTIN SALESMAN
30 JAMES CLERK
MANAGER BLAKE 30
30 ALLEN SALESMAN
Now, let's say we want only who know the departments where the forefront (in order descending ename) a job = 'CLERK', and the following line (also in descending by ename order) = "ANALYST" job, and we want to know the ename of the first row. In other words, the correct output is:
DEPTNO ENAME
---------- ----------
20 SMITH
Note that deptno = 10 is not included, even if the first task is to "CLERK." that was because the second job in deptno = 10 is the "PRESIDENT", not "ANALYST."
Here's a way to get these results:
WITH got_analytics AS
(
SELECT ename, deptno, job
ROW_NUMBER () OVER (PARTITION BY deptno
ORDER BY ename DESC
) AS r_num
LEAD (employment) OVER (PARTITION BY deptno
ORDER BY ename DESC
) AS next_job
FROM scott.emp
)
SELECT deptno, ename
OF got_analytics
WHERE r_num = 1
AND job = 'CLERK '.
AND next_job = 'ANALYST '.
;
I hope that answers your question.
If this isn't the case, then, as Dan (and the FAQ forum) said, post CREATE TABLE and INSERT statements for some sample data and the exact results you want from these data.
Post your query, based on the one I have posted more top and ponit out where he gets results.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum: https://forums.oracle.com/message/9362002#9362002
-
Find a duplicate query MySQL syntax
There is a request to "find duplicates" in SQL which is very useful, but I'm not sure what the syntax is for MySQL. It probably has something to do with a distinct group, chosen by but I'm not used to the use of these.
It is he who finds all duplicates [FullName]
SELECT CONTACTS.*
WHERE (((CONTACTS. (FULLNAME) In (SELECT [FULLNAME] [CONTACTS] As Tmp GROUP BY [FULLNAME])
After HAVING Count (*) > 1)))
ORDER OF CONTACTS. FULLNAME, CONTACTS.COMPANY
Anyone know?RichardODreamweaver wrote:
> There is a request to "find duplicates" in SQL which is very useful, but I don't know
> What's the syntax for MySQL.SELECT COUNT (*) AS, FULLNAME, COMPANY reps
CONTACTS
GROUP BY FULLNAME, COMPANY
HAVE > 1 repetitionsIt is not something that I have used myself, but I got from MySQL Cookbook
by Paul DuBois, published by O'Reilly. I have the first edition, which
covers MySQL 4.0. I think that the second edition covers MySQL 5.0.If you use MySQL on a regular basis, you or your employer must
Buy immediately. It will be to solve most of your problems and repay its
cost several times.--
Adobe Community Expert David Powers
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/ -
How to write this query procedure of ina
IF ((drapeau = ' n ') OR (flag = 'F') OR (FLAG =' WAS))
) THEN
SELECT CUSTOMER_ID, BUSINESS_PASSCODE, LOG ON TO V_CUSTOMER_ID, V_BUSINESS_PASSCODE, V_LOG OF THE CUSTOMER
WHERE V_BUSINESS_PASSCODE AND V_CUSTOMER_ID = & ID = & CODE AND
V_LOG IN('Y')
SO I HAVE TO RETURN THE ISP (OF TYPE VARCHAR) CAN YOU TELL HOW DOMake values flag, id, code as a parameter and an output parameter. You can have any number f settings if you want to return the results of the query.
as
create procedure (identification number, varchar2, varcahr2, out returnvalue flag code varchar2)
as
Start
-your code---
--
IF ((drapeau = ' n ') OR (flag = 'F') OR (FLAG =' WAS))
) THEN
SELECT CUSTOMER_ID, BUSINESS_PASSCODE, LOG ON TO V_CUSTOMER_ID, V_BUSINESS_PASSCODE, V_LOG OF THE CUSTOMER
WHERE V_CUSTOMER_ID = ID AND CODE = V_BUSINESS_PASSCODE AND
V_LOG IN('Y');
returnValue =; -
which way is the best to write this query?
Hello
consider the following 2 queries
SCENARIO 01
=========
Select a.company_name, a.company_address, a.company_contact, b.item_name, b.item_unit_price
of company_master an inner join company_items b on a.company_id = b.company_id
SCENARIO 02
=========
Select a.company_name, a.company_address, a.company_contact, b.item_name, b.item_unit_price
company_master a, company_items b
where a.company_id = b.company_id
which method is the fastest? (more than 1000 companies and 1000000 + elements) is 01 or 02?
and what is the 2nd method usually called? ;)novicedba wrote:
Hello
consider the following 2 queriesSCENARIO 01
=========
Select a.company_name, a.company_address, a.company_contact, b.item_name, b.item_unit_price
of company_master an inner join company_items b on a.company_id = b.company_idSCENARIO 02
=========
Select a.company_name, a.company_address, a.company_contact, b.item_name, b.item_unit_price
company_master a, company_items b
where a.company_id = b.company_idwhich method is the fastest? (more than 1000 companies and 1000000 + elements) is 01 or 02?
and what is the 2nd method usually called? ;)Queries are probably the same, the difference being one are written in ANSI SQL and the other in SQL ANSI traditional pre. To be sure, test the two - get execution plans, execution and execute the metrics.
What is the best, it is to you. Some people prefer ANSI that uses IT to indicate the joins and WHERE indicated condtions filter; Personally, I prefer the traditional syntax it is easier to read.
Outer joins make queries run more slowly. Avoid them if you can. Outer joins are used to return a row in a table if the join data does not exist in the other table. Once again, they degrade performance.
Cross joins are almost never necessary. They are useful to indicate joins Cartesian intentional for the results of Matrix Games. Cartesian joins are notorious for using too many system resources (especially when you work with a large amount of data) and returrning of incorrect results and can happen by running a join without specifying a join condition: all columns will be joined between the two tables, even when they do not match. In 10g and 11g the optimizer sometimes will perfom Cartesian joins make queries run faster but is always scary to see the "Cartesian" word in an execution plan
-
Need a help with this query max
Select SEARCH_ID, SEARCH_KEYWORD, ASSET_TYPE, COUNT of RELEVANCY_TABLE
where SEARCH_KEYWORD = 'search_keyword '.
and ASSET_TYPE is not null
558 search_keyword 3 Office 559 search_keyword 7 Table In fact, I have to get the asset_type which the count is the maximum. In this case, it should be "table". Any help?
adfSonal wrote:
Is there another way? I have to write this query in Java. So, I prefer avoiding rank or such functions.
I will not get the desired result using simply select, where, max, rownum clauses?
What do you mean by "I must write this query in Java? Means of the query will be run against a database of Oracle, correct?
Any means using ROWNUM
Select *.
de)
Select search_id
search_keyword
County
asset_type
of relevancy_table
where search_keyword = 'search_keyword '.
and asset_type is not null
Order
by count desc
)
where rownum = 1
-
Another way to write the query:
Dear Experts, I am a beginner in sql. With my knowledge, I wrote this query
but he took great moment in my database 11g
Please tell any alternative to write this query in another way to increase performance
Please find the explanation below
SELECT DISTINCT * FROM UPGRADATION r1, LOG t2, OMNIACCOUNT m1, (SELECT rid , LISTAGG(ACCOUNTNO, ',') WITHIN GROUP (ORDER BY ACCOUNTNO) AS ACCOUNTNO FROM OMNIACCOUNT GROUP BY rid) mk3, (SELECT mobileno , LISTAGG(ACCOUNTNO, ',') WITHIN GROUP (ORDER BY ACCOUNTNO) AS ACCOUNTNO FROM OMNIACCOUNT GROUP BY mobileno) mk1, (SELECT mobileno , LISTAGG(ACCOUNTNO, ',') WITHIN GROUP (ORDER BY accounttype) AS ACCOUNTTYPE FROM OMNIACCOUNT GROUP BY mobileno) mk2 WHERE t2.txnauthid = r1.txnauthid AND r1.rid = mk3.rid AND t2.txntype = '54' AND mk1.mobileno = r1.regmobileno AND t2.reqtype = '11' AND t2.dc_id IN (SELECT deliverychannel_id FROM Channel) AND t2.custbankcode = '32472' AND t2.txndatetime BETWEEN TO_DATE ('28/12/2011 12:00:00 AM', 'DD/MM/YYYY hh:mi:ss AM' ) AND TO_DATE ('28/12/2012 11:59:59 PM', 'DD/MM/YYYY hh:mi:ss PM' ) AND r1.rid = m1.rid(+) AND t2.txnstatus IN ('C', 'F', 'T') AND t2.paymentinitiator = 'C' AND mk1.mobileno = mk2.mobileno Explain Plan For the above statement : ----------------------------------- SELECT STATEMENT FIRST_ROWSCost: 16 Bytes: 4,270 Cardinality: 1 25 HASH UNIQUE Cost: 16 Bytes: 4,270 Cardinality: 1 24 NESTED LOOPS Cost: 15 Bytes: 4,270 Cardinality: 1 19 NESTED LOOPS Cost: 14 Bytes: 4,270 Cardinality: 1 13 HASH JOIN Cost: 13 Bytes: 2,268 Cardinality: 1 9 NESTED LOOPS OUTER Cost: 4 Bytes: 252 Cardinality: 1 7 NESTED LOOPS Cost: 3 Bytes: 239 Cardinality: 1 4 NESTED LOOPS Cost: 2 Bytes: 119 Cardinality: 1 2 TABLE ACCESS BY INDEX ROWID TABLE LOG Cost: 1 Bytes: 116 Cardinality: 1 1 INDEX RANGE SCAN INDEX IX_LOG Cost: 1 Cardinality: 8 3 INDEX UNIQUE SCAN INDEX (UNIQUE) PK_Channel Cost: 1 Bytes: 3 Cardinality: 1 6 TABLE ACCESS BY INDEX ROWID TABLE UPGRADATION Cost: 1 Bytes: 120 Cardinality: 1 5 INDEX RANGE SCAN INDEX IDX_REG_AUTH Cost: 1 Cardinality: 1 8 INDEX RANGE SCAN INDEX IDX_MLTN_BNK_ACCT Cost: 1 Bytes: 13 Cardinality: 1 12 VIEW Cost: 8 Bytes: 1,110,816 Cardinality: 551 11 SORT GROUP BY Cost: 8 Bytes: 16,530 Cardinality: 551 10 TABLE ACCESS FULL TABLE OMNIACCOUNT Cost: 7 Bytes: 21,210 Cardinality: 707 18 VIEW PUSHED PREDICATE Cost: 1 Bytes: 2,002 Cardinality: 1 17 FILTER 16 SORT GROUP BY Bytes: 33 Cardinality: 1 15 TABLE ACCESS BY INDEX ROWID TABLE OMNIACCOUNT Cost: 1 Bytes: 33 Cardinality: 1 14 INDEX RANGE SCAN INDEX IDX_MUL_LNKG Cost: 1 Cardinality: 1 23 VIEW PUSHED PREDICATE Cost: 1 Cardinality: 1 22 FILTER 21 SORT GROUP BY Bytes: 29 Cardinality: 1 20 INDEX RANGE SCAN INDEX IDX_MLTN_BNK_ACCT Cost: 1 Bytes: 29 Cardinality: 1
It would be best to leave the desired projection.
Probably you don't really want all columns form this query.
However, these are your join criteria:t2.txnauthid = r1.txnauthid AND r1.rid = mk3.rid AND r1.regmobileno = mk1.mobileno AND mk1.mobileno = mk2.mobileno AND r1.rid = m1.rid(+)
Maybe you can get rid of AND mk1.mobileno = mk2.mobileno
with(SELECT mobileno , LISTAGG(ACCOUNTNO, ',') WITHIN GROUP (ORDER BY ACCOUNTNO) AS ACCOUNTNO , LISTAGG(ACCOUNTNO, ',') WITHIN GROUP (ORDER BY accounttype) AS ACCOUNTTYPE FROM OMNIACCOUNT GROUP BY mobileno)
After the combination seems to be suspicious:
AND r1.rid = mk3.rid AND r1.rid = m1.rid(+)
The first limit the result on all lines with the same id in
OMNIACCOUNT and UPGRADATION
so, o see no gain from the outer join.Do you really need the listagg on all OMNIACCOUNT lines.
If this isn't the case, it would be probably better to do the listagg in the final projection.Maybe one way could be to start with OMNIACCOUNT and join outer UPGRADATION to it.
You can do the listagg at the end.Maybe something like
select * from OMNIACCOUNT m1 left outer join (Select projection goes here from log t2 join UPGRADATION r1 on r1.txnauthid = t2.txnauthid AND t2.reqtype = '11' AND t2.dc_id IN (SELECT deliverychannel_id FROM Channel) AND t2.custbankcode = '32472' AND t2.txndatetime BETWEEN TO_DATE ('28/12/2011 12:00:00 AM', 'DD/MM/YYYY hh:mi:ss AM' ) AND TO_DATE ('28/12/2012 11:59:59 PM', 'DD/MM/YYYY hh:mi:ss PM' ) AND t2.txntype = '54' AND t2.txnstatus IN ('C', 'F', 'T') AND t2.paymentinitiator = 'C' ) r1 on m1.rid=r1.rid AND r1.regmobileno = m1.mobileno
Published by: chris227 on 10.01.2013 01:45
fix -
Why between date is not return data for this query?
Hello
I have a table with this structure and I write this query to retrieve a few lines based on certain conditions, but this query returns no data. Can you please tell why?
ID DT 003 11/8/2011 002 10/8/2011 001 9/8/2011
And the execution of the query:
Published by: starting August 13, 2011 07:10SELECT * FROM TABLE_NAME WHERE DT BETWEEN TO_DATE('08/08/2011','dd/mm/yyyy') AND TO_DATE('12/08/2011','dd/mm/yyyy');
>
>
But what is the problem with that, why this date does not match when I'm providing the date format?
What part don't you understand? You have not used TO_DATE when inserting data and default date format is dd/mm/yyyy, right? Same default date format is used if you are running:
SELECT * FROM TABLE_NAME
Original of your post States select returns above:
ID DT 003 11/8/2011 002 10/8/2011 001 9/8/2011
So the dates that you inserted are November 8, 2011, October 8, 2011-September 8, 2011. TO_DATE('08/08/2011','dd/mm/yyyy') is now August 8, 2011 and TO_DATE('12/08/2011','dd/mm/yyyy') is August 12, 2011. Then of course:
SELECT * FROM TABLE_NAME WHERE DT BETWEEN TO_DATE('08/08/2011','dd/mm/yyyy') AND TO_DATE('12/08/2011','dd/mm/yyyy').
will return all the lines. Bottome line - never write code that uses the implicit conversions date since your code becomes dependent on the NLS client settings and maybe working for a client and fail or produce erroneous results for other customers.
SY.
-
How to rewrite this query to get the correct results?
Friends,
DB: 9iR2
I need to get the name of the employee and the employee number that are not in the table of presence.
but this query is not the right answer.
Thank youselect e.eno,e.ename from empl e where e.eno not in (select a.eno from attendance a)
Depending on your data
SQL> create table attendance( 2 ENO VARCHAR2(5), 3 TDATE VARCHAR2(10), 4 IN_TIME VARCHAR2(6), 5 OUT_TIME VARCHAR2(6), 6 SHIFT_NO NUMBER(1)); Table created. SQL> create table empl( 2 ENO VARCHAR2(5), 3 ENAME VARCHAR2(75)); Table created. SQL> insert into empl values('11','AA'); 1 row created. SQL> insert into empl values('12','AB'); 1 row created. SQL> insert into empl values('13','AC'); 1 row created. SQL> insert into empl values('14','AD'); 1 row created. SQL> insert into empl values('15','AF'); 1 row created. SQL> insert into attendance values('11','23-3-2009','9.00','6.00',1); 1 row created. SQL> insert into attendance values('14','24-3-2009','9.00','6.00',1); 1 row created. SQL> insert into attendance values('11','25-3-2009','9.00','6.00',1); 1 row created. SQL> insert into attendance values('13','23-3-2009','9.00','6.00',1); 1 row created. SQL> insert into attendance values('15','23-3-2009','9.00','6.00',1); 1 row created. SQL> commit; Commit complete. select e.eno,e.ename from empl e where not exists(select 1 from attendance a where a.eno=e.eno); ENO ENAME 12 AB
Twinkle
-
Please help me write this SQL query...
Thanks in advanceHi everyone, Please help me in this query. A patient can multiple types of Adresses (types P,M,D).If they have all the 3 types i need to select type: p and if they have (M and D) i need to select type M,and if they have only type D i have to select that. For each address i need to validate whether that particular address is valid or not (by start date and end date and valid flag) Patient table ============= Patient_id First_name last_name 1 sanjay kumar 2 ajay singh 3 Mike John Adress table ============ address_id patient_id adresss city type startdate enddate valid_flg 1 1 6222 dsadsa P 01/01/2007 01/01/2010 2 1 63333 dsad M 01/02/2006 01/01/2007 N 3 1 64564 fdf M 01/01/2008 07/01/2009 4 1 654757 fsdfsa D 01/02/2008 09/10/2009 5 2 fsdfsd fsdfsd M 01/03/2007 09/10/2009 6 2 jhkjk dsad D 01/01/2007 10/10/2010 7 3 asfd sfds D 01/02/2008 10/10/2009 output ===== 1 sanjay kumar 6222 dsadsa P 01/01/2007 01/01/2010 2 ajay singh fsdfsd fsdfsd M 01/03/2007 09/10/2009 3 mike john asfd sfds D 01/02/2008 10/10/2009
PhaniHello, Fabienne,.
This race for you (twisted code of Sarma):
SELECT patient_id, first_name, last_name, address, city, type, startdate, enddate FROM ( SELECT a.patient_id patient_id, first_name, last_name, address, city, type, startdate, enddate, ROW_NUMBER() OVER (PARTITION BY p.patient_id ORDER BY CASE type WHEN 'P' THEN 1 WHEN 'M' THEN 2 WHEN 'D' THEN 3 END) rn FROM patient p JOIN address a ON (p.patient_id = a.patient_id ) WHERE NVL(valid_flg, 'X') != 'N' AND SYSDATE BETWEEN startdate AND NVL(enddate, SYSDATE) ) WHERE rn = 1;
Edit, currently in the trial:
With Patient AS ( SELECT 1 Patient_id , 'sanjay' First_name, 'kumar' last_name FROM DUAL UNION ALL SELECT 2, 'ajay', 'singh' FROM DUAL UNION ALL SELECT 3, 'Mike', 'John' FROM DUAL), Address AS ( SELECT 1 address_id, 1 patient_id, '6222' address, 'dsadsa' city, 'P' type, to_date('01/01/2007', 'DD/MM/YYYY') startdate, to_date('01/01/2010', 'DD/MM/YYYY') enddate, NULL valid_flg FROM DUAL UNION ALL SELECT 2,1,'63333','dsad','M', to_date('01/02/2006', 'DD/MM/YYYY'), to_date('01/01/2007', 'DD/MM/YYYY'), ' N' FROM DUAL UNION ALL SELECT 3,1,'64564','fdf','M', to_date('01/01/2008', 'DD/MM/YYYY'), to_date('07/01/2009', 'DD/MM/YYYY'), NULL FROM DUAL UNION ALL SELECT 4,1,'654757','fsdfsa','D', to_date('01/02/2008', 'DD/MM/YYYY'), to_date('09/10/2009', 'DD/MM/YYYY'), NULL FROM DUAL UNION ALL SELECT 5,2,'fsdfsd ','fsdfsd','M', to_date('01/03/2007', 'DD/MM/YYYY'), to_date('09/10/2009', 'DD/MM/YYYY'), NULL FROM DUAL UNION ALL SELECT 6,2,' jhkjk','dsad','D', to_date('01/01/2007', 'DD/MM/YYYY'), to_date('10/10/2010', 'DD/MM/YYYY'), NULL FROM DUAL UNION ALL SELECT 7,3,'asfd',' sfds',' D', to_date('01/02/2008', 'DD/MM/YYYY'), to_date('10/10/2009', 'DD/MM/YYYY'), NULL FROM DUAL) -- end test data SELECT patient_id, first_name, last_name, address, city, type, startdate, enddate FROM ( SELECT a.patient_id patient_id, first_name, last_name, address, city, type, startdate, enddate, ROW_NUMBER() OVER (PARTITION BY p.patient_id ORDER BY CASE type WHEN 'P' THEN 1 WHEN 'M' THEN 2 WHEN 'D' THEN 3 END) rn FROM patient p JOIN address a ON (p.patient_id = a.patient_id ) WHERE NVL(valid_flg, 'X') != 'N' AND SYSDATE BETWEEN startdate AND NVL(enddate, SYSDATE) ) WHERE rn = 1; PATIENT_ID FIRST_ LAST_ ADDRESS CITY TY STARTDATE ENDDATE ---------- ------ ----- ------- ------ -- --------- --------- 1 sanjay kumar 6222 dsadsa P 01-JAN-07 01-JAN-10 2 ajay singh fsdfsd fsdfsd M 01-MAR-07 09-OCT-09 3 Mike John asfd sfds D 01-FEB-08 10-OCT-09
-
Can anyone simplify this query
Here's a DOF from two tables
1. CREATE TABLE (EMPL)
NUMBER OF SNO
ENAME VARCHAR2 (25).
USE VARCHAR2 (25).
KEY ELEMENTARY SCHOOL (SNO)
);
2. CREATE TABLE EMPL_DET)
NUMBER OF SNO
SAL VARCHAR2 (25)
);
Here are tables LMD
INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (1, 'SMITH', 'CLERK');
INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (2, 'SMITH', 'MANAGER');
INSERT INTO EMPL (SNO, ENAME, JOB) VALUES (3, 'TOM', 'CLK');
INSERT INTO EMPL_DET (SNO, SAL) VALUES (1, '1000');
INSERT INTO EMPL_DET (SNO, SAL) VALUES (2, "10000");
INSERT INTO EMPL_DET (SNO, SAL) VALUES (3, '900');
I want to calculate TotalSAL (column: empl_det.) SAL) of each employee (empl.ename) with job-description (empl.job).
Means I want following the lines of output
1.(Job,TotalSAL,Ename)-> (CLERK, 11000, SMITH)
2->.(Job,TotalSAL,Ename) (MANAGER, 11000, SMITH)
3->.(Job,TotalSAL,Ename) (CLK, 900, TOM)
I tried to write down to unique ename
Select JOB, x.sal, ename in empl,
(
Select sum (sal) sal empl_det where sno in
(select sno to empl where ename = 'SMITH')
) x
where ename = 'SMITH '.
order by ename
each ename, I draw from this query. How can I make the ename list (TOM SMITH) to this request?
Or can anyone simplify this query?Hello
in this case, you need to use is analytical functions:
that is to say:
SELECT e.job, SUM (d.sal) OVER (PARTITION BY e.ename) AS "TotalSal" , e.ename FROM empl e, empl_det d WHERE d.sno = e.sno AND e.ename IN ('SMITH', 'TOM') ORDER BY e.ename; JOB TotalSal ENAME ------------------------- ---------- ------------------------- CLERK 11000 SMITH MANAGER 11000 SMITH CLK 900 TOM
Kind regards.
AlPublished by: Alberto Faenza on 27 November 2012 15:34
Corrected query -
Is it possible to set this query...
This query takes 10 minutes to run on Windows 2003, raid10, Oracle 10.2.0.4.Is it doesn't matter what part I can watch in optimizing the query...
Withdrawal of the request...
Published by: Maran Viswarayar on 13 Nov 2009 13:51
Published by: Maran Viswarayar on November 16, 2009 14:58Try something like this I tested with the substr function:
SQL> select substr(pad,2,3) from t where id=368; SUB --- WET SQL> select substr((select pad from t where id=368),2,3) from dual; SUB --- WET
I used the sql statement to replace the function parameter. Of course, it's stupid to use this way, if you do not need to do, but for your case, this could be a solution. In your case, you could write:
FROM TABLE (ADMIN.find_record_dtl ( 8, (select SUBSTR(pc.pay_calendar_id, 0,3) from pr_calendar pc where = SUBSTR(pc.pay_calendar_id, 0,3) = 'RET')), 'HQPRM003')
Therefore, you put any sql that returns the correct value for your second parameter to your function entry. I hope that I wrote the right of expression, like yesterday, I got some garbage inside the text when I copied it.
From the performance point of view I wouldn't function to extract the lines although it uses in BULK COLLECT but would rather use SQL function and integrate it within the main query and get something like:
...FROM( SELECT DISTINCT pc.pay_calendar_id, CASE WHEN ... END AS pay_amount, rd.employee_id, cal_month, data_name, data_desc, data_seq FROM (SELECT distinct report_id, employee_id, employee_name, pay_calendar_id, data_name, data_desc, data_seq, display_name, section_seq, item_id, base_amt, input_amt, calculated_amt FROM list_record_dtl) rd, pr_calendar pc WHERE pc.pay_calendar_id = rd.pay_calendar_id AND SUBSTR(pc.pay_calendar_id, 0,3) = 'RET' AND rd.employee_id = 'HQPRM003' and rd.report_id = 8 ) a RIGHT JOIN ....
So now you have two options yet :-)
Generally, I would prefer rather to not use the service for the following reasons:
(1.) the change of context between engine sql and pl/sql - Fortunately, in your case the function is executed only once (see column starts from your execution plan)
2.) xxx of the instances of the type of data pay_item_t to the result of the query within the service may use lots of memory. This memory is allocated to the PGA and would be seen in v$ process_memory in the category 'other '. Of course, you can also monitor the consumption of memory of PGA of v$ sesstat. Now you will use the three parameters to the function and fewer rows returned as it was before, when the function has returned 192076 lines.
3.) and the last one the more important one - if you use the online mode you give the CBO opportunity to optimize instruction. If you are using the CBO cannot push the predicates of main query within the query that is executed within the function and so it can be like a suboptimal plan even what exactly was your case when you started this thread. Was clearly the person who developed the conscious function of this problem and has tried to solve this problem by additional parameters that are passed to the function and are used to limit the number of rows returned by the function. Unfortunately these two parameters were not used in your case.
4.) it is always a good practice using only the resources you really need and not much more!Published by: Scheid Joze on 19.11.2009 10:50
Maybe you are looking for
-
Before the upgrade, I could use the right click to access bookmarks, now I have a load of junk that I don't use and no Favorites. How can I get my favorites via my back of mouse? Kind regards Ben
-
I lost the icon charge and all the legends with icons. Help.
I installed an update of Mozy, rebooted and found that firefox had lost the legends with the icons, the icon charge completely. Customize the box covers almost the entire page and doesn't include as much as he used to. My Autofill is over. And he has
-
Satellite A500-1EJ turns on itself - touchpad light problem
Hello, I just bought a Satellite A500-1EJ and I have two problems: It lights by tself and volume and the light button does not work properly.
-
Hi all I remember a standby policy WIFI he put failed standby wifi phone when white screen, there are one to sleep when it is plugged or never who should I put it on. which uses the less battery drain the defect affecting sleep when the white screen?
-
remote control will not control apps even after a reboot
Apple tv appears as usual, but the remote does not affect applications. I can restart buy unplug the apple TV Unit, but not by pressing down and the menu buttons.