Sum expression with IIF clause - reporting-services

I want to sum field called: Fields!Horas.Value depending of Fields!sNombrePlanta.Value
So I do:
=Sum(IIF(Fields!sNombrePlanta.Value = "AUCHI", Fields!Horas.Value, 0))
It work perfectly, but now I want to add another Fields!sNombrePlanta.Value and sum this two, so I try:
=Sum(IIF((Fields!sNombrePlanta.Value = "AUCHI" And
Fields!sNombrePlanta.Value = "AUQRO"), Fields!Horas.Value, 0))
and it just return blank value. What am I doing wrong there? Regards

You are asking one field to have two values:
=Sum(IIF((Fields!sNombrePlanta.Value = "AUCHI" And Fields!sNombrePlanta.Value = "AUQRO"), Fields!Horas.Value, 0))
sNombrePlanta can't have both the values "AUCHI" AND "AUQRO" at the same time. Do you mean to use OR where the field is either of those values?

Related

Link two different if statements to same value (mysql query)

Let's say that I have two if statements on two different columns that determine that value that I wanna display in a third virtual column. Something like:
if(column1 = 'activated'){
return 15;
}
if(colunm2 = 1){
return 'enabled';
}
I want to display the return values in a virtual column called let's say output in a select query. Something like:
SELECT IF(column1 = 'activated', 15, [what do i put here...]),
IF(column2 = 1, 'enabled', [what do i put here]) ..... AS consent
Obviously the above query doesn't work cause I can't separate my ifs with a comma since they have to be bound to the same column called (output).
How can I achieve this?
Thanks for any help.
You seem to want a case expression:
select (case when column1 = 'activated' then '15'
when column2 = 1 then 'enabled'
end) as consent

How to Get sum() of single column values

I have bellow snippet table structure.In this table there is one row count_set that have two values first is 1 and second is 1 here i have to make the sum of this value the sum answer will be 2.
I tried with bellow query but not getting sum value its getting sum with blank value.
so can any one tell me how to do this.
$sql_chk_current_sum_count = "SELECT sum(count_set) FROM wp_lead_count WHERE
end_date = $end_date";
$final_sum= $wpdb->get_results($sql_chk_current_sum_count) or die(mysql_error());
Try this:
SELECT sum(count_set) FROM wp_lead_count WHERE
date_format(end_date,'%Y-%m-%d') = date_format($end_date,'%Y-%m-%d');

Compare two dates in a database table are equal

I'm new here. So, be easy on me. I have a table called 'data' in which one of the columns is 'date' stored in YYYY-MM-DD format. Here is the code that I have been working on. Basically, what I want to do is compare if two dates stored in my table are equal. But, each time I run the code, I keep getting error: Undefined offset right where the code says $lisdate[$i+1]. How do I compare the dates stored in a table? Thank you.
My code
$sth2 = mysql_query("SELECT * FROM data WHERE dest_name='www.myren.net.my'");
while($rowstemp = mysql_fetch_assoc($sth2))
{
$lisdate[] = $rowstemp['date'];
$lisaverage[] = $rowstemp['avg_rtt'];
}
$rows = mysql_num_rows($sth2);
$addAverage[0] = $lisaverage[0];
$numbers = 0;
$j = 0;
for($i = 0; $i<$rows; $i++)
{
if($lisdate[$i] == $lisdate[$i+1])
{
$addAverage[$j] = $addAverage[$j] + $lisaverage[$i+1];
}
else
{
$j++;
$numbers = $numbers +1;
}
}
Gordon has it right. Your for loop is overrunning the end of your array because you're working with consecutive pairs of rows. Change it to read ; $i < $rows - 1; to correct this problem.
But, you have another problem. SQL rows in result sets have an unpredictable order unless your query includes an ORDER BY clause. If these rows, without that clause, appear in ascending order by date, it's dumb luck. Put ORDER BY date in your query.
It's not entirely clear what that code in the question is attempting to achieve.
(As Ollie pointed out in his answer, it bears repeating: the order that MySQL returns the rows is guaranteed ONLY if there's an ORDER BY clause on the query. The result from a "duplicate date" check that's being performed in the code is only going to detect a "duplicate" date value if it appears on contiguous rows.)
We see the code loading a couple of arrays, using the date and avg_rtt column values returned by the query. Then there's some manipulation on the array... the end result will be another it's not clear what the actual intent of that rigmarole is, what it's actually trying to achieve.
If there are any rows It looks like there's going to be another array... if there are no "duplicate" date values found, it's going to have a single element (with the value of rtt_avg from the first row), and $numbers will be set to the number of elements in the original array.
If there is a "duplicate" date values found, the results from the code seem very odd to me, a sparsely populated array. Why?
Personally, I'd be looking to get an actual statement of the specification, and have the database do that processing for me, rather than mucking through two arrays.
If what we want is set of values with no duplicated date values, I'd use a GROUP BY and some aggregation, for example:
SELECT d.dest_name
, d.date
, AVG(d.avg_rtt) AS avg_avg_rtt
, MAX(d.avg_rtt) AS max_avg_rtt
, MIN(d.avg_rtt) AS min_avg_rtt
, SUM(d.avg_rtt) AS sum_avg_rtt
, COUNT(1) AS cnt
, COUNT(d.avg_rtt) AS cnt_avg_rtt
, COUNT(DISTINCT d.avg_rtt) AS cnt_distinct_avg_rtt
FROM data d
WHERE d.dest_name = 'www.myren.net.my'
GROUP
BY d.dest_name
, d.date
ORDER
BY d.dest_name
, d.date
If I was looking for just a count of distinct date values, like what $numbers is going to contain, then just:
SELECT d.dest_name
, COUNT(DISTINCT d.date) AS cnt_distinct_date
FROM data d
WHERE d.dest_name = 'www.myren.net.my'
GROUP BY d.dest_name
ORDER BY d.dest_name
The "looping through array" that the original query just looks like an odd way to achieve a result, whatever that result is supposed to be.

How to sum a column that is based on an expression in SSRS ReportBuilder table

If have a column where the value is based on another field, so the expression is
=iif(Fields!TaskType.Value = "Type1", Fields!Amount.Value, 0)
And I'm trying to get the sum of this in the group totals using the folooing exporession, but it gives #Error (with or without the group name as scope):
=sum(iif(Fields!TaskType.Value="Type1", Fields!Amount.Value, 0), "GrpProjectNumber")
This looks fine to me. What am I doing wrong?
Thanks in advance
I've constructed a simple DataSet to try and mimic yours:
select Amount = cast(100.0 as money), TaskType = 'Type1'
union all select Amount = cast(100.0 as money), TaskType = 'Type1'
union all select Amount = cast(100.0 as money), TaskType = 'Type2'
I replicated your error with your expression, but the following works for me:
=Sum(IIf(Fields!TaskType.Value="Type1", Fields!Amount.Value, CDec(0)), "GrpProjectNumber")
So it seems like you were on the right path; just needed to apply the CDec cast to the 0 constant in the expression.

Smart counting of id in join mysql

Upon running this query, the COUNT function doesn't filter by the 'check_if_new_customer' flag. I read in this article: http://dev.mysql.com/tech-resources/articles/wizard/page4.html that SUM can be used instead of COUNT in some cases to get more accurate results, however when I try that, I get something very different, it seems to show massive doubling of numbers. I think this may be because I'm summing the UUID that is in the id field instead of counting at that point. Any suggestions on what I could put there to get a count of all of the existing customers vs the new customers?
SELECT
YEAR(so.date_entered),
so.technical_address_country,
so.technical_address_state,
COUNT(so.id) as all_sales,
COUNT(mf.id) as all_jobs,
SUM(so.total_value) as all_value,
COUNT(IF(so.check_if_new_customer=1,so.id,0)) as sales_order_new,
SUM(IF(so.check_if_new_customer = 1,so.total_value,0)) as total_value_new,
COUNT(IF(so.check_if_new_customer=1,mf.id,0)) as jobs_new,
COUNT(IF(so.check_if_new_customer=0,so.id,0)) as sales_order_existing,
SUM(IF(so.check_if_new_customer = 0,so.total_value,0)) as total_value_existing,
COUNT(IF(so.check_if_new_customer=0,mf.id,0)) as jobs_existing,
SUM(IF(so.check_if_new_customer=0,mf.id,0)) as jobs_existing_t
FROM
sugarcrm2.so_order so
LEFT JOIN
sugarcrm2.mf_job mf on so.id = mf.sales_order_id
WHERE
so.date_entered > "2011-10-30" AND
so.technical_address_country IS NOT NULL AND
so.technical_address_state IS NOT NULL AND
so.deleted = 0 AND
so.has_been_promoted = 1
GROUP BY
YEAR(so.date_entered),
so.technical_address_country,
so.technical_address_state
ORDER BY
so.technical_address_country, so.technical_address_state
If you want to use SUM() like COUNT() you will need to pass it either a 1 or 0 so that all of the 1's will sum up to your desired count. So in your example, if you want a sum of all the new jobs you would do this:
SUM(IF(so.check_if_new_customer=1,1,0)) as jobs_new
or if so.check_if_new_customer always returns a 1 or 0 you could alternatively do this:
SUM(so.check_if_new_customer) as jobs_new
COUNT() returns the number of records for which its argument, if specified, is non NULL. Since its argument in this case is the result of an IF() expression (which evaluates to some column's value if true and 0 if false), virtually every record will be counted irrespective of the test condition.
SUM(), as its name suggest, sums the values of its argument. In this case, it would sum the values of the referenced column whenever the test condition is true.
Apparently neither is what you're after, although your question is rather ambiguous as to what exactly you do want. At a guess, you might want something like:
SUM(so.check_if_new_customer)
I want to sum +1 for every time the joined table's id field is not null.
SUM(IF(so.check_if_new_customer=0 AND mf.id IS NOT NULL,1,0)) as jobs_existing