Combine 2 sql queries into one with eliminating duplicate colums - sql

I have written 2 sql queries by using multiple join statements.
Now I want to combine both the query results as below.
First query shows these columns
UDC_ID, EXT_ID, VALUE
The second query shows these columns
UDC_ID, EXT_ID, VALUE
In both the queries UDC_ID and EXT_ID columns are the same, but the VALUE column in each is different
So the final output I want to display is,
UDC_ID, EXT_ID, VALUE (From Query1), VALUE (from Query 2)
Can anyone suggest how this can be achieved?
These are my queries:
Query 1 joins three tables:
SELECT
DEV.UDC_ID,
SR.EXT_ID,
SRA.VALUE
FROM SERVICE_REQUEST SR
JOIN DEVICE DEV
ON SR.DEVICE_ID = DEV.ID
JOIN SERVICE_REQUEST_ATTR SRA
ON SR.ID = SERVICE_REQUEST_ID
WHERE SR.SUB_TYPE_CD = 'HMI_22'
--AND DEV.SUB_TYPE = 'ESME'
AND SRA.NAME = 'CommsHubGUID'
AND SR.INSERT_TIME >= TO_DATE('2016-09-21 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND SR.INSERT_TIME <= TO_DATE('2016-09-28 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
ORDER BY SR.INSERT_TIME DESC;
The difference between query 1 and this query is the where clause criterion for SRA.NAME field, otherwise both the queries are same.
SELECT
DEV.UDC_ID,
SR.EXT_ID,
SRA.VALUE
FROM SERVICE_REQUEST SR
JOIN DEVICE DEV
ON SR.DEVICE_ID = DEV.ID`enter code here`
JOIN SERVICE_REQUEST_ATTR SRA
ON SR.ID = SERVICE_REQUEST_ID
WHERE SR.SUB_TYPE_CD = 'HMI_22'
--AND DEV.SUB_TYPE = 'ESME'
AND SRA.NAME = 'Service Location'
AND SR.INSERT_TIME >= TO_DATE('2016-09-21 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND SR.INSERT_TIME <= TO_DATE('2016-09-28 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
ORDER BY SR.INSERT_TIME DESC

SELECT COALESCE(q1.UDC_ID,q2.UDC_ID),
COALESCE(q1.EXT_ID, q2.EXT_ID),
q1.VALUE ,
q2.VALUE
FROM (query 1) q1
FULL OUTER JOIN (query 2) q2 ON q1.UDC_ID=q2.UDC_ID and q1.EXT_ID=q2.EXT_ID

Related

{SAS} Converting a SQL merge in to HASH merge

proc sql;
create table ndd1 as
select a.*, 1 as default_flag, b.retail_account_no, c.limit
from (
select
posting_date, counterparty_id, counterparty_indicator, customer_id, last_default_date, last_out_of_default_date
from gdwh30.tb0_default
where counterparty_id<>'*noval*'
and ((last_out_of_default_date < posting_date and last_default_date >= last_out_of_default_date)
or (last_out_of_default_date > posting_date and last_default_date < last_out_of_default_date))
and posting_date = to_date(%nrbquote(')&tt.%nrbquote('), 'DD-MON-YYYY hh24:mi')) a
left join (
select retail_account_no, REF_account_ID, regexp_substr(REF_account_ID,'[^*]+', 1) as counterparty_id
from gdwh30.TB0_account
/*tb_account*/
where bus_date_from<= to_date(%nrbquote(')&tt.%nrbquote('), 'DD-MON-YYYY hh24:mi')
and bus_date_until>=to_date(%nrbquote(')&tt.%nrbquote('), 'DD-MON-YYYY hh24:mi')
and entity_id='RBBG') b
on a.counterparty_id=b.counterparty_id
;quit;
&tt. is of the form
30APR2020:00:00:00
or date22.
data ndd_1;
if 0 then set gdwh30.tb0_default today ;
if _n_=1 then do;
declare hash k(dataset:"gdwh30.tb0_default");
k.definekey ("posting_date");
k.definedata ("counterparty_id", "posting_date", "last_out_of_default_date", "last_default_date");
k.definedone();
declare hash j(dataset:"today");
j.definekey("today1");
j.definedata ("today1");
j.definedone();
end;
set ndd_1;
if k.find(key:posting_date)=0 and j.find(key:today1)=0 then output;
run;
What I think is must do is format the columns pre N=1;
For the second join on Tb0_acc I want to attempt a fuzzy merger so any help will be greatly appreciated.
Today1 is &tt. but in a table instead of a list. I first try to join the A part just to get a feel for these hash joins.
Posting_date is of the form 31AUG2016:00:00:00

Oracle SQL Groupby and SUM

I have the following query that groups every tender id and description and shows its total Sum:
3020 American Express 20
1000 Cash - primary currency 9903.25
3120 House Card 2605.56
4070 Purchase Order 668.25
3000 Visa 26005.19
SELECT B.TENDER_TYPE_ID, A.TENDER_TYPE_DESC, SUM (B.TENDER_AMT)
FROM POS_TENDER_TYPE_HEAD A, SA_TRAN_TENDER B, SA_TRAN_HEAD C
WHERE A.TENDER_TYPE_ID = B.TENDER_TYPE_ID AND B.TRAN_SEQ_NO = C.TRAN_SEQ_NO
GROUP BY B.TENDER_TYPE_ID, A.TENDER_TYPE_DESC
In the table C (SA_TRAN_HEAD), there is a field of TRAN_DATETIME.
I want the query to return the results falling between an interval of dates but do not want the date to appear in the query columns.
What could I do to achieve this ?
Just add the desired date range to the WHERE clause:
SELECT B.TENDER_TYPE_ID,
A.TENDER_TYPE_DESC,
SUM (B.TENDER_AMT)
FROM POS_TENDER_TYPE_HEAD A,
SA_TRAN_TENDER B,
SA_TRAN_HEAD C
WHERE A.TENDER_TYPE_ID = B.TENDER_TYPE_ID AND
B.TRAN_SEQ_NO = C.TRAN_SEQ_NO AND
C.TRAN_DATETIME BETWEEN TO_DATE('01-JAN-2015', 'DD-MON-YYYY')
AND TO_DATE('31-MAR-2015', 'DD-MON-YYYY')
GROUP BY B.TENDER_TYPE_ID,
A.TENDER_TYPE_DESC
I also suggest you get used to using ANSI join syntax, as it makes the joins much clearer and is more transportable (particularly for outer joins). The following should be equivalent:
SELECT B.TENDER_TYPE_ID,
A.TENDER_TYPE_DESC,
SUM (B.TENDER_AMT)
FROM POS_TENDER_TYPE_HEAD A
INNER JOIN SA_TRAN_TENDER B
ON B.TENDER_TYPE_ID = A.TENDER_TYPE_ID
INNER JOIN SA_TRAN_HEAD C
ON C.TRAN_SEQ_NO = B.TRAN_SEQ_NO
WHERE C.TRAN_DATETIME BETWEEN TO_DATE('01-JAN-2015 00:00:00', 'DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('31-MAR-2015 23:59:59', 'DD-MON-YYYY HH24:MI:SS')
GROUP BY B.TENDER_TYPE_ID,
A.TENDER_TYPE_DESC
This makes it clear what the join criteria are, as opposed to the filter criteria.

how to join 2 query into one in EXCEL ORACLE CONNECTION

I have 2 query.
I am trying to join them so I just write export from one instead of manually joining them in excel.
(SELECT
b.OUT_NO,
a.ACCNO,
a.BILL_ACCNO,
a.NAME,
a.HOUSE_NO,
a.STREET,
a.HOUSE_NO2,
a.ZIP,
a.ID,
b.TIME_STAMP,
b.REST_DATE,
c.RESTORED_TIME,
b.OUT_CMNT
FROM brook.account a,
brook.problem b,
brook.history c
WHERE c.OUT_NO = b.OUT_NO
AND a.ID = c.ID
AND ( (a.NAME Is Not Null)
AND (a.DISC Is Null)
AND (b.TIME_STAMP>?)
AND (c.RESTORED_TIME<?))
)
and
(SELECT
b.OUT_NO,
a.ACCNO,
a.BILL_ACCNO,
a.NAME,
a.HOUSE_NO,
a.STREET,
a.HOUSE_NO2,
a.ZIP,
a.ID,
b.TIME_STAMP,
b.REST_DATE,
c.RESTORED_TIME,
b.OUT_CMNT
FROM brook.account a,
brook.problem b,
brook.history c
WHERE c.OUTAGE_NO = b.OUTAGE_NO
AND a.ID = c.ID
AND ( (a.NAME Is Not Null)
AND (a.DISC Is Null)
AND (b.TIME_STAMP > ? And b.TIME_STAMP < ?)
AND (c.RESTORED_TIME > ? And c.RESTORED_TIME < ?)
)
)
How can I join these 2? into 1, I tried UNION ALL but I get ora-01847 day of month must be between 1 and last day of month ERROR.
? are the parameter, it is linked to cells on spreadsheet.
format of excel data parameter. 11/04/2013 00:00:00
Thanks
Error is about a date format, not about union.
If you pass cell values as string parameters Oracle tries to convert it to dates to comapre with columns of date or timestamp values in table columns. To do this conversion Oracle uses it's internal default date representation format wich is not mm/dd/yyyy hh24:mi:ss in your case.
There are 2 possibilities to fix a situation:
Pass parameters with date type to query and convert values to dates before passing it to Oracle. Check examples on MSDN and description of CreateParameter and Parameters.Append methods.
Convert values to dates in query with to_date Oracle function.
Change conditions in query from
AND (b.TIME_STAMP>?)
AND (c.RESTORED_TIME<?))
and
AND (b.TIME_STAMP > ? And b.TIME_STAMP < ?)
AND (c.RESTORED_TIME > ? And c.RESTORED_TIME < ?)
to
AND (b.TIME_STAMP > to_date(?,'mm/dd/yyyy hh24:mi:ss') )
AND (c.RESTORED_TIME < to_date(?,'mm/dd/yyyy hh24:mi:ss') ))
and
AND (
b.TIME_STAMP > to_date(?,'mm/dd/yyyy hh24:mi:ss')
And
b.TIME_STAMP < to_date(?,'mm/dd/yyyy hh24:mi:ss')
)
AND (
c.RESTORED_TIME > to_date(?,'mm/dd/yyyy hh24:mi:ss')
And
c.RESTORED_TIME < to_date(?,'mm/dd/yyyy hh24:mi:ss')
)

Sql query for following scenario

I have following scenario, for which i need to write sql query.
I have ICCID table and ICCID property table which holds following information.
I want to find out all active iccids and iccid's which are in removed state in month of december 2012.for ICCIDs which are in removed state, date.to.change key in the ICCID property table itself which record the removed date of ICCID.
this is my attempt, but that did not worked
select e.ID_ICCID from ICCID_PROPERTY e where
e.c_key ='STATE' and e.c_value='Active' or(
e.c_key ='STATE' and
e.c_value='Removed' and
e.c_key='date.to.change' and
to_date(e.c_value,'yyyymmdd') >=to_date('2012-DEC-01 00:00:00', 'YYYY-MON-DD HH24:MI:SS') and
to_date(e.c_value,'yyyymmdd') <= to_date('2012-DEC-31 23:59:59', 'YYYY-MON-DD HH24:MI:SS')
))
Thanks in advance for any help
This is one of the issues with a key-value pair design such as this...
You can't just check a single property row to see if it matches the search criteria, since the criteria in this case will span multiple properties... you have to check if a single parent row has all the children properties that match:
SELECT
i.ICCID
FROM
ICCID i
WHERE
EXISTS (
SELECT 1
FROM ICCID_PROPERTY ip
WHERE
ip.ID_ICCID = i.ID_ICCID
AND ip.c_key = 'STATE'
AND ip.c_value = 'Active'
) OR (
EXISTS (
SELECT 1
FROM ICCID_PROPERTY ip
WHERE
ip.ID_ICCID = i.ID_ICCID
AND ip.c_key = 'STATE'
AND ip.c_value = 'Removed'
) AND
EXISTS (
SELECT 1
FROM ICCID_PROPERTY ip
WHERE
ip.ID_ICCID = i.ID_ICCID
AND ip.c_key = 'date.to.change'
AND to_date(ip.c_value,'yyyymmdd') >=
to_date('2012-DEC-01 00:00:00', 'YYYY-MON-DD HH24:MI:SS')
AND to_date(ip.c_value,'yyyymmdd') <=
to_date('2012-DEC-31 23:59:59', 'YYYY-MON-DD HH24:MI:SS')
)
)
I think you could join the Property table three times -- maybe something like this (untested):
SELECT I.ID_ICCID
FROM ICCID I
JOIN ICCID_Property IP ON I.ID_ICCID = IP.ID_ICCID AND IP.C_Key = 'STATE' AND IP.C_Value = 'Active'
JOIN ICCID_Property IP2 ON I.ID_ICCID = IP.ID_ICCID AND IP2.C_Key = 'STATE' AND IP2.C_Value= 'Removed'
JOIN ICCID_Property IP3 ON I.ID_ICCID = IP.ID_ICCID AND IP3.C_Key = 'date.to.change' AND to_date(IP3.C_Value,'yyyymmdd') >= to_date('2012-DEC-01 00:00:00', 'YYYY-MON-DD HH24:MI:SS')
AND to_date(IP3.C_Value,'yyyymmdd') <=
to_date('2012-DEC-31 23:59:59', 'YYYY-MON-DD HH24:MI:SS')
Good luck.

Oracle SQL Correlated Subquery (in WHERE statement) having no results

Hi and thanks for reading,
I am trying to run the following SQL query with correlated subquery and it is bringing back no results. I am using the subquery to only bring back results that have the lowest date range. Both the query alone and the subquery alone work fine.
Any ideas? Am I linking the correlated subquery incorrectly?
Note: The correlated subquery is on the last line of the query
Thank you,
Willz06jw
SELECT aaa."effective_time",
aaa."event_type_c",
"clarity_dep"."department_name",
"patient"."birth_date",
"patient"."pat_mrn_id",
"zc_ped_delivr_meth"."name",
"zc_ped_delivr_meth"."ped_delivr_meth_c",
aaa."department_id",
"clarity_dep"."department_id",
aaa."alt_event_type_c",
aaa."in_event_type_c"
FROM (("CLARITY"."clarity_adt" aaa
inner join "CLARITY"."clarity_dep" "CLARITY_DEP"
ON aaa."department_id" = "clarity_dep"."department_id")
inner join "CLARITY"."patient" "PATIENT"
ON aaa."pat_id" = "patient"."pat_id")
inner join "CLARITY"."zc_ped_delivr_meth" "ZC_PED_DELIVR_METH"
ON "patient"."ped_delivr_meth_c" =
"zc_ped_delivr_meth"."ped_delivr_meth_c"
WHERE ( "patient"."birth_date" >= To_date ('01-12-2012 00:00:00',
'DD-MM-YYYY HH24:MI:SS')
AND "patient"."birth_date" < To_date ('06-12-2012 00:00:00',
'DD-MM-YYYY HH24:MI:SS'
) )
AND ( aaa."department_id" = 236601
OR aaa."department_id" = 236703
OR aaa."department_id" = 236801
OR aaa."department_id" = 236901
OR aaa."department_id" = 237101
OR aaa."department_id" = 237201 )
AND aaa."event_type_c" = 3
AND aaa."effective_time" = (SELECT Min(bbb."effective_time")
FROM "clarity_adt" bbb
WHERE aaa."pat_id" = bbb.pat_id)
Without zifting through the entire statement, the easiest solution would be to just wrap the statement in a select using the ROW_NUMBER function to get the lowest dates.
SQL Statement 1
SELECT *
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY pat_id ORDER BY effective_time) AS rn,
aaa."effective_time",
aaa."event_type_c",
"clarity_dep"."department_name",
"patient"."birth_date",
"patient"."pat_mrn_id",
"zc_ped_delivr_meth"."name",
"zc_ped_delivr_meth"."ped_delivr_meth_c",
aaa."department_id",
"clarity_dep"."department_id",
aaa."alt_event_type_c",
aaa."in_event_type_c"
FROM "CLARITY"."clarity_adt" aaa
inner join "CLARITY"."clarity_dep" "CLARITY_DEP" ON aaa."department_id" = "clarity_dep"."department_id"
inner join "CLARITY"."patient" "PATIENT" ON aaa."pat_id" = "patient"."pat_id"
inner join "CLARITY"."zc_ped_delivr_meth" "ZC_PED_DELIVR_METH" ON "patient"."ped_delivr_meth_c" = "zc_ped_delivr_meth"."ped_delivr_meth_c"
WHERE ( "patient"."birth_date" >= To_date ('01-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
AND "patient"."birth_date" < To_date ('06-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS' ) )
AND ( aaa."department_id" = 236601 OR aaa."department_id" = 236703 OR aaa."department_id" = 236801 OR aaa."department_id" = 236901 OR aaa."department_id" = 237101 OR aaa."department_id" = 237201 )
AND aaa."event_type_c" = 3
) q
WHERE q.rn = 1
Your statement could be simplified (in reading it) by using an IN statement io. all the ORstatements like such
SQL Statement 2
SELECT *
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY pat_id ORDER BY effective_time) AS rn,
aaa."effective_time",
aaa."event_type_c",
"clarity_dep"."department_name",
"patient"."birth_date",
"patient"."pat_mrn_id",
"zc_ped_delivr_meth"."name",
"zc_ped_delivr_meth"."ped_delivr_meth_c",
aaa."department_id",
"clarity_dep"."department_id",
aaa."alt_event_type_c",
aaa."in_event_type_c"
FROM "CLARITY"."clarity_adt" aaa
inner join "CLARITY"."clarity_dep" "CLARITY_DEP" ON aaa."department_id" = "clarity_dep"."department_id"
inner join "CLARITY"."patient" "PATIENT" ON aaa."pat_id" = "patient"."pat_id"
inner join "CLARITY"."zc_ped_delivr_meth" "ZC_PED_DELIVR_METH" ON "patient"."ped_delivr_meth_c" = "zc_ped_delivr_meth"."ped_delivr_meth_c"
WHERE ( "patient"."birth_date" >= To_date ('01-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
AND "patient"."birth_date" < To_date ('06-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS' ) )
AND ( aaa."department_id" IN (236601,236703,236801,236901,237101,237201) )
AND aaa."event_type_c" = 3
) q
WHERE q.rn = 1
In the end, I would even simplify it further by aliassing all tables and use a WITH statement. The final result would look like this
SQL Statement 3
;WITH q AS (
SELECT ROW_NUMBER() OVER (PARTITION BY pat_id ORDER BY effective_time) AS rn,
aaa."effective_time",
aaa."event_type_c",
cd."department_name",
p."birth_date",
p."pat_mrn_id",
pdm."name",
pdm."ped_delivr_meth_c",
aaa."department_id",
cd."department_id",
aaa."alt_event_type_c",
aaa."in_event_type_c"
FROM "CLARITY"."clarity_adt" aaa
inner join "CLARITY"."clarity_dep" cd ON aaa."department_id" = cd."department_id"
inner join "CLARITY"."patient" p ON aaa."pat_id" = p."pat_id"
inner join "CLARITY"."zc_ped_delivr_meth" pdm ON p."ped_delivr_meth_c" = pdm."ped_delivr_meth_c"
WHERE p."birth_date" >= To_date ('01-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS'
AND p."birth_date" < To_date ('06-12-2012 00:00:00', 'DD-MM-YYYY HH24:MI:SS' ) )
AND aaa."department_id" IN (236601,236703,236801,236901,237101,237201)
AND aaa."event_type_c" = 3
)
SELECT *
FROM q
WHERE rn = 1
You're only going to get rows if the row with the earliest date also matches the other conditions on clarity_adt. If you want the earliest record for that subset then you'd have to push those conditions into the subquery.