case

Nested if or switch case statement

痴心易碎 提交于 2019-12-13 04:21:41
问题 From the point of view of optimizing the code run time, is there a thumb rule for where to use "nested if" statement and when to use "switch case" statements ? 回答1: I doubt you will ever find a real-life application where the difference between a nested if and a switch case is even worth measuring. Disk access, web access, etc. take many many orders of magnitude more time. Choose what is easiest to read and debug. Also see What is the difference between IF-ELSE and SWITCH? (possible duplicate

not matching when inside mysql case statement is not showing in result

你说的曾经没有我的故事 提交于 2019-12-13 03:55:23
问题 I have an mysql statement as below SELECT CASE WHEN HOUR(created_at) BETWEEN 0 AND 11 THEN 'Morning' WHEN HOUR(created_at) BETWEEN 12 AND 15 THEN 'Afternoon' WHEN HOUR(created_at) BETWEEN 16 AND 18 THEN 'Evening' WHEN HOUR(created_at) BETWEEN 19 AND 24 THEN 'Night' END AS session, SUM(total) AS `total` FROM `orders` WHERE (purchase_date between '2014-05-01' and '2014-05-30') GROUP BY CASE WHEN HOUR(created_at) BETWEEN 0 AND 11 THEN 1 WHEN HOUR(created_at) BETWEEN 12 AND 16 THEN 2 WHEN HOUR

Case returns more than one value with join

元气小坏坏 提交于 2019-12-13 03:40:48
问题 I have a problem when I'm using case statement with a join. I have two tables. Tbl_a: and Tbl_b: I'm running the following query: SELECT tbl_a.id, ( CASE WHEN tbl_b.param_type = 'Ignition' Then param_value WHEN tbl_b.param_type = 'Turn' Then param_value WHEN tbl_b.param_type = 'Speed' Then param_value WHEN tbl_b.param_type = 'Break' Then param_value END ) as value FROM public.tbl_a JOIN public.tbl_b on tbl_b.id = tbl_a.id I want to get for each id in tbl_a the first match from tbl_b. If there

Hours difference between time in sql server

自闭症网瘾萝莉.ら 提交于 2019-12-13 03:37:23
问题 I am trying to get the hours between the two dates using DATEDIFF(HOUR, FromTime, ToTime) but when the time is 00:00:00. And the I got the negative and wrong hours. Code Below: select FromTime, ToTime, datepart(HH, FromTime) as fromtimehours, datepart(HH, ToTime) as totimehours, DATEDIFF(HOUR, FromTime, ToTime) as totalhours from table 回答1: How about a case statement to catch if it is a negative value and then calculate properly: CASE WHEN DATEDIFF(HOUR, FromTime, ToTime) < 0 THEN 24 -

How to use mutually exclusive flags in your shell and add an optional argument flag ( stuck with getopts)

天大地大妈咪最大 提交于 2019-12-13 02:36:14
问题 I am using a standard getopts logic. But I want to how I can make the options I offer- mutually exclusive. e.g. shell.sh -a SID <accepted> shell.sh -b SID <accepted> shell.sh -ab SID Message- using ab together is the same as running shell.sh without any options supplying just SID . Help usage < ya da ya > shell.sh Please enter SID at the minimum. Usage < ya da ya > shell.sh SID <accepted> I am trying to develop this logic using something like below while getopts ":a:b:" opt; do case $opt in a

Invalid number in Oracle SQL Case

此生再无相见时 提交于 2019-12-13 02:26:51
问题 Hi im habing trouble with a SQL case, the problem is im trying to run a case with 7 different columns, the columns can have different kinds of data (string,date,number) depending on an id. This means that under some id's the rows in a column will be string while under other ids the rows in a column will be number. I realise this isn't a conventional use of astructured database, but this specific table serve a specific purpose where this approach was deemed usefull in the past. The case is

CASE expressions with MAX aggregate functions Oracle

倖福魔咒の 提交于 2019-12-13 02:24:09
问题 Using Oracle, I have selected the title_id with its the associated month of publication with: SELECT title_id, CASE EXTRACT(month FROM pubdate) 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' ELSE 'Dec' END MONTH FROM TITLES; Using the statement: SELECT MAX(Most_Titles) FROM (SELECT count(title_id) Most_Titles, month FROM (SELECT title_id,

SQL switch from decode to case

柔情痞子 提交于 2019-12-13 00:35:43
问题 I have a query that does something like this... SELECT * FROM ... ... ORDER BY DECODE(APR(ACC.RATE,'X'), 'FIRST RATE', 1, 'SECOND RATE', 2, 3); Because I am dealing with h2 database and h2 doesnt have decode and decode alias is very difficult to implement so I was wondering if I could convert this DECODE into CASE statement. How do I convert it? 回答1: Decode means: CASE FirstParam WHEN SecondParam THEN ThirdParam WHEN FourthParam THEN FifthParam WHEN SixthParam THEN Seventh... etcetera ELSE

Hive Case Statement for Insert Overwrite Directory

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 18:17:35
问题 When attempting to run an HQL script with the following logic, I receive the error: ParseException line 4:0 cannot recognize input near 'CASE' 'WHEN' 'mytable' in serde properties specification Script Logic INSERT OVERWRITE DIRECTORY '/example/path' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' CASE WHEN ${hiveconf:tbl_name}='mytable' THEN SELECT * FROM ${hiveconf:tbl_name} LEFT OUTER JOIN ...; WHEN ${hiveconf:tbl_name}='mytable2' THEN SELECT * FROM ${hiveconf:tbl_name} LEFT OUTER JOIN ...;

SQL: Group By with Case Statement for multiple fields

放肆的年华 提交于 2019-12-12 15:25:45
问题 I am trying to write a GROUP BY clause with a CASE statement so I can conditionally GROUP BY according to the value of the parameter in my query. Here is my query that I am using (significantly shortened) and I keep getting "RVPname is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. And DVPname is invalid and AEname is invalid. SELECT DVPname, RVPname, AEname, Dealer, Product, Distribution, CASE WHEN @LEVEL = 'DVP' THEN DVPname