Sum expression with a where clause or if - asp.net-mvc-3

How can I write an expression that will only sum values in a list where a column is not null?
example:
2.1 4 5 23
4 1 2 null
1 3.1 5 1
8 .5 1 null
i would like to get the sum of the first column to all rows who doesn't have the last column null. So my answer would be 2.1+1 = 3.1. Is it possible to have a 'where' clause in a Sum expression? Or how could i use the Sum-if to accomplish this?

have a look whether this helps
select sum(field_1) from table_name where last_field is not null

Related

Running distinct count in quicksight

I want to implement a running distinct count with Amazon Quicksight. Here's an example of what that would look like:
Date
ID
Amount
Running Distinct Count
1/1/1900
a
1
1
1/2/1900
a
3
1
1/2/1900
b
6
2
1/4/1900
a
3
2
1/8/1900
c
9
3
1/22/1900
d
2
4
I've tried runningSum(distinct_count, [Date ASC]), but this returns a sum of the distinct counts for each aggregated date field.
Can this be implemented in QuickSight?
You can use this workaround to get the same functionality as runningDistinctCount() as follows:
runningSum(
distinct_count(
ifelse(
datetime=minOver({Date}, [{ID}], PRE_AGG),
{ID},
NULL
)),
[{Date} ASC],
[]
)
This would give you the runningDistinctCount of ID's over the Date field. It achieves it by considering just the first time the ID appears in the dataset, counting these and finally doing a runningSum on these counts.

Select and sum multiple columns for statistic purposes with Laravel query

I have one table scores where I have saving users scores. It's looks like this
table `scores`
id | points | user_id
1 5 1
2 2 1
3 4 1
4 1 3
5 10 2
I want to select each user, sum his points and show as a ranking. The result from above should be
user_id | points
1 11
2 10
3 1
The query with which I came up is
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->first();
The problem is in ->first() because it's return only one result.. it is working as must. If I try to use ->get() instead I've got Undefined property error. How should I use this?
The query which is working in phpmyadmin
SELECT count(id) as numberId, sum(points) as numberOfPoints FROM `points` GROUP BY `user_id`
You can use something like this
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->get();
foreach($sumPoints as $point){
dd($point); //OR dd($point->numberOfPoints)
}

OBIEE case statement check on non excistence

I have the following situation:
column 1 column 2 column 3
A 1 1
A 1 2
B 3 4
B 3 5
I need to color my letters when the value in column 2 never occurs in column 3. But I need to color all my letters. Does anyone know how to write a case statement for this?
So I'll explain my example: I dont't need to color the letter A because there is a match between column 2 and 3 and the first row.
I do need to color my B's because there is never a match between columns 2 and 3.
I already tried this:
count(distinct(case when "Column 2" != "Column 3" then 1 else 0 end))
but this gives a result for each row and I need a result for the total package.
Thanks!
You can approach this as following:
Create a logical column on your analysis that does a case statement that returns 1 or 0 depending if the values of column2 and column3 are the same (pretty much like the case-when that you provided on your answer but without the count distinct).
Wrap that case statement with a MAX grouped by your column1. This will give you either a consistent 1 or 0 across all your different values of column1. You can use this value for your conditional formatting. The key here is to use the aggregated function with the group by.
You have here some oracle documentation on how to use the logical SQL group by.
Hope that helps! Good luck!

SSRS Switch Case - how to sort by 2 field values

=Switch(Fields!RptSeq.Value="1",Fields!PatientId.Value,
Fields!RptSeq.Value="2",Fields!RxNumber.Value,Fields!PatientId.Value
Fields!RptSeq.Value="5",Fields!DoctorName.Value,Fields!PatientId.Value,
1=1,Fields!PatientId.Value)
I need to sort my report thrugh expression like this, if the sort seq is 1 then by patient ID, if 2 then the report should be first sorted by RxNumber then by PatientID. I don't know how to sort for 2 field values.
if I do Fields!RxNumber.Value,Fields!PatientId.Value the sort does not work, if I do Fields!RxNumber.Value+Fields!PatientId.Value I'm getting error wrong sort expression.
appreciate your help
Thanks in advance.
you can add two lines in the sorting tab of the tablix; one for the first sort priority with this expression:
=Switch(Fields!RptSeq.Value="1",Fields!PatientId.Value,
Fields!RptSeq.Value="2",Fields!RxNumber.Value,
Fields!RptSeq.Value="5",Fields!DoctorName.Value,
1=1,Fields!PatientId.Value)
and the other for the second sort priority set the field to :
Fields!PatientId.Value
You can handle it by passing the parameter to the Dataset query also.
Sample:
declare #RptSeq int = 1
select * from Table
order by
(case #RptSeq when 1 then PatientId
when 2 then RxNumber
when 5 then DoctorName
else PatientId
end),
(case #RptSeq when 2 then PatientId
when 5 then PatientId
end)

Oracle aggregate function to return a random value for a group?

The standard SQL aggregate function max() will return the highest value in a group; min() will return the lowest.
Is there an aggregate function in Oracle to return a random value from a group? Or some technique to achieve this?
E.g., given the table foo:
group_id value
1 1
1 5
1 9
2 2
2 4
2 8
The SQL query
select group_id, max(value), min(value), some_aggregate_random_func(value)
from foo
group by group_id;
might produce:
group_id max(value), min(value), some_aggregate_random_func(value)
1 9 1 1
2 8 2 4
with, obviously, the last column being any random value in that group.
You can try something like the following
select deptno,max(sal),min(sal),max(rand_sal)
from(
select deptno,sal,first_value(sal)
over(partition by deptno order by dbms_random.value) rand_sal
from emp)
group by deptno
/
The idea is to sort the values within group in random order and pick the first.I can think of other ways but none so efficient.
You might prepend a random string to the column you want to extract the random element from, and then select the min() element of the column and take out the prepended string.
select group_id, max(value), min(value), substr(min(random_value),11)
from (select dbms_random.string('A', 10)||value random_value,foo.* from foo)
In this way you cand avoid using the aggregate function and specifying twice the group by, which might be useful in a scenario where your query is very complicated / or you are just exploring the data and are entering manually queries with a lengthy and changing list of group by columns.

Resources