Query Join help
Hello:I need help to write my side of my SQL statement join.
I have four tables with the following fields. The State tables, specialty and Classification are just tables of research.
1 Organization->
-ORG_KEY (PK) NOT NULL default (null)
-STATE_KEY (FK) NOT NULL default (null)
-SPECIALITY_KEY (FK) NOT NULL default (null)
-CLASSIFICATION_KEY (FK) default NULL (null)
2 State->
-STATE_KEY (PK) NOT NULL default (null)
-NAME NOT NULL default (null)
-CODE NON default NULL (null)
3 specialty->
-SPECIALITY_KEY (PK) NOT NULL default (null)
-NAME NOT NULL default (null)
-CODE NON default NULL (null)
4 classification-->
-CLASSIFICATION_KEY (PK) default NULL (null)
-NAME NOT NULL default (null)
-CODE NON default NULL (null)
When I run my select (using SQL Developer) and the SPECIAILITY_KEY and CLASSIFICATION_KEY fields are NOT filled (they have a null value appear as their value), I get no results and in the Express Application, I get an error message indicating "no data found".
At the point where I have enter values in these two areas, the query returns records lines.
When I try to insert a record and do not include a value for SPECIALITY_KEY and CLASSIFICATION_KEY, I get the following error message:
ORA-01400: cannot insert NULL into ('TEST'. "ORGANIZATION '." "" ""CLASSIFICATION_KEY")."
This is my SELECT statement:
select a.org_key,
e.org_key||' - '|| e.name as parent,
e.name as name1,
a.sponsor, a.subscriber, a.provider,
a.is_legal_entity,
a.short_name, a.name, a.status, a.external_id, a.tin, a.region,
a.address_line_1,a.address_line_2, a.city,
b.state_cd||' - '|| b.name as state, a.zip,
a.zip_plus_four, a.live_date, a.contact_salutation, a.contact_first_name,
a.contact_middle_initial, a.contact_last_name, a.contact_name_suffix,
a.contact_email_address, a.primary_phone, a.primary_phone_ext,
a.other_phone, a.other_phone_ext, a.fax_no, a.website_url,
a.web_org_type, a.created_date, a.created_by, a.last_update_date,
a.last_updated_by,
c.classification_key||' - '|| c.name as classification,
d.speciality_key||' - '|| d.name as speciality,
a.bill_address_line_1, a.bill_address_line_2, a.bill_city,
b.state_cd||' - '|| b.name as bill_state, a.bill_zip,
a.bill_zip_plus_four, a.dwkey_org
from mdd.organization a,
mdd.state b,
mdd.org_classification c,
mdd.speciality d,
mdd.organization e
where a.state_key = b.state_key
and a.classification_key = c.classification_key
and a.speciality_key = d.speciality_key
and a.parent_org_key = e.org_key
If I do not write the WHERE clause with all joints, I received several entries for the same record. With this WHERE clause, my query returns one record per org_key.Now, at the time where the user creates an org, the user can ignore the specialty or classification so they would be leave the field blank. It is a legitimate situation.
How can I write my application so if a null value appears in a field, I always get the results?
I would like to see all of my folders without worrying if the fields have a real value or display a NULL value. Showing records that have a NULL value, the user can see what information is missing and later come back and enter.
Can someone help me?
Thank you.
Hello
The CREATE TABLE instructions work when you run? I get a lot of errors, lack of parentheses and commas. Also, many to INSERT it IN the ORGANIZATION of statements fail because the parent records are missing. To get things to work, I ignored all the constraints.
To change your original query so that all ranks of orgainaztion appears, if it has a corresponding line in any other tables, maike all the join conditions outer joins. For example:
where a.state_key = b.state_key (+)
and a.classification_key = c.classification_key (+)
and a.speciality_key = d.speciality_key (+)
and a.parent_org_key = e.org_key (+)
If a match is not in any of the tables b, c, d or e, all columns of the table without a match will be displayed with a NULL value.
Tags: Database
Similar Questions
-
Hello
Use: Oracle 11.2.0.1 on Windows XP
create table tab40 (bno varchar2 (1), pno varchar2 (7), the number (3) weight, lineno number (3));
insert into tab40 values ('1', '1115027', 36, 1);
insert into tab40 values ('2', the '1115100', 25, 2);
insert into tab40 values ('3', the '1115200', 50, 3);
insert into tab40 values ('1', the '1112510', 60, 4);
insert into tab40 values ('+', '1112610', 6, 5)
insert into tab40 values ('+', '1112710', 4, 6);
insert into tab40 values ('+', '1110710', 1, 7)
insert into tab40 values ('2', '1117014', 79, 8);
insert into tab40 values ('+', '1117000', 9, 9)
insert into tab40 values ('+', '1117001', 2, 10);
insert into tab40 values ('1', '1317001', 200, 11);
insert into tab40 values ('1', '1697001', 20, 12);
insert into tab40 values ('2', '2997001', 40, 13);
insert into tab40 values ('1', '2996001', 44, 14);
create table tab41 (bno varchar2 (1), pno varchar2 (7), the number (3) weight, lineno number (3));SQL> select * from tab40; B PNO WEIGHT LINENO - ------- ---------- ---------- 1 1115027 36 1 2 1115100 25 2 3 1115200 50 3 1 1112510 60 4 + 1112610 6 5 + 1112710 4 6 + 1110710 1 7 2 1117014 79 8 + 1117000 9 9 + 1117001 2 10 1 1317001 200 11 B PNO WEIGHT LINENO - ------- ---------- ---------- 1 1697001 20 12 2 2997001 40 13 1 2996001 44 14 14 rows selected. SQL>
insert into tab41 values ('1', '1115027', 36, 1);
insert into tab41 values ('2', the '1115100', 25, 2);
insert into tab41 values ('3', the '1115200', 50, 3);
insert into tab41 values ('1', the '1112510', 60, 4);
insert into tab41 values ('+', '1112610', 6, 5)
insert into tab41 values ('+', '1110710', 1, 6);
insert into tab41 values ('2', '1117014', 79, 7);
insert into tab41 values ('+', '1117000', 9, 8)
insert into tab41 values ('1', '1317001', 200, 9);
insert into tab41 values ('1', the '1697001', 20, 10);
insert into tab41 values ('2', '2997001', 40, 11);
insert into tab41 values ('1', '2996001', 44, 12);
insert into tab41 values ('+', '1112710', 4, 13);
insert into tab41 values ('+', '3332710', 8, 14)
Power required:SQL> select * from tab41; B PNO WEIGHT LINENO - ------- ---------- ---------- 1 1115027 36 1 2 1115100 25 2 3 1115200 50 3 1 1112510 60 4 + 1112610 6 5 + 1110710 1 6 2 1117014 79 7 + 1117000 9 8 1 1317001 200 9 1 1697001 20 10 2 2997001 40 11 B PNO WEIGHT LINENO - ------- ---------- ---------- 1 2996001 44 12 + 1112710 4 13 + 3332710 8 14 14 rows selected. SQL>
Logic: The plus sign in Bno says that no. Packet was sent to the customer with the last packet (order by lineno) either signed number that is for example in tab40 there is line no. 6 whose main package number is 1112510 (from either signed lines previous PNO). So come to the power required; which shows that these signed (whose master PNO is that) packages are not according to the tab40 vs tab41 something as complete an outer join between tab40 and tab41. Example:A B C D E F G H 1 1112510 60 4 1 2996001 44 12 + 1112710 4 6 + 1112710 4 13 2 1117014 79 8 + 1117001 2 10 1 2996001 44 12 + 3332710 8 14
+ 1112710 is associated with 1112510 in tab40, in tab41, it is associated with 2996001; So that's the difference.
+ 1117001 is associated with 1117014 in tab40, while it is not in tab41.
Vice versa;
+ 3332710 is associated with 2996001 in tab41, while it is not in tab40.
I mean, if only more marked parcel number of PNO line mater is not equal to master PNO (previous no more BNO) in both the table, it should be part of the select query please. Usually this happens when our packing service gets a late delivery of prior master package, so that they send just the small package with immediately available (either) a package.
Kindly let me know; I am clear in my request, or I should elaborate more. This can be more complex, because may be bad/data from the design table, but I need the output of the select query please help me.
Full Outer Join help please
Wrongly, I posted the question here, I should post here. Keep will be remembered in the future.
Kindly help me.
Thank you and best regards.
Published by: user12050217 on January 3, 2012 08:38Hello
user12050217 wrote:
HelloUse: Oracle 11.2.0.1 on Windows XP
create table tab40 (bno varchar2 (1), pno varchar2 (7), the number (3) weight, lineno number (3));
insert into tab40 values ('1', '1115027', 36, 1); ...Thanks for posting the version and CREATE TABLE and INSERT statements. It is very useful.
Power required:
A B C D E F G H 1 1112510 60 4 1 2996001 44 12 + 1112710 4 6 + 1112710 4 13 2 1117014 79 8 + 1117001 2 10 1 2996001 44 12 + 3332710 8 14
Those are the best names of columns for this work? Even if your users really insist on having the names, do not forget that people who want to help are also familiar with your application, so it would be useful that you used for the column names more descriptive as bno_40 (meaning tab40 bno), pno_40,..., lineno_41 during the validation of the problem. You can change the column alias back to a, b,..., h after the problem is resolved, if necessary.
Logic: The plus sign in Bno says that no. Packet was sent to the customer with the last packet (order by lineno) either signed number that is for example in tab40 there is line no. 6 whose main package number is 1112510 (from either signed lines previous PNO). So come to the power required; which shows that these signed (whose master PNO is that) packages are not according to the tab40 vs tab41 something as complete an outer join between tab40 and tab41. Example:
+ 1112710 is associated with 1112510 in tab40, in tab41, it is associated with 2996001; So that's the difference.
+ 1117001 is associated with 1117014 in tab40, while it is not in tab41.
Vice versa;
+ 3332710 is associated with 2996001 in tab41, while it is not in tab40.I mean, if only more marked parcel number of PNO line mater is not equal to master PNO (previous no more BNO) in both the table, it should be part of the select query please. Usually this happens when our packing service gets a late delivery of prior master package, so that they send just the small package with immediately available (either) a package.
Kindly let me know; I am clear in my request, or I should elaborate more. This can be more complex, because may be bad/data from the design table, but I need the output of the select query please help me.
Yes, it seems unnecessarily complicated, so you should be especially clear in your description.
It is always useful to describe what each line of output. In this issue, it seems that different production lines represent different things:
(1) some lines represent an incompatibility between -marked lines in tab40 and tab41 (i.e. the lines or table where bno ='' and when the ofn even in the table has a different master_pno, or it the pno even does not exist in the other table). We will call these lines 'follower '.
(2) the other lines represent lines masters of the lines do not match (1)To get the master for each line + marked row in a table, use the analytical LAST_VALUE function. That's what the two first of subqueries, got_master_pno_40 and got_master_pno_41 below.
To get the lines are not (the disciples, or the output type (1)), do an outer join complete, as you said. It's that the subquery follows, mismatched_followers.
To get the final result, a Union of the followers with their respective masters. It's that the main request below.WITH got_master_pno_40 AS ( SELECT bno, pno, weight, lineno , LAST_VALUE ( CASE WHEN bno != '+' THEN pno END IGNORE NULLS ) OVER ( ORDER BY lineno) AS master_pno FROM tab40 ) , got_master_pno_41 AS ( SELECT bno, pno, weight, lineno , LAST_VALUE ( CASE WHEN bno != '+' THEN pno END IGNORE NULLS ) OVER (ORDER BY lineno) AS master_pno FROM tab41 ) , mismatched_followers AS ( SELECT NVL (m40.pno, m41.pno) AS pno , m40.bno AS bno_40 , m40.pno AS pno_40 , m40.weight AS weight_40 , m40.lineno AS lineno_40 , m40.master_pno AS master_pno_40 , m41.bno AS bno_41 , m41.pno AS pno_41 , m41.weight AS weight_41 , m41.lineno AS lineno_41 , m41.master_pno AS master_pno_41 FROM got_master_pno_40 m40 FULL OUTER JOIN got_master_pno_41 m41 ON m40.pno = m41.pno WHERE NVL (m40.master_pno, '?') != NVL (m41.master_pno, '?') AND '+' IN (m40.bno, m41.bno) ) SELECT bno_40, pno_40, weight_40, lineno_40 , bno_41, pno_41, weight_41, lineno_41 , pno , 'F' AS master_or_follower FROM mismatched_followers -- UNION ALL -- SELECT t40.bno, t40.pno, t40.weight, t40.lineno , t41.bno, t41.pno, t41.weight, t41.lineno , f.pno , 'M' AS master_or_follower FROM mismatched_followers f LEFT OUTER JOIN tab40 t40 ON f.master_pno_40 = t40.pno LEFT OUTER JOIN tab41 t41 ON f.master_pno_41 = t41.pno -- ORDER BY pno , master_or_follower DESC ;
This assumes that ofn is unique to each table and that ofn! = ' ?'.
Output:
B PNO_40 WEIGHT_40 LINENO_40 B PNO_41 WEIGHT_41 LINENO_41 PNO M - ------- ---------- ---------- - ------- ---------- ---------- ------- - 1 1112510 60 4 1 2996001 44 12 1112710 M + 1112710 4 6 + 1112710 4 13 1112710 F 2 1117014 79 8 1117001 M + 1117001 2 10 1117001 F 1 2996001 44 12 3332710 M + 3332710 8 14 3332710 F
You did not ask the last two columns, but you need to sort. If you don't want to see them, then you can make the UNION in a subquery, then, in the main query, use "ORDER BY pno, master_or_follower", as above, but do not include the columns in the SELECT clause. Your front end may have an easier way to hide these columns, so that they are still as a result set (and therefore, can be used in the ORDER byclause of a UNION) but are not displayed. In SQL * Plus, for example:
COLUMN pno NOPRINT COLUMN master_or_follower NOPRINT
-
Need help for query joining views
Hi all
I need to run a query that joins a number of tables with multiple instances of the same view. For example, a view named MY_VALUES, is an array of key / value resembles the following:
Application_ID Attirbute_Description value
1 1 000 basic benefit
1 multiplier 1 0.5
1 Multiplier_2 1.2
1 total profit 1,115
2 1 025 basic benefit
2 multiplier 1 0,268
2 Multiplier_2 1.3
Total profit 2 1.4500
Say I want a query that selects basic earnings and a multiplier 1 for Application ID 1, as well as, other information from a table called APPLICATION that has a relationship to display pk - fk. I would do the following:
Select the application. Name, mv1.value, mv2.value
from dbo. App APPLICATION, dbo. MY_VALUES mv1, dbo. Mv2 MY_VALUES
where app.id = mv1.application_id and mv1.attribute_description = "Basic benefits"
and app.id = mv2.application and mv2.attribute_description = id 'multiplier 1'.
and app.id = 1
My actual query is similar to this. However, I have many other attributes of the notice I need to select, which requires that the view be attached to several times. After 16 these joins, the request is fine. After 17, however, the query runs always, never return a result within 45 minutes I have let it run.
Is there a better way to write such a request or an indicator of optimization that would be useful here?
Any help is greatly appreciated.
....
For this type of application, it is often best to extract multiple lines in a single request on the view and their pivot in the output.
For example
Select the application. Name
, max (CASE when mv.attribute_description = "Core service" then mv.value any other purpose) BaseBenefit
, max (CASE when mv.attribute_description = 'Multiplier 1' then mv.value any other purpose) Multiplier1
, max (CASE when mv.attribute_description = 'Multiplier 2' then mv.value any other purpose) Multiplier2
, max (CASE when mv.attribute_description = 'Multiplier 3' then mv.value end to another null) Multiplier3
from dbo. App APPLICATION, dbo. MY_VALUES mv
where app.id = mv1.application_id
and mv.attribute_description IN ('benefits of home', 'multiplier 'multiplier 'multiplier 3' 2' 1')
and app.id = 1
This runs often much better than several joins to the table for the view or the value of the attribute.
-
EXAMPLES OF DATA-
CREATE TABLE ITEM_GRP (ITEM_GRP_ID int NOT NULL, ITEM_ID int, GRP_NAME VARCHAR2 (100),)
PRIMARY KEY (ITEM_GRP_ID),
FOREIGN KEY (ITEM_ID) REFERS TO ITEM_1 (ITEM_ID));
CREATE TABLE TABLE_1 (int A_ITEM_ID, int B_ITEM_ID, VARCHAR2 (100) DESCRIPTION,)
PRIMARY KEY (A_ITEM_ID),
FOREIGN KEY (A_ITEM_ID) REFERENCES ITEM_1 (ITEM_ID),
FOREIGN KEY (B_ITEM_ID) REFERS TO ITEM_1 (ITEM_ID));
INSERT INTO ITEM_1 (ITEM_ID, DESCRIPTION) VALUES (1, 'ITEM1');
INSERT INTO ITEM_1 (ITEM_ID, DESCRIPTION) VALUES (2, 'ITEM2');
INSERT INTO ITEM_1 (ITEM_ID, DESCRIPTION) VALUES (3, 'ITEM3');
INSERT INTO ITEM_1 (ITEM_ID, DESCRIPTION) VALUES (4, "ITEM4");
INSERT INTO ITEM_1 (ITEM_ID, DESCRIPTION) VALUES (5, "ITEM5");
COMMIT;
INSERT INTO ITEM_GRP (ITEM_GRP_ID, ITEM_ID, GRP_NAME) VALUES (10, 1, 'ITEM1 GRP');
INSERT INTO ITEM_GRP (ITEM_GRP_ID, ITEM_ID, GRP_NAME) VALUES (11, 2, 'ITEM2 GRP');
INSERT INTO ITEM_GRP (ITEM_GRP_ID, ITEM_ID, GRP_NAME) VALUES (12, 2, 'ITEM2 GRP');
COMMIT;
INSERT INTO TABLE_1 (A_ITEM_ID, B_ITEM_ID, DESCRIPTION) VALUES (2, 1, ' Description point 2');
INSERT INTO TABLE_1 (A_ITEM_ID, B_ITEM_ID, DESCRIPTION) VALUES (3, 1, ' Description point 3');
INSERT INTO TABLE_1 (A_ITEM_ID, B_ITEM_ID, DESCRIPTION) VALUES (4, 1, ' Description point 4');
INSERT INTO TABLE_1 (A_ITEM_ID, B_ITEM_ID, DESCRIPTION) VALUES (5, 1, ' Description point 5');
COMMIT;
The scenario is, there are three tables - ITEM_1, ITEM_GRP, TABLE_1. The order of the DAY is the main table for all items.
SELECT * FROM TABLE_1 where B_ITEM_ID = 1;
DESCRIPTION OF B_ITEM_ID A_ITEM_ID
---------- ---------- ----------------------------------------------------------------------------------------------------
2 1 description point 2
3 1 description of the point 3
4 1 description of the point 4
5 1 description of point 5
Now the query I try to write should return 1 (or count (*) = 1) if
-TABLE_1.A_ITEM_ID = ITEM_GRP. ITEM_ID and have > 1
In this case, the assigned action returned 4 rows and even if there is a match with A_ITEM_ID = 2 which exist on ITEM_GRP twice, he was no match for other A_ITEM_ID in the ITEM_GRP table. If the query should return 0 or null.
Here is what I started with.
SELECT * FROM ITEM_GRP IG, M TABLE_1
WHERE IG. ITEM_ID (+) = M.A_ITEM_ID
AND M.B_ITEM_ID = 1
- AND IG. ITEM_ID IS NULL
- AND EXISTS (SELECT 1 FROM ITEM_GRP WHERE ITEM_ID = IG. ITEM_ID HAVING COUNT (*) > 1)
(Working on it)
Oracle Version-
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - production
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
AMT for 32-bit Windows: release 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
Hello
Here's a way to do it:
SELECT THE CHECK BOX
WHEN MIN (COUNT (ig.item_id)) > = 2
THEN 1
0 OTHERWISE
END AS all_2
FROM table_1 t1
LEFT OUTER JOIN item_grp ON ig.item_id = t1.a_item_id GI
GROUP BY t1.a_item_id
;
The outer join will ensure that all values of table_1.a_item_id are selected.
COUNT (item_grp.item_id) will indicate the number of lines corresponding to each value of a_item_id. Since it is an outer join, this number may be 0.
If (and only if) the a_item_ids has 2 or more matches, then these counts of 2 minutes or more.
-
Left Outer Join help...
Hello world
I'm still trying to learn the SQL, and I can not specifically with the left outer join. I normally join tables using equijoin, but I don't get the right data set returns, and designed with the help of a left or right outer join would solve the problem...
Here is my SQL that works properly with 1 left outer join. I build the slow query in the SQL following, you will see where I see the error. I don't expect you to understand the data and the columns, I'm trying to join, I think the problems I encounter are related to syntax, and I hope that you can find where are my syntax errors.
Select
s.Name as "Pseudonym,"
SV.view_name as "name of the view.
s_view. Name
Of
s s_screen,
s_screen_view sv
outer join left s_view
WE (sv.view_name = s_view.name)
where
SV.screen_id = ' 1-866 A - 1X3LU' and
s.ROW_ID = sv.screen_id and
s.repository_id = ' 866 A-1-1"and
s_view.repository_id = ' A-1-1 866 ";
Here is the SQL code where I encounter the following error...
Error:
ORA-00904: "SV". "" View_name ": invalid identifier
00904, 00000 - '% s: invalid identifier '.
* Cause:
* Action:
Error on line: column 14: 7
Problematic SQL:
Select
s.Name as "Pseudonym,"
SV.view_name as "name of the view.
s_view. Name,
s_applet. Name as Applet
-b.SID like "name of the cmdlet.
Of
s s_screen,
s_screen_view sv,
wti s_view_wtmpl_it
outer join left s_view
WE (sv.view_name = s_view.name)
outer join left s_applet
WE (wti.name = s_applet.name)
where
SV.screen_id = ' 1-866 A - 1X3LU' and
s.ROW_ID = sv.screen_id and
s.repository_id = ' 866 A-1-1"and
s_view.repository_id = ' A-1-1 866 ";
Thanks in advance for your help.
Chris
> ORA-00904: "S_VIEW_WEB_TMPL." "" ROW_ID ": invalid identifier
I don't see this table in your FROM clause.
-
Looking for right outer join help
Hello
Can anyone help me re - write the query using right below outer join (join ansi) instead of using the (+) symbol.
Select cd.sku_id waitm,
Decode (cl.invn_lock_code, NULL, 'OH', cl.invn_lock_code) WLOCN,
Sum (CD.actl_qty) WQOH
cd, a.case_lock cl, a.case_hdr ch, a.item_master im a.case_dtl
where cd.case_nbr = ch.case_nbr
and cl.case_nbr (+) = ch.case_nbr
and cd.sku_id = im.sku_id
and ch.stat_code < = '64'
Cd.sku_id group,
IM.sku_brcd,
Decode (cl.invn_lock_code, NULL, 'OH', cl.invn_lock_code)
order of cd.sku_id
Kind regards
Gannu
Maybe something like this:
of a.case_dtl cd
Join a.case_hdr ch on (cd.case_nbr = ch.case_nbr)
Join a.item_master im on (cd.sku_id = im.sku_id)
join a.case_lock cl left (cl.case_nbr = ch.case_nbr)
where ch.stat_code<=>=>
I know you asked for right outer join, but I tend to write like that. You can certainly change if you wish.
-
I'm trying to follow some RTF data to a language specification... I have installation test record this follow-up. I am able to find in spec summary test specifications and the specific information I'm looking for in the HTMLData table. What I can't find is the join track to return to the wording PKID of HTMLData. Can I get help with the PL - SQL Query?
The data schema says EP HTMLdata doesn't have all the relationships, but there is a PKID PLM4P referenced in the FKPARENT column, which starts by "2220" which takes me to the richtextsupportingdocuments table. Schema docs say that this table has no relationship either. However richtextsupportingdocuments table has a FKspec column that refers to an object beginning with '5818' and that takes me to gsmFormulationSteps. gsmFormulationSteps titles back to FormulationInput that does not have a formula PKID/FKID of '5618' but rather '1004' for materials, etc... so that's where I'm stuck... I am moving back to the spec formualution ID and not been followed correctly of the 5816 PKID.
Could someone help me get back on track with the data track? Thank you.Good question - the configuration of database for formulating specifications is a little different from other specifications (since you can have snapshots of a language specification). So it is not that simple. You need to join DWBSpecificationRoots and fkWorkingVersionParent of dwbSpecifications column, as below
SELECT ss. SpecID, ss. SpecNumber, as follows. StepName, html. HtmlContent
OF specsummary ss
INNER JOIN DWBSpecificationRoots t2 ON ss.specid = t2.fkOwner
INNER JOIN t3 ON t2.pkid = t3.fkWorkingVersionParent dwbSpecifications
Steps to INNER JOIN gsmFormulationSteps t3.pkid = steps.fkParent
inner join richTextSupportingDoc on richText.fkSpec = steps.pkid richText
HtmlData html on html.fkParent = richText.pkid inner join
where
SS. SpecID = -
Paging query needed help for large table - force a different index
I use a slight modification of the pagination to be completed request to ask Tom: [http://www.oracle.com/technology/oramag/oracle/07-jan/o17asktom.html]
Mine looks like this to extract the first 100 lines of everyone whose last name Smith, ordered by join date:
The difference between this and ask Tom is my innermost query returns just the ROWID. Then, in the outermost query we associate him returned to the members table ROWID, after that we have cut the ROWID down to only the 100 piece we want. This makes it MUCH more (verifiable) fast on our large tables, because it is able to use the index on the innermost query (well... to read more).SELECT members.* FROM members, ( SELECT RID, rownum rnum FROM ( SELECT rowid as RID FROM members WHERE last_name = 'Smith' ORDER BY joindate ) WHERE rownum <= 100 ) WHERE rnum >= 1 and RID = members.rowid
The problem I have is this:
It will use the index for the column predicate (last_name) rather than the unique index that I defined for the column joindate (joindate, sequence). (Verifiable with explain plan). It is much slower this way on a large table. So I can reference using one of the following methods:SELECT rowid as RID FROM members WHERE last_name = 'Smith' ORDER BY joindate
SELECT /*+ index(members, joindate_idx) */ rowid as RID FROM members WHERE last_name = 'Smith' ORDER BY joindate
Whatever it is, it now uses the index of the column ORDER BY (joindate_idx), so now it's much faster there not to sort (remember, VERY large table, millions of records). If it sounds good. But now, on my outermost query, I join the rowid with the significant data in the members table columns, as commented below:SELECT /*+ first_rows(100) */ rowid as RID FROM members WHERE last_name = 'Smith' ORDER BY joindate
As soon as I did this join, this goes back to the use of the index of predicate (last_name) and perform the sort once he finds all the corresponding values (which can be a lot in this table, there is a cardinality high on some columns).SELECT members.* -- Select all data from members table FROM members, -- members table added to FROM clause ( SELECT RID, rownum rnum FROM ( SELECT /*+ index(members, joindate_idx) */ rowid as RID -- Hint is ignored now that I am joining in the outer query FROM members WHERE last_name = 'Smith' ORDER BY joindate ) WHERE rownum <= 100 ) WHERE rnum >= 1 and RID = members.rowid -- Merge the members table on the rowid we pulled from the inner queries
My question therefore, in the query full above, is it possible that I can get to use the ORDER of indexing BY column to prevent having to sort? The join is what makes go back to using the predicate index, even with notes. Remove the join and just return the ROWID for these 100 records and it flies, even over 10 millions of documents.
It would be great if there was some generic hint that could accomplish this, such as if we change the table/column/index, do not change the indicator (indicator FIRST_ROWS is a good example of this, while the INDEX indicator is the opposite), but any help would be appreciated. I can provide explain plans for the foregoing, if necessary.
Thank you!
-
As you can see on the queries below I'm a complete newbie to SQL. But hoping someone can help. I have 3 request each return a number grouped under the name of "deposit". What I want to achieve is to have a query that returns all 3 results in the 1 table.
Total count
SELECT
INS_STAGE.TA_AEVEN. LEV3 DID "deposit."
COUNT (DISTINCT INS_STAGE.TA_AEVEN. NUM_1) AS 'Total number of jobs '.
Of
INS_STAGE.TA_AEVEN
where
(
INS_STAGE.TA_AEVEN. AD_TS BETWEEN % ' 2014060001' AND ' 2014069999% '
AND INS_STAGE.TA_AEVEN. TYCOD NOT IN ('NOW', 'PLAN-OUT', 'POSSUM',
"CABLE_PI_INQUIRY")
)
GROUP BY
INS_STAGE.TA_AEVEN. LEV3;
Second count
SELECT
INS_STAGE.TA_AEVEN. As "Deposit," Lev3
COUNT (DISTINCT INS_STAGE.TA_AEVEN. NUM_1) as "closed by MT.
Of
INS_STAGE.TA_AEVEN
WHERE
INS_STAGE.TA_AEVEN. AD_TS BETWEEN 2014060001 ' %' AND ' 2014069999% '
AND INS_STAGE.TA_AEVEN. CURENT 'T ='
AND INS_STAGE.TA_AEVEN. XTERM as ' $%.
AND INS_STAGE.TA_AEVEN. TYCOD NOT IN ('NOW', 'PLAN-OUT', 'POSSUM',
"CABLE_PI_INQUIRY")
INS_STAGE.TA_AEVEN.lev3 group
Third Chief
SELECT
INS_STAGE.TA_AEVEN. LEV3 DID "deposit."
COUNT (DISTINCT INS_STAGE.TA_AEVEN. NUM_1) AS "not closed by MT.
Of
INS_STAGE.TA_AEVEN
WHERE
INS_STAGE.TA_AEVEN. AD_TS BETWEEN 2014060001 ' %' AND ' 2014069999% '
AND INS_STAGE.TA_AEVEN. CURENT 'T ='
AND INS_STAGE.TA_AEVEN. XTERM NOT LIKE ' $%.
AND INS_STAGE.TA_AEVEN. TYCOD NOT IN ('NOW', 'PLAN-OUT', 'POSSUM',
"CABLE_PI_INQUIRY")
GROUP BY
INS_STAGE.TA_AEVEN. LEV3
Hoping someone can help
See you soon
Buddy
Hey Buddy,
This should be easily possible as long as you account on the same table.
First find the filter which is the least restrictive of all three queries criteria.
It seems that your first query filter is usable. The filter itself seems that can be tuned a bit, but this is a different task and requires further analysis.
The grouping is also the same, which can also be used. In other cases you must break the grouping at the lowest level possible, and then follow the steps in the extra $.
The last and most important step is to change what is counted.
Additional filters should now be made as an expression inside the COUNT function. Remember that NULL values will not be taken into account, that's what we use.
Your final selection will probably look like this. I can't test it, since you provided no examples of scripts. But I'm sure you can eliminate syntax errors I made.
Select t.lev3
count (*) total_count
count (distinct t.num1) as total_jobs
, count (distinct case when t.current 't =' and t.xterm like ' $% ' then t.num1 end) as closed_by_MT
, count (distinct case when t.current 't =' and t.xterm not like ' $% ' then t.num1 end) as not_closed_by_MT
of ins_stage.ta_even t
where t.ad_ts between... and...
and t.tycod not in (...)
T.lev3 group;
BTW. The SEPARATE inside the County very often is not necessary. Compare the total_count with your total column column: jobs and see if there really is a difference. SEPARATE requires an additional sorting in general deteriorating performance.
-
Need help with the query. Help, please
Hey everyone, need your help. Thank you in advance. In my view, there is function Pivot. Just do not know how to use this function. I have the query that works. The result is:
11-111-1111 Vlad 16505 01/04/2013 5 August 13 dental plan pre-tax amount 29,65
11-111-1111 Vlad 16505 01/04/2013 dental pre-tax 5 August 13 Plan level EE + SP
11-111-1111 16505 Vlad 01/04/2013 5 August 13 pre-tax Option TOP dental plan
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax dental care plan pay the value
11-111-1111 16505 Vlad 01/04/2013 dental pre-tax 5 August 13 Plan period Type
11-111-1111 Vlad 16505 01/04/2013 amount pre-tax medical Plan of 5 August 13 149
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Medical Plan level EE + SP
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Plan medical Option MED
11-111-1111 Vlad 16505 01/04/2013 5 August 13 plan pre-tax pay value
11-111-1111 16505 Vlad 01/04/2013 5 August 13 pre-tax Medical Plan period Type
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Plan PPO medical Plan
11-111-1111 Vlad 16505 01/04/2013 5 August 13 Vision Plan amount 5.94 pre-tax
But I need the result to be
Amount of SSN ID name item level Option PayValue period Type
11-111-1111 Vlad 16505 01/04/2013 null null high of 5 August 13 pre-tax Dental Plan 29,65 EE + SP
11-111-1111 Vlad 16505 01/04/2013 null null MED 5 August 13 149 medical plan pre-tax EE + SP
11-111-1111 Vlad 16505 01/04/2013 Vision Plan before taxes of 5 August 13
Select distinct
' 11-111-1111 "as ssn,
WOMEN'S WEAR. Employee_number,
"Vlad" as EMPLOYEE_FULL_NAME,
TO_CHAR (papf.start_date, "MM/DD/YYYY") as Date_Of_Hire
a.effective_start_date,
PETF.element_name,
pivf. Name,
peevf.screen_entry_valueOf
PER_all_PEOPLE_F women's wear
per_assignments_f A
pay_element_types_f petf
pay_element_links_f pelf
PAY_ELEMENT_ENTRIES_F penf
PAY_ELEMENT_ENTRY_VALUES_F peevf
pay_input_values_f pivf
WHERE
PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")
and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID
and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)
and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)
and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)
and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID
and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID
AND papf.employee_number IS NOT NULL
AND A.assignment_type = 'E '.
AND A.person_id = papf.person_id
and papf.effective_end_date > sysdate
and a.effective_end_date > sysdate
and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)
and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)
and a.assignment_id = 42643
and a.assignment_status_type_id = '1'
order of petf.element_name;Change with your query
SELECT * FROM (select distinct)
' 11-111-1111 "as ssn,
WOMEN'S WEAR. Employee_number,
"Vlad" as employee_full_name,
TO_CHAR (papf.start_date, "MM/DD/YYYY") as date_of_hire
a.effective_start_date,
PETF.element_name,
pivf. Name,
peevf.screen_entry_value
Of
PER_all_PEOPLE_F women's wear
per_assignments_f A
pay_element_types_f petf
pay_element_links_f pelf
PAY_ELEMENT_ENTRIES_F penf
PAY_ELEMENT_ENTRY_VALUES_F peevf
pay_input_values_f pivf
WHERE
PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")
and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID
and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)
and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)
and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)
and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID
and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID
AND papf.employee_number IS NOT NULL
AND A.assignment_type = 'E '.
AND A.person_id = papf.person_id
and papf.effective_end_date > sysdate
and a.effective_end_date > sysdate
and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)
and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)
and a.assignment_id = 42643
and a.assignment_status_type_id = '1')
PIVOT (MAX (screen_entry_value) FOR (name) TO ("Amount" AS 'Amount', 'level' AS 'level', 'Option High' AS 'High Option', 'Pay the value' AS 'Value to pay', 'Period of Type' AS 'Type period'))
order by element_name;
(GOLD)
SELECT ssn,
Employee_number,
employee_full_name,
date_of_hire,
effective_start_date,
element_name,
Max (decode (Name, 'Amount', screen_entry_value)) 'amount. "
Max (decode (Name, 'Level', screen_entry_value)) 'level ',.
MAX (DECODE (name, "High Option", screen_entry_value)) "High Option",
MAX (DECODE (name, 'Value of pay', screen_entry_value)) 'value of pay. "
MAX (DECODE (name, 'Period Type', screen_entry_value)) 'period of Type '.
FROM (select distinct)
' 11-111-1111 "as ssn,
WOMEN'S WEAR. Employee_number,
"Vlad" as employee_full_name,
TO_CHAR (papf.start_date, "MM/DD/YYYY") as date_of_hire
a.effective_start_date,
PETF.element_name,
pivf. Name,
peevf.screen_entry_value
Of
PER_all_PEOPLE_F women's wear
per_assignments_f A
pay_element_types_f petf
pay_element_links_f pelf
PAY_ELEMENT_ENTRIES_F penf
PAY_ELEMENT_ENTRY_VALUES_F peevf
pay_input_values_f pivf
WHERE
PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")
and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID
and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)
and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)
and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)
and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID
and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID
AND papf.employee_number IS NOT NULL
AND A.assignment_type = 'E '.
AND A.person_id = papf.person_id
and papf.effective_end_date > sysdate
and a.effective_end_date > sysdate
and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)
and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)
and a.assignment_id = 42643
and a.assignment_status_type_id = '1')
GROUP BY ssn, employee_number, employee_full_name, date_of_hire, effective_start_date, NOM_ELEMENT
order by element_name;
-
Rolling totals query ~ please help
I was stuck on a query for the last week. It would be amazing if someone could help me with this. nothing that I've done has worked so far. I have something after a few days ago and helped a little, but it works just the way I needed. This has been the more difficult application that I've ever worked on.
IM using oracle 11g.
What I'm trying to do, is get a bunch of aggregated values based on the dates of several projects running.
I made an export of the ddl for the current table and reviews, I wanted to not just after a sample of the data that it contains many records, and I think it's maybe easier to have the full extract.
Here is the link to the file http://santoro.us/pl_insert.sql
In the table I have IPS (which corresponds to a project id), create the full date, logged_date, status, date, due date (these are all the important columns)
What I'm trying to get a final result 1 table or 1 discovered that all totals accumulated per month for each project.
Here are the totals im looking to get
Open
Closed
open late
closed end
open closed punctually at the time
The thing is that I have to start the month by the project since the first month, that the project has a record. So if a first registration begins January 1, 2010 the project when the first album with totals should be 01/2010 and 02/2010, 03/2010, etc... until the current month. This must be done by project.
Thus, for example say there are only 2 projects (there are many others).
IPS 123
first record begins 01/01/2010
IPS 456
first record begins 01/01/2011
each have files that open and close on different dates in different months. Some might be late (complete date > expiration date), some closed on time (complete date < = due date).
end result would be a table that has:
Date | IPS | Open | closed | open late. closed end | opened on time. closed time |
01/2010 | 123 | 1. 0 | etc...
months would go completely to the current month
then start proj 2
01/2011 | 456. 1. 0 | etc...
months would go completely to the current month
each record would be counted from the date of creation of the document. As the records get late or closed (based on the due date, full date), then must be added/removed from the running of the data totals.
All this must be in 1 table or view. I know that some people might say it is the wrong way to do it, but that's how it should be done for the project im working on. IM generate xml at the end, so the developer needs him like that.
Yet once thanks for any help and please let me know if you need other information.
Thank you.
Earlier, I understand your condition. Try the below, it gives a correct output for FPS 100824. Check the other ips as well and let me know in case of any problems
WITH qry1 AS (SELECT ips,
ADD_MONTHS (trunc(min_date,'MM'),(Level-1)) act_date
FROM (SELECT MIN (create_date), min_date,
IPS
OF therm_punchlist
GROUP BY ips)
CONNECT BY LEVEL<=>=>
AND PRIOR ips = ips
AND PRIOR DBMS_RANDOM. VALUE IS NOT NULL)
SELECT TO_CHAR (act_date, 'MM-YYYY ',' NLS_DATE_LANGUAGE = ENGLISH') dt,
IPS,
Sum (open_cnt) OVER (PARTITION BY ips ORDER BY act_date)
-SUM (closed_cnt) OVER (PARTITION BY ips ORDER BY act_date) open_cnt,
Sum (closed_cnt) OVER (PARTITION BY ips ORDER BY act_date) closed_cnt,
CASE WHEN SUM (open_latecnt) OVER (PARTITION BY act_date ORDER BY act_date, ips) > 0 THEN
Sum (open_latecnt) OVER (PARTITION BY act_date ORDER BY act_date, ips)
-SUM (closed_cnt) OVER (PARTITION BY ips ORDER BY act_date)
ELSE SUM (open_latecnt) OVER (PARTITION BY act_date ORDER BY act_date, ips)
END as open_latecnt,
Sum (close_latecnt) OVER (PARTITION BY ips ORDER BY act_date) close_latecnt,
CASE WHEN SUM (open_latecnt) OVER (PARTITION BY act_date ORDER BY act_date, ips) > 0 THEN
Sum (open_ontimecnt) OVER (PARTITION BY ips ORDER BY act_date)
-Sum (open_latecnt) OVER (PARTITION BY act_date ORDER BY act_date, ips)
WHEN SUM (open_ontimecnt) OVER (PARTITION BY ips ORDER BY act_date) > 0 THEN
Sum (open_ontimecnt) OVER (PARTITION BY ips ORDER BY act_date)
-Sum (closed_cnt) OVER (PARTITION BY ips ORDER BY act_date)
ELSE SUM (open_ontimecnt) OVER (PARTITION BY ips ORDER BY act_date)
END as open_ontimecnt,
Sum (closed_ontimecnt) OVER (PARTITION BY ips ORDER BY act_date) closed_ontimecnt
FROM (SELECT act_date,
IPS,
(SELECT COUNT (*)
OF therm_punchlist tp
WHERE tp.ips = q1.ips
AND TRUNC (tp.create_date, 'MM') = act_date) open_cnt;
(SELECT COUNT (*)
OF therm_punchlist tp
WHERE tp.ips = q1.ips
AND TRUNC (tp.complete_date, 'MM') = act_date) closed_cnt;
(SELECT COUNT (CASE WHEN act_date > TRUNC(due_date,'MM') THEN status END))
OF therm_punchlist tp
WHERE tp.ips = q1.ips) open_latecnt;
(SELECT COUNT (CASE WHEN complete_date > end_date THEN status END))
OF therm_punchlist tp
WHERE tp.ips = q1.ips
AND TRUNC (tp.complete_date, 'MM') = act_date) close_latecnt;
(SELECT COUNT (CASE WHEN act_date<= trunc(due_date,'mm')="" then="" status="">=>
OF therm_punchlist tp
WHERE tp.ips = q1.ips) open_ontimecnt;
(SELECT COUNT (CASE WHEN complete_date<= due_date="" then="" status="">=>
OF therm_punchlist tp
WHERE tp.ips = q1.ips
AND TRUNC (tp.complete_date, 'MM') = act_date) closed_ontimecnt
OF qry1 q1);
-
My requirement is to calculate a few things like total_cash_amount, TOTAL_CO_PATIENT_AMOUNT etc... For this we will use BILLING. PATIENTBILL, BILLING. PATIENTBILLDETAILS, BILLING. Operating tables. Here it is already developed procedure.
I'm askd to change the same for best performance. This is the part of it that is calculation of total_cash_amount. There will be two categories such as INPATIENT (IP) and OUTPATIENT (OP). Can anyone of you please help me to change the code in a manner better. And can you please suggest that global temporary tables are better or can we use the tables directly.
Thanks in advance...INSERT INTO GTT_PATIENTBILL SELECT * FROM BILLING.PATIENTBILL P WHERE P.LOCATIONID = IN_LOCATIONID AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND TRUNC(IN_FROMDATE); INSERT INTO GTT_PATIENTBILLDETAILS SELECT PB.* FROM BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB WHERE P.LOCATIONID = IN_LOCATIONID AND P.BILLNO = PB.BILLNO AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND TRUNC(IN_FROMDATE); INSERT INTO GTT_TRANSACTION SELECT * FROM BILLING.TRANSACTION T WHERE T.LOCATIONID = IN_LOCATIONID AND TRUNC(T.CREATEDDATE) BETWEEN TRUNC(IN_YEARDATE) AND TRUNC(IN_FROMDATE); DELETE FROM TEMP_1; INSERT INTO TEMP_1 SELECT (CASE WHEN PARTICULARS IS NULL AND FLG = 1 THEN 'TOTAL COLLECTIONS' WHEN PARTICULARS IS NULL AND FLG = 2 THEN 'TOTAL DEDUCTIONS' ELSE PARTICULARS END) PARTICULARS, SUM(A) "FOR THE DAY", SUM(B) "MONTH TO DATE", SUM(C) "YEAR TO DATE" FROM (SELECT PARTICULARS, SUM(TOTAL_CASH_AMOUNT_DAY) A, SUM(TOTAL_CASH_AMOUNT_MONTH) B, SUM(TOTAL_CASH_AMOUNT_YEAR) C, 1 FLG FROM (SELECT 'OP CASH' PARTICULARS, SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_DAY, 0 TOTAL_CASH_AMOUNT_MONTH, 0 TOTAL_CASH_AMOUNT_YEAR FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB WHERE P.LOCATIONID = IN_LOCATIONID --AND P.BILLNO = PB.BILLNO AND P.BILLINGTYPEID = 1 AND P.PATIENTSERVICE = 2 AND TRUNC(P.BILLDATE) =TRUNC(IN_FROMDATE) UNION ALL SELECT 'OP CASH' PARTICULARS, 0, SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_MONTH, 0 FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB WHERE P.LOCATIONID = IN_LOCATIONID --AND P.BILLNO = PB.BILLNO AND P.BILLINGTYPEID = 1 AND P.PATIENTSERVICE = 2 AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_MONTHDATE) AND TRUNC(IN_FROMDATE) UNION ALL SELECT 'OP CASH' PARTICULARS, 0, 0, SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_YEAR FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB WHERE P.LOCATIONID = IN_LOCATIONID --AND P.BILLNO = PB.BILLNO AND P.BILLINGTYPEID = 1 AND P.PATIENTSERVICE = 2 AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND TRUNC(IN_FROMDATE) UNION ALL SELECT 'IP CASH' PARTICULARS, SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_DAY, 0 TOTAL_CASH_AMOUNT_MONTH, 0 TOTAL_CASH_AMOUNT_YEAR FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB WHERE P.LOCATIONID = IN_LOCATIONID --AND P.BILLNO = PB.BILLNO AND P.BILLINGTYPEID = 1 AND P.PATIENTSERVICE = 3 AND TRUNC(P.BILLDATE) = TRUNC(IN_FROMDATE) UNION ALL SELECT 'IP CASH' PARTICULARS, 0, SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_MONTH, 0 FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB WHERE P.LOCATIONID = IN_LOCATIONID --AND P.BILLNO = PB.BILLNO AND P.BILLINGTYPEID = 1 AND P.PATIENTSERVICE = 3 AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_MONTHDATE) AND TRUNC(IN_FROMDATE) UNION ALL SELECT 'IP CASH' PARTICULARS, 0, 0, SUM(P.TOTALSERVICEAMOUNT) TOTAL_CASH_AMOUNT_YEAR FROM GTT_PATIENTBILL P--BILLING.PATIENTBILL P, BILLING.PATIENTBILLDETAILS PB WHERE P.LOCATIONID = IN_LOCATIONID --AND P.BILLNO = PB.BILLNO AND P.BILLINGTYPEID = 1 AND P.PATIENTSERVICE = 3 AND TRUNC(P.BILLDATE) BETWEEN TRUNC(IN_YEARDATE) AND TRUNC(IN_FROMDATE)) T1 GROUP BY PARTICULARS
You can do this in the same query. As long as the underlying table is the same thing that you don't have to have multiple queries to the same table. You just improvise on your selection fields. Use a CASE statement instead of DECIDING that I used and include the other INDICATIONS which I didn't understand.
-
To only convert NOT EXISTS query JOIN plain
Version: 10.2.0.5
Developers have complained that the following query works very poorly and hooked most of the time.
select distinct cust_id from cust_track_dtl TD where not exists (select 1 from cust_master CM where TD.cust_id = CM.cust_id and CM.rgn_code = TD.rgn_code CM.rgn_code = 'AP' ) and TD.stat_code = 'C' and TD.proc_flag = 'Y' TD.rgn_code = 'AP' ; ----------------> Is this logically correct ?
Currently, we are only at the design phase. I'm a bit confused about theCUST_TRACK_DTL table in the outer query has 2 million records CUST_MASTER table in their inner query(sub query) has 30 million records
appearing in internal and external queries.rgn_code = 'AP'
I think that the motion seeks to recover all customers who are not the "AP" region, but the outer query WHERE clause has
Is there something wrong with this logic?TD.rgn_code = 'AP'
If it is logically correct, how can I convert this request with simple JOIN (ie. How can I get rid of NO thing EXISTS)?Your original. Rgn_code = "AP" is not necessary in the subquery but will change the results of the outer query.
select distinct cust_id from cust_track_dtl TD left outer join cust_master CM on (TD.cust_id = CM.cust_iid AND CM.rgn_code = TD.rgn_code) WHERE TD.stat_code = 'C' and TD.proc_flag = 'Y' and TD.rgn_code = 'AP' and CM.cust_id IS NULL;
or the other
select pb.pub_name from publishers pb LEFT OUTER JOIN titles tt ON ON (tt.pubslisher_id = pb.publisher_id and tt.subject = 'RELIGION') where tt.pubslisher_id IS NULL;
-
Can someone help me with this piece of the request that the request is to consume a lot of time. Maybe we can avoid selecting double. Can someone suggest an alternative?
SELECT sd.specification_sequence_no, sd.attribute_desc,
SD.specification_lov, sd.specification_code_table,
SD.specification_query, d.val
OF sa_spec_template_details sd,.
(SELECT l_chr_spec_desc01 dsc, l_chr_spec_val01 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc02 dsc, l_chr_spec_val02 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc03 dsc, l_chr_spec_val03 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc04 dsc, l_chr_spec_val04 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc05 dsc, l_chr_spec_val05 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc06 dsc, l_chr_spec_val06 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc07 dsc, l_chr_spec_val07 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc08 dsc, l_chr_spec_val08 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc09 dsc, l_chr_spec_val09 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc10 dsc, l_chr_spec_val10 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc11 dsc, l_chr_spec_val11 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc12 dsc, l_chr_spec_val12 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc13 dsc, l_chr_spec_val13 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc14 dsc, l_chr_spec_val14 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc15 dsc, l_chr_spec_val15 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc16 dsc, l_chr_spec_val16 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc17 dsc, l_chr_spec_val17 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc18 dsc, l_chr_spec_val18 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc19 dsc, l_chr_spec_val19 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc20 dsc, l_chr_spec_val20 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc21 dsc, l_chr_spec_val21 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc22 dsc, l_chr_spec_val22 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc23 dsc, l_chr_spec_val23 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc24 dsc, l_chr_spec_val24 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc25 dsc, l_chr_spec_val25 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc26 dsc, l_chr_spec_val26 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc27 dsc, l_chr_spec_val27 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc28 dsc, l_chr_spec_val28 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc29 dsc, l_chr_spec_val29 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc30 dsc, l_chr_spec_val30 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc31 dsc, l_chr_spec_val31 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc32 dsc, l_chr_spec_val32 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc33 dsc, l_chr_spec_val33 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc34 dsc, l_chr_spec_val34 val
OF THE DOUBLE
UNION
SELECT l_chr_spec_desc35 dsc, l_chr_spec_val35 val
D FROM DUAL)
WHERE sd.plant = p_plant
AND sd.specification_type = p_specification_type
AND sd.specification_category = p_specification_category
AND sd.attribute_desc = d.dsc (+)
ORDER BY sd.specification_sequence_no; -
I have a requirement as below-
The output I want as below.
I wrote a query as below with hard-code the code produced.EMP_ID LAPTOP DESK LAPTOP LCD MONITOR ROUTER ------------------------------------------------------------------------------------------ 100 Y Y Y Y 101 Y N Y N 102 N Y N N -----------------------------------------------------------------------------------------
I want a solution without Hardcoding code product, because if we need to introduce a new product in the product table, then the output will be fit dynamically...SELECT EMP_ID, NVL(MAX(DECODE(PROD_ID,10,'Y',NULL)),'N') LAPTOP, NVL(MAX(DECODE(PROD_ID,11,'Y',NULL)),'N') DESK_LAPTOP, NVL(MAX(DECODE(PROD_ID,12,'Y',NULL)),'N') LCD_MONITOR, NVL(MAX(DECODE(PROD_ID,13,'Y',NULL)),'N') ROUTER FROM XX_EMP a GROUP BY EMP_ID;
Is it possible to write the query like this?
Is my version of DB - Oracle 10 g
Table creation script-
CREATE TABLE XX_EMP ( EMP_ID NUMBER, PROD_ID NUMBER, SALE_DT DATE ); INSERT INTO XX_EMP VALUES(100,10,sysdate-15); INSERT INTO XX_EMP VALUES(100,11,sysdate-14); INSERT INTO XX_EMP VALUES(100,12,sysdate-14); INSERT INTO XX_EMP VALUES(100,13,sysdate-13); INSERT INTO XX_EMP VALUES(101,11,sysdate-11); INSERT INTO XX_EMP VALUES(101,13,sysdate-10); INSERT INTO XX_EMP VALUES(102,12,sysdate-10); COMMIT; CREATE TABLE XX_PRODUCT(PROD_ID NUMBER, PROD_DES VARCHAR2(50) ); INSERT INTO XX_PRODUCT VALUES(10,'LAPTOP'); INSERT INTO XX_PRODUCT VALUES(11,'DESK LAPTOP'); INSERT INTO XX_PRODUCT VALUES(12,'LCD MONITOR'); INSERT INTO XX_PRODUCT VALUES(13,'ROUTER'); COMMIT;
Hello
See this thread for more options:
Re: County report and the sum of the number of rows by multiple columnsIt seems that the aggregation of the chain might be the best for this work.
For example:
I guess you don't want to view the whole xx_emp table in all reports. In the first auxiliary request, emp_summary, below I've limited the output lines with the sale_dt in the last 30 days. You can change this to anything else, or omit the WHERE all clause if you do not want to include the entire table.
I assume that you will not necessarily interested in the whole xx_product. In the second auxiliary request, product_summary, I've limited the output to products that appear effectively in emp_summary. Again, you can use any conditions you want, or include all the lines.
The main request is the UNION of 3 games: 2 produce header lines, and the third produces the bulk of the production. Note that (as far as Oracle is concerned) there are only 2 columns in the output. The last of them will be foramatted to resemble a variable number of columns.VARIABLE column_width NUMBER EXEC :column_width := 12; SET PAGESIZE 0 WITH emp_summary AS ( SELECT emp_id, prod_id FROM xx_emp WHERE sale_dt >= TRUNC (SYSDATE - 30) -- or whatever GROUP BY emp_id, prod_id ) , product_summary AS ( SELECT prod_id , SUBSTR (prod_des, 1, :column_width) AS short_prod_des , ROW_NUMBER () OVER (ORDER BY prod_id) AS r_num FROM xx_product WHERE prod_id IN ( SELECT prod_id FROM emp_summary ) ) -- -- ===== First Header Line: Product Names ===== -- SELECT NULL AS emp_id , REPLACE ( SYS_CONNECT_BY_PATH ( LPAD ( short_prod_des , :column_width ) , '~' ) , '~' , ' ' ) AS txt FROM product_summary WHERE CONNECT_BY_ISLEAF = 1 START WITH r_num = 1 CONNECT BY r_num = PRIOR r_num + 1 -- UNION ALL -- ===== Second Header Line: Hyphens ===== -- SELECT NULL AS emp_id , SYS_CONNECT_BY_PATH ( LPAD ( '-' , :column_width , '-' ) , ' ' ) AS txt FROM product_summary WHERE CONNECT_BY_ISLEAF = 1 START WITH r_num = 1 CONNECT BY r_num = PRIOR r_num + 1 -- UNION ALL -- ===== Body of Report ===== -- SELECT e.emp_id , REPLACE ( SYS_CONNECT_BY_PATH ( LPAD ( NVL2 ( e.prod_id , 'Y' , 'N' ) , :column_width ) , '~' ) , '~' , ' ' ) AS txt FROM product_summary p LEFT OUTER JOIN emp_summary e PARTITION BY (e.emp_id) ON p.prod_id = e.prod_id WHERE CONNECT_BY_ISLEAF = 1 START WITH p.r_num = 1 CONNECT BY p.r_num = PRIOR p.r_num + 1 AND e.emp_id = PRIOR e.emp_id -- -- ===== Common ORDER BY Clause ===== -- ORDER BY emp_id NULLS FIRST , txt ;
Output:
` LAPTOP DESK LAPTOP LCD MONITOR ROUTER ------------ ------------ ------------ ------------ 100 Y Y Y Y 101 N Y N Y 102 N N Y N
There are a few things you might want to change.
For example, if the first product_name is: characters column_width, the ORDER BY clause will put hyphens, first. Which can be fixed by putting the UNION in a subquery, so that you can ORDER BY columns that do not appear in the output.You could also include all product_des, dividing each item into pieces of no. more: column_width characters. I'll leave that as an exercise.
Published by: Frank Kulash, April 12, 2011 17:49
Example of grouping of extra chain
Maybe you are looking for
-
Notification of MacBook Volume is in the Dock
Hi all. I recently bought a 13 "MacBook Pro early 2015, to replace my MacBook Pro 15" 2009. I noticed a single glitch, however. Whenever I change the volume level, the declarant is on the right side of the dock! Photo below. Any help, Geotrax.
-
Hello I use ascii_diadem_9.1_or_later.zip to merge the data from several .csv files. The problem is that I am so some weird futuristic timestamps in a channel which do not appear in the original .csv files: How to adjust the VB Script automatically a
-
Only specific index of reading
Hello I would like to use the Boolean to select the columns in the table index to read reading of the spreadsheet file. As stated in the mitdb .txt file (102), should I read only column index 4 and index 7 (V2 and V5)! Any help would be appreciated.
-
Cannot install SP3
-
EqualLogic 'virtual storage manager' does not appear in viClient
I have installed and configured all the options necessary to HITVE and the 'virtual storage manager' does not pull through? On my home screen, to the title of "Solutions and Applications" - it indicates that three of the components by default HITVE,