How to create a view with case statement? - oracle
I have a table "hallowelt" which contains 15 column in which one column name is "month".
how to create a view of the table "hallowelt" in which the months are suitably transformed into a new column SEASON by means of a CASE statement into spring, summer, autumn and winter.
Exemple code:
create view hallo_welt
SELECT month, as 'season'
CASE
when month=3 or month<=5 then 'Frühling'
when month=6 or month<=8 then 'sommer'
when month=9 or month<=11 then 'Herbst'
when month=12 or month<=2 then 'winter'
else ''
END
from hallowelt;
You probably want something like
create view hallo_welt
SELECT
month,
CASE
when month in (3,4,5) then 'Frühling'
when month in (6,7,8) then 'sommer'
when month in (9,10,11) then 'Herbst'
when month in (12,1,2) then 'winter'
else null
END season
from hallowelt;
Related
Next week in Oracle
I'm using oracle dbms and I have in Employe table a column Birthdate. I want to write a query that shows the employees who has a birthday next week. Is this correct ? select name from employe where to_char(birthdate,'DD-MM')=to_char(next_day(sysdate,1)+7,'DD-MM');
That is not the correct usage of next_day(): that function returns the date of the the next instance of a day. For example, to find the date of next Friday: select next_day(sysdate, 'FRIDAY') from dual; To find employees whose birthday is seven days from now, you need to just tweak your query a bit: select name from employe where to_char(birthdate,'DD-MM') = to_char(sysdate+7,'DD-MM');
The correct solution would be SELECT name FROM employe WHERE to_char(birthdate /* "move" the birthdate to the current year to get a reliable week number */ + CAST((EXTRACT(year FROM current_date) - EXTRACT(year FROM birthdate)) || '-0' AS INTERVAL YEAR TO MONTH), 'IW') = to_char(current_date + 7, 'IW'); The IW format returns the ISO week containing the date, which is probably what you are looking for. If you start your week on Sunday, add one to both dates.
Dax - Filter by value then count occurence in table
I am working with an employee table and was wondering if I could get some help. My data table has rows with start and end date values. I filter these rows down using Filter(data table, [start date]<=[Measure.MaxMonth]&&[end date]>=[Measure.MaxMonth]. [Measure.MaxMonth] is a measure that sits on a disconnected date table and functions like a parameter. Here is a formula that I have been testing but have not been getting the desired results: Measure.DirectReports = CALCULATE(COUNTROWS(Data Table),filter(Data Table,Data Table[Mgr ID]=Data Table[Emp ID]&&Data Table[Start Date]<=[Meas.LastMonth]&&Data Table[End Date]>=[Meas.LastMonth])) Measure.LastMonth = max(EOM[End of Month]) ->this value can equal any month end between July 2005 and July 2017. EOM is a date table with a row for each month end - 7/31/2005, 8/31/2005,....7/31/2017 This gives me a table structured liked this: Emp ID,Emp Attr,Mgr ID,Start Date,End Date 1,B,4,10/1/2013,10/6/2013 1,B,4,10/7/2013,12/29/2013 1,B,4,12/30/2013,12/28/2014 1,B,8,12/29/2014,10/4/2015 1,B,8,10/5/2015,12/27/2015 1,B,12,12/28/2015,5/15/2016 1,B,12,5/16/2016,10/2/2016 1,B,12,10/3/2016,12/25/2016 2,B,4,12/1/2014,12/28/2014 2,B,4,12/29/2014,12/27/2015 2,B,4,12/28/2015,2/7/2016 2,B,4,2/8/2016,3/6/2016 2,B,8,3/7/2016,6/1/2016 3,B,6,7/1/2015,12/27/2015 3,B,8,12/28/2015,6/30/2016 3,B,6,7/1/2016,9/4/2016 3,B,6,9/5/2016,12/25/2016 3,B,6,12/26/2016,5/7/2017 3,B,4,5/8/2017,6/11/2017 3,B,4,6/12/2017,6/25/2017 3,B,4,6/26/2017,7/9/2017 3,B,19,7/10/2017,12/31/9999 4,A,,7/1/1996,4/2/2006 4,A,,4/3/2006,12/31/2007 4,A,,1/1/2008,5/22/2011 4,A,,5/23/2011,11/16/2014 4,A,,11/17/2014,6/11/2017 4,A,,6/12/2017,6/25/2017 4,A,,6/26/2017,12/31/9999 5,B,4,11/8/2010,1/2/2011 5,B,4,1/3/2011,5/22/2011 5,B,4,5/23/2011,1/1/2012 5,B,4,1/2/2012,5/31/2012 5,B,4,6/1/2012,7/1/2012 5,B,4,7/2/2012,9/7/2012 6,B,4,1/3/2011,5/22/2011 6,B,4,5/23/2011,9/5/2011 6,B,4,9/6/2011,1/1/2012 6,B,4,1/2/2012,12/30/2012 6,B,4,12/31/2012,12/29/2013 6,B,4,12/30/2013,5/18/2014 6,B,4,5/19/2014,11/16/2014 6,B,4,11/17/2014,12/28/2014 6,B,4,12/29/2014,3/22/2015 6,B,4,3/23/2015,12/27/2015 6,B,4,12/28/2015,3/6/2016 6,B,4,3/7/2016,8/21/2016 6,B,4,8/22/2016,10/30/2016 6,B,4,10/31/2016,12/25/2016 6,B,4,12/26/2016,1/8/2017 6,B,4,1/9/2017,5/7/2017 6,B,4,5/8/2017,6/11/2017 6,B,4,6/12/2017,6/25/2017 6,B,4,6/26/2017,12/31/9999 7,B,4,1/2/2012,12/30/2012 7,B,4,12/31/2012,12/29/2013 7,B,4,12/30/2013,5/18/2014 7,B,4,5/19/2014,11/16/2014 7,B,4,11/17/2014,12/28/2014 7,B,4,12/29/2014,3/8/2015 7,B,4,3/9/2015,1/18/2016 7,B,4,1/19/2016,2/19/2016 8,B,6,12/31/2012,11/3/2013 8,B,4,11/4/2013,12/29/2013 8,B,4,12/30/2013,1/26/2014 8,B,4,1/27/2014,5/18/2014 8,B,4,5/19/2014,11/16/2014 8,B,4,11/17/2014,12/28/2014 8,B,4,12/29/2014,3/22/2015 8,B,4,3/23/2015,12/27/2015 8,B,4,12/28/2015,7/1/2016 10,B,4,10/3/2011,12/18/2011 10,B,4,12/19/2011,12/30/2012 10,B,4,12/31/2012,2/23/2013 10,B,4,2/24/2013,11/20/2014 11,B,4,2/1/2011,2/27/2011 11,B,4,2/28/2011,5/1/2011 12,B,4,9/15/2012,12/31/2012 12,B,4,9/15/2012,12/31/2012 12,B,4,1/1/2013,12/31/2013 12,B,4,1/1/2013,4/30/2014 12,B,4,1/1/2014,4/30/2014 12,B,4,5/1/2014,11/16/2014 12,B,4,5/1/2014,12/28/2014 12,B,4,11/17/2014,11/30/2014 12,B,4,12/1/2014,12/28/2014 12,B,4,12/29/2014,12/27/2015 12,B,4,12/29/2014,12/30/2016 12,B,4,12/28/2015,12/30/2016 12,B,4,12/31/2016,12/31/2016 12,B,4,1/1/2017,6/11/2017 12,B,4,6/12/2017,6/25/2017 12,B,4,6/26/2017,7/9/2017 12,B,19,7/10/2017,12/31/9999 13,B,4,12/28/2015,9/4/2016 13,B,4,9/5/2016,12/25/2016 13,B,4,12/26/2016,6/11/2017 13,B,4,6/12/2017,6/25/2017 13,B,4,6/26/2017,12/31/9999 14,B,4,1/12/2015,12/27/2015 14,B,4,12/28/2015,12/25/2016 14,B,4,12/26/2016,6/11/2017 14,B,4,6/12/2017,6/25/2017 14,B,4,6/26/2017,12/31/9999 16,B,4,9/14/2015,10/19/2015 17,B,6,8/22/2016,12/25/2016 17,B,6,12/26/2016,5/7/2017 17,B,4,5/8/2017,6/11/2017 17,B,4,6/12/2017,6/25/2017 17,B,4,6/26/2017,7/9/2017 17,B,19,7/10/2017,12/31/9999 18,B,6,9/12/2016,12/25/2016 18,B,6,12/26/2016,5/7/2017 18,B,13,5/8/2017,6/11/2017 18,B,13,6/12/2017,6/25/2017 18,B,13,6/26/2017,7/9/2017 18,B,19,7/10/2017,12/31/9999 19,B,4,7/10/2017,12/31/9999 Empl ID is a unique employee number. Mgr ID references the Empl ID of the employee's manager at the desired point in time (Measure.LastMonth). Emp Attr is an attribute of the employee, such as level. Does anyone have any ideas on how to create a measure that will count the occurrences of Empl ID in Mgr ID? Ideally, if I am creating a visual in Power BI and I filter based on Empl Attr ="A", can the resulting measure value give me the result = 3 -> the empl id "1" occurs 3 times in the mgr id column. I need this to be a measure and not a calculated column so that I can trend the results over time (end of month on X axis in trend visual). Thanks for the help and let me know if you have any questions!
Edit - Updating basically the entire answer due to new information about the problem. I feel that your core problem is trying to create a relationship between two tables based on the evaluation of an expression (End of Month between Start Date and End Date). From my experience, the easiest way to workaround this is to CROSSJOIN the two tables and then filter it down based on whatever expression you would like. For this, I created a new table with this formula. Results = DISTINCT( SELECTCOLUMNS( FILTER( CROSSJOIN('Data Table', EOM), 'Data Table'[Start Date] <= EOM[End of Month] && 'Data Table'[End Date] >= EOM[End of Month] ), "Emp ID", [Emp ID], "End of Month", [End of Month] ) ) One more piece before finally making the relationships, we need a list of unique employee IDs. That can easily be obtained by creating a new table with this formula. Employees = DISTINCT( SELECTCOLUMNS('Data Table', "Emp ID", 'Data Table'[Emp ID] ) ) From here, create relationships between all of the tables as shown in the image below. I know you asked for a measure, but bear with me as I feel confident that this will get you what you want. Add a new column to the Results table with this formula. DirectReports = CALCULATE( COUNTROWS('Data Table'), FILTER(ALL('Data Table'), 'Data Table'[Mgr ID] = EARLIER(Results[Emp ID]) && 'Data Table'[Start Date] <= EARLIER(Results[End of Month]) && 'Data Table'[End Date] >= EARLIER(Results[End of Month]) ) ) At this point, I would hide the Emp ID and End of Month from the results table and any other field(s) you desire. From there, make your visuals. For example, you said you wanted to show direct report count over time, so I made this simple line chart and card.
Select Month in Oracle Query
I tried the below query in oracle select cast(TO_DATE (cal.MONTH,'MM') AS varchar2(30)) as result FROM JOBCONTROL_USER.ods_calendar_weeks cal WHERE cal.YEAR NOT IN (0, 9999) it gives result in dd-mon-yy format. Now I want only mon from the result, how can I achieve this without using to_char()?
If you're avoiding Oracle functions and the month number is stored on its own as a varchar2 field, then you could brute-force it: select case cast(month as number) when 1 then 'Jan' when 2 then 'Feb' when 3 then 'Mar' when 4 then 'Apr' when 5 then 'May' when 6 then 'Jun' when 7 then 'Jul' when 8 then 'Aug' when 9 then 'Sep' when 10 then 'Oct' when 11 then 'Nov' when 12 then 'Dec' end as mon from ods_calendar_weeks cal where cal.year not in (0, 9999); But you're having to specify the language; you don't get Oracle's conversion of the month to the NLS date language. Which might be a bonus or a problem depending on your context. I'd be tempted to put the conversions into a look-up table instead and join to that; or to add the month name as a separate column on the table, as a virtual column in 11g.
You can try somthing like this:- SELECT EXTRACT(MONTH FROM CAST(TO_DATE (cal.MONTH,'MM') AS varchar2(30))) as RESULT FROM JOBCONTROL_USER.ods_calendar_weeks cal WHERE cal.YEAR NOT IN (0, 9999) Hope this will help you.
select to_char(cal.Month,'month') ) AS result FROM JOBCONTROL_USER.ods_calendar_weeks cal WHERE cal.YEAR NOT IN (0, 9999); This will gives month. the to_char() is a function which has two arguments 1. Column name ans 2. Month. Column name is of date data type so we have to convert the date into character data type and we required only the month so the second column will describes what will be extracted from the date. Finally the result is displayed as a character datatype. This query will returns the months name if year neither 0 nor 9999.
"BETWEEN" SQL Keyword for Oracle Dates -- Getting an error in Oracle
I have dates in this format in my database "01-APR-12" and the column is a DATE type. My SQL statement looks like this: SELECT DISTINCT c.customerno, c.lname, c.fname FROM customer c, sales s WHERE c.customerno = s.customerno AND s.salestype = 1 AND (s.salesdate BETWEEN '01-APR-12' AND '31-APR-12'); When I try to do it that way, I get this error -- ORA-01839: date not valid for month specified. Can I even use the BETWEEN keyword with how the date is setup in the database? If not, is there another way I can get the output of data that is in that date range without having to fix the data in the database? Thanks!
April has 30 days not 31. Change SELECT DISTINCT c.customerno, c.lname, c.fname FROM customer c, sales s WHERE c.customerno = s.customerno AND s.salestype = 1 AND (s.salesdate BETWEEN '01-APR-12' AND '31-APR-12'); to SELECT DISTINCT c.customerno, c.lname, c.fname FROM customer c, sales s WHERE c.customerno = s.customerno AND s.salestype = 1 AND (s.salesdate BETWEEN '01-APR-12' AND '30-APR-12'); and you should be good to go.
In case the dates you are checking for range from 1st day of a month to the last day of a month then you may modify the query to avoid the case where you have to explicitly check the LAST day of the month SELECT DISTINCT c.customerno, c.lname, c.fname FROM customer c, sales s WHERE c.customerno = s.customerno AND s.salestype = 1 AND (s.salesdate BETWEEN '01-APR-12' AND LAST_DAY(TO_DATE('APR-12', 'MON-YY')); The LAST_DAY function will provide the last day of the month.
The other answers are missing out on something important and will not return the correct results. Dates have date and time components. If your salesdate column is in fact a date that includes time, you will miss out on any sales that happened on April 30 unless they occurred exactly at midnight. Here's an example: create table date_temp (temp date); insert into date_temp values(to_date('01-APR-2014 15:12:00', 'DD-MON-YYYY HH24:MI:SS')); insert into date_temp values(to_date('30-APR-2014 15:12:00', 'DD-MON-YYYY HH24:MI:SS')); table DATE_TEMP created. 1 rows inserted. 1 rows inserted. select * from date_temp where temp between '01-APR-2014' and '30-APR-2014'; Query Result: 01-APR-14 If you want to get all records from April that includes those with time-components in the date fields, you should use the first day of the next month as the second side of the between clause: select * from date_temp where temp between '01-APR-2014' and '01-MAY-2014'; 01-APR-14 30-APR-14
Adding one month to saved date(oracle)
I have a table A which contains a Date type attribute. I want to write a query to select the date in another table B with value one month after the value in A.Any one know how to do it in oracle?
uhm... This was the first hit on google: http://psoug.org/reference/date_func.html It seems you're looking for the "add_months" function.
You need to use the ADD_MONTHS function in Oracle. http://www.techonthenet.com/oracle/functions/add_months.php Additional info: If you want to use this function with today's date you can use ADD_MONTHS(SYSDATE, 1) to get one month from now.
The question is to select a date_field from table b where date_field of table b is one month ahead of a date_field in table a. An additional requirement must be taken into consideration which is currently unspecified in the question. Are we interested in whole months (days of month not taken into consideration) or do we want to include the days which might disqualify dates that are one month ahead but only by a couple of days (example: a=2011-04-30 and b=2011-05-01, b is 1 month ahead but only by 1 day). In the first case, we must truncate both dates to their year and month values: SELECT TRUNC( TO_DATE('2011-04-22','yyyy-mm-dd'), 'mm') as trunc_date FROM dual; gives: trunc_date ---------- 2011-04-01 In the second case we don't have to modify the dates. At least two approaches can be used to solve the initial problem: First one revolves around adding one month to the date_field in table a and finding a row in table b with a matching date. SELECT b.date_field FROM tab_a as a ,tab_b as b WHERE ADD_MONTHS( TRUNC( a.date_field, 'mm' ), 1) = TRUNC( b.date_field, 'mm' ) ; Note the truncated dates. Leaving this out will require a perfect day to day match between dates. The second approaches is based on calculating the difference in months between two dates and picking a calculation that gives a 1 month difference. SELECT b.date_field FROM tab_a as a ,tab_b as b WHERE months_between( TRUNC( b.date_field, 'mm') , TRUNC(a.date_field, 'mm') ) = 1 The order of the fields in months_between is important here. In the provided example: for b.date_field one month ahead of a.date_field the value is 1 for b.date_field one month before a.date_field the value is -1 (negative one) Reversing the order will also reverse the results. Hope this answers your question.