Sql custom statement - sql

What is the Programme for Sql data in Microsoft Expression Web Custom Statement similar to following Access database statement:
Sum(IIf([accident]![Rly]='CR',1,0))

Im not sure you will be able to use this format as this will run against each individual row and always return 1 or 0. To run against your whole query to get the total sum you need something similar to this:
SELECT SUM(Rly) FROM
(SELECT ID, CASE WHEN Accident.Rly = 'CR' THEN 1 ELSE 0 END Rly
FROM Accident)
The syntax may be slightly different but hopefully this will set you in the right direction.

Related

Scalar subqueries produced more than one element

I know this error has been produced many time and a lot of answers came by but I believe every situation might be unique.
So i am trying to get a deficit value(imports - exports) from a table. Both values are on one column
value account
100 export
200 import
SO now i need to calculate the deficit or surplus, which is either import-export or export-import. I tried scalar subqueries but i am always getting this error.
SELECT label, product_type,status,((select value from Task2.quarterly_report where account="Imports") - (select value from Task2.quarterly_report where account="Exports")) As trade_deficit
so basically i am trying to get a table with:-
label product_type status trade_deficit
Can anyone explain to me the issue and why is it happening and how to solve it.
Thanks in advance
You can use the conditional aggregation:
select sum(case when account = 'import' then value
when account = 'export' then - value
end)
from t;
This is based on the question and sample data. I don't see what your query has to do with the rest of the question.

Is there an easier way than nested iifs to count mismatched conditions in SQL server

The picture below shows a table of accounts and the outcomes that I want to count. Ie every time the account number is 106844 and the outcome is "MESSAGE" or "ESCALATION EMAIL" that should count as 1 where any other outcome counts as 0. What I would normally do is a horrible mess of iifs like
sum( iif([account] = '106719' and [Outcome] in ('MESSAGE','ESCALATION_EMAIL'),1,iif([account] = '310827' and [outcome] <> 'ABORT' and 'CALL_OUTCOME_LB' in ("Call patched to Customer Care","Message Taken"),1,iif( ... , 0) as [Total Outcomes]
and so on but man it feel like there's got to be an easier way or one less prone to making a random mistake in the 7th nested iif and messing the whole thing up. Any ideas?
Don't use iif(). It is a function brought into SQL Server for back-compatibility to MS ACCESS. Why would you want to be backwards compatible to such a thing?
Use the ANSI standard CASE expression:
sum(case when account = '106719' and Outcome in ('MESSAGE', 'ESCALATION_EMAIL')
then 1
when account = '310827' and outcome <> 'ABORT' and
'CALL_OUTCOME_LB' in ("Call patched to Customer Care", "Message Taken")
then 1
. . .
else 0
end) as Total_Outcomes
I would also advise you to name your columns so they don't need to be escaped (why "Total Outcomes" became "Total_Outcomes"). That simplifies the code.
Why not use a lookup table that has the Account and Outcome and use that? Then, as requirements change, you could update the lookup table and not worry about your code.
Yeah... there is
The last parameter is for when the condition is false, everything else will fall in there.
SUM( IIF([ACCOUNT] = '106719' AND [OUTCOME] IN ('MESSAGE','ESCALATION_EMAIL'),1,0))

How to write an expression for two different attributes in the same field in qlikview

Please help me write the script for the following statement in qlikview which I have it in SQL.
SELECT CASE
WHEN Total_A=0 THEN 0
ELSE cast(((Total_B+Total_C)/Total_A) AS decimal (5,2))
END AS ratio
I have Total_A , Total_B and Total_C in the same field called Total_val
The SQL CASE is usually replaceable by the QlikView if().
Try this
if(Total_A=0,0,(Total_B+Total_C)/Total_A) as Ratio
if the A,B,C switch is inside the Val column then it will get a lot more tricky as you will have to aggregate and use nested ifs. But I believe the statement I wrote is equivalent to the SQL you gave us. If my answer doesn't work please give us a few rows of data to look at

Query to update values in a table with averages from the same table

I'm looking for a bit of support regarding using a value from a separate query in an update query. The background is that i have a query calle qry_AvgOfXCoeff which calculates the average of tbl_ConvertToDouble.XCoeff. What i would like to do is replace any Xcoeff value that is greater than 0 with the avg calculated in the first query. At present i cannot use the qry directly in an Update query as i received the dreaded 'Must use a updateable query' error.
qry_AvgOfXCoeff:
SELECT Avg(tbl_ConvertToDouble.XCoeff) AS [Avg]
FROM tbl_ConvertToDouble;
Now i've been informed that i should be able to do this by using an IN condition in the update query, but im really stumped with this one and cannot seem to find any examples of how i would implement this. I've had a play with some code as per below, but please can someone help with this. It seems such a simple thing.
UPDATE qry_AvgOfXCoeff, tbl_ConvertToDouble SET tbl_ConvertToDouble.[Xcoeff]
WHERE (( ( tbl_ConvertToDouble.[xcoeff] ) IN (SELECT [qry_AvgOfCoeff].[Avg]
FROM [qry_AvgOfCoeff] AS Tmp
Where [tbl_ConvertToDouble].[Xcoeff] > 0) ))
ORDER BY tbl_calcreg.[xcoeff];
Thank you kindly in advance.
Donna
Access offers Domain Aggregate Functions that can be helpful in avoiding the "Operation must use an updateable query" issue. In this case, you can use the DAvg() function
UPDATE tbl_ConvertToDouble
SET XCoeff = DAvg("XCoeff", "tbl_ConvertToDouble")
WHERE XCoeff>0

Qlikview - calculate and use calculated variable in script

As a new Qlikview user, I'm looking for the best way to create calculated variables, and variables based on calculated variables, in my data and use them in displays. My data is connected via ODBC.
For example, let's say I want a variable Rating based on the "Risk" variable in my dataset. The raw data contains a Risk variable that is "L" or "H". I would like to create an indicator, like Risk_H, that is 0 or 1 (if Risk='H'). Then I would like to create the Rating like "Rating = 1 + Risk_H*2". Can I do all of this in a script and have the variable Rating in my dataset?
When I try the above, I can create the Risk_H variable, but then I am not sure how to reference it in the script to calculate the Rating variable. I have read other posts that address using the load statement (Qlikview Calculated Fields with Load Script) but have been unsuccessful using calculated variables to create new variables.
Example code (which works):
SQL SELECT *,
case when (Risk = 'H') then 1
else 0
end as Risk_H
FROM [Data];
How can I create Risk_H in order to use it in the same script, like the below? In other settings, I would use something like "calculated Risk_H" to refer to it.
SQL SELECT *,
case when (Risk = 'H') then 1
else 0
end as Risk_H,
(10 + Risk_H*2) as Rating // Qlikview says it can't find Risk_H
FROM [Data];
I've tried creating Risk_H in a load script, but Qlikview doesn't recognize Risk_H in a later SQL statement. I've also tried creating a table with Risk_H , and pulling the data from that table. And in reality I'm trying to create 10+ indicators, not just one, so nested case statements aren't the answer.
EDIT: I'm told that resident tables may be the answer to performing calculations. If you can provide syntax for this using tables connected via ODBC that may answer the question.
It appears that your second Select statement is not valid SQL so as a result QlikView will complain that it cannot find Risk_H. You could try a more complicated SQL query with a sub-query to resolve this, or you could use a resident load in QlikView as follows:
Source_Data:
SQL SELECT *,
case when (Risk = 'H') then 1
else 0
end as Risk_H
FROM [Data];
Calculated_Data:
NOCONCATENATE
LOAD
*,
(10 + Risk_H*2) as Rating
RESIDENT Source_Data;
DROP TABLE Source_Data;
You also mentioned that you have around 10 indicators that you wish to use, so I agree, a case statement would probably not be a good idea. You can move this part into QlikView as well if you like using a MAPPING load and the ApplyMap function as follows:
Indicator_Map:
MAPPING
LOAD
*
INLINE [
Risk, Value
H, 1
I, 2
J, 3
];
Source_Data:
SQL SELECT *,
case when (Risk = 'H') then 1
else 0
end as Risk_H
FROM [Data];
Calculated_Data:
NOCONCATENATE
LOAD
*,
(10 + (ApplyMap('Indicator_Map',Risk, 0) * 2)) as Rating
RESIDENT Source_Data;
DROP TABLE Source_Data;
I added a couple of extra entries for your Risk "indicators" to give you an idea. Of course, the table doesn't need to be inline, it could come from another SQL statement, other file etc.
In the above example, what happens is that the Risk field's value is supplied as a parameter to the mapping table Indicator_Map which then returns the associated value. If no risk value is found, it returns 0 (the third parameter).