Help with re-writing this query
Dear experts;
create table t2
ID varchar2(1000),
name varchar2(1000),
place varchar2(1000)
create table mappingt2
grades varchar2(1000)
individual varchar2(1000)
insert into t2 values ('James', 'John', 'newyork');
insert into t2 values ('Linda', 'James', 'London');
insert into t2 values ('Alex', 'kim', 'Mexico');
insert into t2 values ('karen', 'Jack', 'Tunis');
insert into t2 values ('CC', 'Jack', 'Peru');
insert into t2 values ('Linda', 'James', 'Germany');
insert into mappingt2 values ('A', 'James');
insert into mappingt2 values ('C', 'Kim');
insert into mappingt2 values ('C', 'Linda');
insert into mappingt2 values ('B', 'CC');I have the following query below
SELECT (select grades from mappingt2 t
where t.individual = pt.ID) as current_grade
,(select grades from mappingt2 ty
where ty.individual = pt.name) alt_grade
,pt.place
FROM t2 ptI was just wondering whether this query can be better written. All help is appreciated. Thank you
user13328581 wrote:
Hi Frank, Tom Kyte claims sub scalar queries is usually betterHe didn't say usually he said if you want the first row faster
>
if your goal is to optimize your query for initial response time, you may want to use the former query, with the scalar subquery, because Oracle Database would have to get just the first row from DEPT, run the scalar subquery (select count...) against EMP to get the count, and then return it. The process for returning that first row would be very fast.
>
The corollary to this that was not explicitly mentioned in the article that I can see is that this probably comes at the expense of the time taken to return all the rows which can then take longer.
Similar Messages
-
Help me in writing this query plz....
There are two tables - Table1 (table1_id, value1) and Table2 (table2_id,
table1_id, value2). The field Table2.table1_id is a foreign key to
Table1.table1_id. It is necessary to write a SQL query that would find
all records in Table1 that do not have a corresponding record in Table2.
The query should not use sub-selectsuser554495 wrote:
Can you provide me the queryNo, we're not about to do your homework for you. How do you expect to learn anything if all the answers are served to you on a silver platter?
Try and work it out for yourself; if you get stuck, we'll try and help from that point, but you'd have to provide the sql queries you've tried (along with table information and sample input and expected output data). -
Please help me in writing this query
Hi all,
How to do a token match of a string with the token of another mathc
EX:
LET THE FIRST STRINGS WITH A COMMA separated like below
"firsttoken,second,third,are,the,parts,of,a,string"
LET THE SECOND STRING WITH A COMMA separated like below
"this,is,the,search,tokens"
Now I have to match any token of the SECOND string with any TOKEN OF THE FIRST string
If atleast one match is exist then its treated as success..
I need only sql, not pl/sql
Preferrable if analytical functions not used.
Thanks in advance,
KhaleelKhaleel,
I understand the limitation to use PL/SQL. I have similar kind of limitations at my end, I am not allowed
to leave any custom procedure/function definitions in the db with out taking the permissions for them.
Here is the SQL solution for you for variable number of TOKENS. Only requirement is UPPER BOUND
should be known. You could have variable number of tokens in either of the strings as long as you code it for the maximum number of tokens in each string. Here I coded it for 4 tokens in each string, hence I could use any number of tokens <= 4 in each string. Couple of tests here, -- Strings with different number of tokens but with a matching word
SQL> SELECT DECODE(count(*), 0, 'MATCH NOT FOUND', 'MATCH FOUND') yes_no FROM DUAL,
2 (SELECT DECODE(str1_comma1, 0, str1, SUBSTR(str1, 1, str1_comma1 - 1)) str1_token1,
3 DECODE(str1_comma1, 0, NULL, decode(str1_comma2, 0, SUBSTR(str1, str1_comma1 + 1),
4 SUBSTR(str1, str1_comma1 + 1, str1_comma2 - str1_comma1 - 1))) str1_token2,
5 DECODE(str1_comma2, 0, NULL, decode(str1_comma3, 0, SUBSTR(str1, str1_comma2 + 1 ),
6 SUBSTR(str1, str1_comma2 + 1, str1_comma3 - str1_comma2 - 1))) str1_token3,
7 DECODE(str1_comma3, 0, NULL, SUBSTR(str1, str1_comma3 + 1)) str1_token4,
8 -- string2 tokens
9 DECODE(str2_comma1, 0, str1, SUBSTR(str2, 1, str2_comma1 - 1)) str2_token1,
10 DECODE(str2_comma1, 0, NULL, decode(str2_comma2, 0, SUBSTR(str2, str2_comma1 + 1),
11 SUBSTR(str2, str2_comma1 + 1, str2_comma2 - str2_comma1 - 1))) str2_token2,
12 DECODE(str2_comma2, 0, NULL, decode(str2_comma3, 0, SUBSTR(str2, str2_comma2 + 1 ),
13 SUBSTR(str2, str2_comma2 + 1, str2_comma3 - str2_comma2 - 1))) str2_token3,
14 DECODE(str2_comma3, 0, NULL, SUBSTR(str2, str2_comma3 + 1)) str2_token4
15 FROM
16 (
17 Select A.str1, Instr(A.str1, ',', 1, 1) str1_comma1,
18 Instr(A.str1, ',', 1, 2) str1_comma2,
19 Instr(A.str1, ',', 1, 3) str1_comma3,
20 B.str2, Instr(B.str2, ',', 1, 1) str2_comma1,
21 Instr(B.str2, ',', 1, 2) str2_comma2,
22 Instr(B.str2, ',', 1, 3) str2_comma3
23 From
24 (Select 'Shaik,Khaleel,Java,Solution' str1 from dual) A,
25 (Select 'SriDHAR,SQL,Solution' str2 from dual) B
26 )
27 ) X
28 WHERE X.str2_token1 IN (X.str1_token1, X.str1_token2, X.str1_token3, X.str1_token4)
29 OR X.str2_token2 IN (X.str1_token1, X.str1_token2, X.str1_token3, X.str1_token4)
30 OR X.str2_token3 IN (X.str1_token1, X.str1_token2, X.str1_token3, X.str1_token4)
31 OR X.str2_token4 IN (X.str1_token1, X.str1_token2, X.str1_token3, X.str1_token4)
32 /
YES_NO
MATCH FOUND
-- Strings with same numer of tokens, with no matching word
SQL> SELECT DECODE(count(*), 0, 'MATCH NOT FOUND', 'MATCH FOUND') yes_no FROM DUAL,
2 (SELECT DECODE(str1_comma1, 0, str1, SUBSTR(str1, 1, str1_comma1 - 1)) str1_token1,
3 DECODE(str1_comma1, 0, NULL, decode(str1_comma2, 0, SUBSTR(str1, str1_comma1 + 1),
4 SUBSTR(str1, str1_comma1 + 1, str1_comma2 - str1_comma1 - 1))) str1_token2,
5 DECODE(str1_comma2, 0, NULL, decode(str1_comma3, 0, SUBSTR(str1, str1_comma2 + 1 ),
6 SUBSTR(str1, str1_comma2 + 1, str1_comma3 - str1_comma2 - 1))) str1_token3,
7 DECODE(str1_comma3, 0, NULL, SUBSTR(str1, str1_comma3 + 1)) str1_token4,
8 -- string2 tokens
9 DECODE(str2_comma1, 0, str1, SUBSTR(str2, 1, str2_comma1 - 1)) str2_token1,
10 DECODE(str2_comma1, 0, NULL, decode(str2_comma2, 0, SUBSTR(str2, str2_comma1 + 1),
11 SUBSTR(str2, str2_comma1 + 1, str2_comma2 - str2_comma1 - 1))) str2_token2,
12 DECODE(str2_comma2, 0, NULL, decode(str2_comma3, 0, SUBSTR(str2, str2_comma2 + 1 ),
13 SUBSTR(str2, str2_comma2 + 1, str2_comma3 - str2_comma2 - 1))) str2_token3,
14 DECODE(str2_comma3, 0, NULL, SUBSTR(str2, str2_comma3 + 1)) str2_token4
15 FROM
16 (
17 Select A.str1, Instr(A.str1, ',', 1, 1) str1_comma1,
18 Instr(A.str1, ',', 1, 2) str1_comma2,
19 Instr(A.str1, ',', 1, 3) str1_comma3,
20 B.str2, Instr(B.str2, ',', 1, 1) str2_comma1,
21 Instr(B.str2, ',', 1, 2) str2_comma2,
22 Instr(B.str2, ',', 1, 3) str2_comma3
23 From
24 (Select 'Shaik,Khaleel,Java,Program' str1 from dual) A,
25 (Select 'SriDHAR,SQL,Solution,Possible' str2 from dual) B
26 )
27 ) X
28 WHERE X.str2_token1 IN (X.str1_token1, X.str1_token2, X.str1_token3, X.str1_token4)
29 OR X.str2_token2 IN (X.str1_token1, X.str1_token2, X.str1_token3, X.str1_token4)
30 OR X.str2_token3 IN (X.str1_token1, X.str1_token2, X.str1_token3, X.str1_token4)
31 OR X.str2_token4 IN (X.str1_token1, X.str1_token2, X.str1_token3, X.str1_token4)
32 /
YES_NO
MATCH NOT FOUNDLooks cumbersome, but works. You should be able to take it from here to extend the functionality also,
like if you want to know which words are matching OR which words are not matching etc.., you should be
able to tweak the SQL accordingly.
Good luck,
Thx,
SriDHAR -
Please help to re-write this query using exists or with
Hi please help to re-write this query using exists or with, i need to write same code for 45 day , 90 days and so on but sub query condition is same for all
SELECT SUM (DECODE (t_one_mon_c_paid_us, 0, 0, 1)) t_two_y_m_mul_ca_
FROM (SELECT SUM (one_mon_c_paid_us) t_one_mon_c_paid_us
FROM (
SELECT a.individual_id individual_id,
CASE
WHEN NVL
(b.ship_dt,
TO_DATE ('05-MAY-1955')
) >= SYSDATE - 45
AND a.country_cd = 'US'
AND b.individual_id in (
SELECT UNIQUE c.individual_id
FROM order c
WHERE c.prod_cd = 'A'
AND NVL (c.last_payment_dt,
TO_DATE ('05-MAY-1955')
) >= SYSDATE - 745)
THEN 1
ELSE 0
END AS one_mon_c_paid_us
FROM items b, addr a, product d
WHERE b.prod_id = d.prod_id
AND d.affinity_1_cd = 'ADH'
AND b.individual_id = a.individual_id)
GROUP BY individual_id)
Edited by: user4522368 on Aug 23, 2010 9:11 AMPlease try and place \ before and after you code \Could you not remove the inline column select with the following?
SELECT a.individual_id individual_id
,CASE
when b.Ship_dt is null then
3
WHEN b.ship_dt >= SYSDATE - 90
3
WHEN b.ship_dt >= SYSDATE - 45
2
WHEN b.ship_dt >= SYSDATE - 30
1
END AS one_mon_c_paid_us
FROM items b
,addr a
,product d
,order o
WHERE b.prod_id = d.prod_id
AND d.affinity_1_cd = 'ADH'
AND b.individual_id = a.individual_id
AND b.Individual_ID = o.Individual_ID
and o.Prod_CD = 'A'
and NVL (o.last_payment_dt,TO_DATE ('05-MAY-1955') ) >= SYSDATE - 745
and a.Country_CD = 'US' -
Urgent Help required in Tunning this query
I have table ACCOUNT SPONSOR HOMESTORE ASH with more than 30 million rows.
My batch daily need to update or insert into this table from a temporary table TEMP_HSTRALCT. The data for temporary table is populated by below query which selects from two tables TRANSACTION POINTS and REDEMPTIONS.However both these tables are partitioned on date time and is run daily and this is running for hours.
Can anyone please help me on tuning this query
INSERT INTO temp_hstralct
(tmp_n_collector_account_num, tmp_v_location_id,
tmp_v_sponsor_id, tmp_v_source_file_name,
tmp_n_psc_insert_ind, tmp_n_psc_update_ind,
tmp_n_transaction_amount, tmp_n_transaction_points,
tmp_n_acc_insert_ind, tmp_n_ash_insert_ind,
tmp_n_col_insert_ind, tmp_n_check_digit,
tmp_n_collector_issue_num, tmp_n_csl_insert_ind,
tmp_v_offer_code, tmp_n_psa_insert_ind)
SELECT DISTINCT trp_n_collector_account_num account_num,
trp_v_location_id location_id,
trp_v_sponsor_id sponsor_id,
trp_c_creation_user batch_id, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
FROM transaction_points, ACCOUNT, locations_master,homestores
WHERE hsr_v_accrual_allowed = 'Y'
AND trp_n_collector_account_num = ACCOUNT.acc_n_account_num(+)
AND ( ( ( ACCOUNT.acc_v_account_type = 'C'
OR ACCOUNT.acc_v_account_type IS NULL
AND hsr_v_b2c_accounts = 'Y'
OR ( ACCOUNT.acc_v_account_type = 'B'
AND hsr_v_nfb_accounts = 'Y'
OR ( ACCOUNT.acc_v_account_type = 'H'
AND hsr_v_hybrid_accounts = 'Y'
AND trp_d_creation_date_time BETWEEN SYSDATE-3
AND SYSDATE
AND trp_v_sponsor_id = 'JSAINSBURY'
AND trp_v_location_id =
locations_master.lnm_v_location_id
AND locations_master.lnm_v_partner_id = 'JSAINSBURY'
AND ( ( ( (INSTR
(hsr_v_store_status,
locations_master.lnm_c_location_status
) > 0
AND (INSTR
(hsr_v_store_type,
locations_master.lnm_c_location_type
) > 0
AND hsr_v_homestore_assignment = 'ST'
OR ( ( locations_master.lnm_c_homestore_ind =
'Y'
AND (INSTR
(hsr_v_store_status,
locations_master.lnm_c_location_status
) > 0
AND hsr_v_homestore_assignment = 'HS'
UNION ALL
SELECT DISTINCT rdm_n_collector_account_num account_num,
rdm_v_location_id location_id,
rom_v_supplier_id sponsor_id,
rdm_c_creation_user batch_id, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
FROM redemption_details,
reward_offer_master,
ACCOUNT,
locations_master,
HOMESTORES
WHERE hsr_v_redemption_allowed = 'Y'
AND rdm_n_collector_account_num = ACCOUNT.acc_n_account_num(+)
AND ( ( ( ACCOUNT.acc_v_account_type = 'C'
OR ACCOUNT.acc_v_account_type IS NULL
AND hsr_v_b2c_accounts = 'Y'
OR ( ACCOUNT.acc_v_account_type = 'B'
AND hsr_v_nfb_accounts = 'Y'
OR ( ACCOUNT.acc_v_account_type = 'H'
AND hsr_v_hybrid_accounts = 'Y'
AND rdm_d_creation_date_time BETWEEN SYSDATE-3
AND SYSDATE
AND rom_v_reward_offer_id = rdm_v_reward_id
AND rom_v_supplier_id = 'JSAINSBURY'
AND rdm_v_location_id =
locations_master.lnm_v_location_id
AND locations_master.lnm_v_partner_id ='JSAINSBURY'
AND ( ( ( (INSTR
(hsr_v_store_status,
locations_master.lnm_c_location_status
) > 0
AND (INSTR
(hsr_v_store_type,
locations_master.lnm_c_location_type
) > 0
AND hsr_v_homestore_assignment = 'ST'
OR ( ( locations_master.lnm_c_homestore_ind =
'Y'
AND (INSTR
(hsr_v_store_status,
locations_master.lnm_c_location_status
) > 0
AND hsr_v_homestore_assignment = 'HS'
);I have copied the explain as it is and can you please try pasting in the text pad.Can you let me know whether parallel hint on this will speed up the select queries.
Plan
INSERT STATEMENT CHOOSECost: 410,815 Bytes: 2,798,394 Cardinality: 15,395
32 UNION-ALL
15 SORT UNIQUE Cost: 177,626 Bytes: 2,105,592 Cardinality: 11,896
14 FILTER
13 HASH JOIN Cost: 177,312 Bytes: 2,105,592 Cardinality: 11,896
2 TABLE ACCESS BY INDEX ROWID LMHOLTP.LOCATIONS_MASTER Cost: 37 Bytes: 23,184 Cardinality: 966
1 INDEX RANGE SCAN NON-UNIQUE LMHOLTP.IX_LOCATIONS_MASTER_3 Cost: 3 Cardinality: 1
12 FILTER
11 HASH JOIN OUTER
8 MERGE JOIN CARTESIAN Cost: 155,948 Bytes: 702,656,660 Cardinality: 4,845,908
3 TABLE ACCESS FULL LMHOLTP.HOMESTORES Cost: 2 Bytes: 104 Cardinality: 1
7 BUFFER SORT Cost: 155,946 Bytes: 198,682,228 Cardinality: 4,845,908
6 PARTITION RANGE ITERATOR Partition #: 12
5 TABLE ACCESS BY LOCAL INDEX ROWID LMHOLTP.TRANSACTION_POINTS Cost: 155,946 Bytes: 198,682,228 Cardinality: 4,845,908 Partition #: 12
4 INDEX RANGE SCAN NON-UNIQUE LMHOLTP.IX_TRANSACTION_POINTS_1 Cost: 24,880 Cardinality: 6,978,108 Partition #: 12
10 PARTITION RANGE ALL Partition #: 15 Partitions accessed #1 - #5
9 TABLE ACCESS FULL LMHOLTP.ACCOUNT Cost: 6,928 Bytes: 68,495,680 Cardinality: 8,561,960 Partition #: 15 Partitions accessed #1 - #5
31 SORT UNIQUE Cost: 233,189 Bytes: 692,802 Cardinality: 3,499
30 FILTER
29 FILTER
28 NESTED LOOPS OUTER
24 HASH JOIN Cost: 226,088 Bytes: 664,810 Cardinality: 3,499
16 TABLE ACCESS FULL LMHOLTP.REWARD_OFFER_MASTER Cost: 8 Bytes: 2,280 Cardinality: 114
23 HASH JOIN Cost: 226,079 Bytes: 8,327,280 Cardinality: 48,984
20 TABLE ACCESS BY INDEX ROWID LMHOLTP.LOCATIONS_MASTER Cost: 37 Bytes: 432 Cardinality: 18
19 NESTED LOOPS Cost: 39 Bytes: 2,304 Cardinality: 18
17 TABLE ACCESS FULL LMHOLTP.HOMESTORES Cost: 2 Bytes: 104 Cardinality: 1
18 INDEX RANGE SCAN NON-UNIQUE LMHOLTP.IX_LOCATIONS_MASTER_3 Cost: 3 Cardinality: 966
22 PARTITION RANGE ITERATOR Partition #: 28
21 TABLE ACCESS FULL LMHOLTP.REDEMPTION_DETAILS Cost: 226,019 Bytes: 261,636,270 Cardinality: 6,229,435 Partition #: 28
27 PARTITION RANGE ITERATOR Partition #: 30
26 TABLE ACCESS BY LOCAL INDEX ROWID LMHOLTP.ACCOUNT Cost: 2 Bytes: 8 Cardinality: 1 Partition #: 30
25 INDEX UNIQUE SCAN UNIQUE LMHOLTP.CO_PK_ACCOUNT Cost: 1 Cardinality: 1 Partition #: 30 -
Help needed in writing a Query/Procedure
Hello All,
Need all ur help in writing a query or procedure
Lets say the Table name is DEMO
There i have one column like num it has values
1
2
3
4
5
8
9
10
my query output should be
1-5
8-10
i,e .. if the diff between two rows is greater than 1 then it result should be in a separate group
I need a query/Procedure for this. Kindly help
Regards,
ChandraTried obtaining the output using SQL and the result is as follows:
SQL> WITH T AS
2 (
3 SELECT 1 COL1 FROM DUAL
4 UNION
5 SELECT 2 COL1 FROM DUAL
6 UNION
7 SELECT 3 COL1 FROM DUAL
8 UNION
9 SELECT 4 COL1 FROM DUAL
10 UNION
11 SELECT 5 COL1 FROM DUAL
12 UNION
13 SELECT 8 COL1 FROM DUAL
14 UNION
15 SELECT 9 COL1 FROM DUAL
16 UNION
17 SELECT 10 COL1 FROM DUAL
18 UNION
19 SELECT 13 COL1 FROM DUAL
20 UNION
21 SELECT 14 COL1 FROM DUAL
22 UNION
23 SELECT 15 COL1 FROM DUAL
24 UNION
25 SELECT 16 COL1 FROM DUAL
26 UNION
27 SELECT 23 COL1 FROM DUAL
28 UNION
29 SELECT 24 COL1 FROM DUAL
30 )
31 SELECT OUTPUT FROM
32 (
33 SELECT DECODE(COL3,NULL,COL1, COL2) || '-' || LEAD(DECODE(COL3,NULL,COL3, COL1)) OVER (ORDER BY DECODE(COL3,NULL,COL1, COL2)) OUTPUT FROM
34 (
35 SELECT COL1, LEAD(COL1) OVER (ORDER BY COL1) COL2, LAG(COL1) OVER (ORDER BY COL1) COL3 FROM T
36 )
37 WHERE
38 (COL2 - COL1 > 1 OR COL2 IS NULL OR COL3 IS NULL)
39 )
40 WHERE OUTPUT != '-';
OUTPUT
1-5
8-10
13-16
23-24 -
Is there any better way of writing this query??
I have a query on insert which looks like this,
INSERT INTO TEMP( I1,I2)
SELECT TI1 FROM CLIENT1
WHERE R_CD ='PR' OR 'SR',
SELECT TI2 FROM CLIENT2
WHERE R_CD = 'MN' OR 'OP
There are two tables where the source data is coming from and inserted into TEMP table. I find this query to be inefficient. Anybody who can help me writing a good one?? Thanks.<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by [email protected]:
I have a query on insert which looks like this,
INSERT INTO TEMP( I1,I2)
SELECT TI1 FROM CLIENT1
WHERE R_CD ='PR' OR 'SR',
SELECT TI2 FROM CLIENT2
WHERE R_CD = 'MN' OR 'OP
There are two tables where the source data is coming from and inserted into TEMP table. I find this query to be inefficient. Anybody who can help me writing a good one?? Thanks.<HR></BLOCKQUOTE>
A possible solution,
INSERT INTO TEMP( I1,I2)
SELECT C1.TI1, C2.TI2
FROM CLIENT1 C1, CLIENT2 C2
WHERE (C1.R_CD = 'PR' OR C1.R_CD ='SR') AND
(C2.R_CD = 'MN' OR C2.R_CD = 'OP')
null -
Need help with division within a query
Hello all~
I am trying to divide these two columns to get a % of case numbers involving an accident. Im pretty sure you need to use decode to avoid the divide by 0 error but not sure how to implement this within my query. When i run this query below, it gives me the result of "1", which is not correct. Can someone help me please?
Oracle Version 10g
ACCIDENT is a datatype VARCHAR
CASE_NUMBER is a datatype VARCHAR
select to_char(count(ACCIDENT),'999,999,999') as "ACCIDENT",
to_char(COUNT(CASE_NUMBER),'999,999,999')as "CASE NUMBER",
round(ratio_to_report(count(accident))
OVER()*100,2)as "%"
from "PURSUIT"
WHERE ACCIDENT = 'Y'
AND
(:P1_BEG_DATE IS NULL AND :P1_END_DATE IS NULL
OR
pursuit_date BETWEEN to_date(:p1_beg_date,'MM/DD/YYYY') and to_date
(:p1_end_date,'MM/DD/YYYY'))
AND(:P1_TROOP=pursuit.officer_troop OR :p1_troop IS NULL)
AND(:P1_RADIO=pursuit.officer_radio OR :p1_radio IS NULL)
group by case_numberThanks
DeannaAre you sure that the ANDs and ORs in your WHERE clause will take precedence properly?
Also, if you always select only cases where there has been an accident, what percentage would you like to display? Surely in this case the percentage of cases involving in accident in cases where there was an accident.. is 100%?
as a simpler example
SELECT
accident,
ROUND(RATIO_TO_REPORT(count(*)) OVER() * 100)
FROM
pursuit
GROUP BY
accidentHere's a trick to neaten up those IS NULLs:
SELECT
accident,
ROUND(RATIO_TO_REPORT(count(*)) OVER() * 100)
FROM
pursuit
WHERE
pursuit_date >= COALESCE(:p1_beg_date, pursuit_date) AND
pursuit_date <= COALESCE(:p1_end_date, pursuit_date) AND
officer_troop LIKE :p1_troop AND
offcier_radio LIKE :p1_radio
GROUP BY
accidentTo wildcard a date, simply pass NULL in; the coalesce will replace the null with the pursuit_date from the record (thus the >= and <= becomes true)
To wildcard the troop or the radio, simply pass a '%' symbol as the value of the parameter. If the front end code is already set up to pass nulls, use the COALESCE trick above -
Help with aggregate function max query
I have a large database which stores daily oil life, odo readings from thousands of vehicles being tested but only once a month.
I am trying to grab data from one month where only the EOL_READ = 0 and put all of those values into the previous month's query where EOL_READ = anything.
Here is the original query which grabs everything
(select distinct vdh.STID, vdh.CASE_SAK,
max(to_number(decode(COMMAND_ID,'EOL_READ',COMMAND_RESULT,-100000))) EOL_READ,
max(to_number(decode(COMMAND_ID,'ODO_READ',COMMAND_RESULT,-100000))) ODO_READ,
max(to_number(decode(COMMAND_ID,'OIL_LIFE_PREDICTION',COMMAND_RESULT,-100000))) OIL_LIFE_PREDICTION
from veh_diag_history vdh, c2pt_data_history cdh
where vdh.veh_diag_history_sak = cdh.veh_diag_history_sak
and cdh.COMMAND_ID in ('EOL_READ','ODO_READ','OIL_LIFE_PREDICTION')
and vdh.LOGICAL_TRIGGER_SAK = 3
and cdh.CREATED_TIMESTAMP =vdh.CREATED_TIMESTAMP
and cdh.CREATED_TIMESTAMP >= to_date('03/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
and cdh.CREATED_TIMESTAMP < to_date('04/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
group by vdh.STID, vdh.case_sak
having count(distinct command_id) = 3
order by OIL_LIFE_PREDICTION)
which gives 5 columns:
STID, CASE_SAK, EOL_READ, ODO_READ, and OIL_LIFE_PREDICTION
and gives me about 80000 rows returned for the above query
I only want one reading per month, but sometimes I get two.
STID is the unique identifier for a vehicle.
I tried this query:
I tried this query which nests one request within the other and SQL times out every time:
select distinct vdh.STID, vdh.CASE_SAK,
max(to_number(decode(COMMAND_ID,'EOL_READ',COMMAND_RESULT,-100000))) EOL_READ,
max(to_number(decode(COMMAND_ID,'ODO_READ',COMMAND_RESULT,-100000))) ODO_READ,
max(to_number(decode(COMMAND_ID,'OIL_LIFE_PREDICTION',COMMAND_RESULT,-100000))) OIL_LIFE_PREDICTION
from veh_diag_history vdh, c2pt_data_history cdh
where vdh.veh_diag_history_sak = cdh.veh_diag_history_sak
and cdh.COMMAND_ID in ('EOL_READ','ODO_READ','OIL_LIFE_PREDICTION')
and vdh.LOGICAL_TRIGGER_SAK = 3
and cdh.CREATED_TIMESTAMP =vdh.CREATED_TIMESTAMP
and cdh.CREATED_TIMESTAMP >= to_date('02/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
and cdh.CREATED_TIMESTAMP < to_date('03/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
group by vdh.STID, vdh.case_sak
having count(distinct command_id) = 3
and vdh.stid in (select distinct vdh.STID, vdh.CASE_SAK,
max(to_number(decode(COMMAND_ID,'EOL_READ',COMMAND_RESULT,-100000))) EOL_READ,
max(to_number(decode(COMMAND_ID,'ODO_READ',COMMAND_RESULT,-100000))) ODO_READ,
max(to_number(decode(COMMAND_ID,'OIL_LIFE_PREDICTION',COMMAND_RESULT,-100000))) OIL_LIFE_PREDICTION
from veh_diag_history vdh, c2pt_data_history cdh
where vdh.veh_diag_history_sak = cdh.veh_diag_history_sak
and cdh.COMMAND_ID in ('EOL_READ','ODO_READ','OIL_LIFE_PREDICTION')
and vdh.LOGICAL_TRIGGER_SAK = 3
and cdh.CREATED_TIMESTAMP =vdh.CREATED_TIMESTAMP
and cdh.CREATED_TIMESTAMP >= to_date('03/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
and cdh.CREATED_TIMESTAMP < to_date('04/01/2007 12:00:00 AM','mm/dd/yyyy HH:MI:SS AM')
group by vdh.STID, vdh.case_sak
having count(distinct command_id) = 3
and (max(to_number(decode(COMMAND_ID,'EOL_READ',COMMAND_RESULT,-100000)))) = 0)
order by OIL_LIFE_PREDICTION
so in summary I am trying to select values from the previous month only from those stids where this month's value for EOL_READ = 0
Any ideas.....please help.You can use row_number() within each stid and each month to determine the first read of each month. Then you can use lag() to get the previous month's reading of the current month's reading is zero.
with all_data as (
select stid,
case_sak,
eol_read,
timestamp,
row_number() over (partition by stid, trunc(timestamp,'mm') order by timestamp) AS rn
from veh_diag_history
select stid,
case_sak,
case
when eol_read = 0
then lag(eol_read) over (partition by stid order by timestamp)
else eol_read
end as eol_read
from all_data
where rn = 1; -
Hi I am using oracle 10g. Trying to aggregate duplicate count records. I have so far:
SELECT 'ST' LEDGER,
CASE
WHEN c.Category = 'E' THEN 'Headcount Exempt'
ELSE 'Headcount Non-Exempt'
END
ACCOUNTS,
CASE WHEN a.COMPANY = 'ZEE' THEN 'OH' ELSE 'NA' END MARKET,
'MARCH_12' AS PERIOD,
COUNT (a.empl_id) head_count
FROM essbase.employee_pubinfo a
LEFT OUTER JOIN MMS_DIST_COPY b
ON a.cost_ctr = TRIM (b.bu)
INNER JOIN MMS_GL_PAY_GROUPS c
ON a.pay_group = c.group_code
WHERE a.employee_status IN ('A', 'L', 'P', 'S')
AND FISCAL_YEAR = '2012'
AND FISCAL_MONTH = 'MARCH'
GROUP BY a.company,
b.district,
a.cost_ctr,
c.category,
a.fiscal_month,
a.fiscal_year;
which gives me same rows with different head_counts. I am trying to combine the same rows as a total (one record). Do I use a subquery?Hi,
Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved.
Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
user610131 wrote:
... which gives me same rows with different head_counts.If they have different head_counts, then the rows are not the same.
I am trying to combine the same rows as a total (one record). Do I use a subquery?Maybe. It's more likely that you need a different GROUP BY clause, since the GROUP BY clause determines how many rows of output there will be. I'll be able to say more after you post the sample data, results, and explanation.
You may want both a sub-query and a different GROUP BY clause. For example:
WITH got_group_by_columns AS
SELECT a.empl_id
, CASE
WHEN c.category = 'E'
THEN 'Headcount Exempt'
ELSE 'Headcount Non-Exempt'
END AS accounts
, CASE
WHEN a.company = 'ZEE'
THEN 'OH'
ELSE 'NA'
END AS market
FROM essbase.employee_pubinfo a
LEFT OUTER JOIN mms_dist_copy b ON a.cost_ctr = TRIM (b.bu)
INNER JOIN mms_gl_pay_groups c ON a.pay_group = c.group_code
WHERE a.employee_status IN ('A', 'L', 'P', 'S')
AND fiscal_year = '2012'
AND fiscal_month = 'MARCH'
SELECT 'ST' AS ledger
, accounts
, market
, 'MARCH_12' AS period
, COUNT (empl_id) AS head_count
FROM got_group_by_columns
GROUP BY accounts
, market
;But that's just a wild guess.
You said you wanted "Help with count and sum". I see the COUNT, but what do you want with SUM? No doubt this will be clearer after you post the sample data and results.
Edited by: Frank Kulash on Apr 4, 2012 5:31 PM -
Help with Finding Duplicate records Query
HI,
I am trying to write a query that will find duplicate records/cases.
This query will be used in a report.
So, here are the requirements:
I need to find duplicate cases/records based on the following fields:
DOB, DOCKET, SENT_DATEI was able to do that with the following query. The query below is able to give me all duplicate records based on the Criteria above
SELECT DEF.BIRTH_DATE DOB,
S.DOCKET DOCKET,
S.SENT_VIO_DATE SENT_DATE, COUNT(*)
FROM SENTENCES S,
DEFENDANTS DEF
WHERE S.DEF_ID = DEF.DEF_ID
AND S.CASE_TYPE_CODE = 10
GROUP BY DEF.BIRTH_DATE, S.DOCKET, S.SENT_VIO_DATE
HAVING COUNT(*) > 1;
//I AM GOING TO CALL THIS QUERY 'X'Now, the information to be displayed on the report: defendants Name, DOB, District, Docket, Def Num, Sent Date, and PACTS Num if possible.
The problem that I need help on is how to combine those queries together (what I mean is a sub query). the 'X' query returns multiple values. please have a look at the comments on the query below to see what I'm trying to achieve.
here is the main query:
SELECT INITCAP(DEF.LAST_NAME) || ' ' || INITCAP(DEF.FIRST_NAME) || ' ' || INITCAP(DEF.MIDDLE_NAME) DEFENDANT_NAME,
DEF.BIRTH_DATE DOB,
TRIM(DIST.DISTRICT_NAME) DISTRICT_NAME,
S.DOCKET DOCKET,
S.DEF_NUM DEF_NUM,
S.SENT_VIO_DATE SENT_DATE,
DEF.PACTS_ID PACTS_NUM
FROM USSC_CASES.DEFENDANTS DEF,
USSC_CASES.SENTENCES S,
LOOKUP.DISTRICTS DIST
WHERE DEF.DEF_ID = S.DEF_ID
AND S.DIST_ID = DIST.USSC_DISTRICT_ID
AND S.CASE_TYPE_CODE = 10
AND S.USSC_ID IS NOT NULL
AND // what i'm trying to do is: DOB, DOCKET, SENT_DATE IN ('X' QUERY), is this possible ??
ORDER BY DEFENDANT_NAME; thanks in advance.
I am using Oracle 11g, and sql developer.
if my approach doesn't work, is there a better approach ?
Edited by: Rooney on Jul 11, 2012 3:50 PMIf I got it right, you want to join table USSC_CASES.DEFENDANTS to duplicate rows in USSC_CASES. If so:
SELECT INITCAP(DEF.LAST_NAME) || ' ' || INITCAP(DEF.FIRST_NAME) || ' ' || INITCAP(DEF.MIDDLE_NAME) DEFENDANT_NAME,
DEF.BIRTH_DATE DOB,
TRIM(DIST.DISTRICT_NAME) DISTRICT_NAME,
S.DOCKET DOCKET,
S.DEF_NUM DEF_NUM,
S.SENT_VIO_DATE SENT_DATE,
DEF.PACTS_ID PACTS_NUM
FROM USSC_CASES.DEFENDANTS DEF,
SELECT *
FROM (
SELECT S.*,
COUNT(*) OVER(PARTITION BY DEF.BIRTH_DATE, S.DOCKET, S.SENT_VIO_DATE) CNT
FROM USSC_CASES.SENTENCES S
WHERE CNT > 1
) S,
LOOKUP.DISTRICTS DIST
WHERE DEF.DEF_ID = S.DEF_ID
AND S.DIST_ID = DIST.USSC_DISTRICT_ID
AND S.CASE_TYPE_CODE = 10
AND S.USSC_ID IS NOT NULL
ORDER BY DEFENDANT_NAME;If you want to exclude duplicates from the query and do not care which row out of duplicate rows to choose:
SELECT INITCAP(DEF.LAST_NAME) || ' ' || INITCAP(DEF.FIRST_NAME) || ' ' || INITCAP(DEF.MIDDLE_NAME) DEFENDANT_NAME,
DEF.BIRTH_DATE DOB,
TRIM(DIST.DISTRICT_NAME) DISTRICT_NAME,
S.DOCKET DOCKET,
S.DEF_NUM DEF_NUM,
S.SENT_VIO_DATE SENT_DATE,
DEF.PACTS_ID PACTS_NUM
FROM USSC_CASES.DEFENDANTS DEF,
SELECT *
FROM (
SELECT S.*,
ROW_NUMBER() OVER(PARTITION BY DEF.BIRTH_DATE, S.DOCKET, S.SENT_VIO_DATE ORDER BY 1) RN
FROM USSC_CASES.SENTENCES S
WHERE RN = 1
) S,
LOOKUP.DISTRICTS DIST
WHERE DEF.DEF_ID = S.DEF_ID
AND S.DIST_ID = DIST.USSC_DISTRICT_ID
AND S.CASE_TYPE_CODE = 10
AND S.USSC_ID IS NOT NULL
ORDER BY DEFENDANT_NAME;SY. -
Help needed in writing a query
Hi all,
Following is the structure of my table.
Data
Key Number
Id Number
Value varchar2(100)
activity_name varchar2(100)
Creation_Date Date
Eval_Point varchar2(100)
In the above table Id is the primary key.
The column eval_point holds only two types of entries 'activation' or 'completion'
The activity_name column holds the name of the activity.
The sample entries in the table are as follows
Key Value activity_name Creation_Date Id Eval_Point
260002 XXX assign_1 2007-09-21 16:58:41.920000 951 activation
260002 XXX assign_1 2007-09-21 16:58:43.392000 953 completion
260002 XXX assign_2 2007-09-21 16:59:03.732000 956 activation
260002 XXX assign_2 2007-09-21 16:59:04.112000 954 completion
260002 XXX assign_3 2007-09-21 16:59:24.331000 958 activation
260002 XXX assign_3 2007-09-21 16:59:24.421000 957 completion
i need to write a query which gives me data in the following format
value id start_date end_date
XXX YYY 2007-09-21 16:58:41.920000 2007-09-21 16:58:43.392000
where start_date is the creation date of the 'activation' and end_date is the creation_date of 'completion'.
Can somebody help?
-thanks
lavanyahello all,
I would like to re frame my question.
this is the output of the base query
select id,instance_key,sensor_target,activity_sensor,creation_date,eval_point from bpel_variable_sensor_values where instance_key=260002;
953 260002 Assign_1 952 2007-09-21 16:58:43.392000 completion
951 260002 Assign_1 952 2007-09-21 16:58:41.920000 activation
956 260002 Assign_2 955 2007-09-21 16:59:03.732000 activation
954 260002 Assign_2 955 2007-09-21 16:59:04.112000 completion
958 260002 Assign_3 959 2007-09-21 16:59:24.331000 activation
957 260002 Assign_3 959 2007-09-21 16:59:24.421000 completion
962 260002 Assign_4 960 2007-09-21 16:59:44.741000 completion
961 260002 Assign_4 960 2007-09-21 16:59:44.640000 activation
964 260002 Assign_5 965 2007-09-21 17:00:05.290000 completion
963 260002 Assign_5 965 2007-09-21 17:00:04.950000 activation
I am trying out this query
select a.instance_key,a.creation_date,b.creation_date,a.id
from bpel_variable_sensor_values a, bpel_variable_sensor_values b
where a.instance_key=b.instance_key
and a.instance_key=260002
and a.eval_point='activation'
and b.eval_point='completion'
and i am getting 25 entries i.e a cartesian product of a.creation_date
260002 2007-09-21 16:58:41.920000 2007-09-21 16:58:43.392000 951
260002 2007-09-21 16:58:41.920000 2007-09-21 16:59:04.112000 951
260002 2007-09-21 16:58:41.920000 2007-09-21 16:59:24.421000 951
260002 2007-09-21 16:58:41.920000 2007-09-21 16:59:44.741000 951
260002 2007-09-21 16:58:41.920000 2007-09-21 17:00:05.290000 951
260002 2007-09-21 16:59:03.732000 2007-09-21 16:58:43.392000 956
260002 2007-09-21 16:59:03.732000 2007-09-21 16:59:04.112000 956
260002 2007-09-21 16:59:03.732000 2007-09-21 16:59:24.421000 956
260002 2007-09-21 16:59:03.732000 2007-09-21 16:59:44.741000 956
260002 2007-09-21 16:59:03.732000 2007-09-21 17:00:05.290000 956
260002 2007-09-21 16:59:24.331000 2007-09-21 16:58:43.392000 958
260002 2007-09-21 16:59:24.331000 2007-09-21 16:59:04.112000 958
260002 2007-09-21 16:59:24.331000 2007-09-21 16:59:24.421000 958
260002 2007-09-21 16:59:24.331000 2007-09-21 16:59:44.741000 958
260002 2007-09-21 16:59:24.331000 2007-09-21 17:00:05.290000 958
260002 2007-09-21 16:59:44.640000 2007-09-21 16:58:43.392000 961
260002 2007-09-21 16:59:44.640000 2007-09-21 16:59:04.112000 961
260002 2007-09-21 16:59:44.640000 2007-09-21 16:59:24.421000 961
260002 2007-09-21 16:59:44.640000 2007-09-21 16:59:44.741000 961
260002 2007-09-21 16:59:44.640000 2007-09-21 17:00:05.290000 961
260002 2007-09-21 17:00:04.950000 2007-09-21 16:58:43.392000 963
260002 2007-09-21 17:00:04.950000 2007-09-21 16:59:04.112000 963
260002 2007-09-21 17:00:04.950000 2007-09-21 16:59:24.421000 963
260002 2007-09-21 17:00:04.950000 2007-09-21 16:59:44.741000 963
260002 2007-09-21 17:00:04.950000 2007-09-21 17:00:05.290000 963
can soembody help me to reduce these to 5 rows. -
Please can you help me in Tuning this query..?
Hi ,
Please can you help me in re-structuring this query? .Details are given below.
I have 2 tables as shown below and data is like this.
Position
COD IND
AAA N
BBB N
CCC N
DDD Y
Distance
orig dest
AAA BBB
BBB CCC
AAA CCC
I need to create the records like this
start end
DDD AAA
DDD BBB
DDD CCC
The query which i am using now for this is
select p.code AS start,
P1.CODE AS end
from position p, position p1
where
P.CODE != P1.CODE
AND (P.ind = 'Y' or P1.IND = 'Y')
AND not exists
(select 1
from distance d
where (d.orig = p.code or d.dest = p.code)
and (d.orig = p1.code or d.dest = p1.code))
table is having above a crore record. so its taking a lot of time.
Please someone please help in tuning this query?
Thanks and regards,
ShabirLooks like you want this
select a.strt, b.ends from
(select p.code strt from position p where p.ind='Y') a,
(select p.code ends from position p where p.ind='N') b
where not exists (select 1 from distance d where d.orig=a.strt or d.dest=a.strt);
DDD AAA
DDD BBB
DDD CCCYour query result is:
AAA DDD
BBB DDD
CCC DDD
DDD AAA
DDD BBB
DDD CCCYou should be more descriptive about what kind of result you want, so that people can get more interested in helping you. -
CREATE TABLE S1(ID VARCHAR2(10), CAT_CODE NUMBER, CAT VARCHAR2(10))
CREATE TABLE S2(ID VARCHAR2(10),NUM NUMBER,DESCRIPTION VARCHAR2(10))
INSERT INTO S1 VALUES('2-1',NULL,'BUSINESS')
INSERT INTO S2 VALUES('3-1',10,'BUSINESS')
SELECT A.ID,B.ID FROM S1 A,S2 B WHERE A.CAT_CODE=B.NUM(+) AND A.CAT=B.DESCRIPTION(+)
OUTPUT IS : 2-1 BLANK
SELECT A.ID,B.ID FROM S1 A,S2 B WHERE A.CAT_CODE(+)=B.NUM AND A.CAT(+)=B.DESCRIPTION
OUTPUT IS : BLANK 3-1
IF I WANT THE OUTPUT TO BE
2-1 3-1 HOW SHALL I WRITE THE QUERY
I TRIED WITH UNIONPlease read my query if your table is not having any common data field.
select a.employee_code,b.employee_Code from
(select rownum srno,employee_code from pms_employee_master
where company_code=48 and category_code='01') a,
(select rownum srno1,employee_code from pms_employee_master
where company_code=48 and category_code='01') b
where a.srno=b.srno1
Above I am using my one table with rownum u can user different tables for this purpose.
Thx
Shishu Paul -
Need Help with setting up this Filter
Hi,
I need help making a filter sound like the one used in this youtube song.
I tried setting it up but I've had no success; even though I know for a fact that this filter was made in Logic Pro 9 according to the maker of this song.
I think there might be a bit of 'Fuzz-Wah' in there too but I've never really used that effect so any tips on that would be great, too.
Link: http://www.youtube.com/watch?v=tf017M8SZZE&feature=plcp
Any help with this would be widely appreciated.
Thank you :-)It sounds like a simple hi Q filter sweep to me but getting close to the original synth sound that it is being used on is the challenge. It might well be that it is the filter on the synth itself is what is being swept. Either way, turn up the filter resonance on a band pass filter and sweep it with the lfo or adsr to modulate the filter cutoff if its the synth filter, or automate the filter frequency sweep if you use the channel filter.
Maybe you are looking for
-
Personal web sharing+php 5.2.2
I installed PHP 5.2.2 for Apache 2 (I'm working on making a simple photo gallery) and it worked just after i installed it but it didn't give me any errors when my code didn't work. But now whenever I try and start web sharing it just says "web sharin
-
SOA Server getting down frequently
Hi All, Following are the versions: WebLogic Version : 10.3.3.0 SOA : 11.1.1.3.0 We are facing frequent server breakdown issues. But we could not find the root cause for it. Does anyone faced this scenario ? What could be the possible reasons regardi
-
I deleted mesages and i need them back
i need to get my messeges back how do i do that?
-
BI 7.0 demo .ppt format
Hi, is there a BI demo in ppt format that is available here or somewhere. I looked very hard all over this board and other links I just could not find it. I did find sap.help kind of stuff but I need something more nicer than that. Can you all help m
-
Mailing Error records in file-to-file Sp14
Hi All, Can anyone suggest me how to send error records to the concerned person when I'm doing File-to-File scenario. According my understanding I should configure SMTP adapter.but if anyone confirms this and suggest any process would be a great help