Home » SQL & PL/SQL » SQL & PL/SQL » "Merge" Values in one field with case? (Oracle 11.2 on Windows 2008R2)
icon5.gif   "Merge" Values in one field with case? [message #641303] Thu, 13 August 2015 02:54 Go to next message
forumwurm
Messages: 12
Registered: March 2014
Junior Member
Hi all

I'm quite new to Oracle and SQL and still learning a lot. just could not figure out the statement I need to achieve the following:

There is a table: 0_Calender:

Year Month Day
2014 2 10
2014 4 11
2014 6 12
2014 8 13
2015 1 10
2015 2 11
2015 3 12
2015 4 13
2015 5 14
2015 6 15
2015 7 16
2015 8 17

a table 1_birthday:

Year Month Day Birthday
2013 3 9 Betty
2014 4 11 John Doe
2015 5 12 Jack D.


and a table 2_month:

Year Month Remarks
2014 4 EasterHoliday 14
2015 4 EasterHoliday 15
2015 8 SummerHoliday


With the following query:

SELECT
T0."Year",
T0."Month",
T0."Day",
T1."Birthday",
T2."Remarks"
FROM
User1."0_Calendar" T0
LEFT JOIN User1."2_month" T2 ON T2."Year" = T0."Year"
AND T2."Month" = T0."Month"
LEFT JOIN User1."1_birthday" T1 ON T1."Month" = T0."Month"
AND T1."Day" = T0."Day"

I get the fields T1."Birthday" and T2.Remarks.

Now I'm trying to display those values in only one field with the rule, if there is an entry coming from T1, take this, elsif there is an entry comming from T2, take this, else 'No entry found'.

Please find attached the expeced output.

something like this for an extra field:

CASE
WHEN T0.MONTH||DAY exists in T1.MONTH||DAY THAN T1.Birthday
ELSE WHEN T0.YEAR||MonTH exists in T2.YEAR||MONTH THAN T2.Remarks
ELSE 'No entry found'
END AS FIELDMERGE

Many thanks for hints, tipps, links...
icon4.gif  Re: "Merge" Values in one field with case? [message #641304 is a reply to message #641303] Thu, 13 August 2015 03:00 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

From your previous topic:

John Watson wrote on Sun, 27 July 2014 14:10
...
And please use [code] tags to format data, as described here How to use [code] tags and make your code easier to read


BlackSwan wrote on Sun, 27 July 2014 23:35
...
Please read and follow the forum guidelines, to enable us to help you:
http://www.orafaq.com/forum/t/88153/0/


Michel Cadot wrote on Mon, 28 July 2014 07:07

...We have not your tables and data.
If you want specific query valid for your case you must post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data.


Also if you want to continue to get help have a nice behaviour and feedback and thank people who spent time to help you.
So I summarize:




[Updated on: Thu, 13 August 2015 03:01]

Report message to a moderator

Re: "Merge" Values in one field with case? [message #641305 is a reply to message #641303] Thu, 13 August 2015 03:02 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
If there's no entry in T1 for the row in T0 then the T1 column you're selecting will be null.
Same for the T2 column.
So you just need to do some null checks in the CASE statement.
Or just use a nested nvl instead.
Re: "Merge" Values in one field with case? [message #641316 is a reply to message #641304] Thu, 13 August 2015 05:10 Go to previous messageGo to next message
forumwurm
Messages: 12
Registered: March 2014
Junior Member
Thanks Michel and sorry for my first and worst post ever here. Will try to get this correct the next time.

I couldn't find an edit button, so I would have change the first post.

Anyway. I have now the sql file attached to create the 3 tables.

SELECT
T0."Year",
T0."Month",
T0."Day",
T1."Birthday",
T2."Remarks"
FROM
User1."0_Calendar" T0
LEFT JOIN User1."2_month" T2 ON T2."Year" = T0."Year"
AND T2."Month" = T0."Month"
LEFT JOIN User1."1_birthday" T1 ON T1."Month" = T0."Month"
AND T1."Day" = T0."Day"
Re: "Merge" Values in one field with case? [message #641317 is a reply to message #641305] Thu, 13 August 2015 05:13 Go to previous messageGo to next message
forumwurm
Messages: 12
Registered: March 2014
Junior Member
Hi cookiemonster

thanks, but the NULL values I can replace with the last ELSE statement:

Following is not SQL code, but the logical behavior I'm trying to achieve.
Quote:

CASE
WHEN T0.MONTH||DAY exists in T1.MONTH||DAY THEN T1.Birthday
ELSE WHEN T0.YEAR||MonTH exists in T2.YEAR||MONTH THEN T2.Remarks
ELSE 'No entry found'
END AS FIELDMERGE

[Updated on: Thu, 13 August 2015 05:19]

Report message to a moderator

Re: "Merge" Values in one field with case? [message #641318 is a reply to message #641317] Thu, 13 August 2015 05:21 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
I see nothing in your last post to suggest my answer isn't correct.
Re: "Merge" Values in one field with case? [message #641322 is a reply to message #641318] Thu, 13 August 2015 06:05 Go to previous messageGo to next message
forumwurm
Messages: 12
Registered: March 2014
Junior Member
Cookiemonster, not sure if this is what you suggested but I think this will do the job:

Quote:
SELECT
T0."Year",
T0."Month",
T0."Day",
CASE
WHEN T0."Month" || T0."Day" = T1."Month" || T1."Day" THEN
T1."Birthday"
WHEN T0."Year" || T0."Month" = T2."Year" || T2."Month" THEN
T2."Remarks"
ELSE
'No value'
END AS MERGEFIELD,
T1."Birthday",
T2."Remarks"
FROM
User1."0_Calendar" T0
LEFT JOIN User1."2_month" T2 ON T2."Year" = T0."Year"
AND T2."Month" = T0."Month"
LEFT JOIN User1."1_birthday" T1 ON T1."Month" = T0."Month"
AND T1."Day" = T0."Day"




Question is, is there something I can optimize (performance wise). Because in the real world I have to create this view or later a stored procedure for hundred of thousands records.

[Updated on: Thu, 13 August 2015 06:08]

Report message to a moderator

Re: "Merge" Values in one field with case? [message #641325 is a reply to message #641322] Thu, 13 August 2015 06:32 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
I meant precisely what I said, though your version will work as well.

If you've got this join:
LEFT JOIN User1."1_birthday" T1 ON T1."Month" = T0."Month"
 AND T1."Day" = T0."Day

then the only case where this isn't true:
WHEN T0."Month" || T0."Day" = T1."Month" || T1."Day" 

Is when t1.month and t1.day are null, so it's equivalent to this:
WHEN t1.day is not null


And since it all comes down to two null checks the CASE can be replaced with two nested nvl's.

Past that there's nothing optimizable here.
Re: "Merge" Values in one field with case? [message #641327 is a reply to message #641325] Thu, 13 August 2015 06:33 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And when I say t1.month and t1.day are null I don't mean they're null in t1 itself, rather the outer-join renders them as null because there isn't a matching row in t1 for the current t0.
Re: "Merge" Values in one field with case? [message #641333 is a reply to message #641327] Thu, 13 August 2015 07:36 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Oh and if you really have mixed case column names I strongly suggest you stop doing that, it's only going to cause confusion.

[Updated on: Thu, 13 August 2015 07:36]

Report message to a moderator

Previous Topic: insert incremented value
Next Topic: Execute stored procedure after interval
Goto Forum:
  


Current Time: Fri Apr 26 03:17:47 CDT 2024