I want to write a select statement that should filter data based on wildcard characters. I have written something like this but it doesn't serve my purpose:
Select r.CompanyID,r.Description,c.BusinessUnitID,c.BusinessSourceID as BusinessSrcID,
c.Description as BusinessDesc from RCompanyTable r
join CBusinessUnitTable c on r.CompanyID=c.CompanyID
WHERE r.CompanyID like CASE WHEN COALESCE('Regexp(*)', '') = '' THEN r.CompanyID ELSE 'Company2' END
But in this it always executes the else part.
What i am looking for is it should give me all data when i pass * to the condition.
Since in my RCompanyTable i have two records Company1 and Company2, I want that if i pass * in that query then it should return me both company1 and company2 data but if i pass regexp(any1) it should provide me Comapany1 Data and if both conditions are not true then it should go to else part displaying Company2 data
Looking forward to your answer.
Thanks in advance
I don't know exactly what you want with * and regexp(any1), but assuming that they are constant strings, then this query should work:
SET #parameter = '';
Select r.CompanyID
, r.Description
, c.BusinessUnitID
, c.BusinessSourceID AS BusinessSrcID
, c.Description AS BusinessDesc
FROM RCompanyTable r
JOIN CBusinessUnitTable c ON r.CompanyID = c.CompanyID
WHERE (#parameter LIKE '%*%' AND r.CompanyID IN ('Company1', 'Company2'))
OR (#parameter LIKE 'regexp(any1)' AND r.CompanyID = 'Company1')
OR (#parameter NOT IN ('%*%', 'regexp(any1)') AND r.CompanyID = 'Company2')
The #parameter is what you are going to pass in the query.
WHERE r.CompanyID = CASE WHEN r.CompanyId LIKE '%*%' THEN r.CompanyID ELSE 'Company2' END
This where clause returns all records where companyID has a * in it, or the companyID is Company2. Is this what you are after?
Related
I have several stored procedures that are almost identical but have some different AND parts inside a WHERE clause.
Based on a variable deptname, I want to add additional AND/OR conditions to my already existing WHERE clause. So kind of like IF/CASE WHEN on the part that is different.
Think about it as string concatenation
query_string = 'WHERE a= XYZ AND B= 123"
if deptname = a: query_string + "AND additional conditions for dept a"
else if deptname = b:query_string + "AND additional conditions for dept b"
What is the appropriate way to use a variable?
here is some pseudo code of what I am trying to do
SELECT
personID AS pid,
personcode,
persondeptcode,
more_fields AS fields
FROM
TABLE_XYZ
WHERE
--shared parts
personcode = 'C'
AND
persondeptcode = 'MAJ'
--- NOW the different part
IF #deptname = "deptA"
AND
(
PROGRAM_LDESCR IN
(
'prog1',
'prog2',
'prog3'
)
OR
aprogram IN ('aprogram1')
OR
(aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123'))
);
--- THIS IS A DIFFERENT DEPT SO WE HAVE DIFFERENT AND PART
ELSE IF #deptname = "deptB"
(
PROGRAM_LDESCR IN
(
'1234'
)
OR
aprogram IN ('a1234')
);
You can use a CASE expression in this case, the important thing is to make sure you have an ELSE clause to ensure the expression remains true if #deptname is not one of the two values with extra conditions:
WHERE personcode = 'C'
AND persondeptcode = 'MAJ'
AND (CASE #deptname
WHEN "deptA" THEN PROGRAM_LDESCR IN ('prog1', 'prog2', 'prog3')
OR aprogram IN ('aprogram1')
OR aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123')
WHEN "deptB" THEN PROGRAM_LDESCR IN ('1234')
OR aprogram IN ('a1234')
ELSE 1
END)
Here is a simple demo of a CASE expression used in this fashion.
You seem to want something like:
AND
(#deptname = 'dept123' AND (PROGRAM_LDESCR IN ('1234') OR aprogram IN ('a1234')) OR
#deptname <> 'dept123'
)
To combine the last part of the WHERE clause (if I'm understanding your commented-code correctly), you could do something like the following:
SELECT
personID AS pid,
personcode,
persondeptcode,
more_fields AS fields
FROM
TABLE_XYZ
WHERE
personcode = 'C'
AND persondeptcode = 'MAJ'
AND (
(#deptname="deptA" AND (PROGRAM_LDESCR IN ('prog1', 'prog2', 'prog3') OR aprogram IN ('aprogram1') OR (aprogram IN ('aprogram2') AND PLAN_LDESCR IN ('plan123'))))
OR
(#deptname="deptB" AND (PROGRAM_LDESCR IN ('1234') OR aprogram IN ('a1234'))
)
Normally you would use the WHERE clause to filter out unnecessary rows of data and a CASE statement if you wanted to actually change the value in the SELECT statement (I rarely see CASE statements outside a SELECT clause, unless it is doing something like a complex sort).
Is it possible to compare the result of a query to a string in MySQL? Something like :
select case when 'Captain' = (select role from roleassociation ra
inner join users urs
on ra.userentityid = urs.invuserid
where invuserid = 007)
then 'true' else 'false' end as result;
The above query is incorrect, but is it possible to implement? Thanks in advance.
I would probably just put the CASE expression in the subquery:
SELECT
CASE WHEN role = 'Captain' THEN 'true' ELSE 'result' END AS result
FROM roleassociation ra
INNER JOIN users urs
ON ra.userentityid = urs.invuserid
WHERE
invuserid = 007;
Assuming you expect the result to be only a single record, you will still have just a single result with this approach.
I am not so into SQL and I have the following problem woring on a MySql query. I try to explain you what I have to do.
I have this query, it works fine:
SELECT
LNG.id AS language_id,
LNG.language_name AS language_name,
LNG.language_code AS language_code,
CLP.is_default AS id_default_language
FROM Country_Language_Preference AS CLP
INNER JOIN Country AS CNT
ON CLP.country_id = CNT.id
INNER JOIN Languages AS LNG
ON CLP.language_id = LNG.id
WHERE
CNT.country_name = "Senegal"
This query have a single WHERE input parameter, this:
CNT.country_name = "Senegal"
I want to implement the following behavior: if the passed parameter have value Senegal or Rwanda perform the previous query.
If this input parameter have a different value form Senegal or Rwanda perform the same query but using this WHERE condition_
CNT.country_name = "GLOBAL"
Can I do something like this using SQL?
Using CASE Statement, this should be possible.
Try this:
SELECT
LNG.id AS language_id,
LNG.language_name AS language_name,
LNG.language_code AS language_code,
CLP.is_default AS id_default_language
FROM Country_Language_Preference AS CLP
INNER JOIN Country AS CNT
ON CLP.country_id = CNT.id
INNER JOIN Languages AS LNG
ON CLP.language_id = LNG.id
WHERE
CNT.country_name = CASE WHEN #Country = "Senegal" OR #Country = "Rwanda" THEN "Senegal"
ELSE "GLOBAL" END
Simply use OR:
WHERE (CNT.country_name = #country OR #country = 'GLOBAL')
#country is whatever parameter you are passing in.
If you want to limit to those two countries, then:
WHERE (CNT.country_name = #country OR
(#country NOT IN ('Rwanda', 'Senegal') AND CNT.country_name = 'GLOBAL')
)
But the first version seems more versatile.
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.
Here is my query:
SELECT
CASE
WHEN hbn.users.showDistance = 'T'
THEN hbn.distance(u2.lat, u2.lon, hbn.users.lat, hbn,users,lon)
ELSE 0
END as distance,
hbn.users.id,
hbn.users.username,
From hbn.users,
(select hbn.users.lat, hbn.users.lon from hbn.users where hbn.users.id = '1') AS u2
where hbn.users.Id = '8';
This does not work!
I need to use output of the second select statement as input for distance() function.
It looks like you have commas instead of full-stops in the last parameter to hbn.distance?