Case Statement with comparison - mysql

In the existing statement below I am comparing the host_name and ip_adress with records retrieved from an other table and mark the column as true when found, but I would like to tweak it to give a true result when the host_name is also a part of identifer from the record retrieved
i.e
host_name=abc
identifier=abc.google.com should also be true
CASE
WHEN
(
(`inv_y`.`host_name` <> '')
AND
`inv_y`.`host_name` IN (SELECT `inv_x`.`identifier`
FROM `inv_x`
ORDER BY `inv_x`.`creation_date` DESC
)
)
OR
(
(`inv_y`.`ip_address` <> '')
AND
inv_y`.`ip_address` IN (SELECT `inv_x`.`identifier`
FROM `inv_x`
ORDER BY `inv_x`.`creation_date` DESC
)
)
THEN
'True'
ELSE
'False'
END

It looks like you could benefit from the EXISTS function. Something like the following could work:
SELECT CASE WHEN EXISTS(SELECT 1
FROM inv_y
WHERE (ip IN (SELECT identifier
FROM inv_x)
OR hostname IN (SELECT identifier
FROM inv_x))
AND hostname LIKE '%eb%'
) = 1 THEN "True"
ELSE "False"
END AS Present;
Here's a demo of this working: SQL Fiddle
Note, the above could be simplified further if you can work with a 1 or 0 instead of true or false. EXISTS returns a 1 if the row is found, and 0 if not, and the CASE is there simply to translate this into 'True' or 'False', so you could simply have the following:
SELECT EXISTS(SELECT 1
FROM inv_y
WHERE (ip IN (SELECT identifier
FROM inv_x)
OR hostname IN (SELECT identifier
FROM inv_x)
)
AND hostname LIKE '%eb%') AS Present;

Related

MySQL Query optimize for performance

Could you please optimize this query for me:
SELECT
case when EXISTS (
SELECT
1
FROM
usergroups item_t1
WHERE
(item_t0.p_b2bunits = item_t1.p_uid)
AND (item_t1.TypePkString = 8796095217746)
) THEN true ELSE false END as active,
item_t0.p_b2bunits as code,
item_t0.p_accountname as name1,
item_t0.p_accountnameextension as name2,
case when EXISTS (
SELECT
1
FROM
user2salreprelation item_t2
WHERE
(
item_t2.SourcePK = item_t0.PK
and item_t2.TargetPK = 8828959653892
)
AND (item_t2.TypePkString = 8796150399058)
) THEN true ELSE false END as isFav
FROM
account2salesrep item_t0
WHERE
(
item_t0.p_showondashboard = 1
AND item_t0.p_district = '4'
AND (
LOWER(item_t0.p_accountname) LIKE CONCAT('%', CONCAT('crampel alu', '%'))
OR LOWER(item_t0.p_accountnameextension) LIKE CONCAT('%', CONCAT('crampel alu', '%'))
OR LOWER(item_t0.p_b2bunits) LIKE CONCAT('%', CONCAT('crampel alu', '%'))
)
)
AND (item_t0.TypePkString = 8796149842002)
order by
item_t0.p_accountname
LIMIT
0, 11;
Typing If TypePkString is a VARCHAR, then you must quote 8796150399058, else performance will be really bad. (The opposite situation does not matter.)
true/false A boolean expression (such as EXISTS(...)) return 0 for false and 1 true, so this
CASE WHEN (...) THEN true ELSE false AS ...
can be simplified
(...) AS ...
Some of these indexes may help:
item_t0: INDEX(p_showondashboard, p_district, TypePkString, p_accountname)
item_t1: INDEX(p_uid, TypePkString)
item_t2: INDEX(SourcePK, TargetPK, TypePkString)
Unnecessary LOWER Assuming that p_accountname has a collation ending in _ci [Please provide SHOW CREATE TABLE], you can remove LOWER() as being unnecessary and slow.
LIKE with a leading wildcard (%) defies indexing. Have you considered using FULLTEXT (if the users enter "words", not random strings). It would involve
FULLTEXT(p_accountname, p_accountnameextension, p_accountnameextension)
MATCH(p_accountname, p_accountnameextension, p_accountnameextension)
AGAINST('+crampel alu' IN BOOLEAN MODE)
Is crampel alu a column name? A prefix? Something else?

MyBatis select statement returns null, but MySQL execute returns has values

I'm trying to run a somewhat complex sql statement using the 'group_concat' function for statistics.
When I run the sql statement using navicat, the correct result is returned.But when I use mybatis debug this sql in idea, it returns null value.
I use HashMapHashMap<String,Long> to receive the result set executed by mybatis,
I guess the result set returned by mybatis executing sql is wrongly encapsulated.
But I tried for a long time and couldn't find a solution.
I can't post pictures here,I display the results in a table.
cha
hege
lianghao
youxiu
1
1
2
1
Receive declaration at the mapper layer
HashMap<String, Long> getOption(#Param("column") String column);
Here is the sql code:
<select id="getOption" resultType="java.util.HashMap">
SELECT GROUP_CONCAT(form.cha SEPARATOR '') cha,
GROUP_CONCAT(form.hege SEPARATOR '') hege,
GROUP_CONCAT(form.lianghao SEPARATOR '') lianghao,
GROUP_CONCAT(form.youxiu SEPARATOR '') youxiu
FROM (
SELECT
( CASE WHEN #{column} = '0' THEN count(*) ELSE NULL END) AS 'cha',
( CASE WHEN #{column} = '1' THEN count(*) ELSE NULL END ) AS 'hege',
( CASE WHEN #{column} = '2' THEN count(*) ELSE NULL END ) AS 'lianghao',
( CASE WHEN #{column} = '3' THEN count(*) ELSE NULL END ) AS 'youxiu'
FROM corp_feedback_student
GROUP BY #{column}
) form
</select>
Thanks everyone for helping me!!

Subquery returned more than 1 value.4...Different query

Hello I have this query that i am trying to execute and i keep getting this error "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.", Kindly help please.
DECLARE #NUMCOUNT BIT
Select #NUMCOUNT = (SELECT
CASE WHEN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ('A')
) IN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ( 'A','C') ) THEN 1 else 0 END AS NUMCOUNT1
FROM R5REQUISLINES JOIN
R5REQUISITIONS ON R5REQUISLINES.RQL_REQ = R5REQUISITIONS.REQ_CODE
GROUP BY R5REQUISLINES.RQL_REQ, R5REQUISITIONS.REQ_CODE,R5REQUISLINES.RQL_STATUS
)
IF #NUMCOUNT = '1'
begin
UPDATE R5REQUISITIONS
SET R5REQUISITIONS.REQ_STATUS = 'CP'
end
Ok, it sounds like what you actually want to do is update R5REQUISITIONS when there is no RQL_STATUS = 'C' in R5REQUISLINES, since you said you want to count the records where the RQL_STATUS is A and where it's A or C, and then do the update if the counts are the same.. You can greatly simplify this task with the following query:
UPDATE r5
SET r5.REQ_STATUS = 'CP'
FROM R5REQUISITIONS r5
WHERE NOT EXISTS (SELECT 1 FROM R5REQUISLINES r5q WHERE r5q.RQL_REQ = r5.REQ_CODE AND r5q.RQL_STATUS = 'C')
Your 'SELECT CASE' is returning more than 1 record, so it can't be assigned to #NUMBER. Either fix the sub-query to only return the record your looking for or hack it to return only 1 with a 'LIMIT 1' qualification.
I don't know what your data looks like so I can't tell you why your case subquery returns more records than you think it should.
Try running this and see what it returns, that will probably tell you wall you need to know:
SELECT
CASE WHEN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ('A')
) IN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ( 'A','C')
)
THEN 1
ELSE 0
END AS NUMCOUNT1
FROM R5REQUISLINES JOIN
R5REQUISITIONS ON R5REQUISLINES.RQL_REQ = R5REQUISITIONS.REQ_CODE
GROUP BY R5REQUISLINES.RQL_REQ, R5REQUISITIONS.REQ_CODE,R5REQUISLINES.RQL_STATUS
If there is more than 1 row returned, that's where your problem is.

SSRS - Display Blank Values

Parameter Values
I am trying to display all the records with any status. When the user select 'AllLogs' from the dropdwon in report.
But when I run the report it is excluding the records with blank
status.
How can i pull the records with any status(blank,NULL,Approved,etc) ?
Label: Status
Value: %
I have also enabled NULL and Blank values in the parameter properties.
This is my where clause.
( ( l.Status LIKE '%' + #status + '%' ) OR
( l.Status LIKE #status ) OR
( LTRIM(RTRIM(l.Status))='') OR
( l.status IS NULL ) )
The most surefire way to include all records is to simply not filter in your where, which can be achieved with a short-circuiting case statement:
where case when #status = '%' -- This can be any value, such as 'All'
then 1
else case when l.Status = #status
then 1
else 0
end
end = 1
Whilst this is a little verbose, it has the added benefit of not actually running the comparison if your All option is selected, which will improve query performance.

SELECT STATEMENT IN A CASE STEMENT INSIDE A SELECT STATEMENT

I am trying to display a field value based on the value of field and then find a external table record.
can I do it?
SELECT
CASE
WHEN (dsp_notes IS NOT NULL) THEN '*'
WHEN (dsp_notes IS NULL) THEN ''
ELSE ''
END,
CASE
WHEN (dsp_priority = '1') THEN [SELECT uvi_value FROM PUB.universalinfo WHERE uvi_key = 'DSP01SHORT']
Is this possible?
Yes. This is called a scalar subquery and it needs to return one column and one row:
(CASE WHEN dsp_priority = '1'
THEN (SELECT ui.uvi_value FROM PUB.universalinfo ui WHERE ui.uvi_key = 'DSP01SHORT')
END) as NewCol
I strongly encourage you to use table aliases on your column references.