Trying to do a simple expression in SSRS - sql

I am trying to calculate the average price of an order. I have the following simple expression:
=( Max(Fields!Price, "Totals") - Max(Fields!Discount_Value.Value, "Totals") + Max(Fields!Tax_Value.Value, "Totals") + Max(Fields!Freight_Charges.Value, "Totals") - Max(Fields!Total_Freight_Cost.Value, "ShipCodesShipped") / Max(Fields!Total_Orders_Count.Value, "Totals"))
It does not seem to work at all and just shows up as #Error. Does anyone know what I'm doing wrong?

The first part of your expression
=( Max(Fields!Price, "Totals") ...
should be
=(Max(Fields!Price.Value, "Totals") ...

Related

how to get a value from the results of another column calculation in same table

I tried to retrieve a value from the calculation of several columns,
in this case try to apply the formula "(a + (a / 25 * b)) - c" to be processed using sql language which I will use in codeigniter.
I also tried using "derived table" like SELECT .... FROM (SELECT... FROM...) AS dt
but I had difficulty when applying it to my case in codeigniter
private function _get_datatables_query(){
$intvl = '2';
$tgl_stok = '2019-09-30';
$this->db->SELECT('p.hso, p.no_part, p.nama_part, jml, sp.oh, sum(p.qty)+(sum(p.qty)/25*$intvl)-sp.oh as suggest');
$this->db->FROM('penjualan p');
$this->db->JOIN('stok_part sp', 'sp.no_part = p.no_part', 'left');
$this->db->WHERE("sp.tgl = '$tgl_stok' AND p.tgl BETWEEN DATE_SUB('$tgl_stok', INTERVAL $intvl DAY) AND '$tgl_stok'");
$this->db->GROUP_BY('p.no_part');
//...other code...
}
I want a column with the alias suggest in the code to produce a calculated value of several other columns
I know writing code that I created is not in accordance with the rules of writing SQL, I tried a number of ways but it did not work. I am very grateful for your help
Instead of var you should use parameter in this way you can easly pass the value you need and avoid sqlinjection eg:
$sql = "SELECT p.hso, p.no_part, p.nama_part, jml, sp.oh, sum(p.qty)+(sum(p.qty)/25*?)-sp.oh as suggest
FROM enjualan p
LEFT JOIN stok_part sp ON sp.no_part = p.no_part
WHERE sp.tgl = ? AND p.tgl BETWEEN DATE_SUB(?, INTERVAL ? DAY) AND ?
GROUP BY p.no_part";
$this->db->query($sql, array($intval,$tgl_stok, $tgl_stok, $intvl, tgl_stok ));
}
I was solved this case.
the problem is in my query:
$this->db->SELECT('p.hso, p.no_part, p.nama_part, jml, sp.oh, sum(p.qty)+(sum(p.qty)/25*$intvl)-sp.oh as suggest');
and replace it with
$this->db->SELECT("p.hso, p.no_part, p.nama_part, sum(p.qty) as jml, sp.oh, sum(p.qty)+(sum(p.qty)/25*'$intvl')-sp.oh as s_po");
my mistake was writing quotation marks in the query

Getting incorrect results in Qlikview Analyser

I need help with a report I am trying to create with Olikview Analyser. The problem is I keep getting incorrect results. I think it may be related to the metrics being slightly wrong. The data source definitely has the correct figures, but when I am trying to run select queries results are not right. Here is an example. To see sales for Last Week Last Year (sales for Last week 2015)
Currently the Analyser code is as follows:
sum({$<DATE_WEEK={$(LastWeekLY)}, DATE_FYEAR {$(LastWeekYearLY)}>}POS_SALES_VALUE_GROSS)
+
sum({$<DATE_WEEK={$(LastWeekLY)}, DATE_FYEAR {$(LastWeekYearLY)}>}CS_SALES_VALUE_GROSS)
+
sum({$<DATE_WEEK={$(LastWeekLY)}, DATE_FYEAR={$(LastWeekYearLY)}>}CO_ITEM_TOTAL)
However, the figures I get are far too high. Are there any obvious errors in the code?
Thanks in advance for your help
Try
sum({$<DATE_WEEK={'$(LastWeekLY)'}, DATE_FYEAR={'$(LastWeekYearLY)'}>} numsum(POS_SALES_VALUE_GROSS,CS_SALES_VALUE_GROSS,CO_ITEM_TOTAL))
sometimes the weeks/years are stored as text and not numbers..so this will fix this issue.
If this does not work then you have a problem in your variables.
Please try
sum( {$< DATE_WEEK = {$(#LastWeekLY)} , DATE_FYEAR = {$(#LastWeekYearLY)} >} POS_SALES_VALUE_GROSS ))+
sum( {$< DATE_WEEK = {$(#LastWeekLY)} , DATE_FYEAR = {$(#LastWeekYearLY)} >} CS_SALES_VALUE_GROSS ))+
sum( {$< DATE_WEEK = {$(#LastWeekLY)} , DATE_FYEAR = {$(#LastWeekYearLY)} >} CO_ITEM_TOTAL))

what's wrong in this code mdx?

I wonder what's wrong in this code?
with
member [CA Espagne] As
((select [Measures].[CA], [[Site].[Pays - Site].[Pays].&[ES]))
select {[Measures].[CA], [CA Espagne]} on 0,
[Temps].[Année - Mois - Jour].[Année].&[20100101].children on 1
from [DistrisysOLAP_Global];
Better to tell us the error you're getting... but I guess you should simply define your calculated member as :
with member [CA Espagne] As ( [Measures].[CA], [[Site].[Pays - Site].[Pays].&[ES] )
then the remaining of the statement looks ok (remove the trailing ; ). Otherwise, here is a page explaining the syntax of the calculated members.

Wrong number of arguments SQL MSACCESS

I am getting an error indicating the wrong number of arguments when I run the following query:
SELECT
population_postcodes.*,
target_postcodes.*,
SQR( EXP(population_postcodes.longitude- target_postcodes.longitude, 2) + EXP(population_postcodes.latitude-target_postcodes.latitude, 2) ) as distance
FROM population_postcodes INNER JOIN target_postcodes on Population_postcodes.Population_postcode = Target_postcodes.Target_postcode;
Could anyone please suggest how I can fix this?
I have also tried the following code:
SELECT Population_postcodes.*, Target_postcodes.*
FROM population_postcodes
INNER JOIN target_postcodes
ON Population_postcodes.Population_postcode = Target_postcodes.Target_postcode
SQR( (population_postcodes.longitude- target_postcodes.longitude)^2 + (population_postcodes.latitude-target_postcodes.latitude)^2 ) as distance;
And this code:
SELECT Population_postcodes.*, Target_postcodes.*, SQR( (population_postcodes.longitude- target_postcodes.longitude)^2 + (population_postcodes.latitude-target_postcodes.latitude)^2 ) as distance
FROM population_postcodes
INNER JOIN target_postcodes
ON Population_postcodes.Population_postcode = Target_postcodes.Target_postcode;
Exp needs one parameter, you give two.
Old: EXP(population_postcodes.longitude- target_postcodes.longitude, 2)
New: (population_postcodes.longitude- target_postcodes.longitude)*(population_postcodes.longitude- target_postcodes.longitude)
Try replacing...
EXP(<expression>, 2)
...to...
<expression>^2
In Access, the EXP function returns e (the base of natural logarithms) raised to a power. To raise an expression to a power, use the ^ operator.
In your case, be careful to put brackets around the expression, for example...
(population_postcodes.longitude- target_postcodes.longitude)^2
...to force the power to be applied last. By default, the ^ operator is evaluated before the - operator.

iif blank show zero

I have a visual studio report that I want to state that if a calculation box is blank due to there being no figures available on that particluar project then show zero.
My calculation is :- =((Sum(Fields!TotalCost.Value, "Accrued") + Sum(Fields!TotalCost.Value, "serv1")) /
(Sum(Fields!Quantity.Value, "serv1") + Sum(Fields!Quantity.Value, "Accrued"))
)
And I want to try and include an IIF statement in that to show zero if blank.
Any ideas on how to best achieve my aim?
so far I have got to
=iif((Sum(Fields!TotalCost.Value, "Accrued") + Sum(Fields!TotalCost.Value, "serv1")) /
(Sum(Fields!Quantity.Value, "serv1") + Sum(Fields!Quantity.Value, "Accrued"))
) = "" ,false,0 ) but I am getting a little confused.
Most likely value is not blank string but a Nothing. Try following construct:
=IIf(IsNothing(((Sum(Fields!TotalCost.Value, "Accrued") + Sum(Fields!TotalCost.Value, "serv1")) / (Sum(Fields!Quantity.Value, "serv1") + Sum(Fields!Quantity.Value, "Accrued"))), 0, ((Sum(Fields!TotalCost.Value, "Accrued") + Sum(Fields!TotalCost.Value, "serv1")) / (Sum(Fields!Quantity.Value, "serv1") + Sum(Fields!Quantity.Value, "Accrued")))
It's a bit awkward since you have to repeat the expression twice, to avoid it you may want to write a custom function.