i'm using Pentaho and I was wondering if it's possible to do some query like this :
SELECT Something
FROM Somewhere
WHERE (CASE WHEN condition1 = 0 THEN Option IN (Parameter) ELSE
(Option IN (SELECT Option FROM Somewhere_else)) END);
If you want some precision, i want to select everything if my condition is not respected in my WHERE clause (the thing i want to select in the where is different from the original select).
Don't hesitate to ask me for my approach and of course to answer !
Thank you !
PS: The Parameter is a Pentaho parameter which represents here an array
Just use regular conditions:
SELECT Something
FROM Somewhere
WHERE (condition1 = 0 AND Option IN (Parameter))
OR (condition1 != 0 AND Option IN (SELECT Option FROM Somewhere_else));
You should try following,
SELECT Something
FROM Somewhere
WHERE
1 = case when condition1 = 0 THEN
case when Option IN (Parameter) then 1 else 0 end
else
case when Option IN (SELECT Option FROM Somewhere_else) then 1 else 0 end
end
CASE is irrelevant in your statement, and is pretty much irrelevant in the WHERE clause at all.
Use simple conditions as suggested by #X.L.Ant, or you could do some nasty joining like
SELECT *
FROM table a
INNER JOIN somewhere ON (option = 0 AND a.id = somewhere.id)
INNER JOIN parameter ON (option != 0 AND a.id = parameter.id)
Assuming your parameter is somewhat a table.
Related
Is it possible to use CASE statement in WHERE condition like below?
SELECT act.id_activity FROM activity act
LEFT JOIN work w ON w.id_work = act.id_work
WHERE
w.work_type=1
AND w.work_tender in (1,2)
AND act.id_activity_type IN
(CASE WHEN w.work_tender=1 THEN '2,3' WHEN w.work_tender=2 THEN '2,3,4,9' END)
it returns no error but the results always display act.id_activity_type = 2 instead of 2,3 or 2,3,4,9
In this case 1 work (table work) can have many activities (table activity). i want to display activities based on work.work_tender type. if work.work_tender=1 then need to choose activity.id_activity_type IN (2,3). if work.work_tender=2 then need to choose activity.id_activity_type IN (2,3,4,9)
You can try to write correct logic by OR & AND.
SELECT act.id_activity FROM activity act
LEFT JOIN work w ON w.id_work = act.id_work
WHERE
w.work_type=1
AND (
(act.id_activity_type IN ('2','3') AND w.work_tender=1) OR
(act.id_activity_type IN ('2','3','4','9') AND w.work_tender=2)
)
I think case is used for that case. I think it is possible to use in that case. And also for orderby phrase.
Maybe you can try with this:
SELECT *
FROM activity a
JOIN work w
ON w.work_tender=
CASE WHEN a.id_activity_type IN (2,3) THEN 1
WHEN a.id_activity_type IN (2,3,4,9) THEN 2 END;
I'm using CASE expression here to assign value matching w.work_tender if the a.id_activity_type fit the condition. So, if a.id_activity_type IN (2,3) THEN 1 will match with w.work_tender=1 similarly with a.id_activity_type IN (2,3,4,9) THEN 2 will match with w.work_tender=2.
Demo fiddle
I am looking for a (sub-)query and/or If statement in a MYSQL query. After Googling and searching, I THINK it should be possible with a CASE statement, but I am not sure.
My Questions are:
1: is it possible at all?
2: If it is: Could someone point me in the right direction on how to achieve this?
ISSUE:
I have a table with PRODUCTS. Each PRODUCT can have a
-'in_stock' value of 0 or 1 (in stock NO or YES).
-'status' with possible value of 'NULL' or 'DELETED'.
A product that has the status 'DELETED' can NOT be ordered, UNLESS in is in stock.
QUESTION
Is it possible to apply (some kind of) IF statement in a SQL query? Something like:
SELECT
*
FROM
products
WHERE
CASE in_stock = 0
THEN (AND status != 'DELETED')
ELSE ()
I hope someone can help me out. Thanks in advance!
Consider:
SELECT *
FROM products
WHERE
(in_stock = 0 AND status <> 'DELETED') OR
in_stock > 0;
You can use AND and OR in a WHERE clause. The following query gives you all products that are either in stock or have not been deleted (or both):
SELECT *
FROM products
WHERE in_stock = 1 OR status <> 'DELETED';
Another way of writing the same:
SELECT *
FROM products
WHERE NOT (in_stock = 0 AND status = 'DELETED');
You can have many conditions that you combine with AND, OR, and parentheses.
Using this query:
SELECT
linerbltbl.billofladingid,
linerbltbl.grossweighttotal,
linerbltbl.netweighttotal,
linerblwisecontainerdetailstbl.containertype,
linerblwisecontainerdetailstbl.containernumber,
linerblwisecontainerdetailstbl.sealnumber,
linerblwisecontainerdetailstbl.principlecharge
FROM linerbltbl,
linerblwisecontainerdetailstbl
WHERE linerbltbl.shippername IS NOT NULL
AND linerbltbl.billofladingid =
linerblwisecontainerdetailstbl.billofladingid
See this image. It shows what my output IS and what I would like it to be.
You will note that the repeated data is blanked out. Is there a way to do this in my query?
First of all, I think you are going about this wrong. While you CAN do this from within the query itself (see this page) you are probably better off doing it in whatever is consuming the output. For example, if you are taking this data and pasting it into MS Excel, you could check out this post which does is via conditional formatting. If you are consuming it from inside code, you can add logic to handle it there (since I don't know what you are going to do with it, it's hard to add any examples).
But, if you insist on doing it from within SQL it's going to be something to this effect (borrowing from the linked example):
;with t as
(SELECT
linerbltbl.billofladingid,
linerbltbl.grossweighttotal,
linerbltbl.netweighttotal,
linerblwisecontainerdetailstbl.containertype,
linerblwisecontainerdetailstbl.containernumber,
linerblwisecontainerdetailstbl.sealnumber,
linerblwisecontainerdetailstbl.principlecharge
FROM linerbltbl,
linerblwisecontainerdetailstbl
WHERE linerbltbl.shippername IS NOT NULL
AND linerbltbl.billofladingid =
linerblwisecontainerdetailstbl.billofladingid
)
SELECT
CASE WHEN RR = 1 THEN billofladingid ELSE '' END BillOfLadingID,
CASE WHEN RR = 1 THEN grossweighttotal ELSE '' END GrossWeight,
CASE WHEN RR = 1 THEN netweighttotal ELSE '' END NetWeight,
containertype,
containernumber,
sealnumber,
principlecharge
FROM
(SELECT (ROW_NUMBER() OVER(PARTITION BY billofladingid ORDER BY billofladingid, grossweighttotal)) [RR], *
FROM t) SUBQUERY1
For example, if queue_watch shows a 0, I want the actual mysql output to say No, else say Yes. Changing the scheme is not an option.
SELECT user.user_name, queue.queue_name, queue_access.queue_access,
queue_access.queue_watch
FROM queue_access
INNER JOIN user ON user.user_id = queue_access.user_id
INNER JOIN queue ON queue.queue_id = queue_access.queue_id
WHERE queue_access <> '' OR queue_watch = 1
ORDER BY user_name;
The SQL works for what I need, but I like to know if changing the output using SQL rather than PHP is possible. Also, is it possible to group all the queue_names in one row to each user_name so I don't have to have 153 rows returned.
Thanks
It not an over head. It is better to be done in the query, I feel.
Try this -
SELECT user.user_name, queue.queue_name, queue_access.queue_access,
IF(queue_access.queue_watch = 0, 'No', 'Yes')
FROM queue_access
INNER JOIN user ON user.user_id = queue_access.user_id
INNER JOIN queue ON queue.queue_id = queue_access.queue_id
WHERE queue_access <> '' OR queue_watch = 1
ORDER BY user_name;
You can use the standard CASE expression. There are two slightly different ways to write a CASE expression.
Simple case expression:
SELECT
CASE queue_access.queue_watch
WHEN 0 THEN 'No'
ELSE 'Yes'
END AS your_alias,
...
Searched case expression:
SELECT
CASE WHEN queue_access.queue_watch = 0 THEN 'No' ELSE 'Yes' END AS your_alias,
...
Or you could use the MySQL specific IF construct:
SELECT
IF(queue_access.queue_watch, 'Yes', 'No') AS your_alias,
...
You may want to consider what should happen if your value is something other than 0 or 1.
I need to add an if/else stament inside a MySQL query but can't get it to work.
Below is an example with pseudo-code of what I want to accomplish.
SELECT *
FROM calendar
WHERE calendar.alert = 1
IF calendar.repeat = 0 THEN HAVING calendar.UTC_1 > UNIX_TIMESTAMP();
ELSE "get all records regardless of calendar.repeat value";
END IF;
Any suggestions to make this happen? I couldn't find the correct syntax for this MySQL IF ELSE.
Thanks for helping!
SELECT *
FROM calendar
WHERE calendar.alert = 1
AND CASE
WHEN `repeat` =0 THEN UTC_1 > UNIX_TIMESTAMP()
ELSE 1=1 END;
You can use IF-ELSE only inside the body of stored procedure/trigger. You can use IF(condition, value_if_condition_is_true, value_if_condition_is_false) in SELECT, but I prefer CASE(you can easily rewrite the query above to
.... AND IF(`repeat` = 0, UTC_1>UNIX_TIMESTAMP(),1=1)
Not sure I got the syntax all right here, but I like the idea of somethign like this. In my opinion it is much easier to look at in the future and see the two groups you're grabbing with the following example. I'm not educated on efficiency of case vs union in MySQL but it seems to me like the case would be less efficient as well. Maybe someone can answer that for sure?
SELECT *
FROM calendar
WHERE
calendar.alert = 1
AND calendar.repeat = 0
AND calendar.UTC_1 > UNIX_TIMTESTAMP();
UNION
SELECT *
FROM calendar
WHERE
calendar.alert = 1
AND calendar.repeat != 0