How I can optimize this SQL query
I require your help, I want to know how I can optimize this query
SELECT
"F42119". "SDLITM" as "Code1"
"F42119". "SDAITM" as "Code2"
"F42119". "SDDSC1" as "Product"
"F42119". "SDMCU" as "Bodega"
Sum ("F42119". "SDSOQS" / 10000) as "Number",
Sum ("F42119". "SDUPRC" / 10000) as "preciou"
Sum ("F42119". "SDAEXP" / 100) as "Value",
Sum ("F42119". "SDUNCS" / 10000) as "CostoU"
Sum ("F42119". "SDECST" / 100) as "Cost"
"F4101". "IMSRP1" as "Division"
"F4101". "IMSRP2" as "classification",
"F4101". "IMSRP8" as "Brand"
"F4101". "IMSRP9" as "Aroma"
"F4101". "IMSRP0" as "Presentation"
"F42119". "SDDOC" as "Type",
"F42119". "SDDCT" as "Document",
"F42119". "SDUOM" as "Unit"
"F42119". "SDCRCD" as "currency"
"F0101". "ABAN8" as "ABAN8"
"F0101". "ABALPH" as "Customer"
"F0006". "MCRP22" as "Establishment"
from "PRODDTA". "F0101" "F0101"
"PRODDTA". "F42119" "F42119"
"PRODDTA". "F4101" "F4101"
"PRODDTA". "F0006" "F0006"
where "F42119". "SDAN8" = "F0101". "ABAN8"
and "F0006". "MCMCU" = "F42119". "SDMCU"
and "F4101". "IMITM" = "F42119". "SDITM"
and "F42119". "SDDCT" in ('RI', 'RM', 'RN')
and CAST (EXTRACT (MONTH FROM TO_DATE (substr ((to_date ('01-01-'| | to_char (round (1900 + (CAST ("F42119". "SDDGL" as int) / 1000))),' DD- MM- YYYY ') + substr (to_char (CAST ("F42119". "SDDGL" as int)), 4,3) -1), 1,10))) AS INT) in : Month
and CAST (EXTRACT (YEAR FROM TO_DATE (substr ((to_date ('01-01-'| | to_char (round (1900 + (CAST ("F42119". "SDDGL" as int) / 1000))),' DD- MM- YYYY ')+ Substr (to_char (CAST ("F42119". "SDDGL" as int)), 4,3) -1), 1,10))) AS INT) in: Year
and trim ("F0006". "MCRP22") =: Establishment
and trim ("F4101". "IMSRP1") =: Division
Group By "F42119". "SDLITM"
"F42119". "SDAITM"
"F42119". "SDDSC1"
"F4101". "IMSRP1"
"F42119". "SDDOC"
"F42119". "SDDCT"
"F42119". "SDUOM"
"F42119". "SDCRCD"
"F0101". "ABAN8"
"F0101". "ABALPH"
"F4101". "IMSRP2"
"F4101". "IMSRP8"
"F4101". "IMSRP9"
"F4101". "IMSRP0"
"F42119". "SDMCU"
"F0006". "MCRP22"
I appreciate the help you can give me
It seems to me that part of fixing it could be how you join the tables.
Instead of the humongous where clause, put the applicable conditions on the join.
You have
from "PRODDTA". "F0101" "F0101"
"PRODDTA". "F42119" "F42119"
"PRODDTA". "F4101" "F4101"
"PRODDTA". "F0006" "F0006"
where "F42119". "SDAN8" = "F0101". "ABAN8"
and "F0006". "MCMCU" = "F42119". "SDMCU"
and "F4101". "IMITM" = "F42119". "SDITM"
and "F42119". "SDDCT" in ('RI', 'RM', 'RN')
and CAST (EXTRACT (MONTH FROM TO_DATE (substr ((to_date ('01-01-'| | to_char (round (1900 + (CAST ("F42119". "SDDGL" as int) / 1000))),' DD- MM- YYYY ') + substr (to_char (CAST ("F42119". "SDDGL" as int)), 4,3) -1), 1,10))) AS INT) in : Month
and CAST (EXTRACT (YEAR FROM TO_DATE (substr ((to_date ('01-01-'| | to_char (round (1900 + (CAST ("F42119". "SDDGL" as int) / 1000))),' DD- MM- YYYY ')+ Substr (to_char (CAST ("F42119". "SDDGL" as int)), 4,3) -1), 1,10))) AS INT) in: Year
and trim ("F0006". "MCRP22") =: Establishment
and trim ("F4101". "IMSRP1") =: Division
INSTEAD try something like
from JOIN "PRODDTA". "F0101" "F0101" ON "F42119". "SDAN8" = "F0101". "ABAN8"
JOIN "PRODDTA". "F42119" "F42119" ON "F0006". "MCMCU" = "F42119". "SDMCU"
JOIN "PRODDTA". "F4101" "F4101" ON join condition
JOIN "PRODDTA". "F0006" "F0006" ON join condition.
Not sure exactly how you need things joined, but above is the basic idea. Remove criteria for joining the tables from the WHERE clause and put them
in the join statements. That might clean things up and make it more efficient.
Similar Messages
-
I don't know how I can optimize this SQL
Dear ALL:
I don't know how I can optimize this SQL.
Is it possible to make a better SQL or PL/SQL?
Please let me know your good thought.
Thank you.
Sincerely,
===========================================================
(SELECT A, B, C, SUM(D) as D, AVG(E) as E
FROM T1, T2
WHERE T1.timestamp BETWEEN TO_DATE('00:00:00','HH24:MI:SS')
AND TO_DATE('01:00:00','HH24:MI:SS')
GROUP BY A, B, C
UNION ALL
(SELECT A, B, C, SUM(D) as D, AVG(E) as E
FROM T1, T2
WHERE T1.timestamp BETWEEN TO_DATE('01:00:01','HH24:MI:SS')
AND TO_DATE('02:00:00','HH24:MI:SS')
GROUP BY A, B, C
UNION ALL
(SELECT A, B, C, SUM(D) as D, AVG(E) as E
FROM T1, T2
WHERE T1.timestamp BETWEEN TO_DATE('02:00:01','HH24:MI:SS')
AND TO_DATE('03:00:00','HH24:MI:SS')
GROUP BY A, B, C
UNION ALL
(SELECT A, B, C, SUM(D) as D, AVG(E) as E
FROM T1, T2
WHERE T1.timestamp BETWEEN TO_DATE('03:00:01','HH24:MI:SS')
AND TO_DATE('04:00:00','HH24:MI:SS')
GROUP BY A, B, C
ORDER BY A ASC, B ASC, C ASC
===========================================================Dear Warren:
Actually, for this query, it takes a few second to complete the query.
But, I have to create several time period and I have to optimize the SQL.
Time period is 1 hour, 30 min, 15 min, 5 min.
That is,
===========================================================
WHERE T1.timstamp BETWEEN TO_DATE('00:00:00','HH24:MI:SS')
AND TO_DATE('01:00:00','HH24:MI:SS')
UNION ALL
WHERE T1.timestamp BETWEEN TO_DATE('23:00:01','HH24:MI:SS')
AND TO_DATE('24:00:00','HH24:MI:SS')
===========================================================
WHERE T1.timstamp BETWEEN TO_DATE('00:00:00','HH24:MI:SS')
AND TO_DATE('00:30:00','HH24:MI:SS')
UNION ALL
WHERE T1.timestamp BETWEEN TO_DATE('23:30:01','HH24:MI:SS')
AND TO_DATE('24:00:00','HH24:MI:SS')
===========================================================
WHERE T1.timstamp BETWEEN TO_DATE('00:00:00','HH24:MI:SS')
AND TO_DATE('00:15:00','HH24:MI:SS')
UNION ALL
WHERE T1.timestamp BETWEEN TO_DATE('23:45:01','HH24:MI:SS')
AND TO_DATE('24:00:00','HH24:MI:SS')
===========================================================
WHERE T1.timstamp BETWEEN TO_DATE('00:00:00','HH24:MI:SS')
AND TO_DATE('00:05:00','HH24:MI:SS')
UNION ALL
WHERE T1.timestamp BETWEEN TO_DATE('23:55:01','HH24:MI:SS')
AND TO_DATE('24:00:00','HH24:MI:SS')
===========================================================
Do you know how this SQL is optimized?
Thank you, -
How i can execute the sql query in java code
I already have sql query in jave plateform i need to execute this code how i can do that. i have unix env and with oracle database. should i just run this query in my sqlplus. this file has extention .java. thanks
you can create a project in JDeveloper add the java file to it, add the Oracle JDBC library to the project properties and then hit the run button.
-
Is there anyway i could optimize the below mentioned SQL. Since our test DB is down now, i'll be posting the EXPLAIN PLAN later.
Is there anything i could do syntactically to optimize this sql?
SELECT SUBSTR(STK_INF.TASK_GENRTN_REF_NBR,1,12) AS PICK_WAVE_NBR,
( CASE WHEN ( SUM(STK_INF.QTY_ALLOC) > 0 ) THEN
ROUND ( (SUM(CASE WHEN (STK_INF.NEXT_TASK_ID = 0 AND STK_INF.ALLOC_INVN_DTL_ID = 0) THEN
STK_INF.QTY_ALLOC ELSE 0 END ) / (SUM(STK_INF.QTY_ALLOC))),2 ) * 100
ELSE 0 END ) AS PICK_PER,
( CASE WHEN ( SUM(STK_INF.QTY_ALLOC) > 0 ) THEN
ROUND( (SUM(CASE WHEN (STK_INF.NEXT_TASK_ID = 0 AND STK_INF.ALLOC_INVN_DTL_ID = 0) THEN
STK_INF.QTY_PULLD ELSE 0 END ) / (SUM(STK_INF.QTY_ALLOC))),2 ) * 100
ELSE 0 END ) AS TILT_PERCENT
FROM STK_INF, TRK_DTL
WHERE STK_INF.TASK_GENRTN_REF_CODE = '44' AND STK_INF.CARTON_NBR IS NOT NULL
AND ((STK_INF.NEXT_TASK_ID = 0 AND STK_INF.STAT_CODE <= 90) OR (STK_INF.NEXT_TASK_ID > 0 AND STK_INF.STAT_CODE < 99))
AND PULL_LOCN_ID = TRK_DTL.LOCN_ID(+) AND (TRK_DTL.AREA <>'C' OR TRK_DTL.AREA IS NULL)
GROUP BY SUBSTR(STK_INF.TASK_GENRTN_REF_NBR,1,12)This is the EXPLAIN PLAIN i got after i issued EXECUTE DBMS_STATS.GATHER_TABLE_STATS ('schema_name','table_name'); for both the tables involved.
The number of row returned is still 222 rows. But in the EXPLAIN PLAN it is shown as 3060 !!
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 3267659036
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 3060 | 194K| | 16527 (10)| 00:03:19 |
| 1 | HASH GROUP BY | | 3060 | 194K| | 16527 (10)| 00:03:19 |
|* 2 | FILTER | | | | | | |
|* 3 | HASH JOIN RIGHT OUTER| | 177K| 11M| 7344K| 16397 (10)| 00:03:17 |
| 4 | TABLE ACCESS FULL | TRK_DTL | 313K| 3669K| | 2378 (6)| 00:00:29 |
|* 5 | TABLE ACCESS FULL | STK_INF | 177K| 9205K| | 13030 (11)| 00:02:37 |
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
2 - filter("TRK_DTL"."AREA"<>'C' OR "TRK_DTL"."AREA" IS NULL)
3 - access("PULL_LOCN_ID"="TRK_DTL"."LOCN_ID"(+))
5 - filter("STK_INF"."CARTON_NBR" IS NOT NULL AND
"STK_INF"."TASK_GENRTN_REF_CODE"='44' AND ("STK_INF"."NEXT_TASK_ID"=0 AND
"STK_INF"."STAT_CODE"<=90 OR "STK_INF"."NEXT_TASK_ID">0 AND
"STK_INF"."STAT_CODE"<99))
22 rows selected.You mentioned about creating indexes in STK_INF table. STK_INF.NEXT_TASK_ID has 186385 distinct values out of a total 782087. Does NEXT_TASK_ID make a good candidate for a B-Tree index
STK_INF.STAT_CODE has only four distinct values, would this make a good candidate for a Bitmap index? -
Error in Migration Db sql server to sql Azure, how i can fix this errors
I get an error TOTAL_WRITE is not supported in current version when i try validate my database from sql server migration in to sql azure. How I can Fix this error migrate to sql azure
Also I git this error sp_spaceused is not supported in current version of Azure SQL Database – Reynel Useche Velasco 22 mins ago
StoredProcedure [dbo].[p_sizetables] -- 'db_name(' is supported only for the local database. You should test to verify the results are what you want. – Reynel Useche Velasco just now edit
Reynel Alfredo Useche VelascoHi Reynel,
I see that you have posted this query on stack overflow.
I would like to involve our SQL experts in this topic and get the best solution.
As of now to my knowledge @@TOTAL_WRITE is not supported on SQL Azure.
In the meantime, you can refer the below links for migrating your database from on-prem to Azure.
https://msdn.microsoft.com/en-us/library/hh313125(v=sql.110).aspx
https://msdn.microsoft.com/en-us/library/hh313129(v=sql.110).aspx
Hope this helps you.
Girish Prajwal -
How to optimize this sql by writing MINUS function.
Hi all,
how to optimize the sql by writing MINUS function.
these are my tables
1. CREATE TABLE POSTPAID
RECORD VARCHAR2(2000 BYTE),
FLAG NUMBER
Record format:
Mobile no in 1:10 of that length
2. CREATE TABLE SUBSCRIBER
PHONE_NO VARCHAR2(10 BYTE)
My requirement is following sql need write using ‘minus’ as this one is very slow
select record record from POSTPAID where substr(record,9,10) NOT in (select PHONE_NO from SUBSCRIBER)
ThanksWhy are you very particular about using "MINUS". You can optimize the sql by using "NOT EXISTS" instead of "NOT IN" as below:
SELECT RECORD FROM POSTPAID A WHERE NOT EXISTS (SELECT 1 FROM SUBSCRIBER B WHERE SUBSTR(A.RECORD,9,10) = B.PHONE_NO) -
How I can change this query, so I can display the name and scores in one r
How I can change this query, so I can add the ID from the table SPRIDEN
as of now is giving me what I want:
1,543 A05 24 A01 24 BAC 24 BAE 24 A02 20 BAM 20in one line but I would like to add the id and name that are stored in the table SPRIDEN
SELECT sortest_pidm,
max(decode(rn,1,sortest_tesc_code)) tesc_code1,
max(decode(rn,1,score)) score1,
max(decode(rn,2,sortest_tesc_code)) tesc_code2,
max(decode(rn,2,score)) score2,
max(decode(rn,3,sortest_tesc_code)) tesc_code3,
max(decode(rn,3,score)) score3,
max(decode(rn,4,sortest_tesc_code)) tesc_code4,
max(decode(rn,4,score)) score4,
max(decode(rn,5,sortest_tesc_code)) tesc_code5,
max(decode(rn,5,score)) score5,
max(decode(rn,6,sortest_tesc_code)) tesc_code6,
max(decode(rn,6,score)) score6
FROM (select sortest_pidm,
sortest_tesc_code,
score,
row_number() over (partition by sortest_pidm order by score desc) rn
FROM (select sortest_pidm,
sortest_tesc_code,
max(sortest_test_score) score
from sortest,SPRIDEN
where
SPRIDEN_pidm =SORTEST_PIDM
AND sortest_tesc_code in ('A01','BAE','A02','BAM','A05','BAC')
and sortest_pidm is not null
GROUP BY sortest_pidm, sortest_tesc_code))
GROUP BY sortest_pidm;
Hi,
That depends on whether spriden_pidm is unique, and on what you want for results.
Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statements, relevamnt columns only) for all tables, and the results you want from that data.
If you can illustrate your problem using commonly available tables (such as those in the scott or hr schemas) then you don't have to post any sample data; just post the results you want.
Either way, explain how you get those results from that data.
Always say which version of Oracle you're using.
It looks like you're doing something similiar to the following.
Using the emp and dept tables in the scott schema, produce one row of output per department showing the highest salary in each job, for a given set of jobs:
DEPTNO DNAME LOC JOB_1 SAL_1 JOB_2 SAL_2 JOB_3 SAL_3
20 RESEARCH DALLAS ANALYST 3000 MANAGER 2975 CLERK 1100
10 ACCOUNTING NEW YORK MANAGER 2450 CLERK 1300
30 SALES CHICAGO MANAGER 2850 CLERK 950On each row, the jobs are listed in order by the highest salary.
This seems to be analagous to what you're doing. The roles played by sortest_pidm, sortest_tesc_code and sortest_test_score in your sortest table are played by deptno, job and sal in the emp table. The roles played by spriden_pidm, id and name in your spriden table are played by deptno, dname and loc in the dept table.
It sounds like you already have something like the query below, that produces the correct output, except that it does not include the dname and loc columns from the dept table.
SELECT deptno
, MAX (DECODE (rn, 1, job)) AS job_1
, MAX (DECODE (rn, 1, max_sal)) AS sal_1
, MAX (DECODE (rn, 2, job)) AS job_2
, MAX (DECODE (rn, 2, max_sal)) AS sal_2
, MAX (DECODE (rn, 3, job)) AS job_3
, MAX (DECODE (rn, 3, max_sal)) AS sal_3
FROM (
SELECT deptno
, job
, max_sal
, ROW_NUMBER () OVER ( PARTITION BY deptno
ORDER BY max_sal DESC
) AS rn
FROM (
SELECT e.deptno
, e.job
, MAX (e.sal) AS max_sal
FROM scott.emp e
, scott.dept d
WHERE e.deptno = d.deptno
AND e.job IN ('ANALYST', 'CLERK', 'MANAGER')
GROUP BY e.deptno
, e.job
GROUP BY deptno
;Since dept.deptno is unique, there will only be one dname and one loc for each deptno, so we can change the query by replacing "deptno" with "deptno, dname, loc" throughout the query (except in the join condition, of course):
SELECT deptno, dname, loc -- Changed
, MAX (DECODE (rn, 1, job)) AS job_1
, MAX (DECODE (rn, 1, max_sal)) AS sal_1
, MAX (DECODE (rn, 2, job)) AS job_2
, MAX (DECODE (rn, 2, max_sal)) AS sal_2
, MAX (DECODE (rn, 3, job)) AS job_3
, MAX (DECODE (rn, 3, max_sal)) AS sal_3
FROM (
SELECT deptno, dname, loc -- Changed
, job
, max_sal
, ROW_NUMBER () OVER ( PARTITION BY deptno -- , dname, loc -- Changed
ORDER BY max_sal DESC
) AS rn
FROM (
SELECT e.deptno, d.dname, d.loc -- Changed
, e.job
, MAX (e.sal) AS max_sal
FROM scott.emp e
, scott.dept d
WHERE e.deptno = d.deptno
AND e.job IN ('ANALYST', 'CLERK', 'MANAGER')
GROUP BY e.deptno, d.dname, d.loc -- Changed
, e.job
GROUP BY deptno, dname, loc -- Changed
;Actually, you can keep using just deptno in the analytic PARTITION BY clause. It might be a little more efficient to just use deptno, like I did above, but it won't change the results if you use all 3, if there is only 1 danme and 1 loc per deptno.
By the way, you don't need so many sub-queries. You're using the inner sub-query to compute the MAX, and the outer sub-query to compute rn. Analytic functions are computed after aggregate fucntions, so you can do both in the same sub-query like this:
SELECT deptno, dname, loc
, MAX (DECODE (rn, 1, job)) AS job_1
, MAX (DECODE (rn, 1, max_sal)) AS sal_1
, MAX (DECODE (rn, 2, job)) AS job_2
, MAX (DECODE (rn, 2, max_sal)) AS sal_2
, MAX (DECODE (rn, 3, job)) AS job_3
, MAX (DECODE (rn, 3, max_sal)) AS sal_3
FROM (
SELECT e.deptno, d.dname, d.loc
, e.job
, MAX (e.sal) AS max_sal
, ROW_NUMBER () OVER ( PARTITION BY e.deptno
ORDER BY MAX (sal) DESC
) AS rn
FROM scott.emp e
, scott.dept d
WHERE e.deptno = d.deptno
AND e.job IN ('ANALYST', 'CLERK', 'MANAGER')
GROUP BY e.deptno, d.dname, d.loc
, e.job
GROUP BY deptno, dname, loc
;This will work in Oracle 8.1 and up. In Oracle 11, however, it's better to use the SELECT ... PIVOT feature. -
How to execute this SQL Query in ABAP Program.
Hi,
I have a string which is the SQL Query.
How to execute this sql Query (SQL_STR) in ABAP Program.
Code:-
DATA: SQL_STR type string.
SQL_STR = 'select * from spfli.'.
Thanks in Advance,
VinayHi Vinay
Here is a sample to dynamically generate a subroutine-pool having your SQL and calling it.
REPORT dynamic_sql_example .
DATA: BEGIN OF gt_itab OCCURS 1 ,
line(80) TYPE c ,
END OF gt_itab .
DATA gt_restab TYPE .... .
DATA gv_name(30) TYPE c .
DATA gv_err(120) TYPE c .
START-OF-SELECTION .
gt_itab-line = 'REPORT generated_sql .' .
APPEND gt_itab .
gt_itab-line = 'FORM exec_sql CHANGING et_table . ' .
APPEND gt_itab .
gt_itab-line = SQL_STR .
APPEND gt_itab .
gt_itab-line = 'ENDFORM.' .
APPEND gt_itab .
GENERATE SUBROUTINE POOL gt_itab NAME gv_name MESSAGE gv_err .
PERFORM exec_sql IN PROGRAM (gv_name) CHANGING gt_restab
IF FOUND .
WRITE:/ gv_err .
LOOP AT gt_result .
WRITE:/ .... .
ENDLOOP .
*--Serdar -
How Can I find SQL Query in Database
Dear Experts,
How Can I find SQL Query in Database.Hi,
U mena what query got executed in db? u can try V$SQL for it.
Regards
Bharath -
How I can change this query, I got it from someone on the list, and it works just fine, Thank you! but I can not hardcode the data, the data is actually in a repiting table
SARAPPD_PIDM, SARAPPD_TERM_CODE_ENTRY, SARAPPD_APPL_NO 1 SARAPPD_SEQ_NO
We can have a person with one record another one with two or three, we just don't know, each record have a sequence number SARAPPD_SEQ_NO
this SARAPPD_PIDM = 2232040 with will a paramater pass to the function (SARAPPD_PIDM = p_pidm)
excuse my ignorance, but I just don't have experience with this kind of queries..
with t as ( -- sample data
select 2232040 SARAPPD_PIDM, 200990 SARAPPD_TERM_CODE_ENTRY, 1 SARAPPD_APPL_NO, 1 SARAPPD_SEQ_NO, to_date('12/03/2008','mm/dd/yyyy') sARAPPD_APDC_DATE, 'S*' SARAPPD_APDC_CODE from dual union all
select 2232040 ,200990 ,1 ,2 ,to_date('12/08/2008','mm/dd/yyyy'), 'D1' from dual union all
select 2232040 ,200990 ,1 ,3 ,to_date('03/18/2009','mm/dd/yyyy'), 'S*' from dual union all
select 2232040 ,200990 ,1 ,4 ,to_date('03/29/2009','mm/dd/yyyy') ,'WL' from dual union all
select 2232040 ,200990 ,1 ,4 ,to_date('03/27/2009','mm/dd/yyyy') ,'WL' from dual
) -- end sample data
SELECT * FROM (
SELECT
A.SARAPPD_PIDM,
A.SARAPPD_APDC_CODE,
A.sarappd_apdc_date,
ROW_NUMBER() OVER (PARTITION BY SARAPPD_PIDM, SARAPPD_TERM_CODE_ENTRY ORDER BY SARAPPD_APDC_DATE
DESC) row_num
,lead (SARAPPD_APDC_DATE,1) over (ORDER BY SARAPPD_APDC_DATE) AS next_date
FROM t A
where
SARAPPD_PIDM = 2232040
AND sarappd_apdc_code IN ('SA', 'FA', 'S-', 'F-', 'RF', 'F*','AD', 'W-', 'R2', 'S*', 'HF', 'WL','HD', 'R1', 'HS', 'D2', 'W+')
where next_date >= to_date('3/27/2009','mm/dd/yyyy');Edited by: user648177 on May 1, 2009 8:52 AMThank you
I am getting this result
SARAPPD_PIDM SARAPPD_APDC_CODE SARAPPD_APDC_DATE SARADAP_ADMT_CODE ROW_NUM NEXT_DATE
2232040 S* 12/03/2008 16:30:45 D1 3 03/18/2009 08:58:06
2232040 S* 03/18/2009 08:58:06 D1 2 03/21/2009 13:53:23I only want to retrieve the one with the max date ( 03/18/2009 08:58:06)
I try to add the
and SARAPPD_SEQ_NO = (select max(b.SARAPPD_SEQ_NO)
from SARAPPD b
where SARAPPD_pidm = b.SARAPPD_pidm)
{code}
{code}
but it did not work
{code}
SELECT *
FROM (SELECT A.SARAPPD_PIDM,
A.SARAPPD_APDC_CODE,
A.sarappd_apdc_date,
saradap_admt_code,
ROW_NUMBER() OVER(PARTITION BY SARAPPD_PIDM, SARAPPD_TERM_CODE_ENTRY ORDER BY SARAPPD_APDC_DATE DESC) row_num,
lead(SARAPPD_APDC_DATE, 1) over(ORDER BY SARAPPD_APDC_DATE) AS next_date
FROM saturn.sarappd A, SARADAP
where SARAPPD_PIDM = 2232040
and SARADAP_PIDM = SARAPPD_PIDM
AND saradap_term_code_entry = SARAPPD_TERM_CODE_ENTRY
AND SARADAP_APPL_NO = A.SARAPPD_APPL_NO
-- and SARAPPD_SEQ_NO = (select max(b.SARAPPD_SEQ_NO)
-- from SARAPPD b
-- where SARAPPD_pidm = b.SARAPPD_pidm)
AND sarappd_apdc_code IN
('SA', 'FA', 'S-', 'F-', 'RF', 'F*', 'AD', 'W-', 'R2', 'S*', 'HF', 'WL', 'HD', 'R1', 'HS', 'D2', 'W+'))
Where next_date <= saturn_midd.utlq.f_get_adm_freeze_date(saradap_admt_code)
{code}
i now that if I add and row_num=2 it will work, but this is just an example I don't know the row_num of all the records
Edited by: user648177 on May 4, 2009 5:57 AM -
Re: How to converting from PL/SQL query to T-SQL query
How to converting from PL/SQL query to T-SQL query... Its Urgent
Download the
SQL Server Migration Assistant for Oracle. It will convert whole Oracle databases, or single queries or PL/SQL stored procedures.
With caution that If your database is using Collation which is case sensitive SSMA will not work.SSMA doesnt guarantees 100% for conversion of Queries/stored proc /database if it fails to do so for some queries you will have to do it manually.
But you can try
Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers -
What is the best way to Optimize a SQL query : call a function or do a join?
Hi, I want to know what is the best way to optimize a SQL query, call a function inside the SELECT statement or do a simple join?
Hi,
If you're even considering a join, then it will probably be faster. As Justin said, it depends on lots of factors.
A user-defined function is only necessary when you can't figure out how to do something in pure SQL, using joins and built-in functions.
You might choose to have a user-defined function even though you could get the same result with a join. That is, you realize that the function is slow, but you believe that the convenience of using a function is more important than better performance in that particular case. -
How do I write this SQL command in Oracle
Hi all
I wriote this SQ L statement in Ms SQL Server. How do I write this sql command in Oracle?
ALTER VIEW dbo.ConsumptionAS SELECT TOP 100 PERCENT ID,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200710' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Oct2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200711' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Nov2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200712' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Dec2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200801' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jan2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200802' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Feb2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200803' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Mar2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200804' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Apr2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200805' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS May2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200806' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jun2008 ,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200807' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jul2008 ,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200808' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS Aug2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200809' AND NbrDaysUsed != 0 THEN (QtyUsed NbrDaysUsed) * 748.05 ELSE 0 END)) AS Sep2008
FROM dbo.MasterConsumption WHERE YEAR_MONTH >= '200710' AND YEAR_MONTH <= '200809' GROUP BY ID ORDER BY ID
I am very interested in this part:
SUM(CASE WHEN YEAR_MONTH = '200710' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Oct2007
thanks
Edited by: user631364 on Oct 27, 2008 8:25 AM
Edited by: user631364 on Oct 27, 2008 8:26 AM
Edited by: user631364 on Oct 27, 2008 8:27 AMThank you!!
Now let me aslk the second part of my question.
This sql command:
ALTER VIEW dbo.ConsumptionAS SELECT TOP 100 PERCENT ID,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200710' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Oct2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200711' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Nov2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200712' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Dec2007,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200801' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jan2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200802' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Feb2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200803' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Mar2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200804' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Apr2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200805' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS May2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200806' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jun2008 ,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200807' AND NbrDaysUsed != 0 THEN (QtyUsed/ Days_Usage) * 748.05 ELSE 0 END)) AS Jul2008 ,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200808' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS Aug2008,
CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = '200809' AND NbrDaysUsed != 0 THEN (QtyUsed NbrDaysUsed) * 748.05 ELSE 0 END)) AS Sep2008
FROM dbo.MasterConsumption WHERE YEAR_MONTH >= '200710' AND YEAR_MONTH <= '200809' GROUP BY ID ORDER BY ID
was created with this query in SQL Server and then I saved it in a store procedure, that I scheduled to run montlhy
SET ANSI_NULLS ON
DECLARE @SQLString NVARCHAR(4000)
/* Build the SQL string once.*/
SET @SQLString = 'ALTER VIEW dbo.Consumption AS SELECT TOP 100 PERCENT ID, CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = ' +
"'" + dbo.CONLastMonth_fn(getdate(), month(getdate()) - 12) +
"'" +
' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS ' +
dbo.CONMonthInEnglish(getdate(), month(getdate()) - 12) +
… (GOES FROM current month -12 to current month -1)
, CONVERT(decimal(10, 2), SUM(CASE WHEN YEAR_MONTH = ' +
"'" + dbo.CONLastMonth_fn(getdate(), month(getdate()) - 1) +"'" +
' AND NbrDaysUsed != 0 THEN (QtyUsed/ NbrDaysUsed) * 748.05 ELSE 0 END)) AS ' +
dbo.CONMonthInEnglish(getdate(), month(getdate()) - 1) +
' FROM dbo.MasterConsumption WHERE YEAR_MONTH >= ' +
"'" + dbo.CONLastMonth_fn (getdate(), month(getdate())-12 ) +"'" +
' AND YEAR_MONTH <= ' +
"'" + dbo.CONLastMonth_fn (getdate(), month(getdate())-1 ) +"'" +
' GROUP BY ID ORDER BY ID '
EXEC sp_executesql @SQLString
Is that something that can be done in Oracle in the same way?
Do you use another approach?
please advice
Edited by: user631364 on Oct 27, 2008 10:19 AM
Edited by: user631364 on Oct 27, 2008 10:21 AM
Edited by: user631364 on Oct 27, 2008 10:21 AM
Edited by: user631364 on Oct 27, 2008 10:22 AM
Edited by: user631364 on Oct 27, 2008 10:23 AM
Edited by: user631364 on Oct 27, 2008 10:23 AM
Edited by: user631364 on Oct 27, 2008 10:24 AM -
How to compare result from sql query with data writen in html input tag?
how to compare result
from sql query with data
writen in html input tag?
I need to compare
user and password in html form
with all user and password in database
how to do this?
or put the resulr from sql query
in array
please help me?Hi dejani
first get the user name and password enter by the user
using
String sUsername=request.getParameter("name of the textfield");
String sPassword=request.getParameter("name of the textfield");
after executeQuery() statement
int exist=0;
while(rs.next())
String sUserId= rs.getString("username");
String sPass_wd= rs.getString("password");
if(sUserId.equals(sUsername) && sPass_wd.equals(sPassword))
exist=1;
if(exist==1)
out.println("user exist");
else
out.println("not exist"); -
Trying to optimize this simple query
Hi,
I am trying to optimize this simple query but the two methods I am trying actually make things worse.
The original query is:
SELECT customer_number, customer_name
FROM bsc_pdt_account_mv
where rownum <= 100
AND Upper(customer_name) like '%SP%'
AND customer_id IN
SELECT cust_id FROM bsc_pdt_assoc_sales_force_mv
WHERE area_identifier IN (
SELECT area_identifier FROM bsc_pdt_assoc_sales_force_mv
WHERE ad_identifier = '90004918' or rm_identifier = '90004918' or tm_identifier = '90004918'
The result set of this query returns me the first 100 rows in 88 seconds and they are all distinct by default (don't know why they are distinct).
My first attempt was to try to use table joins instead of the IN conditions:
SELECT
distinct -- A: I need to use distinct now
customer_number, customer_name
FROM bsc_pdt_account_mv pdt,
bsc_pdt_assoc_sales_force_mv asf,
SELECT distinct area_identifier FROM bsc_pdt_assoc_sales_force_mv
WHERE ad_identifier = '90004918' or rm_identifier = '90004918' or tm_identifier = '90004918'
) area
where
area.area_identifier = asf.area_identifier
AND asf.cust_id = pdt.customer_id
AND Upper(customer_name) like '%SP%'
AND rownum <= 100 -- B: strange when I comment this out
order by 1
I dont understand two things with this query. First issue, I now need to put in the distinct because the result set is not distinct by default. Second issue (very strange), when I put the rownum condition (<100) I get two rows in 1.5 seconds. If I remove the condition, I get 354 rows (whole result set) in 326 seconds.
My second attempt was to use EXISTS instead of IN:
SELECT
customer_number, customer_name
FROM bsc_pdt_account_mv pdt
where Upper(customer_name) like '%SP%'
AND rownum <= 100
AND EXISTS
select 1 from
bsc_pdt_assoc_sales_force_mv asf,
SELECT distinct area_identifier FROM bsc_pdt_assoc_sales_force_mv
WHERE ad_identifier = '90004918' or rm_identifier = '90004918' or tm_identifier = '90004918'
) area
where
area.area_identifier = asf.area_identifier
AND asf.cust_id = pdt.customer_id
This query returns a similar distinct result set as teh original one but takes pretty much the same time (87 seconds).The query below hangs when run in TOAD or PL/SQL Dev. I noticed there is no rows returned from the inner table for this condition.
SELECT customer_number, customer_name
FROM
bsc_pdt_account_mv pdt_account
where rownum <= 100
AND exists (
SELECT pdt_sales_force.cust_id
FROM bsc_pdt_assoc_sales_force_mv pdt_sales_force
WHERE pdt_account.customer_id = pdt_sales_force.cust_id
AND (pdt_sales_force.rm_identifier = '90007761' or pdt_sales_force.tm_identifier = '90007761') )
ORDER BY customer_name
-- No rows returned by this query
SELECT pdt_sales_force.cust_id
FROM bsc_pdt_assoc_sales_force_mv pdt_sales_force
WHERE pdt_sales_force.rm_identifier = '90007761' or pdt_sales_force.tm_identifier = '90007761'
Maybe you are looking for
-
For the past couple of weeks now I've been plagued with the problem of not being able to properly upgrade my flash player to version 9. I'd get pass the installation, sure, but they'd be corrupted installations. I can't view some flash websites and t
-
External monitor blurry image rMBP 2012
Hey guys! I have 2012 rMPB which works perfectly and i am connecting it to my 22" Benq monitor via MiniDisplayPort to VGA connector (monitor only has VGA). Image on the external monitor is really bad, it looks blurry, not sharp enough and brightness
-
Authorisation after formatting Pc
How can get authorisation from itunes after having formatted my pc, when i cannot connect to the itunes store( i cannot play purchased tracks). Also i cannot update my ipod. can anybody help.
-
Issue Tracker App - Error Message
Hello, all, I'm trying to learn, basically by following the steps to produce the Issue Tracker application from the Tutorials section in Apex 3.1. The application was written for Apex 2.x and at one point wants me to put the following query into a re
-
The Boolean value getting null?
Hello EveryBody........ Any one help me why the Boolean value gives always null instead of false; what will the default value of this......... private Boolean result; public Boolean getResult() { return result; public void