Crystal Report SUM function with CASE - vb.net

I have the following column values in my crystal report:
|Code |Unit |
|A |2 |
|B |3 |
|C |2 |
|D |3 |
|E |1 |
|F |1 |
|G |4 |
|H |(3) |
I want to summarize the Unit except the Units which has Code H,J,K, and L.
The Codes: H,J,K, and L contains units which has parenthesis.
Is there a way to do this?

If you want to exclude any row or value from summary, it can be done by writing your case inside Use a formula field under Evaluate in Running Total Field
Refer the following Image...
The rows or fields which doesn't satisfy the condition will be skipped from Evaluation of summary.
Try this and get back with results !!

If you want to omit only units with ‘(‘ in it just convert this filed to number
Use
Val ({Unit})
this will return 0 for non-numeric text and number for numeric create sum of these you will get what you want
If you want not to use any special then create formula field like this
if {fa_rep_vr_Main.CustomTitle} not in('A','B','C') then
0
else
val({Unit})
use sum of this
If you want sum of NewPriceAD then use it in mentained field

Related

Conditional count of rows where at least one peer qualifies

Background
I'm a novice SQL user. Using PostgreSQL 13 on Windows 10 locally, I have a table t:
+--+---------+-------+
|id|treatment|outcome|
+--+---------+-------+
|a |1 |0 |
|a |1 |1 |
|b |0 |1 |
|c |1 |0 |
|c |0 |1 |
|c |1 |1 |
+--+---------+-------+
The Problem
I didn't explain myself well initially, so I've rewritten the goal.
Desired result:
+-----------------------+-----+
|ever treated |count|
+-----------------------+-----+
|0 |1 |
|1 |3 |
+-----------------------+-----+
First, identify id that have ever been treated. Being "ever treated" means having any row with treatment = 1.
Second, count rows with outcome = 1 for each of those two groups. From my original table, the ids who are "ever treated" have a total of 3 outcome = 1, and the "never treated", so to speak, have 1 `outcome = 1.
What I've tried
I can get much of the way there, I think, with something like this:
select treatment, count(outcome)
from t
group by treatment;
But that only gets me this result:
+---------+-----+
|treatment|count|
+---------+-----+
|0 |2 |
|1 |4 |
+---------+-----+
For the updated question:
SELECT ever_treated, sum(outcome_ct) AS count
FROM (
SELECT id
, max(treatment) AS ever_treated
, count(*) FILTER (WHERE outcome = 1) AS outcome_ct
FROM t
GROUP BY 1
) sub
GROUP BY 1;
ever_treated | count
--------------+-------
0 | 1
1 | 3
db<>fiddle here
Read:
For those who got no treatment at all (all treatment = 0), we see 1 x outcome = 1.
For those who got any treatment (at least one treatment = 1), we see 3 x outcome = 1.
Would be simpler and faster with proper boolean values instead of integer.
(Answer to updated question)
here is an easy to follow subquery logic that works with integer:
select subq.ever_treated, sum(subq.count) as count
from (select id, max(treatment) as ever_treated, count(*) as count
from t where outcome = 1
group by id) as subq
group by subq.ever_treated;

SQL triggers - conditionals and math operations

I have some database, lets say like that:
|a |b |c |d |e |
----------------------------------
|0 |12 |NA | | |
|NA|NA |30 | 42 | |
|NA|NA |NA | |53 |
I'd like to do few things:
for first row - set value to column D to be sin(b)
for second row - if the value is NA for column B, copy the previous known value (i.e (a,b)=(0,12))
on third row - if the value is NA for column B , copy the previous known value (i.e (a,b)=(0,12)) and set value to column D to be sin(b).
Those three examples of activities I have to execute on my databases for each row.
If triggers are not the right solution for this, I'd like to have recommendation how to solve it.

Counts based on an "as of" date

My apologies if I'm not wording the question correctly, and that's why I can't find any previous question/answers on this.....
My specific situation can be generalized as:
I have a table containing records of bed assignments for patients at a system of hospitals. A patient's placement into a bed is tagged with a date, and a reason for their placement there.
Patient |Hospital |Bed |Reason |Date
--------|---------|----|-------|--------
1234 |HOSP1 |111 |A |1/1/2016
5678 |HOSP1 |222 |A |2/1/2016
9012 |HOSP2 |333 |B |3/1/2016
3456 |HOSP3 |444 |C |3/1/2016
2345 |HOSP3 |555 |A |3/1/2016
7890 |HOSP1 |111 |D |4/1/2016
Based on the very small sample set above, I need to get a count of the "Reasons", per Hospital, given an "as of" date. So given an "as of" date of 3/15/2016:
As of Date: 3/15/2016
Hospital|Reason |Count
--------|---------|-----
HOSP1 |A |2
HOSP2 |B |1
HOSP3 |A |1
HOSP3 |C |1
But when changing the "as of" date to 4/1/16, I would hope to see the following:
As of Date: 4/15/2016
Hospital|Reason |Count
--------|---------|-----
HOSP1 |A |1
HOSP1 |D |1
HOSP2 |B |1
HOSP3 |A |1
HOSP3 |C |1
Any suggestions on the best route to accomplish this without melting my CPU or the servers? (my real record set is about 36m rows, going back 15 years). And my ultimate end goal is determine yearly averages of "reason" counts at each "hospital", but I know the first step is to get these initial counts finalized first (or is it???).
What you want is the most recent record before a certain date. This is pretty easy to do using window functions:
select hospital, reason, count(*)
from (select t.*,
row_number() over (partition by hospital, bed order by date desc) as seqnum
from t
where date <= '2016-03-15'
) t
where seqnum = 1
group by hospital, reason;

Retrieve data based on specified format

I have a table with the configuration to
|Format|Value|Number
|%v-%n |A |1
|%v %n |B |2
|%v(%n)|C |3
|%v |D |4
|%n |E |5
|%v%%n |F |6
Using this configuration the result should be as follows (%v -> value %n - Number)
A-1
B 2
C(3)
D
5
F%6
I can do this using a case statement by including all known combinations like
CASE WHEN format = '%v-%n' THEN VALUE || '-' || NUMBER END
How can I make it generic to accept any combination rather than hardcoding things in a case statement?
So I tested this out with mysql:
SELECT REPLACE(REPLACE(format, '%v', value), '%n', number) from format;
I hope this helps!

I want merge the entire row after that next row display the result

The actual output is:
|ctrcode|ctrname|empcode|empname|ewt |percent%|
|_______|_______|_______|_______|____|________|
|1 |Chain |1001 |A |25.6|15 |
|_______|_______|_______|_______|____|________|
|2 |Chain |1002 |B |15.6|10 |
|_______|_______|_______|_______|____|________|
but I want:
|empcode|empname|ewt |percent%|
|_______|_______|____|________|
|Chain |
|_____________________________|
|1001 |A |25.6|15 |
|_______|_______|____|________|
|1002 |B |15.6|10 |
|_______|_______|____|________|
How do I get it to look like this? I have used a DataGridView and filled the grid.
DataGridView is not able to merge cells, as far as I know. Please see this thread, to find an alternative solution: https://social.msdn.microsoft.com/Forums/windows/en-US/fea3dcff-5447-450a-8f7c-bb1c4f63371d/merging-cells-in-datagridview?forum=winformsdatacontrols
Have a nice weekend.