How to write an expression for two different attributes in the same field in qlikview - 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

Related

SQL - HAVING (execution vs structure)

I'm a beginner, studying on my own... please help me to clarify something about a query: I am working with a soccer database and trying to answer this question: list all seasons with an avg goal per Match rate of over 1, in Matchs that didn’t end with a draw;
The right query for it is:
select season,round((sum(home_team_goal+away_team_goal) *1.0) /count(id),3) as ratio
from match
where home_team_goal != away_team_goal
group by season
having ratio > 1
I don't understand 2 things about this query:
Why do I *1.0? why is it necessary?
I know that the execution in SQL is by this order:
from
where
group
having
select
So how does this query include: having ratio>1 if the "ratio" is only defined in the "select" which is executed AFTER the HAVING?
Am I confused?
Thanks in advance for the help!
The multiplication is added as a typecast to convert INT to FLOAT because by default sum of ints is int and the division looses decimal places after dividing 2 ints.
HAVING. You can consider HAVING as WHERE but applied to the query results. Imagine the query is executed first without HAVING and then the HAVING condition is applied to result rows leaving only suitable ones.
In you case you first select grouped data and calculate aggregated results and then skip unnecessary results of aggregation.
the *1.0 is used for its ".0" part so that it tells the system to treat the expression as a decimal, and thus not make an integer division which would cut-off the decimal part (eg 1 instead of 1.33).
About the second part: select being at the end just means that the last thing
to be done is showing the data. Hoewever, assigning an alias to a calculated field is being done, you could say, at first priority. Still, I am a bit doubtful; I am almost certain field aliases cannot be used in the where/group by/having in, say, sql server.
There is no order of execution of a SQL query. SQL is a descriptive language not a procedural language. A SQL query describes the result set that the query is producing. The SQL engine can execute it however it likes. In fact, most SQL engines compile the query into a directed acyclic graph, which looks nothing like the original query.
What you are referring to might be better phrased as the "order of interpretation". This is more simply described by simple rules. Column aliases can be used in the ORDER BY clause in any database. They cannot be used in the FROM, WHERE, or GROUP BY clauses. Some databases -- such as SQLite -- allow them to be referenced in the HAVING clause.
As for the * 1.0, it is because some databases -- such as SQLite -- do integer arithmetic. However, the logic that you want is probably more simply expressed as:
round((avg(home_team_goal + away_team_goal * 1.0), 3)

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

Apply a single case statement to all columns in sql

I need to get the sum of each column of my table. So i used select sum(col1),col2 etc.
If the sum is null, i need to get 0, else the value of the sum. So I used "select case when sum(col1) is null then 0 else sum(col1) end as sum_col1".
I have around 40 such columns in my table. Do i need to write " case when sum(col n) then..." 40 times in my query?
Im working on oracle 9 g.
Thanks
I have around 40 such columns in my table. Do i need to write " case
when sum(col n) then..." 40 times in my query?
Short answer: Yes.
Longer answer: You might be able to use some kind of dynamic SQL to generate the statement automatically from the column metadata. But it might not be worth the trouble, as you can often just as easily copy-paste the statement in your query editor. All things considered, having a table with 40 columns that you need to sum, indicates a bad data model design. When working with a badly designed data model, you pay the price at query time...

Sql custom statement

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.

SQL statement HAVING MAX(some+thing)=some+thing

I'm having trouble with Microsoft Access 2003, it's complaining about this statement:
select cardnr
from change
where year(date)<2009
group by cardnr
having max(time+date) = (time+date) and cardto='VIP'
What I want to do is, for every distinct cardnr in the table change, to find the row with the latest (time+date) that is before year 2009, and then just select the rows with cardto='VIP'.
This validator says it's OK, Access says it's not OK.
This is the message I get: "you tried to execute a query that does not include the specified expression 'max(time+date)=time+date and cardto='VIP' and cardnr=' as part of an aggregate function."
Could someone please explain what I'm doing wrong and the right way to do it? Thanks
Note: The field and table names are translated and do not collide with any reserved words, I have no trouble with the names.
Try to think of it like this - HAVING is applied after the aggregation is done.
Therefore it can not compare to unaggregated expressions (neither for time+date, nor for cardto).
However, to get the last (principle is the same for getting rows related to other aggregated functions as weel) time and date you can do something like:
SELECT cardnr
FROM change main
WHERE time+date IN (SELECT MAX(time+date)
FROM change sub
WHERE sub.cardnr = main.cardnr AND
year(date)<2009
AND cardto='VIP')
(assuming that date part on your time field is the same for all the records; having two fields for date/time is not in your best interest and also using reserved words for field names can backfire in certain cases)
It works because the subquery is filtered only on the records that you are interested in from the outer query.
Applying the same year(date)<200 and cardto='VIP' to the outer query can improve performance further.