Oracle query help
Hi all
I have the order by clause and the Description field gets data
like (shipping) and (shipping - Volumes traded) , but the
InStr, function cannot tell the difference between them and can not get good results
order by
DECODE (instr (Description, 'Shipping costs', 1), 1, 1, 3),
DECODE (instr (Description, ' delivery charges for ', 1), 1.2, 3).
DECODE (instr ("Description, ' delivery charges - the Volumes traded", 1), 1, 3, 3)
any help how to solve this problem
I expect results like this in order to
1 delivery charge
Delivery 2 to
3 delivery - traded volumes
example 1
1 delivery charge
Delivery 2 to
3 delivery - traded volumes
but my performance varies and I get
3 delivery - traded volumes
1 delivery charge
Delivery 2 to
example 2
1 delivery charge
Delivery charges 2 - traded volumes
My varies and I get
Delivery charges 2 - traded volumes
1 delivery charge
Hello
Your use of INSTR assumes that the string "delivery fees" always appears at the beginning of the string (INSTR returns 1 because that is where the search string is found to start)... Baring all in mind...
WITH the list in the FORM (SELECT "delivery Charge #1" ") | Description OF the dual UNION ALL | |
SELECT "delivery fees". | Double UNION ALL | |
SELECT "shipping for. | Double UNION ALL | |
SELECT "shipping costs - negotiated Volumes." | Double UNION ALL | |
SELECT "Shipping A in the beginning" | Double UNION ALL | |
SELECT 'delivery charges against '. | OF the double | |
) |
SELECT description
LIST
In case of ORDERS description WHEN = "delivery charges - the Volumes negotiated" THEN 3
Description WHEN AS "shipping costs for %. | THEN 2 | |
Description WHEN = "delivery fees". | THEN 1 | |
ANOTHER 99 | ||
END; |
DESCRIPTION
--------------------------------
Delivery charges
Delivery charges for
Delivery charges - traded Volumes
Delivery charges against
A delivery charge, not at the beginning
Delivery #1
6 selected lines.
See you soon,.
Gas
Tags: Database
Similar Questions
-
Hierarchical Oracle query help needed - path between the crux of two brothers and sisters
I want to find the path between two nodes of oracle hierarchical Table.
Consider the following case-
NodeId - ParentId
=============
1 > > > > > > 0
2 > > > > > > 1
3 > > > > > > 2
4 > > > > > > 3
5 > > > > > > 0
6 > > > > > > 5
Here I want to query the database table that if there is a path between nodes 3 and 5?
The previous query you provided work upwards to the root node.
Here is my expected result, 3-> 2-> 1-> 0-> 5
Yet once if I have a query in the table to get the path between 1 and 3, I want to get out of the way - next
1-> 2-> 3
Therefore, the query works in both cases. Where ADI root can act as an intermediate or no node.
Can you please guide me how I can get it?
Thank you.Hello
user13276471 wrote:
I want to find the path between two nodes of oracle hierarchical Table.Consider the following case-
NodeId - ParentId
=============
1 >>>>>> 0
2 >>>>>> 1
3 >>>>>> 2
4 >>>>>> 3
5 >>>>>> 0
6 >>>>>> 5
Here I want to query the database table that if there is a path between nodes 3 and 5?
The previous queryWhat application is this? If you're referering to another thread, then post a link, such as {message identifier: = 10769125}
you provided work upwards to the root node.
Here is my expected result, 3--> 2--> 1--> 0--> 5
Yet once if I have a query in the table to get the path between 1 and 3, I want to get out of the way - next
1--> 2--> 3Therefore, the query works in both cases. Where ADI root can act as an intermediate or no node.
Can you please guide me how I can get it?
I think you want something like this:
WITH bottom_up_from_src AS ( SELECT nodeid , parentid FROM table_x START WITH nodeid = :src_nodeid CONNECT BY nodeid = PRIOR parentid ) , bottom_up_from_dst AS ( SELECT * FROM bottom_up_from_src UNION ALL SELECT parentid AS nodeid , nodeid AS parentid FROM table_x WHERE nodeid NOT IN ( SELECT nodeid FROM bottom_up_from_src ) START WITH nodeid = :dst_nodeid CONNECT BY nodeid = PRIOR parentid ) SELECT :src_nodeid || SYS_CONNECT_BY_PATH (parentid, '-->') AS display_path FROM bottom_up_from_dst WHERE parentid = :dst_nodeid START WITH nodeid = :src_nodeid CONNECT BY nodeid = PRIOR parentid ;
This will show how you can get it from: src_nodeid at dst_nodeid, moving to the top or to the bottom of a hierarchy at a time step. This will work regardless of the fact that
- : src_nodeid is the ancestor of the: dst_nodeid, or
- : src_nodeid is a descendant of: dst_nodeid, or
- both: src_nodeid and: dst_nodeid are the descendants of another node (e.g. 0).
- : src_nodeid is the ancestor of the: dst_nodeid, or
I hope that answers your question.
If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) and also publish outcomes from these data.
Explain, using specific examples, how you get these results from these data.
Always say what version of Oracle you are using (for example, 11.2.0.2.0). It is always important, but particularly so with CONNECT BY queries, because each version since Oracle 7 had significant improvements in this area.
See the FAQ forum {message identifier: = 9360002}
I just downloaded Oracle XE and migrated my database from MySQL to Oracle successfully.
And I'm doing/reviewing my application now especially the query syntax.
I am facing a problem, I have a request in the form below in MySQL and it works fine but when I tried it at the Oracle XE database, it cannot run :( So I'm asking for help here, I have already searched on the internet but had no luck.
SELECT A.INVOICEDATE,
*@Step1:=cast (adddate (A.INVOICEDATE, A.TERMOFPAYMENT) as date) AS STEP1, *.
*@Step2:=if (@step1 > CURDATE (), A.TOTALAMOUNT, 0) AS STEP2, *.
*@step3:=if (@step2 <>0,0,IF(DATEDIFF(CURDATE(),@step1) < = 30, A.TOTALAMOUNT, 0)) AS step 3,
T_invoice FROM has
WHERE A.STATUS = 'Open'
I can conclude the problem is based on the variable declaration in Oracle, but I still can't understand.
What I do in MySQL, it's that I want to fill the variable with the expression/logic so I can use 'than' variable more later in the other part of the query output syntax.
I used variables to be filled by value due to I can't use / select ALIAS later in the other party.
So please help to convert to Oracle query syntax. Thank you.
Oracle and MySql are two different animals. You need to learn SQL and Oracle's PL/SQL.
Your SQL
SELECT A.INVOICEDATE,
@step1:=cast(ADDDATE(A.INVOICEDATE,A.TERMOFPAYMENT) as date) AS STEP1,
@step2:=IF(@step1>CURDATE(),A.TOTALAMOUNT,0) AS STEP2,
@step3:=IF(@step20,0,IF(DATEDIFF(CURDATE(),@step1)<=30,A.TOTALAMOUNT,0)) AS STEP3,
FROM t_invoice A
WHERE A.STATUS='Open'
Can be writern as
select invoicedate,
step1,
step2,
case when step2 = 0
then case when sysdate-step1 <=30 then A.TOTALAMOUNT else 0 end
end AS step3
from (
select A.invoicedate,
A.invoicedate+A.termofpayment step1,
case when (A.invoicedate+A.termofpayment) > sysdate then A.totalamount else 0 end as step2,
from t_invoice A
where A.status = 'Open'
)
Question the use of Oracle query Variables
HelloI am new to Oracle, I tried to extract the data from the Oracle database by using the following query which includes 1 variable SYSDATE_UTS, but when I try to execute the query I get an error. Please let me know what I am doing wrong and how I can fix.
Error message
-----
ORA-06550: line 4, column 1:
PLS-00428: an INTO clause in this SELECT statement
-----
Oracle query
DECLARE SYSDATE_UTS NUMBER: = (sysdate-to_date('19700101','yyyymmdd')) * 86400;
BEGIN
SELECT
INCIDENT_NUMBER,
TO_DATE (to_char ((1/86400 * REPORTED_DATE) + to_date ('19700101', 'YYYYMMDD'), "jj/mm/aaaa hh24:mi:ss"), 'hh24:mi:ss dd/mm/yyyy') as REPORTED_DATE_TIME,
GROUP_TRANSFERS
LAST_MODIFIED_BY
, to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 * LAST_MODIFIED_DATE, "mm/dd/yyyy hh24:mi:ss"), 'hh24:mi:ss dd/mm/yyyy') as LAST_MODIFIED_DATE
, (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 * SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) age
CASE
WHEN (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 * SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) BETWEEN 0 AND 1, THEN ' 0 - 1 days
WHEN (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 * SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) BETWEEN 2 AND 4 and THEN 2-4 days
WHEN (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 * SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) BETWEEN 5 AND 9, THEN 5-9 days
WHEN (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 * SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) BETWEEN 10 AND 19, THEN 10-19 days
WHEN (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 * SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) > 20 THEN ' Days 20 + ".
ANOTHER "UNKNOWN".
END AS AGE_GROUP
OF IncidentDataBase
and the STATE not in (4,5,6)
and rownum < 10;
END;
If you run this query in sql * plus you can declare a sql * more variable, assign a numeric value according to 'now' and use in your application, i.e.
SQL > variable NUMBER of SYSDATE_UTS;
SQL > exec SYSDATE_UTS: = (sysdate-to_date('19700101','yyyymmdd')) * 86400;
You would then call your request, referring to sql * more variable with a colon as SYSDATE_UTS prior to it (i.e.: SYSDATE_UTS)
SELECT
INCIDENT_NUMBER,
TO_DATE (to_char ((1/86400 * REPORTED_DATE) + to_date ('19700101', 'YYYYMMDD'), "jj/mm/aaaa hh24:mi:ss"), 'hh24:mi:ss dd/mm/yyyy') as REPORTED_DATE_TIME,
GROUP_TRANSFERS
LAST_MODIFIED_BY
, to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 * LAST_MODIFIED_DATE, "mm/dd/yyyy hh24:mi:ss"), 'hh24:mi:ss dd/mm/yyyy') as LAST_MODIFIED_DATE
, (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 *: SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) age
CASE
WHEN (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 *: SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) BETWEEN 0 AND 1, THEN ' 0 - 1 days
WHEN (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 *: SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) BETWEEN 2 AND 4 and THEN 2-4 days
WHEN (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 *: SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) BETWEEN 5 AND 9, THEN 5-9 days
WHEN (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 *: SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) BETWEEN 10 AND 19, THEN 10-19 days
WHEN (to_date (to_char (to_date ('01011970 ', 'ddmmyyyy') + 1/24/60/60 *: SYSDATE_UTS, ' mm/dd/yyyy'), ' mm/dd/yyyy'))-(to_date (to_char (+ to_date('19700101','yyyymmdd') + 1/86400 * REPORTED_DATE, ' mm/dd/yyyy'), ' mm/dd/yyyy')) > 20 THEN ' Days 20 + ".
ANOTHER "UNKNOWN".
END AS AGE_GROUP
OF IncidentDataBase
and the STATE not in (4,5,6)
and rownum<>
Table stores IncidentDataBase the "dates" in the number of seconds since the epoch unix?
Hello
I have a customer as requirement below
We have the table header and contains data such as
ID, custname, socket
101, raju, 514
102, ratna, 12
103, rakesh, 16
104, joseph, 129
and we mappingtable like below as
sampval, socket
244094,512
244095,2
244096,4
244097,8
244098,16
244102,128
244103,1
If header.mapvalue is 514 analysis then it out on the mappingtable basis to be exported for a value of 244094. 244095.
Header.mapvalue is 12 parsing it out based on mappingtable to be exported for a value of 244096. 244097 and so on...
Could you please help me how to get the functionality in a database query.
Thanks in advance
Try to query below and let me know
SELECT id,
custName,
HT.mapvalue,
req_val
From ht header_tbl,
(SELECT SAMPVAL,
socket,
CITY,
SUMVAL,
REQ_VAL
Of
(SELECT SAMPVAL,
socket,
CITY,
SUMVAL,
REQ_VAL,
ROW_NUMBER() over (ORDER BY lvl SUMVAL PARTITION) rn
Of
(SELECT sampval,
socket,
City,
LEVEL lvl,
CASE
WHEN ((= LEVEL 2)
AND (socket = socket connect_by_root))
THEN socket
ANOTHER socket + connect_by_root socket
END as sumval,
CASE
WHEN ((= LEVEL 2)
AND (socket = socket connect_by_root))
THEN TO_CHAR (sampval)
Of OTHER TO_CHAR (CONNECT_BY_ROOT sampval
||'|'
|| sampval)
END AS req_val
A mapping
CONNECT BY LEVEL<=>=>
)
WHERE the lvl = 2
)
WHERE rn = 1
) qry_rslt
WHERE ht.mapvalue = qry_rslt.sumval;
An alternative with xmlagg, you can use this query to achieve your requirement
SELECT id, custname, RTRIM (xmlagg (xmlelement(e,mp.sampval||'|')). Extract ('//Text ()'),'| ') req_val
From header_tbl, mapping mp ht
WHERE mp.mapvalue = BITAND (mp.mapvalue, ht.mapvalue)
GROUP BY id, custname;
Hierarchical query help need to Oracle
HelloI'm in a deep trouble.
I want to do a hierarchical query in my TreeView table in oracle. But I want that the query retrieves information path between two nodes.
Review the table below.
------------------------------------------------------
Node ID | Name of the node. ID of the parent
-----------------------------------------------------
1A 0
2 1
3C0
4 2
-----------------------------------------------------
What I want is that I'll give you 2 node id in the request. One is the source node and the other is the destination node.
so if I put 1 node and node 4, I want to be the output
1-> 2-> 4
I want just a return line. Or there may be several rows but stands only the node of this path.
1
2
4
----
This means that I don't want to node 3 in the search result.
More if I have the source as 4 node and the node of destination as 1 then I will also get an output of the query as below-
4-> 2-> 1
This means that the search for the query for the result in a way directional bi.
If anyone can give me some information about who it's going to be great for me.
Published by: user13276471 on December 31, 2012 03:21
Hello
Welcome to the forum!
Assuming you have 2 separate nodes: Node_A and: node_b, you can find the hierarchy from one to the other like this:
SELECT SYS_CONNECT_BY_PATH (node_id, ',') AS lineage
FROM table_x
WHERE node_id IN (:node_a, :node_b)
AND LEVEL > 1
START WITH node_id IN (:node_a, :node_b)
CONNECT BY parent_id = PRIOR node_id
;
It will work if: the Node_A is an ancestor of the: node_b, or if: node_b is an ancestor of the: Node_A.
If is the ancestor of the other, then it will produce 0 rows.
Query Help Oracle [beginner]
HelloI want to display the records of these employees, whose qualities are between 2-4 by the EMP, Salgrade table of oracle... Schema Scott.
Help, please.
Select *.
from emp e, s salgrade
where e.sal > = s.losal and e.sal<>
and s.grade between 2 and 4
SQL query help in oracle database 11g
I have test table name these data and column as
Select t.user_id, t.user_date, t.amount
t-test
----------------------------------------------------------
User_id date amount
10 1/1 / 2011 10
20/1 2/11/2011
11 1/3 / 2011 30
40/2 10/2/2011
You want to display data in this format...
User_id date amount here is the logic how to get the amount
1 10/1/2011 10 10
11 1/2 / 2011 30 10 + 20
11 1/3 / 2011 60 (10 + 20) + 30
2 10/2/2011 40 40
SELECT | t.user_id, t.user_date, t.amount | |
, | SUM (t.amount) OVER (PARTITION BY t.user_id |
ORDER BY | t.user_date |
) AS the amount | ||
Of | t-test |
ORDER BY t.user_id |
, | t.user_date |
; |
Oracle 11g Release 2
Frank Kulash was able to help on this issue yesterday. But I got additional requirements. Details below.
CASE 1:
create table t
(key primary id number,)
supplier_id number,
number of supplier_desc_id
batch number,
date of dt_recv
)
/
Insert into t
values (35405,605,3809,0,TO_DATE('14-JUN-2013','DD-MON-yyyy')
/
Insert into t
values (58543,605,3809,0,TO_DATE('10-DEC-2013','DD-MON-yyyy')
/
Insert into t
values (136793,605,3809,1,TO_DATE('11-NOV-2014','DD-MON-yyyy')
/
Insert into t
values (96510,605,3809,1,TO_DATE('11-JUN-2014','DD-MON-yyyy')
/
Insert into t
values (94222,605,3809,1,TO_DATE('09-MAY-2014','DD-MON-yyyy')
/
Insert into t
values (108229,605,3809,3,TO_DATE('09-SEP-2014','DD-MON-yyyy')
/
Insert into t
values (114585,605,3809,2,TO_DATE('28-OCT-2014','DD-MON-yyyy')
/
commit;
Select * from t;
ID SUPPLIER_ID SUPPLIER_DESC_ID BATCH DT_RECV
---------- ----------- ------------------------ ----------------- -------------
35405 605 3809 0 14 JUNE 2013
58543 605 3809 0 10 DECEMBER 2013
3809 605 136793 1 11 NOVEMBER 2014
96510 605 3809 1 10 JUNE 2014
94222 605 3809 1 9 MAY 2014
108229 605 3809 3 09 - SEP - 2014
114585 605 3809 2 28 OCTOBER 2014
RULE: when there are 2 or more records with batch = 1, return the two most
recent recordings with batch = 1 AND any recording (no matter the batch) that
has a DT_RECV > = only the DT_RECV of the 2nd record most of batch = 1 (June 10, 2014)
96510 10 June 2014 (2nd most active record with batch = 1)
136793 11 November 2014 (more current label with batch = 1)
94222 may 9, 2014 (not interested in this matter, since it is greater than 2 versions)
The results should be:
ID SUPPLIER_ID SUPPLIER_DESC_ID STATUS_ID DT_RECV ---------- ----------- ------------------------ ----------------- ------------- 96510 605 3809 1 10-JUN-2014 136793 605 3809 1 11-NOV-2014 114585 605 3809 2 28-OCT-2014 >= 10-JUN-2014 108229 605 3809 3 09-SEP-2014 >= 10-JUN-2014 This query returns the correct results: WITH got_r_num AS ( SELECT id, supplier_id, supplier_desc_id, status_id, dt_recv , ROW_NUMBER () OVER ( PARTITION BY supplier_id,supplier_desc_id,status_id ORDER BY dt_recv DESC ) AS r_num FROM t ) , got_dt_cutoff AS ( SELECT id, supplier_id, supplier_desc_id, status_id, dt_recv , MIN ( CASE WHEN status_id = 1 AND r_num <= 2 THEN dt_recv END ) OVER (PARTITIN BY supplier_id,supplier_desc_id) AS dt_cutoff FROM got_r_num ) SELECT id, supplier_id, supplier_desc_id, status_id, dt_recv FROM got_dt_cutoff WHERE dt_recv >= dt_cutoff ORDER BY dt_recv ; NOTE: records are grouped by supplier_id/supplier_desc_id
CASE 2:
truncate table t; insert into table t values(45401,801300,4466,0,TO_DATE('21-AUG-2013','DD-MON-YYYY') / insert into table t values(44414,801300,4466,0,TO_DATE('08-AUG-2013','DD-MON-YYYY') / commit ; select * from t; CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE ---------- ----------- ------------------------ ----------------- ------------ 451 801300 4466 0 21-AUG-2013 44414 801300 4466 0 08-AUG-2013 801300 4466 0 08-AUG-2013
RULE: when there is no batch = 1, then return all rows
The query above does not work for this case.
CASE 3:
truncate table t; insert into table t values(29887,609051,1781,0,TO_DATE('19-APR-2013','DD-MON-YYYY') / insert into table t values(33623,609051,1781,0,TO_DATE('24-MAY-2013','DD-MON-YYYY') / insert into table t values(45477,609051,1781,0,TO_DATE('22-AUG-2013','DD-MON-YYYY') / insert into table t values(54013,609051,1781,1,TO_DATE('22-OCT-2013','DD-MON-YYYY') / commit; select * from t; CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE ---------- ----------- ------------------------ ----------------- ------------- 29887 609051 1781 0 19-APR-2013 33623 609051 1781 0 24-MAY-2013 45477 609051 1781 0 22-AUG-2013 54013 609051 1781 1 22-OCT-2013
RULE: When there is only to record with batch = 1, return all rows
The query above does not work for this case.
Hello
orclrunner wrote:
Oracle 11g Release 2
Frank Kulash was able to help on this issue yesterday. But I got additional requirements. Details below.
CASE 1:
create table t
(key primary id number,)
supplier_id number,
number of supplier_desc_id
batch number,
date of dt_recv
)
/
Insert into t
values (35405,605,3809,0,TO_DATE('14-JUN-2013','DD-MON-yyyy')
/
Insert into t
values (58543,605,3809,0,TO_DATE('10-DEC-2013','DD-MON-yyyy')
/
Insert into t
values (136793,605,3809,1,TO_DATE('11-NOV-2014','DD-MON-yyyy')
/
Insert into t
values (96510,605,3809,1,TO_DATE('11-JUN-2014','DD-MON-yyyy')
/
Insert into t
values (94222,605,3809,1,TO_DATE('09-MAY-2014','DD-MON-yyyy')
/
Insert into t
values (108229,605,3809,3,TO_DATE('09-SEP-2014','DD-MON-yyyy')
/
Insert into t
values (114585,605,3809,2,TO_DATE('28-OCT-2014','DD-MON-yyyy')
/
commit;
Select * from t;
ID SUPPLIER_ID SUPPLIER_DESC_ID BATCH DT_RECV
---------- ----------- ------------------------ ----------------- -------------
35405 605 3809 0 14 JUNE 2013
58543 605 3809 0 10 DECEMBER 2013
3809 605 136793 1 11 NOVEMBER 2014
96510 605 3809 1 10 JUNE 2014
94222 605 3809 1 9 MAY 2014
108229 605 3809 3 09 - SEP - 2014
114585 605 3809 2 28 OCTOBER 2014
RULE: when there are 2 or more records with batch = 1, return the two most
recent recordings with batch = 1 AND any recording (no matter the batch) that
has a DT_RECV > = only the DT_RECV of the 2nd record most of batch = 1 (June 10, 2014)
96510 10 June 2014 (2nd most active record with batch = 1)
136793 11 November 2014 (more current label with batch = 1)
94222 may 9, 2014 (not interested in this matter, since it is greater than 2 versions)
The results should be:
- ID SUPPLIER_ID SUPPLIER_DESC_ID BATCH DT_RECV
- ---------- ----------- ------------------------ ----------------- -------------
- 96510 605 3809 1 10 JUNE 2014
- 3809 605 136793 1 11 NOVEMBER 2014
- 114585 605 3809 2 28 OCTOBER 2014 > = JUNE 10, 2014
- 108229 605 3809 3 09 - SEP - 2014 > = JUNE 10, 2014
- This query returns the correct results:
- WITH got_r_num AS
- (
- SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
- , ROW_NUMBER () OVER (PARTITION BY supplier_id, supplier_desc_id, batch)
- ORDER BY dt_recv DESC
- ) AS r_num
- T
- )
- got_dt_cutoff AS
- (
- SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
- MIN (CASE
- WHEN batch = 1
- AND r_num<=>=>
- THEN dt_recv
- END
- ) ON (PARTITIN BY supplier_id, supplier_desc_id) AS dt_cutoff
- OF got_r_num
- )
- SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
- OF got_dt_cutoff
- WHERE dt_recv > = dt_cutoff
- ORDER BY dt_recv
- ;
- NOTE: the records are grouped by supplier_id/supplier_desc_id
CASE 2:
- truncate table t;
- insert into table t
- values (45401,801300,4466,0,to_date('21-Aug-2013','DD-mon-YYYY')
- /
- insert into table t
- values (44414,801300,4466,0,to_date('08-Aug-2013','DD-mon-YYYY')
- /
- commit;
- Select * from t;
- CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE
- ---------- ----------- ------------------------ ----------------- ------------
- 451 801300 4466 0 21 AUGUST 2013
- 44414 801300 4466 0 AUGUST 8, 2013
- 801300 4466 0 AUGUST 8, 2013
RULE: when there is no batch = 1, then return all rows
The query above does not work for this case.
CASE 3:
- truncate table t;
- insert into table t
- values (29887,609051,1781,0,to_date('19-Apr-2013','DD-mon-YYYY')
- /
- insert into table t
- values (33623,609051,1781,0,to_date('24-May-2013','DD-mon-YYYY')
- /
- insert into table t
- values (45477,609051,1781,0,to_date('22-Aug-2013','DD-mon-YYYY')
- /
- insert into table t
- values (54013,609051,1781,1,to_date('22-Oct-2013','DD-mon-YYYY')
- /
- commit;
- Select * from t;
- CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE
- ---------- ----------- ------------------------ ----------------- -------------
- 29887 609051 1781 0 19 APRIL 2013
- 33623 609051 1781 0 24 MAY 2013
- 45477 609051 1781 0 22 AUGUST 2013
- 54013 609051 1781 1 22 OCTOBER 2013
RULE: When there is only to record with batch = 1, return all rows
The query above does not work for this case.
Want to get answers that work, or is it possible to get responses that cause errors?
Make sure that the INSERT statements you post too much work. Test (and, if necessary, correct) them before posting. All the instructions insert above have errors.
The query above (once you correct the spelling of PARTITION) returns all the lines after a date limit. It's always what you want, only the details of how calculated this date limit changed. In accordance with the new requirements, the closing date must be earlier than the actual lines dt_recv in there are not 2 (or more) with batch = 1 for any combination of (supplier_id, supplier_desc_id). All you have to do is change "r_num".<= 2"="" to="" "r_num="2" ,"="" when="" computing="" dt_cutoff,="" and="" return="" an="" impossiblly="" early="" date="" if="" there="" is="" no="" such="" row. ="" (i="" assume="" that="" dt_recv="" can="" not="" be="">=>
WITH got_r_num AS
(
SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
ROW_NUMBER () OVER (PARTITION BY supplier_id
supplier_desc_id
batch
ORDER BY dt_recv DESC
) AS r_num
T
)
got_dt_cutoff AS
(
SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
, NVL ( MIN (CASE)
WHEN batch = 1
AND = 2 r_num - not <=, as="">=,>
THEN dt_recv
END
) OVER (PARTITION BY supplier_id
supplier_desc_id
)
, TO_DATE ('1', 'J') - first DATE in Oracle
( ) AS dt_cutoff
OF got_r_num
)
SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
OF got_dt_cutoff
WHERE dt_recv > = dt_cutoff
ORDER BY supplier_id
supplier_desc_id
dt_recv
;
If dt_recv can be NULL, it is a bit more complicated, but only a little. Post instructions INSERT (work) and outcomes if you would like to help with this scenario.
need help on creating pivot query
SELECT * FROM TEST1
----------------
VALUE OF PERSON COMPUTERNAME
COMP1 ABC 3
COMP2 ABC 5
COMP1 CAD 3
COMP3 CAD 5
COMP2 TES 1
COMP1 TES 5
COMP3 ABC 2
myQuery
----------------------------
Select the link null, label, value1 COUNT (VALUE)
from 'test1 '.
CONTROL group PER PERSON
Results
---------
Link label value1
- ABC 3
- CAD 2
- TES 2
My requirement
---------------
can we have something like that out using the concept of pivot? If so can you share an example query pls.
Link label value1
-ABC ORDI1, COMP2, COMP3
-CAD COMP1, COMP2
-YOUR ORDI1, COMP3
Hello
Subhash C-Oracle wrote:
need help on creating pivot query
SELECT * FROM TEST1
----------------
VALUE OF PERSON COMPUTERNAME
COMP1 ABC 3
COMP2 ABC 5
COMP1 CAD 3
COMP3 CAD 5
COMP2 TES 1
COMP1 TES 5
COMP3 ABC 2
myQuery
----------------------------
Select the link null, label, value1 COUNT (VALUE)
from 'test1 '.
CONTROL group PER PERSON
Results
---------
Link label value1
- ABC 3
- CAD 2
- TES 2
My requirement
---------------
can we have something like that out using the concept of pivot? If so can you share an example query pls.
Link label value1
-ABC ORDI1, COMP2, COMP3
-CAD COMP1, COMP2
-YOUR ORDI1, COMP3
This sounds like a job for LISTAGG:
SELECT NULL AS link
label
LISTAGG (comp_name, ',')
THE Group (ORDER BY ComputerName) AS value1
OF test1
GROUP BY label
;
If you would care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.
Are you sure that the results you posted are what you want from data provided?
Is of the order of the elements in a significant list? In other words, when you say you want to get the results:
COMP1, COMP2
you'd be just as happy with
ORDI1, COMP2
? If the order is important, explains what this order.
SQL Query Help (not working not properly)
Hello everyone,
I use JDeveloper 12.1.2.0.0. I do a two-way communication using 3 tables, with links between them (and using schema HR).
In my example, I have something like:
Departments, employees, and the SalaryByJobs (I created this table where it shows a departmentd id, employee id, salary).
Whenever I click on one OR more departments, the employees up-to-date table by putting on the table, employees who belong to the selected department and the salaryByjob to put the jobs of the employees selected on the employees table.
So it's something like this:
The departments selected-> employees selected-> salarybyjbobs of these employees.
My query used (in the view) has the following code:
SELECT Employees.COMMISSION_PCT,
Employees.DEPARTMENT_ID,
Employees.EMAIL,
Employees.EMPLOYEE_ID,
Employees.FIRST_NAME,
Employees.HIRE_DATE,
Employees.JOB_ID,
Employees.LAST_NAME,
Employees.MANAGER_ID,
Employees.PHONE_NUMBER,
Employees.SALARY
Employees EMPLOYEES
WHERE (department_id IN (select * from THE (select cast (in_list(:variavel3) as mytableType) double) a))
Since I use the links, the employees table does not show anything at the beginning, so I added this to my query used to go: OR nvl(:variavel3,0) = 0
But now, whenever I try to select multiple lines, it gives me the invalid numbers and I don't understand why...
It's only one line of code and it is not in the bean.
Can someone help me?
PS - The bean will Department by Department, adds the departments with ',' and for each Department, gets employees who belongs to them.
My best regards,
Frederico Barracha.
The expression NVL (: variavel3, 0) = 0 is not correct. The data type of the return value of the NVL function is considered to be equal to the data type of the argument of 1 (that is, the data type of the variable binding: variavel3). You said that this variable contained a list separated by commas to ID, so the data type of the variable is VARCHAR2. As long as you compare the NVL expression of a number, you get 'Invalid number' exception, because Oracle expects a numeric data type on the left side of the comparison operator.
To avoid the exception "Invalid number", you can modify the expression by using one of the following options:
: variavel3 IS NULL
NVL (: variavel3, ' *') = ' *'
NVL (: variavel3, ' 0') = '0'
Option 1, so the simplest and clearest.
Dimitar
Oracle query sort by case-sensitivity
Hi all
I use the oracle 11g database.
My use case is that I have a table with the following values
Name table - test
product id productsortdescription H58098 ACETAMIDOHYDROXYPHENYLTHIAZOLE 043994 Alloy .MM.INTHICK
My query is
select * from test order by productsortdescription;
This query gives the result as it is like
product id productsortdescription
H58098 ACETA
product id productsortdescription
H58098 ACETA
produit id productsortdescription
H58098 ACETA
product id productsortdescription
H58098 ACETA
product id productsortdescription H58098 ACETAMIDOHYDROXYPHENYLTHIAZOLE 043994 Alloy .MM.INTHICKMIDOHYDROXYPHENYLTHIAZOLE
043994 alloy. MR. INTHICK
but early output/outcome should be as below:
product id productsortdescription
043994 | Alloy. MR. INTHICK |
H58098 ACETAMIDOHYDROXYPHENYLTHIAZOLE
like all and ACE in productsortdescription
l is in a small suitcase to C.
The NLS Session parameters are as follows
SELECT * from NLS_SESSION_PARAMETERS;
NLS_LANGUAGE AMERICAN
NLS_TERRITORY AMERICA
NLS_CURRENCY $
NLS_ISO_CURRENCY AMERICA
NLS_NUMERIC_CHARACTERS.,.
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE AMERICAN
NLS_SORT BINARY
NLS_TIME_FORMAT HH.MI. SSXFF AM
NLS_TIMESTAMP_FORMAT-DD-MON-RR HH.MI. SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI. SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI. SSXFF AM TZR
NLS_DUAL_CURRENCY $
BINARY NLS_COMP
NLS_LENGTH_SEMANTICS BYTES
NLS_NCHAR_CONV_EXCP FAKE
Please, help me in this scenario.
One option is to use the NLSSORT function. Most of the ASCII character set defines the sort lowercase before uppercase but EBCDIC kinds of lowercase before uppercase. So you can use
with t as)
Select the id "H58098", "ACETAMIDOHYDROXYPHENYLTHIAZOLE" union str double all the
SELECT id, ' 043994 ',' alloy. Mr. INTHICK' double str
)
SELECT id, str
t
NLSSORT order (str, 'NLS_SORT = EBCDIC')
;ID STR
------ ------------------------------
043994 alloy. MR. INTHICK
H58098 ACETAMIDOHYDROXYPHENYLTHIAZOLE
Of course, if your strings can contain non-alphanumeric characters, you should check that the sort of EBCDIC order is acceptable for them as well. To check this, you can use something like
with t (str, ascii) as long as)
Select chr(level+32), level + 32 double connect by level<=>=>
)
Select str, ascii from t by NLSSORT (str, 'NLS_SORT = EBCDIC')
;
or simply do a search of the internet on EBCDIC. You can also substitute other kinds of language for EBCDIC and see if any of them meet your needs. See Appendix A of the Guide to support globalization for the list of valid values for NLS_SORT.
You say that you are on 11g - if you want to say 11.2.x, then you can use the listagg function to get a more compact view of the sort order:
with t (str, ascii) as long as)
Select chr(level+32), level + 32 double connect by level<=>=>
)
Select listagg (str) in the Group (order by NLSSORT (str, 'NLS_SORT = EBCDIC')) as EBCDIC_order
t
;
EBCDIC_order
----------------------------------------------------------------------------------------------
. <(+|&!$*);- %_="">?': #@'="abcdefghijklmnopqr~stuvwxyz[^]{ABCDEFGHI}JKLMNOPQR\STUVWXYZ0123456789
Kind regards
Bob
(+|&!$*);->Column1 Column2 Column3
Tier1 Group1 1
Tier1 Group1 2
Group level 1 2 3
Tier1 Group2 4
Group level 1 3 5
Level 1 Group 3 6
Expected result:
Column1 Column2 Column3
Tier1 Group1 1
2
2 3 Group
4
3 5 group
6
Thanks for your help
Hello
Your front end that can probably not for you.
In SQL * Plus, for example:
BREAK ON column1 ON column2
SELECT column1, column2, column3
FROM table_x
ORDER BY column3
;
If you havd do in SQL, here's one way:
SELECT CASE
WHEN ROW_NUMBER () OVER ( PARTITION BY column1
ORDER BY column3
) = 1
THEN column1
END AS col1
, CASE
WHEN ROW_NUMBER () OVER ( PARTITION BY column2
ORDER BY column3
) = 1
THEN column1
, END AS col2
, column3
FROM table_x
ORDER BY column3
;
I hope that answers your question.
If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.
Highlight a few places where the queries above are getting incorrect results and explain, using specific examples, how you get the results of the data provided in these places.
Always tell what version of Oracle you are using.
Published by: Frank Kulash, March 21, 2012 16:15
I'm new to the development of Oracle.
Oracle 10 g 2
My original query:
SELECT APP, count (*)
TRANSACTION
WHERE TYPE in ('ShipmentConfirmPR', 'ShipmentConfirm', 'ShipConfirm')
and the APP ("SAPPI", "SAPPI", "SAPR3") and INTERVENE ('9320 ', '9332','1208 ')
GROUP BY APP TYPE
order of the APP
the result of this query:
SAPPI 100
SAPPI 600
SAPR3 440
My requirement
And I want to have something like output
LDCS 100
TSW 600
PDC 440
IE.the APPP and STEP combinations. Must return the specified values
SAPPI & 9320-> LOC (here SAPPI IE APP is the same for both... but it's a coincidence IE APP can be sliced also)
SAPPI & 9332-> tsw
SAPR3 & 1208-> pdc
Options, I tried:
Query provided by one of the Forum members...
SELECT THE CHECK BOX
WHEN APP = "SAPP1" THEN DECODE (step, '9320', 'LSW', '9332', "TSW")
WHEN APP = "SAPR3" step = '1208' AND 'PDC '.
END app
COUNT (*)
TRANSACTION
WHERE TYPE in ('ShipmentConfirmPR', 'ShipmentConfirm', 'ShipConfirm')
AND THE APP ("SAPPI", "SAPPI", "SAPR3")
AND STEP IN ('9320', '9332', ' 1208')
GROUP BY APP, STEP
ORDER OF THE APP.
EXECUTION PLAN
| ID | Operation | Name |
------------------------------------------------------------------------
| 0 | SELECT STATEMENT |
| 1. GROUP SORT BY |
| 2. INLIST ITERATOR.
| 3. TABLE ACCESS BY INDEX ROWID | TRANSACTION |
| 4. INDEX RANGE SCAN | TRANSACTION_IDX |
The output of the query (as above) must partially match the following query (a particular combination of CLO)
SELECT count (1)
TIBCO. TRANSACTION_HISTORY
WHERE TYPE = 'ShipmentConfirm '.
and APP in ("SAPPI") and INTERVENE ('9332')
My Questions:
1.*There are indexes on all 3 APP passes it's IE, STEP and TYPE *. I don't want a FULL table Scan (as one would use the index). Can change us the query / use of indices, etc. to make it faster?
2. is the right to approach? Would the use of the concat operator in the function decode work better for my needs?
Something like
Select decode (APP |) STEP, 'SAPP9332', 'X') of TRANSACTION_HISTORY where < COND >
If Yes can you please provide the query?
3. ANY other approach / request for my requirement.
Thanks in advance.
Hello
user13517642 wrote:
... EXECUTION PLAN| ID | Operation | Name |
------------------------------------------------------------------------
| 0 | SELECT STATEMENT |
| 1. GROUP SORT BY |
| 2. INLIST ITERATOR.
| 3. TABLE ACCESS BY INDEX ROWID | TRANSACTION |
| 4. INDEX RANGE SCAN | TRANSACTION_IDX |The output of the query (as above) must partially match the following query (a particular combination of CLO)
SELECT count (1)
TIBCO. TRANSACTION_HISTORY
WHERE TYPE = 'ShipmentConfirm '.
and APP in ("SAPPI") and INTERVENE ('9332')My Questions:
1.*There are indexes on all 3 APP passes it's IE, STEP and TYPE *. I don't want a FULL table Scan (as one would use the index). Can change us the query / use of indices, etc. to make it faster?
A full table scan might be the fastest way to get results. Do you have any reason to think that it would be faster to go through the index? How selective are the clues? In other words, what is the percentage of rows in the table correspond to each of the values in the WHERE clause?
2. is the right to approach?
It depends on what you're trying to do, which is not at all clear to me.
Would the use of the concat operator in the function decode work better for my needs?
Something likeSelect decode (APP |) STEP, 'SAPP9332', 'X') of TRANSACTION_HISTORY where
If you use this approach, look out for the problem Ab asse crevit . For example, if you have these 4 rows and 2 columns:
str1 str2
---- ----
(NULL) FOO
F OO
FO O
FOO (NULL)
There are 4 values of distict of str1 (counting NULL) and 4 separate values of str2, str1 but | str2 is the same for each of them. In the above example, it there is no way to know, just by looking at the concatenated string, including str1 and str2 ends begins. Maybe it's not the case for your specific data (for example, if the application is still exactly 5 characters long). otherwise, you may need to add some kind of delimiter, like this
app || '+' || step
where you know that '+' never occurs in one of these columns.
3. ANY other approach / request for my requirement.
CASES, as I mentioned in your previous message:
Decode the help function
and as you have used above.
In this thread, you said "I have to use the decode function. Why? It is a competition of school Etudieeo DECODE is explicitly required? Why you don't want in the best way, what that turns out to be?
Your WHERE clause:
AND APP IN ('SAPPI', 'SAPPI', 'SAPR3')
AND STEP IN ('9320', '9332', '1208')
admits 6 possible combinations of APA and step:
app step
----- ----
SAPP1 9320
SAPP1 9332
SAPP1 1208
SAPP3 9320
SAPP3 9332
SAPP3 1208
but you are looking for only 3 of these combinations in DECODE it or the expression BOX. (Have 2 copies of 'SAPP1' e list won't do any good, but it does hurt real, either.)
By the way, is "SAPPI" app with the letter 'I' at the end, or "SAPP1", with the number '1' at the end?
Published by: Frank Kulash, March 24, 2011 19:44
This is a Customers table and we will have to find they are elgible for product produced B or both or none.
But if you're serviceble by product B, it is certain that you will be good product a. (imagine the product A is greater coverage of Prouct and be produced is the smallest)
What I have done becomes the product of A B and get the remains that are usable for the product and not product B.
< pre >
CREATE TABLE CUST_TEST
(
Number of cust_id,
Column_serviceable_A varchar2 (2),
Column_Serviceable_B varchar2 (2),
Column_Color_A varchar2 (2),
Column_Color_B varchar2 (2),
Area_OO varchar2 (2),
Area_H varchar2 (2),
Varchar2 (10) STATE
);
insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
values(1,'Y','Y','G','G','Y','Y','HYD');
insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
values(2,'Y','Y','G','G','Y','Y','HYD');
insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
values(3,'Y',,'G',,'Y','Y','HYD');
insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
values(4,'Y',,'R',,'Y','Y','BANG');
insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
values(5,,,,,'Y','Y','BANG');
insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
values(6,'Y','Y','G','G','Y','Y','CHEN');
insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
values(7,'Y',,'G',,'Y','Y','CHEN');
insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
values(8,'Y',,'G',,'Y','Y','CHEN');
< / pre >
< pre >
Select
(
(
Select sum (case when area_oo = 'Y' or area_h = 'Y' then 1 else 0 end) of CUST_TEST one
where column_serviceable_A = 'Y '.
and Column_Color_A ('g', 'A')
and status = 'HYD.
)
-
(
Select sum (case when area_oo = 'Y' or area_h = 'Y' then 1 else 0 end) of CUST_TEST one
where Column_Serviceable_B = 'Y '.
and Column_Color_B ('g', 'A')
and status = 'HYD.
)
)
Double;
< / pre >
But I need this 15 times as there are 15 States, I want to do in a single request via the Group of. CUST_TESTis also a view and each query takes once NGOs as customers there 15 million.
Please let me know if you need more information.
Desirable output;
COUNT (*) STATE
HYD 1
BANG null
CHEN 2
Thank you and best regards,
Cracky
Published by: CrackerJack 22 November 2010 16:51
Hello
Thanks for posting the sample data in a useful form. That really helps.
So you want a query that produces a line of output for each value in the column state.
That's exactly what "State GROUP BY" don't:
SELECT state
, COUNT ( CASE
WHEN column_serviceable_a = 'Y'
AND column_color_a IN ('G', 'A')
THEN 1
END
)
- COUNT ( CASE
WHEN column_serviceable_b = 'Y'
AND column_color_b IN ('G', 'A')
THEN 1
END
) AS cnt
FROM cust_test
WHERE area_oo = 'Y'
AND area_h = 'Y'
GROUP BY state
ORDER BY state
;
In fact, this becomes 0 as the county when no line meets the criteria of "has".
If you prefer that NULL, as you posted, then replace the first indictment by AMOUNT:
SELECT state
, SUM ( CASE
WHEN column_serviceable_a = 'Y'
AND column_color_a IN ('G', 'A')
THEN 1
END
)
- COUNT ( CASE
WHEN column_serviceable_b = 'Y'
AND column_color_b IN ('G', 'A')
THEN 1
END
) AS cnt
FROM cust_test
WHERE area_oo = 'Y'
AND area_h = 'Y'
GROUP BY state
ORDER BY state
;
Output:
STATE CNT
---------- ----------
BANG
CHEN 2
HYD 1
Note that we do not need duplicate in this problem. Whenever you are tempted to use double, ask yourself why. See if the information you want to be taken from another table that you use when even.
This should be much faster than the original request. Instead of making 2 passes through the table for each status value (2 * 15 = 30 past total), just make a pass through the table.
Published by: Frank Kulash, 22 November 2010 20:14
Simplified 2nd query (for number of NULL).
Maybe you are looking for
-
Sound appear through the speakers connection adapter to Satellite P300
I have a Satellite P300 and every time that I conect the connector AC/DC for power I hear a sound in the headphones or the speakers. This sounds like a terrible sound and I use the p300 to work with sound and I need help to lose this sound. Can anyon
-
Re: Toshiba Recovery software download
I tried going through the F8 option and go to the recovery screen. When I access the menu options it does not give me the option to "TOSHIBA HDD Recovery" just the normal windows. I spoke with TOSHIBA who think that the recovery folder has been delet
-
HP Officejet Pro 8715: eliminate unwanted faxes
I recently bought an officejet pro 8715. I want to eliminate unwanted faxes. But many shippers send the fax with the fax number not identified. (I can't block these faxes unidentified). I want to store all the faxes on the 8715 and then post them
-
PCI-6229 60 Hz noise and drift voltage
I'm having some trouble with an acquisition of data PCI-6229, connected via a CB-68LP. I'm under Labview8.6.1. The problem is with all the analog inputs. There seems to be a kind of noise in the signal, and I can't determine a reason for this. As it
-
HP Pavilion 15 laptop memory upgrade
Hey! I would appreciate to get instruction how to upgrade the RAM in my laptop HP Pavilion 15. Produktnummer: K6Y48EA #UUW, name of the model: HP Pavilion Notebook - 15-p154no Thanks in advance Ambjorn