I need to write a query based on the following conditions:
Within one table, the following condition must be met:
table name: DLS_RBM_RBI
columns within the table
RBM_3_CD must be blank
If RBM_3_CD is blank, then RBM_2_CD must be 1
If RBM_3_CD is blank and RBM_2_CD is blank, then RBM_1_CD must be 1
How would these conditions be coded in a SQL statement? In an IF within a where clause or case in a select?
I tried using a where clause with these conditions.
AND (D.RBM_3_CD = '')
OR (D.RBM_3_CD = '' AND D.RBM_2_CD = '1')
OR (D.RBM_3_CD = '' AND RBM_2_CD = '' AND RBM_1_CD = '1')
The first statement
AND (D.RBM_3_CD = ' ')
negates the need for the second and third. There's no way for the first statement to false while the second or third are true.
try this instead
where D.RBM_3_CD = ' '
and ( (D.RBM_3_CD = '' AND D.RBM_2_CD = '1')
or (D.RBM_3_CD = '' AND RBM_2_CD = '' AND RBM_1_CD = '1')
)
I need to do a simple query, Select Statement
I want to search in Table all record with value "ValueA, ValueB".
If I use this code, not work well:
String255 valueToFilter;
valueToFilter = 'ValueA, ValueB';
select count (RecId) from MyTable
where MyTable.Field like valueToFilter ;
But not working, I need to keep all record with value "ValueA" or "ValueB", if in the file there is value like : "ValueA, ValueC" I want to get too.
I don't know the number of values (valueToFilter).
Thanks!
From my point of view the easiest way to accomplish this is to split your filter string:
String255 valueToFilterA = 'ValueA';
String255 valueToFilterB = 'ValueB';
;
select count (RecId) from MyTable
where MyTable.Field like valueToFilterA
|| MyTable.Field like valueToFilterB;
If you don't know the number of values you should use query object to add ranges dynamically:
Query query = new Query();
QueryRun queryRun;
QueryBuildDataSource qbds;
QueryBuildRange queryRange;
container conValues;
;
qbds = query.addDataSource(tableNum(MyTable));
for (i = 1; i <= conlen(conValues); i++)
{
queryRange = qbds.addRange(fieldNum(MyTable, Field));
queryRange.value(SysQuery::valueLike(conPeek(conValues, i)));
}
queryRun = new QueryRun(query);
info(strFmt("Records count %1", SysQuery::countTotal(queryRun)));
I have a stored procedures and a few boolean variables (Bit).
I want to put a WHERE statement if a certain variables is False.
I know I could do something like this :
IF (#myBoolean = 1)
BEGIN
SELECT * FROM myTable
END
ELSE
SELECT * FROM myTable WHERE myTable.Foo = 'Bar'
Is there a way to make the WHERE statement optionnal ? Because I have so many boolean variable I don't want to have a different query for each possibilities.
This is what I have in mind (I know it does not work) :
SELECT * FROM myTable
CASE WHEN #myBoolean = 0
THEN WHERE myTable.Foo = 'Bar'
ELSE --Do nothing
SELECT * FROM myTable
where ( #myboolean = 1 and foo = 'bar' ) or #myboolean = 0
I have created a report using Ireport 4.5 but report is running very slow I think just because of multiple UNION and JOINS.
I am copying a simple query for testing purpose:-
SELECT b.Project_Id,
b.Project_Manager,
b.project_title,
b.Project_location,
b.Project_Level,
SUM(COALESCE(b.Project_Budget, 0)) Projected,
SUM(COALESCE(c.Accounting, 0)) Actual
FROM t_authorized_budget a, t_project_c b,t_project_allocation c
WHERE a.Project_Id = b.Project_Id and b.project_id=c.`Key`
and a.Project_Id = c.`Key`
and $X{IN,b.project_location,p_project_location}
and $X{IN,b.project_manager,p_project_manager}
and $X{IN,b.project_id,p_project_id};
So I created a procedure CALL GetAllcompo() using this query but without
$X{IN,b.project_location,p_project_location}
and $X{IN,b.project_manager,p_project_manager}
and $X{IN,b.project_id,p_project_id};
Now i am trying to add these conditions in procedure while calling from Ireport.
How can I do that?
Do you need to use the procedure? I do this by adding another parameter. First, prompt the user to define what type of WHERE clause you're using:
$P{PROJECT_PROMPT}
And then create a second parameter ($P{PROJECT_SQL_DEF}) with a default expression that defines your WHERE clause:
$P{PROJECT_PROMPT} == 'SHORT' ?
" ' a.Project_Id = b.Project_Id
and b.project_id=c.Key
and a.Project_Id = c.Key '" :
" ' a.Project_Id = b.Project_Id and b.project_id=c.`Key`
and a.Project_Id = c.`Key`
and $X{IN,b.project_location,p_project_location}
and $X{IN,b.project_manager,p_project_manager}
and $X{IN,b.project_id,p_project_id} ' "
In your query:
SELECT b.Project_Id,
b.Project_Manager,
b.project_title,
b.Project_location,
b.Project_Level,
SUM(COALESCE(b.Project_Budget, 0)) Projected,
SUM(COALESCE(c.Accounting, 0)) Actual
FROM t_authorized_budget a, t_project_c b,t_project_allocation c
WHERE $P!{PROJECT_SQL_DEF}
OK, the umpteenth conditional column question:
I'm writing a stored proc that takes an input parameter that's mapped to one of several flag columns. What's the best way to filter on the requested column? I'm currently on SQL2000, but about to move to SQL2008, so I'll take a contemporary solution if one's available.
The table queried in the sproc looks like
ID ... fooFlag barFlag bazFlag quuxFlag
-- ------- ------- ------- --------
01 1 0 0 1
02 0 1 0 0
03 0 0 1 1
04 1 0 0 0
and I want to do something like
select ID, name, description, ...
from myTable
where (colname like #flag + 'Flag') = 1
so if I call the sproc like exec uspMyProc #flag = 'foo' I'd get back rows 1 and 4.
I know I can't do the part in parens directly in SQL. In order to do dynamic SQL, I'll have to stuff the entire query into a string, concatenate the #flag param in the WHERE clause and then exec the string. Aside from the dirty feeling I get when doing dynamic SQL, my query is fairly large (I'm selecting a couple dozen fields, joining 5 tables, calling a couple of functions), so it's a big giant string all because of a single line in a 3-line WHERE filter.
Alternately, I could have 4 copies of the query and select among them in a CASE statement. This leaves the SQL code directly executable (and subject to syntax hilighting, etc.) but at the cost of repeating big chunks of code, since I can't use the CASE on just the WHERE clause.
Are there any other options? Any tricky joins or logical operations that can be applied? Or should I just get over it and exec the dynamic SQL?
There are a few ways to do this:
You can do this with a case statement.
select ID, name, description, ...
from myTable
where CASE
WHEN #flag = 'foo' then fooFlag
WHEN #flag = 'bar' then barFlag
END = 1
You can use IF.
IF (#flag = 'foo') BEGIN
select ID, name, description, ...
from myTable
where fooFlag = 1
END ELSE IF (#flag = 'bar') BEGIN
select ID, name, description, ...
from myTable
where barFlag = 1
END
....
You can have a complicated where clause with a lot of parentheses.
select ID, name, description, ...
from myTable
where (#flag = 'foo' and fooFlag = 1)
OR (#flag = 'bar' and barFlag = 1) OR ...
You can do this with dynamic sql:
DECLARE #SQL nvarchar(4000)
SELECT #SQL = N'select ID, name, description, ...
from myTable
where (colname like ''' + #flag + 'Flag'') = 1'
EXECUTE sp_ExecuteSQL #SQL, N''
There are more, but I think one of these will get you going.
"Alternately, I could have 4 copies of the query and select among them in a CASE statement."
You don't need to copy your entire query 4 times, just add all the possibilities into the where clauses in your single copy of the query:
select ID, name, description, ...
from myTable
where (#flag = 'foo' and fooFlag = 1) OR (#flag = 'bar' and barFlag = 1) OR ...
What I would do is CASE some variables at the beginning. Example:
DECLARE
#fooFlag int,
#barFlag int,
#bazFlag int,
#quuxFlag int
SET #fooFlag = CASE WHEN #flag = 'foo' THEN 1 ELSE NULL END
SET #barFlag = CASE WHEN #flag = 'bar' THEN 1 ELSE NULL END
SET #bazFlag = CASE WHEN #flag = 'baz' THEN 1 ELSE NULL END
SET #quuxFlag = CASE WHEN #flag = 'quux' THEN 1 ELSE NULL END
SELECT ID, name, description, ...
FROM myTable
WHERE (fooFlag >= ISNULL(#fooFlag, 0) AND fooFlag <= ISNULL(#fooFlag, 1))
AND (barFlag >= ISNULL(#barFlag, 0) AND barFlag <= ISNULL(#barFlag, 1))
AND (bazFlag >= ISNULL(#bazFlag, 0) AND bazFlag <= ISNULL(#bazFlag, 1))
AND (quuxFlag >= ISNULL(#quuxFlag, 0) AND quuxFlag <= ISNULL(#quuxFlag, 1))
The good thing about this query is that, because the possible values for "flags" are bounded, you can calculate all your conditionals as prerequisites instead of wrapping columns in them. This guarantees a high-performance index seek on whichever columns are indexed, and doesn't require writing any dynamic SQL. And it's better than writing 4 separate queries for obvious reasons.
You could have a parameter for each possible flag column, then check if the parameter is null or the value in the column is equal to the parameter. Then you pass in a 1 for the flags that you want to check and leave the others null.
select id, name, description, ...
from myTable
where (#fooFlag is null or fooFlag = #fooFlag) AND
(#barFlag is null or barFlag = #barFlag) AND
...
Honestly, though, this seems like an ideal candidate for building a dynamic LINQ query and skipping the SPROC once you get to SQL2008.
int should be accepted as varchar value
declare #CompanyID as varchar(10) = '' -- or anyother value
select * from EmployeeChatTbl chat
where chat.ConversationDetails like '%'+#searchKey+'%'
and
(
(0 = CASE WHEN (#CompanyID = '' ) THEN 0 ELSE 1 END)
or
(chat.CompanyID = #CompanyID)
)
working
when the companyID is present , then filtration based on it is done, other wise , filtration is skipped.
where
case when #value<>0 then Field else 1 end
=
case when #value<>0 then #value else 1 end