SQL question using UNION.
Table and data creation:
CREATE TABLE STATE_HOSPITALS.T1
T1_ID NUMBER(5)
CREATE TABLE STATE_HOSPITALS.T2
T1_ID NUMBER(5),
T2_BALANCE NUMBER(5,2)
CREATE TABLE STATE_HOSPITALS.T3
T1_ID NUMBER(5),
T3_DUE NUMBER(5,2),
T3_ID NUMBER(5)
CREATE TABLE STATE_HOSPITALS.T4
T3_ID NUMBER(5),
T4_AMOUNT NUMBER(5,2)
INSERT INTO STATE_HOSPITALS.T1 (
T1_ID)
VALUES ( 1 );
INSERT INTO STATE_HOSPITALS.T1 (
T1_ID)
VALUES ( 2 );
INSERT INTO STATE_HOSPITALS.T1 (
T1_ID)
VALUES ( 3 );
INSERT INTO STATE_HOSPITALS.T2 (
T1_ID, T2_BALANCE)
VALUES ( 1,250);
INSERT INTO STATE_HOSPITALS.T2 (
T1_ID, T2_BALANCE)
VALUES ( 3,0);
INSERT INTO STATE_HOSPITALS.T3 (
T1_ID, T3_DUE)
VALUES ( 1,10);
INSERT INTO STATE_HOSPITALS.T3 (
T1_ID, T3_DUE)
VALUES ( 2,20);
INSERT INTO STATE_HOSPITALS.T4 (
T3_ID, T4_AMOUNT)
VALUES ( 2,200);
INSERT INTO STATE_HOSPITALS.T4 (
T3_ID, T4_AMOUNT)
VALUES ( 3,300);
I want to use the following query with some correction..
SELECT t1.t1_id,
t2.t2_balance
FROM t1,t2
WHERE t1.t1_ID = t2.t1_id
UNION
SELECT t1.t1_id,
0
FROM t1
WHERE t1.t1_ID IN
( SELECT t1.t1_ID FROM t1, t3
WHERE t1.t1_id = t3.t1_id
AND t3.t3_due <> 0 )
UNION
SELECT t1.t1_ID,
0
FROM t1
WHERE t1.t1_ID IN
( SELECT t1.t1_ID FROM T1, t3,t4
WHERE T1.t1_id = t3.t1_id
AND t3.t3_ID = t4.t3_ID
AND t4.t4_AMOUNT <> 0 );
I want to retrieve as follows
T1_ID T2_BALANCE
==== =======
1 250
2 0
3 0
Thanks.
Hi,
I'm not sure of your requirements.
You can add conditions to Solomon's query, like this:
SELECT t1.t1_id
, NVL (t2_balance, 0) AS t2_balance
FROM t1
LEFT OUTER JOIN t2 ON t1.t1_id = t2.t1_id
WHERE t2.t2_balance > 0
OR t1.t1_id IN (
SELECT t1_id
FROM t3
WHERE t3_due > 0
OR t1.t1_id IN (
SELECT t3_id
FROM t4
WHERE t4_amount > 0
ORDER BY t1.t1_id
;This gets the results you want:
` T1_ID T2_BALANCE
1 250
2 0
3 0but so did Solomon's solution. If none of the solutions given so far do what you want, then post some additional sample data that shows where they fail, and explain why you want the results you do in those places.
Similar Messages
-
hi
i need the query to fetch the data ...
i have two table
1. plan
2. voidplan
each table has id uniquely, and each table has rep_id but it is not unique.
i retrieved the data from both table using union. now i need the data for the rep who is in both table. i want to check union by id, and i need to retrieve the reps who is in both tables.
my code is
select "PLAN"."ID" as "ID",
"PLAN"."REP_ID" as "REP_ID",
"PLAN"."REGION" as "REGION",
"PLAN"."LOB" as "LOB",
"PLAN"."PLAN_HIERARCHY_CHANGE" as "PLAN_HIERARCHY_CHANGE"
from "PLAN" "PLAN" ,"VOIDPLAN" "VOIDPLAN" where "PLAN"."REP_ID" IN (SELECT REP_ID FROM VOIDPLAN)
UNION
SELECT "VOIDPLAN"."ID" as "ID",
"VOIDPLAN"."REP_ID" as "REP_ID",
"VOIDPLAN"."REGION" as "REGION",
"VOIDPLAN"."LOB" as "LOB",
"VOIDPLAN"."PLAN_HIERARCHY_CHANGE" as "PLAN_HIERARCHY_CHANGE"
from "VOIDPLAN" "VOIDPLAN"
THIS QUERY GIVES ALL UNION RESULT, NUT IT DOES'T FILTER THE REP_ID, IT SHOWS ALL THE REP_ID, I NEED THE REPS WHO IS IN BOTH TABLES
CAN ANYONE PLEASE TELL ME THE SOLUTION
regards
vally.sSQL> create table plan
2 as
3 select 1 id, 1111 rep_id, 'vally' rep_name from dual union all
4 select 2, 2222, 'kavi' from dual union all
5 select 3, 3333, 'shyam' from dual
6 /
Tabel is aangemaakt.
SQL> create table voidplan
2 as
3 select 5 id, 1111 rep_id, 'vally' rep_name from dual union all
4 select 6, 2222, 'kavi' from dual
5 /
Tabel is aangemaakt.
SQL> select case x when 1 then p.id else v.id end id
2 , p.rep_id
3 , p.rep_name
4 from plan p
5 , voidplan v
6 , (select 1 x from dual union select 2 from dual) x
7 where p.rep_id = v.rep_id
8 order by 1
9 /
ID REP_ID REP_N
1 1111 vally
2 2222 kavi
5 1111 vally
6 2222 kavi
4 rijen zijn geselecteerd.Regards,
Rob. -
SQL - JOIN using UNION ?? UNION using JOIN ?? with example!
I was asked this question during one of my interviews. Can you do JOIN using UNION keyword? Can you do UNION using JOIN keyword?
That is -
1. I should get same output as JOIN without using JOIN keyword, but using UNION Keyword?
2. I should get same output as UNION without using UNION keyword, but using JOIN Keyword?
Can you give me an example of how to do this if possible?Hi,
Welcome to the forum!
user13067794 wrote:
I was asked this question during one of my interviews. Can you do JOIN using UNION keyword? Can you do UNION using JOIN keyword?The correct answer to those questions is: Why would you want to? All versions of Oracle (and probably any other database product) provide JOIN to do certain things and UNION to do other things. Why not use those features the way they were designed to be used? Even if it is possible to do what you ask, it's going to be more complicated and less efficient.
If you really must:
That is -
1. I should get same output as JOIN without using JOIN keyword, but using UNION Keyword? You can select the relevant columns from each table, and NULLs for all the columns from other tables, in a UNION query. Then you can use GROUP BY or analytic functions to combine data from different rows. For example, this JOIN:
SELECT d.dname
, e.mgr
FROM scott.dept d
JOIN scott.emp e ON d.deptno = e.deptno
;could be written using UNION, but no JOIN, like this:
WITH union_data AS
SELECT deptno
, dname
, NULL AS empno
, NULL AS mgr
FROM scott.dept
UNION ALL
SELECT deptno
, NULL AS dname
, empno
, mgr
FROM scott.emp
, quasi_join AS
SELECT MAX (dname) OVER (PARTITION BY deptno) AS dname
, mgr
, empno
FROM union_data
SELECT dname
, mgr
FROM quasi_join
WHERE empno IS NOT NULL
;Depending on your tables and your requirements, you might be able to do something a little simpler.
2. I should get same output as UNION without using UNION keyword, but using JOIN Keyword?A FULL OUTER JOIN is similar to UNION.
This UNION query:
SELECT dname AS txt
FROM scott.dept
UNION
SELECT TO_CHAR (mgr) AS txt
FROM scott.emp
;Can be written like this, using JOIN but no UNION:
SELECT DISTINCT
NVL2 ( e.empno
, TO_CHAR (e.mgr)
, d.dname
) AS txt
FROM scott.dept d
FULL OUTER JOIN scott.emp e ON 1 = 2
user13067794 wrote:I too don't any example as such, but I am thinking on this line -
Select a.x, b.y
from a,b
where a.key=b.key and sal<5000
UNION
Select a.x, b.y
From a,b
Where a.key=b.key and sal>7000
can we get same result using JOIN?That's a very special case. You can get the same results without using UNION like this:
Select distinct
a.x
, b.y
from a
, b
where a.key = b.key
and ( sal < 5000
OR sal > 7000
Can we do something similar using UNION without using JOIN keyword??What you posted does not use the JOIN keyword.
To get the same results without using a join (either with or without the JOIN keyword), you can use UNION together with aggregate or analytic functions, as I showed earlier.
Edited by: Frank Kulash on Jul 5, 2011 9:01 PM -
Need sql query to remove duplicates using UNION ALL clause
Hi,
I have a sql query which has UNION clause.But the UNION clause is causing some performance issues.
To overcome that I have used UNION ALL to improve performance but its returning duplicates.
Kindly anyone send a sample SQL query where my primary objective is used to use UNION ALL clause and to consider unique rows (elimating duplicate
ones)
Any help will be needful for me
Thanks and Regardswhy not UNION? :(
another way also use MINUS
SQL>
SQL> with t as
2 (
3 select 1 if from dual union all
4 select 2 if from dual union all
5 select 1 if from dual union all
6 select 3 if from dual union all
7 select 3 if from dual
8 )
9 ,t2 as
10 (
11 select 1 if from dual union all
12 select 2 if from dual union all
13 select 3 if from dual union all
14 select 4 if from dual union all
15 select 5 if from dual
16 )
17 (select if from t
18 union all
19 select if from t2)
20 /
IF
1
2
1
3
3
1
2
3
4
5
10 rows selected
SQL> so
SQL>
SQL> with t as
2 (
3 select 1 if from dual union all
4 select 2 if from dual union all
5 select 1 if from dual union all
6 select 3 if from dual union all
7 select 3 if from dual
8 )
9 ,t2 as
10 (
11 select 1 if from dual union all
12 select 2 if from dual union all
13 select 3 if from dual union all
14 select 4 if from dual union all
15 select 5 if from dual
16 )
17 (select if from t
18 union all
19 select if from t2)
20 minus
21 select -99 from dual
22 /
IF
1
2
3
4
5
SQL> -
Select extra row without using UNION ALL in pl/sql
Hi,
Can anyone tell me how to select extra row without using UNION or UNION ALL in pl/sql. Actually I want to have my o/p of query as partitioned by designation and ordered by salary and than one extra row which will contain the highest salary in a particular salary. My table has first_name,emp_id,designation and salary column. And I wnt the o/p as.
Mohinish,12212,SI,46000
Ram,11212,SSI,47000
Shyam,12133,SI,48000
Rick,9898,SI,46000
Rocky,12312,SSI,56000
Sariq,23948,SI,43000
Suman,12789,HR,49000
Sampy,12780,SI,46000
Parna,11111,HR,50000
Now the o/p should be.
Mohinish,12212,SI,46000
Rick,9898,SI,46000
Sariq,23948,SI,43000
Shyam,12133,SI,48000
Shyam,12133,SI,48000
Ram,11212,SSI,47000
Rocky,12312,SSI,56000
Rocky,12312,SSI,56000
Suman,12789,HR,49000
Parna,11111,HR,50000
Parna,11111,HR,50000
Thanks in AdvanceYou don't have to do a UNION or UNION ALL in PL/SQL but you would need to in SQL to get the desired output:
with data_recs
as (select 'Mohinish' first_name,12212 emp_id,'SI' designation,46000 salary from dual union
select 'Ram',11212,'SSI',47000 from dual union
select 'Shyam',12133,'SI',48000 from dual union
select 'Rick',9898,'SI',46000 from dual union
select 'Rocky',12312,'SSI',56000 from dual union
select 'Sariq',23948,'SI',43000 from dual union
select 'Suman',12789,'HR',49000 from dual union
select 'Sampy',12780,'SI',46000 from dual union
select 'Parna',11111,'HR',50000 from dual)
select first_name, emp_id, designation, salary from data_recs union all
select s.first_name, s.emp_id, s.designation, s.salary
from (select first_name,
emp_id,
designation,
salary,
row_number() over (partition by designation order by salary desc) high_salary
from data_recs
order by designation, salary) s
where s.high_salary = 1
order by designation, salary;
FIRST_NAME EMP_ID DESIGNATION SALARY
Suman 12789 HR 49000
Parna 11111 HR 50000
Parna 11111 HR 50000
Sariq 23948 SI 43000
Rick 9898 SI 46000
Mohinish 12212 SI 46000
Sampy 12780 SI 46000
Shyam 12133 SI 48000
Shyam 12133 SI 48000
Ram 11212 SSI 47000
Rocky 12312 SSI 56000
Rocky 12312 SSI 56000 -
Hello!
I need some correction in the following query. I am trying to make a 2 page QPLD so I am joining 2 queries and then printing the report into 2 pages.
SELECT T0.[U_OANumber], T0.[custmrName], T0.[U_Inspection], T0.[U_Cust_PO_Num],
T0.[U_ModelType], T0.[U_Duty], T0.[U_NamePlate],
T0.[U_Indicator_Obs], T0.[U_Fastners], T0.[U_Mounting_Type], T0.[U_Casing_Orientation], T0.[U_Impeller_Dia],
T0.[U_Bearing_Style], T0.[U_Motor_HP_Size], T0.[U_InsulationWedges], T0.[U_Varnish], T0.[U_Paint_Shade],
T0.[U_PlugsSeal], T0.[U_Remarks], T0.[U_Despatch]
FROM OINS T0 WHERE T0.[U_OANumber] = '[%0]' or T0.[customer] = '[%1]' or T0.[manufSN] = '[%2]'
Union all
SELECT T0.[U_ModelType], T0.[U_Size], T0.[U_Discharge], T0.[U_Head], T0.[U_RPM], T0.[U_YOM], T0.[U_HP],
T0.[U_Amps], T0.[U_Insulation_Class], T0.[U_Winding_Connection]
FROM OINS T0 WHERE
T0.[U_OANumber] = '[%0]' or T0.[customer] = '[%1]' or T0.[manufSN] = '[%2]'
Do I have to use the where clause just once since it is identical to both statements?
I get the error stating that all queries using Union, Intersect or Except must have an equal number of expressions. what does this mean?
scorp
Edited by: scorpion 666 on Feb 12, 2009 1:30 PMUnion has to have exact same numbers of fields to combine two results. Your query shows no need of union because your where clauses are identical.
Right syntax would be like this:
SELECT T0.[U_OANumber], T0.[custmrName], T0.[U_Inspection], T0.[U_Cust_PO_Num],
T0.[U_ModelType], T0.[U_Duty], T0.[U_NamePlate],
T0.[U_Indicator_Obs], T0.[U_Fastners], T0.[U_Mounting_Type], T0.[U_Casing_Orientation], T0.[U_Impeller_Dia],
T0.[U_Bearing_Style], T0.[U_Motor_HP_Size], T0.[U_InsulationWedges], T0.[U_Varnish], T0.[U_Paint_Shade],
T0.[U_PlugsSeal], T0.[U_Remarks], T0.[U_Despatch], T0.[U_Size], T0.[U_Discharge], T0.[U_Head], T0.[U_RPM], T0.[U_YOM], T0.[U_HP],
T0.[U_Amps], T0.[U_Insulation_Class], T0.[U_Winding_Connection]
FROM DBO.OINS T0 WHERE T0.[U_OANumber] = '[%0]' or T0.[customer] = '[%1]' or T0.[manufSN] = '[%2]'
Thanks,
Gordon -
Writing query using UNION Operator
Question -
(1)
Write an SQL Statement to list the following items: Customer ID, Customer Name, number of invoices, sum of total for invoices. Ensure that all customers are returned in the result set.
Answer for the above is written as below by one person. That seams to be correct. Is there another way of writing this same query.;
select c.CUSTOMER_ID,c.NAME,i.cnt,i.s
from gee_customer c,(select customer_id,count(*) as cnt, sum(TOTAL) as s
from gee_invoice
group by customer_id) i
where c.CUSTOMER_ID = i.customer_id (+)
(2)
My other question is How to write the above answer (or what you sugest) using UNION operator ?
Any ideas please
Message was edited by:
user483578In fact the outer join means you use the union of two result sets - usual join result
and the rows from the outer table for which there is not any row in the inner table.
SQL> select d.deptno, e.ename from emp e, dept d
2 where d.deptno = e.deptno(+)
3 order by 1,2
4 /
DEPTNO ENAME
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
40
15 rows selected.
SQL> select d.deptno,e.ename from emp e, dept d
2 where d.deptno = e.deptno
3 union
4 select deptno, null
5 from dept d where not exists (select null from emp e where e.deptno = d.deptno)
6 order by 1,2
7 /
DEPTNO ENAME
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
40
15 rows selected.In your example something like (NOT tested !)
with i as (select customer_id,count(*) as cnt, sum(TOTAL) as s
from gee_invoice group by customer_id)
select c.CUSTOMER_ID,c.NAME,i.cnt,i.s
from gee_customer c, i
where c.CUSTOMER_ID = i.customer_id
union
select c.CUSTOMER_ID,c.NAME,null,null
from gee_customer c
where not exists (select null from i where c.CUSTOMER_ID = i.customer_id)
Rgds. -
Query takes long when using UNION
Hi ,
I habe a query as follows;
SELECT
'9999' site_id,
m.ghi_prov_num provnum,
SUBSTR (l.seq_num, 1, 3)provloc,
t.dea_number dea,
t.license_number statelicensenumber ,
n.npi_num npi,
m.prefix prefixname,
m.lastname lastname,
m.firstname firstname,
t.middle_name middleinitial,
m.suffix suffixname,
null clinicname,
l.street1 addressline1,
l.street2 addressline2,
l.city city,
l.state state,
l.zip5 zip,
l.phone phoneprimary,
null ext,
null fax,
null email,
null alt_phone,
null alt_phone_ext
FROM provider m, LOCATION l, npi n,TEMP_VITAL_CACTUS t,test_provider_pin pin
WHERE m.ghi_prov_num=l.ghi_prov_num
and m.ghi_prov_num=n.ghi_prov_num(+)
and m.ghi_prov_num=t.ghi_prov_num(+)
and m.tax_id=pin.tax_id
UNION
SELECT
'9999', m.ghi_prov_num ,
m.location provloc,
null ,
null ,
n.npi_num ,
null ,
m.lastname ,
m.firstname ,
null,
null ,
null ,
m.street1 ,
m.street2 ,
m.city ,
m.state ,
m.zip5 ,
m.phone ,
null ,
null ,
null ,
null ,
null
FROM dental_provider m, npi n,test_provider_pin pin
WHERE m.ghi_prov_num=n.tax_id(+)
and m.location=n.location(+)
and pin.tax_id=m.ghi_prov_num;The query takes for ever;
But Individual query takes less than a sec to execute.Is there any way can i rewrite the query?
Please help
Hena.user11253970 wrote:
But Individual query takes less than a sec to execute.Is there any way can i rewrite the query?Have a feeling you are using Toad/SQL Navigator or similar tool which returns data one screen at a time. If so, then it does not "takes less than a sec to execute" but rather to fetch first screen of rows. When you use UNION Oracle has to return distinct rows from both queries. Therefore it must fetch not just first screen but all rows. To verify, issue first query and in yiour GUI tool click on get last screen. Then you'll know how long whole select takes. Do the same for second query. Also, do you need distinct rows or akll rows? IF all rows, change UNION to UNION ALL.
SY. -
How to use union statement with declare & set function?
Hi Experts,
i have small query about how to use union statement with declare & set function?
Example as below :
DECLARE @name AS date
Declare @name2 AS date
/* SELECT FROM [2013].[dbo].[OINV] T0 */
/* WHERE */
SET @name = /* T0.DocDate */ '[%1]'
SET @name2 = /* T0.DocDate */ '[%2]'
select '2013',t5.U_salmannm,t1.CardName,t2.sumapplied as CollectionAmount,t2.DcntSum ,t3.DocTotal as InvoiceTotal,
datediff(dd,t3.DocDate,t1.Docdate) as Days
from 2013.dbo.orct t1
inner join 2013.dbo.RCT2 t2 on t1.DocNum = t2.DocNum
left join 2013.dbo.oinv t3 on
t3.docentry = t2.baseAbs
inner join 2013.dbo.ocrd t4 on t1.Cardcode = t4.CardCode
inner join [2013].[dbo].[@CQ_RTSM] t5 on t4.U_BeatCode = t5.U_RoutCode
where t2.DcntSum <> 0.000000 and t3.DocDate between [%1] and [%2]
Union
/* SELECT FROM [2014].[dbo].[OINV] T0 */
/* WHERE */
SET @name = /* T0.DocDate */ '[%1]'
SET @name2 = /* T0.DocDate */ '[%2]'
select '2014',t5.U_salmannm,t1.CardName,t2.sumapplied as CollectionAmount,t2.DcntSum ,t3.DocTotal as InvoiceTotal,
datediff(dd,t3.DocDate,t1.Docdate) as Days
from 2014.dbo.orct t1
inner join 2014.dbo.RCT2 t2 on t1.DocNum = t2.DocNum
left join 2014.dbo.oinv t3 on
t3.docentry = t2.baseAbs
inner join 2014.dbo.ocrd t4 on t1.Cardcode = t4.CardCode
inner join [2014].[dbo].[@CQ_RTSM] t5 on t4.U_BeatCode = t5.U_RoutCode
where t2.DcntSum <> 0.000000 and t3.DocDate between [%1] and [%2]You have to create stored procedure in SQL only .
Like u must have create for Crystal .
You can execute procedure in query manager but you have to enter parameter manually..
example
Exec @Test '20140101' '20140501'
Every time user has to enter it manually in yyyymmdd format in case of date parameters.
Example
Create Proc [@Test]
as begin
DECLARE @name AS date
Declare @name2 AS date
/* SELECT FROM [2013].[dbo].[OINV] T0 */
/* WHERE */
select '2013',t5.U_salmannm,t1.CardName,t2.sumapplied as CollectionAmount,t2.DcntSum ,t3.DocTotal as InvoiceTotal,
datediff(dd,t3.DocDate,t1.Docdate) as Days
from 2013.dbo.orct t1
inner join 2013.dbo.RCT2 t2 on t1.DocNum = t2.DocNum
left join 2013.dbo.oinv t3 on
t3.docentry = t2.baseAbs
inner join 2013.dbo.ocrd t4 on t1.Cardcode = t4.CardCode
inner join [2013].[dbo].[@CQ_RTSM] t5 on t4.U_BeatCode = t5.U_RoutCode
where t2.DcntSum <> 0.000000 and t3.DocDate between @Name and @Name2
Union
/* SELECT FROM [2014].[dbo].[OINV] T0 */
/* WHERE */
select '2014',t5.U_salmannm,t1.CardName,t2.sumapplied as CollectionAmount,t2.DcntSum ,t3.DocTotal as InvoiceTotal,
datediff(dd,t3.DocDate,t1.Docdate) as Days
from 2014.dbo.orct t1
inner join 2014.dbo.RCT2 t2 on t1.DocNum = t2.DocNum
left join 2014.dbo.oinv t3 on
t3.docentry = t2.baseAbs
inner join 2014.dbo.ocrd t4 on t1.Cardcode = t4.CardCode
inner join [2014].[dbo].[@CQ_RTSM] t5 on t4.U_BeatCode = t5.U_RoutCode
where t2.DcntSum <> 0.000000 and t3.DocDate between
between @Name and @Name2
end -
Hello, everyone,
I have an SSIS package with a XML package configuration file. I deployed it to the sql server using the import feature in SSMS (in Stored Packages, right click MSDB, select Import, select the package to deploy in file system). My question is
Is the XML package configuration file also deployed? If so, which folder is it stored so that I can change the values in it? If not, is setting up the Configuration tab when scheduling a agent job the way to go manually bring the configuration file in?
Your help and information is much appreciated.
RegardsThank you Arthur for your reply. I appreciate it.
A followup questions:
I used direct setting for the configuration file path on C:\. But it needs to be on D:\ on the remote server. I can't create the D:\ path because I don't have D:\ drive on my computer (I understand I could use indirect setting to change the path in the remote
server. But that's not the concern here).
My question is: since the configuration is not deployed, can I make it up by setting up the Configurations in creating the agent job step? That is, copy the configuration file to the D:\ drive on the remote server, in Configurations tab, add the configuration
file on D:\ to the configuration. Will it work? Will the package still look for the original config file on C:\? and if it doesn't find it there, will it cause any error?
Thank you in advance for your help.
Regards -
Hi Gurus,
I have three tables. I want to join all tables using union in SQL statement. The query is returning all the records from both tables but i only require unique rows based on a specific column value. Here is my table structure -
TableA -
LIC_ID NUMBER(10) NOT NULL
LIC_NUMBER VARCHAR2(20)
COMMENCE_DATE DATE
EXPIRY_DATE DATE
TERM VARCHAR2(20)LIC_ID is the primary key in this table -
Sample data from TableA
LIC_ID LIC_NUMBER COMMENCE_DATE EXPIRY_DATE TERM
2 TR4323 12/04/2008 11/03/2010 2 Years
34 TR5432 23/07/2009 22/07/2010 1 Year
45 TR5321 24/06/2009 23/06/2010 1 Year
65 TR6666 23/07/2010 22/07/2011 1 Year
32 TR2423 30/05/2010 29/05/2011 1 YearTableB -
MAR_ID NUMBER(10) NOT NULL
LIC_ID NUMBER(10) NOT NULL
ZONE_NAME VARCHAR2(20)
DEPARTMENT VARCHAR2(20)
ACTIVITIES VARCHAR2(200)
COMMENTS VARCHAR2(200)MAR_ID is the primary key in this table and LIC_ID is the foreign key on TableA
Sample data from TableB -
MAR_ID LIC_ID ZONE_NAME DEPARTMENT ACTIVITIES COMMENTS
23 2 ZONE A IT NONE
43 34 ZONE B IT NONE
33 65 ZONE C ACCOUNT NONE
TableC
REC_ID NUMBER(10) NOT NULL
LIC_ID NUMBER(10) NOT NULL
DIST_NAME VARCHAR2(20)
REGION VARCHAR2(20)
ACTIVITIES VARCHAR2(200)
COMMENTS VARCHAR2(200)REC_ID is the primary key in this table and LIC_ID is the foreign key.
Sample data -
REC_ID LIC_ID DIST_NAME REGION ACTIVITIES COMMENTS
2 45 SA NORTH NONE
3 65 TA NORTH NONE
5 32 NT SOUTH NONEHere is my sql query -
select a.lic_id, a.lic_number, a.commence_date, a.expiry_date from
TableA a, TableB b
where a.lic_id=b.lic_id
union
select a.lic_id, a.lic_number, a.commence_date, a.expiry_date from
TableA a, TableC c
where a.lic_id=c.lic_idThe above query returns -
lic_id lic_number commence_date expiry_date
2 TR4323 12/04/2008 11/03/2010
34 TR5432 23/07/2009 22/07/2010
45 TR5321 24/06/2009 23/06/2010
65 TR6666 23/07/2010 22/07/2011
32 TR2423 30/05/2010 29/05/2011
65 TR6666 23/07/2010 22/07/2011 LIC_ID 65 exists in both table TableB and TableC hence it repeats in query but I want to display that only once. How can I do that? I want to return unique record on LIC_NUMBER.
Hope this make sence.
Many thanks,
TajuddinThanks for all your reply and suggestions. David altering session did not work.
Sven your idea helped me to figure it out what to do. I found a way around to fix it. Here is my current code -
select a.lic_id, a.lic_number, a.commence_date, a.expiry_date from
TableA a, TableB b
where a.lic_id=b.lic_id
union
select a.lic_id, a.lic_number, a.commence_date, a.expiry_date from
TableA a, TableC c
where a.lic_id=c.lic_id and c.lic_id not in ( select lic_id from TableB)This will exclude any LIC_ID exists in TableB.
Thanks again for your help guys.
Regards,
M Tajuddin
Web: http://tajuddin.whitepagesbd.com
Blog: http://aspblog.whitepagesbd.com -
SQL Tuning using Enterprise manager in oracle 10g
Hi,
In oracle 10g you have the enterprise manager which can be used to tune sql statements using the SQL Tuning ADvisor and SQL access advisor.
I believe in oracle 10g the process of SQL Tuning is slightly easier using the Enterprise Manager ...so if some one could explain me that process...
Again thanking you in advance
regds
Manoj GokhaleHi Manoj,
Didn't you already start two other threads about this same question the SQL forum?
How do i do the SQL Statement tuning
Enterprise Manager - sql tuning advisor , Access advisor for SQL Tuning -
Construct a Sql block using With Clause to improve the performance
I have got four diff parametrized cursor in my Pl/Sql Procedure. As the performance of the Procedure is very pathetic,so i have been asked to tune the Select statements used in those cursors.
So I am trying to use the With Clause in order to club all those four Select Statements.
I would appreciate if anybody can help me to construct the Sql Block using With Clause.
My DB version is..
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0 - Production
Four Diff cursors are defined below.
CURSOR all_iss (
b_batch_end_date IN TIMESTAMP,
IS
SELECT isb.*
FROM IMPLMN_STEP_BREKPN isb
, ISSUE iss
WHERE isb.issue_id = iss.issue_id
AND iss.issue_status_id = 50738
AND ewo_no IN
(SELECT TO_CHAR(wo_no)
FROM MGO_PLANT_AUDIT
WHERE dml_status = 'U' OR dml_status = 'I')
UNION ALL
SELECT isb.*
FROM IMPLMN_STEP_BREKPN isb
, ISSUE iss
WHERE isb.issue_id = iss.issue_id
AND iss.issue_status_id = 50738
AND CAST (isb.last_updt_timstm AS TIMESTAMP) >=
b_batch_end_date;
CURSOR ewo_plant ( p_ewo_no IN IMPLMN_STEP_BREKPN.ewo_no%TYPE)
IS
SELECT DISTINCT wo_no ,
plant_code
FROM MGO_PLANT
WHERE TO_CHAR(wo_no) = p_ewo_no;
CURSOR iss_ewo_plnt (
p_issue_id IN IMPLMN_STEP_BREKPN.issue_id%TYPE ,
p_ewo_no IN IMPLMN_STEP_BREKPN.EWO_NO%TYPE,
p_plnt_code IN IMPLMN_STEP_BREKPN.PLT_FACLTY_ID%TYPE)
IS
SELECT *
FROM IMPLMN_STEP_BREKPN
WHERE issue_id = p_issue_id
AND ewo_no = p_ewo_no
AND
(plt_faclty_id = p_plnt_code
OR
plt_faclty_id IS NULL);
CURSOR iss_ewo_plnt_count (
p_issue_id IN IMPLMN_STEP_BREKPN.issue_id%TYPE ,
p_ewo_no IN IMPLMN_STEP_BREKPN.EWO_NO%TYPE,
p_plnt_code IN IMPLMN_STEP_BREKPN.PLT_FACLTY_ID%TYPE)
IS
SELECT COUNT(*)
FROM IMPLMN_STEP_BREKPN
WHERE issue_id = p_issue_id
AND ewo_no = p_ewo_no
AND
(plt_faclty_id = p_plnt_code
OR
plt_faclty_id IS NULL);Not tested. Some thing like below. i just made the queries as tables and given name as a,b,c and substituted columns for the parameters used in the 2nd cursor and third cursor. Try like this.
CURSOR all_iss (
b_batch_end_date IN TIMESTAMP,
IS
select a.*,b.*,c.* from
( SELECT isb.*
FROM IMPLMN_STEP_BREKPN isb
, ISSUE iss
WHERE isb.issue_id = iss.issue_id
AND iss.issue_status_id = 50738
AND ewo_no IN
(SELECT TO_CHAR(wo_no)
FROM MGO_PLANT_AUDIT
WHERE dml_status = 'U' OR dml_status = 'I')
UNION ALL
SELECT isb.*
FROM IMPLMN_STEP_BREKPN isb
, ISSUE iss
WHERE isb.issue_id = iss.issue_id
AND iss.issue_status_id = 50738
AND CAST (isb.last_updt_timstm AS TIMESTAMP) >=
b_batch_end_date) a,
( SELECT DISTINCT wo_no ,
plant_code
FROM MGO_PLANT
WHERE TO_CHAR(wo_no) = p_ewo_no) b,
( SELECT *
FROM IMPLMN_STEP_BREKPN
WHERE issue_id = p_issue_id
AND ewo_no = p_ewo_no
plt_faclty_id IS NULL) c
where b.wo_no = c.ewo_no and
c.issue_id = a.issue_id ;
vinodh
Edited by: Vinodh2 on Jul 11, 2010 12:03 PM -
HI,
I want to select data from 2 tables using Union in Popup LOV ,
please help.
I am getting a error message like
"LOV query is invalid, a display and a return value are needed, the column names need to be different. If your query contains an in-line query, the first FROM clause in the SQL statement must not belong to the in-line query."
when i use union query
my query is like this
select a from table 1
union
select b from table 2
order by a;Hi,
Three things -
1) Remove the semi-colon from the end of your query (it can work without it, but you'll eventually run into problems some day by putting a semi-colon on the end, so better to get into the habit of not doing it).
2) You need to provide 2 columns in your query, a display value and a return value (look at the error message you are getting). You are only returning a single column.
3) Wrap your query inside another query to get it to work.
Something like this should work -
select * from (
select 1 as r, 2 as d from dual
union
select 3 as r, 4 as d from dual)
order by dObviously change the values/columns to suit your own purposes.
Hope this helps,
John. -
Cannot export query output when using UNION ALL
Hi
I can run a query in SQL Developer 1.5.5 and if it's a SELECT statement it works fine, but if I output the result of several SELECT statements using UNION ALL after some 10,000 records the SQL Developer crashes and it generates a file whose size is 0 kb
Is there a workaround for this??
Thanks and Regards!
IsaacShould be fixed in the upcoming 2.1... you can try the RC1 also...
Regards,
K.
Maybe you are looking for
-
Adobe Media Encoder error when encoding: winfile.cpp-759
Mods: Please move this to the appropriate thread wasn't sure to move it to Premiere or Flash I receive this error when trying to encode AVI to H.264 using Adobe Media Encoder CS5 (64bit): When searching through the forums, several suggestions for CS4
-
Validation for duplicate AP invoice
We would like to prevent duplicate AP invoices from being posted through FB60. We created an FI validation in OB28 in which the prerequisite = Tcode FB60, and the check is a user exit to check the values in vendor / company code / reference fields
-
CDATA appearing but not rendering as html...
simple XML <products> <product> <process>Preservation</process> <application>Crack Filling</application> <rank>100</rank> <productType>Viscosity Grade Asphalts</productType> <productName>AC-20 As
-
Hi, The JCA transport supports URI rewriting in a proxy service message flow. Under what context would URL rewriting be used? Any help in this context would be really appreciated. Regards Priya.
-
The bloody thing comes up with something called "Live Bookmarks" instead of the regular save menu. How do I get rid of it and back to the normal save bookmarks system?