i am giving query and table column name with datatype.
Column name Data Type
iBasicRate Decimal
iTaxesAndLevies Decimal
vsUnitRate nchar
iAmount Deciaml
vsQuantity Deciaml
First Query Find Unit Rate
Formula is Unit Rate=Basic Rate + Taxes
CASE tBOQ.iBOQLineItemType WHEN '3' THEN Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iBasicRate)) + Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iTaxesAndLevies))
WHEN '5' THEN Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iBasicRate)) + Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iTaxesAndLevies))
ELSE '---'
END as vsUnitRate,
In this query this is not giving add (addition of amount ) it is just append the next amount.
Example :
Basic rate Tax Unit rate(Basic rate+tax)
4500.00 225.00 4500225 (Show like this)
And Second Query Find Amount
Formula is Unit Rate*Quantity=Amount
CASE tBOQ.iBOQLineItemType WHEN '3' THEN CONVERT(varchar,tBOQ.iAmount)
WHEN '5' THEN CONVERT(varchar,tBOQ.iAmount)
ELSE '---'
END as iAmount ,
Maybe something like this?:
CASE WHEN tBOQ.iBOQLineItemType in ('3', '5') THEN tBOQ.iBasicRate +
tBOQ.iTaxesAndLevies
ELSE 0
END as vsUnitRate,
CASE WHEN tBOQ.iBOQLineItemType in ('3', '5') THEN (tBOQ.iBasicRate +
tBOQ.iTaxesAndLevies) *
tBOQ.vsQuantity
ELSE 0
END as iAmount ,
Related
I have created the below code:
with t as (select *,
case
when `2kids`= '1' then '2kids' else'' end as new_2kids,
case
when `3kids`= '1' then '3kids' else'' end as new_3kids,
case
when kids= '1' then 'kids' else'' end as kids
from test.family)
select concat_ws('/',new_2kids, new_3kids, new_kids) as 'nc_kids'
from t;
If I run this query my output will be:
nc_kids
2kids/new_3kids/
2kids//
/new_3kids/new_kids
2kids/new_3kids/new_kids
How can I remove all the unnecessary '/' which not followed by character.
For example:
nc_kids
2kids/new_3kids
2kids
new_3kids/new_kids
2kids/new_3kids/new_kids
concat_ws() ignore nulls, so you can just turn the empty strings to null values at concatenation time:
select concat_ws('/',
nullif(new_2kids, ''),
nullif(new_3kids, ''),
nullif(new_kids, '')
) as nc_kids
from t;
Better yet, fix the case expressions so they produce null values instead of empty stings in the first place:
with t as (
select f.*,
case when `2kids`= 1 then '2kids' end as new_2kids,
case when `3kids`= 1 then '3kids' end as new_3kids,
case when kids = 1 then 'kids' end as kids
from test.family f
)
select concat_ws('/',new_2kids, new_3kids, new_kids) as nc_kids
from t;
I have a problem which I think relates to having a multiple value parameter.
In my TblActivity there are two fields TblActivity.ActivityServActId and TblActivity.ActivityContractId which I want to include in my WHERE statement.
Filtering by these is optional. If the user selects 'Yes' for the parameter #YESNOActivity, then I want to filter the query looking for rows where TblActivity.ActivityServActId matches one of the options in the parameter #ServiceActivity.
The same goes for the #YESNOContract, TblActivity.ActivityContractId and #Contract respectively
I managed to get to this:
WHERE
(CASE WHEN #YESNOActivity = 'Yes' THEN TblActivity.ActivityServActId ELSE 0 END)
IN (CASE WHEN #YESNOActivity = 'Yes' THEN #ServiceActivity ELSE 0 END)
AND (CASE WHEN #YESNOContract = 'Yes' THEN TblActivity.ActivityContractId ELSE 0 END)
IN (CASE WHEN #YESNOContract = 'Yes' THEN #Contract ELSE 0 END)
However, although this code works fine if there is only one value selected in the parameter #ServiceActivity or #Contract, as soon as I have more than one value in these parameters, I get the error:
Incorrect syntax near ','.
Query execution failed for dataset 'Activity'. (rsErrorExecutingCommand)
An error has occurred during report processing. (rsProcessingAborted)
Can anyone see what I'm doing wrong? I could understand it if I had an = instead of IN in the WHERE statement but can't figure this one out.
Using SQL Server 2008 and SSRS 2008-r2
If your #ServiceActivity is something like 1,2,3
You can do something like this
WHERE `,1,2,3,` LIKE `%,1,%`
So you format your variables
WHERE ',' + #ServiceActivity + ',' LIKE '%,' + ID + ',%'
SQL FIDDLE DEMO
SELECT *
FROM
(SELECT '1,2,3,4' as X UNION ALL
SELECT '2,3,4,5' as X UNION ALL
SELECT '3,4,5,6' as X UNION ALL
SELECT '1,3,4,5' as X
) as T
WHERE ',' + X + ',' LIKE '%,1,%'
For Your Case
(CASE WHEN #YESNOActivity = 'Yes'
THEN ',' + #ServiceActivity + ','
ELSE NULL
END)
LIKE
(CASE WHEN #YESNOActivity = 'Yes'
THEN '%,' + TblActivity.ActivityServActId + ',%'
ELSE 0
END)
In SQL, the IN clause does not support parameters the way you are using them. The general syntax is
IN (1, 2, 3, 4)
you have
IN (#Param)
where something like
#Param = '1, 2, 3, 4'
Internally, SQL will turn this into
IN ('1, 2, 3, 4')
Note the quotes... you are now matching against a string!
There are a number of ways to address this. Search SO for "sql in clause parameter", pick one that works for you, and upvote it.
(Added)
Parameterize an SQL IN clause seems pretty definitive on the subject. While long ago I upvoted the third reply (the one with table-value parameters), any of the high-vote answers could do the trick. The ideal answer depends on the overall problem you are working with. (I am not familiar with SSRS, and can't give more specific advice.)
So after a lot of messing around I put together a simple workaround for this by dropping my use of CASE altogether - but I have a suspicion that this is not a terribly efficient way of doing things.
WHERE
(#YESNOActivity = 'No' OR (#YESNOActivity = 'Yes' AND
TblActivity.ActivityServActId IN (#ServiceActivity)))
AND
(#YESNOContract = 'No' OR (#YESNOContract = 'Yes' AND
TblActivity.ActivityContractId IN (#Contract)))
i am trying to calculate the values for a new column impressions which is equal to the sum of ( sum_retweet and sum_reply ) multiply by the value in the column followers_count. so for the first row it would be : (2+1)*812 but on condition that the total sum of sum_retweet and sum_reply must be greater than zero. If the sum is equal to zero than impressions will = to just the followers_count.
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count, Reach
'9', '2008-06-11', 'A', '2', '1', '812', '1624'
'9', '2008-06-12', 'B', '0', '1', '813', '813'
Here is my current code:
CREATE VIEW `tweet_sum` AS
select `tweets`.`account_id` AS `account_id`,
`tweets`.`user_screenname` AS `user_screenname`,
CAST(`tweets`.`datetime` as date) AS `period`,
MAX(`tweets`.`followers_count`) AS `followers_count`,
SUM(`tweets`.`is_reply`) AS `sum_reply`,
SUM(`tweets`.`is_retweet`) AS `sum_retweet`,
MAX(`tweets`.`followers_count`) * ((SUM(`tweets`.`is_reply`) > 0) + (SUM(`tweets`.`is_retweet`) > 0)) as reach
from `tweets`
group by cast(`tweets`.`datetime` as date), tweets.username;
HOw do i add the calculations for the impressions column in?
Use a case statement for this:
case when SUM(`tweets`.`is_reply`) + SUM(`tweets`.`is_retweet`) > 0
then (SUM(`tweets`.`is_reply`) + SUM(`tweets`.`is_retweet`))
* `tweets`.`followers_count`
else `tweets`.`followers_count` END as newColumn
This would do it in SQL, but this syntax may not work in mysql (dont know):
CASE WHEN (Sum_Retweet + Sum_Reply) > 0
THEN (Sum_Retweet + Sum_Reply) * followers_count
ELSE followers_count
END
Field names would need to be changed to your actual field names.
i want to use a if condition in a sql query according to following need.
"if a year field is null then do not calculate age and if it is set then it have to execute.
here is my query.where is the problem?please consider this scenario
'if month and date is there like for example 0000-03-12'
SELECT id, name, birth_date, birth_time,
city_native, country_native, sex,
city_current, country_current, image,
if(YEAR(birth_date)='','',YEAR(CURDATE()) - YEAR(birth_date) -
(RIGHT(CURDATE(),5) < RIGHT(birth_date,5)),'') AS age
FROM birth_user u
WHERE <condition>;
You can use IFNULL() to check birth_date is NULL
CASE WHEN IFNULL(birth_date,0)=0 THEN '' ELSE YEAR(CURDATE()) - YEAR(birth_date) END as age
If your need is "if it is null", why are you comparing it to zero?
if(birth_date is null, 0, YEAR(CURDATE()) - ......) AS age
Where that 0 there is a suitable "default age".
Use NULL instead of 0000
SELECT id, name, birth_date, birth_time,
city_native, country_native, sex,
city_current, country_current, image,
if(birth_date is null,'',YEAR(CURDATE()) - YEAR(birth_date) -
(RIGHT(CURDATE(),5) < RIGHT(birth_date,5)),'') AS age
FROM birth_user u
WHERE " . implode(' AND ', $where);
Hi I'm sure this is a simple fix but I cannot figure it out. I'm trying to label records that are overdue the completion date (CompleteDate-CurrentDate) (these numbers will be negatives) to "Overdue" for a report. I would also like the records to not be altered for the numbers that are not negatives. Here is a snippet of the code which is currently giving me NULL entries
Select CASE DATEDIFF(targetcompletedate, NOW())
When count(*) <=0 then 'Overdue'
END 'Days Left',
Any help would be greatly appreciated
Thanks
There are two variants of CASE:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result_else
END
CASE scalar_expression
WHEN value1 THEN result1
WHEN value2 THEN result2
...
ELSE result_else
END
In your case, it should be the first syntax, because you are not comparing to specific values but to a range. Instead, your query is actually using the second syntax. The count(*)<=0 expression is evaluated to a boolean which is then implicitly converted to an integer, the type implied by the DATEDIFF result.
You just need to use the first syntax, something like this:
select case when targetcompletedate is null
then 'Not set'
when DATEDIFF(targetcompletedate, NOW()) <= 0
then 'Overdue'
else DATEDIFF(targetcompletedate, NOW())
end as 'Days Left'
I would suggest that you eliminate the datediff() entirely:
Select (CASE when targetcompletedate <= NOW() the 'Overdue' else 'Days Left' end)
If you want to show things as numbers, then you want the datediff(). For clarity, I would explicitly convert to character strings:
select (case when targetcompletedate <= NOW() then 'Overdue'
else cast(DATEDIFF(targetcompletedate, NOW()) as varchar(255))
end)
Or, perhaps:
select (case when targetcompletedate <= NOW() then 'Overdue'
else concat(DATEDIFF(targetcompletedate, NOW()), ' days left')
end)
The philosophy being: don't use a function if there is a simpler and clearer way to express what you want.
However, I wonder if you want to count the number in each group:
select sum(case when targetcompletedate <= NOW() then 1 else 0 end) as NumOverdue,
sum(case when targetcompletedate <= NOW() then 0 else 1 end) as NumWithDaysLeft
Just try this code:
SELECT
CASE
WHEN datediff(dd,targetcompletedate,now()) <= 0 THEN 'Overdue'
WHEN datediff(dd,targetcompletedate,now()) > 0 THEN 'Days_left'
ELSE NULL END,
datediff(dd,targetcompletedate,now()) AS 'days'
FROM tablename
The output is like:
Overdue -23
Days_left 13