crystal report(case) - crystal-reports

im using asp.net crystal report ........
sql query:
CONVERT(NUMERIC(17,3), CASE
WHEN
CASE WHEN GLDD_DOC_AMOUNT > 0 THEN GLDD_DOC_AMOUNT ELSE 0 END = 0 THEN NULL
ELSE
CASE WHEN GLDD_DOC_AMOUNT > 0 THEN GLDD_DOC_AMOUNT ELSE 0 END
END) DR,
CONVERT(NUMERIC(17,3), CASE
WHEN (- 1 *
CASE WHEN GLDD_DOC_AMOUNT < 0 THEN GLDD_DOC_AMOUNT ELSE 0 END) = 0 THEN NULL
ELSE - 1 *
CASE WHEN GLDD_DOC_AMOUNT < 0 THEN GLDD_DOC_AMOUNT ELSE 0 END
END) CR,
the above query is which is from sql server 2005.....
i have to use this query in crystal report Formula editor.....
how?

You can either paste the two SQL parts into SQL Expression formulas (one for the CR part and another for the DR part), or rewrite them as Crystal formulas.
Both formulas have some redundancy, and are easier in SQL as
CONVERT(NUMERIC(17,3), Case When GLDD_DOC_AMOUNT > 0 Then GLDD_DOC_AMOUNT Else null End) AS DR,
CONVERT(NUMERIC(17,3), Case When GLDD_DOC_AMOUNT < 0 Then -1*GLDD_DOC_AMOUNT Else null End) AS CR
In Crystal Syntax, you can use If...Else

Related

percentage difference when using CASE WHEN clause POSTGRES

Is this the correct syntax?
I am using the traditional method of (new/old)-1 to work out the % difference between yesterday's registrations and a week from yesterday's registrations. I am getting '0' which does not seem right...
select
sum(case when (timestampregistered::date = current_date - 1) then 1 else 0 end)
/ sum(case when timestampregistered::date = current_date - 8 then 1 else 0 end) - 1 as pct_diff
from customers_table
You are doing integer arithmetics, so your answers will be integers.
Try
[...]then 1.0 else 0.0 end

Multiple Case Statements With Identical Expressions

I'm trying to evaluate a specific column to return five different columns - but the columns are based off the same expression in the CASE statements.
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 1
ELSE 0
END AS [Invalid] ,
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 0
ELSE 1
END AS [validMICcode] ,
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 0
ELSE 1
END AS [validSerialNumber] ,
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 0
ELSE 1
END AS [validFormat] ,
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 0
ELSE 1
END AS [validProductionYear] ,
I feel like is causing the table / column in question to be searched for the pattern five times, but I cannot figure out how rewrite it - or if it is even possible - to have one pattern search and define the columns based on the one search.
I'm trying different variations, but I cannot come up with the correct syntax for this issue:
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%' THEN 1
ELSE 0
CASE WHEN 1 THEN 1 END AS [Invalid]
CASE WHEN 0 THEN 0 END AS [validMICode]
CASE WHEN 0 THEN 0 END AS AS [validSerialNumber]
CASE WHEN 0 THEN 0 END AS AS [validFormat]
CASE WHEN 0 THEN 0 END AS AS [validProductionYear]
END
One way would be a sub query and Bitwise NOT.
select
result Invalid,
~result validMICcode,
~result validSerialNumber,
~result validFormat,
~result validProductionYear
from
(
select
CASE WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%'
THEN CAST(1 As bit)
ELSE CAST(0 As bit)
End result
from ...
) tbl
You could do that with a CTE..
with cte as(
select *,
CASE
WHEN va.HIN LIKE '%[-=!##$%^&*()<>?:|\;./,]%'
THEN 1
ELSE 0
END as SomeColumn)
select *,
CASE WHEN SomeColumn = 1 THEN 1 END AS [Invalid]
CASE WHEN SomeColumn = 0 THEN 0 END AS [validMICode]
...
from cte

Conversion error using CASE on a varchar column

I'm trying to grab information to send to Google Charts for a graph I'm looking at the column Active which can be true/false but is set to varchar in my database.
Using this query:
SELECT
SUM(CASE WHEN CONVERT(int, Active) Active = 1 THEN 1 ELSE 0 END) AS Active,
SUM(CASE WHEN CONVERT(int, Active) Active = 0 THEN 1 ELSE 0 END) AS Inactive
FROM
Events
throws this error:
Conversion failed when converting the varchar value 'True' to data type int.`
Ints are not booleans.... they are called bit in SQL Server. try;
SELECT
SUM(CASE WHEN CONVERT(bit, Active) = 1 THEN 1 ELSE 0 END) AS Active,
SUM(CASE WHEN CONVERT(bit, Active) = 0 THEN 1 ELSE 0 END) AS Inactive
FROM Events
Or just test for your String
SELECT
SUM(CASE WHEN Active = 'True' THEN 1 ELSE 0 END) AS Active,
SUM(CASE WHEN Active = 'False' THEN 1 ELSE 0 END) AS Inactive
FROM Events
P.S. Storing Boolean values as String columns is silly... Consider changing the column to be a bit column so you don't even need to convert it

order by with operation on select case query

I want to rank a table by multi-columns and sum of each point that is decided by value.
For example, I can get some columns and each points by below query.
select
(case when seller=true then 50 else 0 end) as sel,
(case when buyer=true then 40 else 0 end) as buy
from company;
but I can't order by this values by like this query
select
(case when seller=true then 50 else 0 end) as sel,
(case when buyer=true then 40 else 0 end) as buy
from company
order by (sel + by);
or this
select
(case when seller=true then 50 else 0 end) as sel,
(case when buyer=true then 40 else 0 end) as buy,
(sell, buy) as sm
from company
order by sm;
How can I do that?
Oh, sorry, I found the answer.
select * from
(select
(case when seller=true then 50 else 0 end) as sel,
(case when buyer=true then 40 else 0 end) as buy
from company) as tmp
order by (tmp.sel + tmp.buy);

Changing position of data in columns depending on actual data

Suppose I have a table like below
ID Marks1 Marks2 Marks3
-------------------------
1 10 0 4
2 0 40 90
Now, I need to select from this table in a way that will give precedence to positive values first. So if the marks are 0 then it will be shifted to right. The SELECT should give following output
ID Marks1 Marks2 Marks3
-------------------------
1 10 4 0
2 40 90 0
Can you please guide me for the approach? It will be great if it can be done in a select statement itself. Thanks in advance.
Something like this you will need to check for each subsequent row that the previous column isn't 0. Have selected the values out as null as it makes the code slightly easier to read as i can use coalesce
Select
Coalesce(Marks1, Marks2, Marks3,0) as Marks1,
Case when marks1 is not null
then Coalesce(Marks2, Marks3, 0) else 0
end as Marks2,
case when marks1 is not null
and marks2 is not null
then Coalesce(Marks3,0)
end as Marks3
from
(
Select
Case when Marks1 =0 then null else Marks1 end as Marks1,
Case when Marks2 =0 then null else Marks2 end as Marks2,
Case when Marks3 =0 then null else Marks3 end as Marks3
From mytbl
)