Lets say I have a table A with attribute numbers that looks like this.
A
numbers
1
2
3
4
5
6
7
8
9
10
What will this query return? How is the 5 getting compared?
SELECT numbers
FROM A
WHERE 5 > ALL (SELECT numbers FROM a)
The ALL statement requires that ALL of the results returned by your subquery
(SELECT numbers FROM A)
to respect the condition (to be smaller than 5), otherwise the condition is not met and no results are returned.
In your case, there are numbers returned by the subquery, SELECT numbers FROM a, 6, 7, 8, 9, 10 which are greater than 5, thus not ALL numbers respect the condition, so the condition is evaluated to FALSE, and no rows are returned.
Update:
Based on your comments I added details to my answer:
The statement using ALL condition should be read as:
"If ALL of the numbers returned by (SELECT numbers FROM A) are smaller than 5, then return the numbers selected by your MAIN SELECT."
The statement using ANY condition should be read as:
"If ANY of the numbers returned by (SELECT numbers FROM A) are smaller than 5, then return the numbers selected by your MAIN SELECT."
You can run the query in this SQLFiddle to see how the results change, just replace ANY with ALL and see the difference.
It will return an empty resultset (no rows).
The WHERE clause is evaluated for each row in the table A [first instance].
The WHERE clause tests whether 5 is greater than EACH row in table A [second instance].
It is not (there are several rows where the value is greater than 5) so the WHERE clause is always false.
Therefore no rows from table A [first instance] pass the query, therefore no rows are returned.
Related
Within my database my data can look one of two ways
1 -
hh_match_count: 5,
hh_total_fhc_0: 6,
hh_total_fhc_1: 5,
hh_total_fhc_2: 3,
hh_total_fhc_3: 2,
hh_total_fhc_4: 4
2 -
hh_match_count: 3,
hh_total_fhc_0: 6,
hh_total_fhc_1: 5,
hh_total_fhc_2: 3,
hh_total_fhc_3: null,
hh_total_fhc_4: null
What I want to do is calculate the number of times a value is >= 1 (will want to expand this to >= 2, >= 3 etc) from each of hh_total_fhc_0, hh_total_fhc_1, hh_total_fhc_2, hh_total_fhc_3, hh_total_fhc_4 and then divide that by hh_match_count. So basically getting the % of occurrences.
What query should I be looking at executing here? Slowly getting more involved with SQL statements.
coalesce returns the first non-null value it's passed. That turns your null values into zeroes, since you need to count them as zero for the average. Next step is to add least to the mix:
SELECT least(1, coalesce(hh_total_fhc_0, 0)) FROM fixtures gives you a 0 if the value is zero (crucially, null isn't a number so least(1, null) is 1!), and a 1 if it's a positive value. Apply that to each of your columns and then you can calculate the hit percentage exactly as you were thinking.
SELECT AVG(f.P_PRICE_LOW) as TOP10_Average
FROM (SELECT top 10 P_PRICE_LOW
FROM fp_basic_bd
WHERE fs_perm_sec_id='B00242-S-US'
ORDER BY fs_perm_sec_id
) as f
By using this query I am calculating the average of top 10 price values. My question is:
How the average will be calculated if the subquery returns less than 10 values?
The average is calculated over whatever the inner query returns, with NULL values being ignored by AVG().
If inner query returns 10 non-null values, then result = SUM(10 values) / 10.
If inner query returns 3 non-null values, then result = SUM(3 values) / 3.
If inner query returns no non-null values or even no values at all, then result = NULL.
You can think of TOP as restricting the total number of rows returned, not adding or manipulating any values. So if your query returns 90 rows total, TOP will just return the first N of those, so the first 10 in your case. If there are less than N rows returned, then TOP will return all rows found since it did not exceed the maximum value specified.
For your specific case, if your subquery returned <10 rows, the average value found would be based on those rows instead of 10. Since you are using the AVG function instead of manually finding the average yourself, the value found will still be the correct average of the rows found. So if you have 6 rows returned in the subquery, AVG would do the same as (r1 + r2 + ... + r6)/6.
I have a Select query:
SELECT
SAMPLE_NUMBER, SAMPLE_TYPE, STORAGE_ADDRESS, EXTERNAL_NUMBER
FROM
SYSTMX2.TM2_SAMPLES
And what I need is to add two more columns with the results of some .Net syntax code so I would end up with something like"
SELECT
SAMPLE_NUMBER, SAMPLE_TYPE, STORAGE_ADDRESS, EXTERNAL_NUMBER,
SomeCodearound(STORAGE_ADDRESS) as RowPosition,
SomeCodearound(STORAGE_ADDRESS) as ColumnPosition
FROM
SYSTMX2.TM2_SAMPLES
The row and column positions are based upon where they would fall in a 9 x 9 Grid. 9 numbers for columns across the top and 9 numbers as rows down the side. This would be a lab specimen box that would hold 81 vials. Every vial has a number from 1 to 81 and is the last three characters of the STORAGE_ADDRESS value similar to FR2-S01-R01-001 or FR2-S01-R01-081. Vial number 1 would be in column 1 and row 1; vial 81 in row 9 column 9. My .net code for the row is to take the last three character of the STORAGE_ADDRESS and test with decimal.
TryParse(STORAGE_ADDRESS.Substring(STORAGE_ADDRESS.Length - 3), value)
and then take that value and convert with:
CInt(Decimal.Truncate(((value+ 9 - 1) / 9))).
And the column code is:
(value + 9) - (CInt(Decimal.Truncate(((value + 9 - 1) / 9))) * 9).
I need to make it into an inline Select statement that I can call from a web service to the Oracle server, I do not have any way to create anything database side. Right now what I do is to call the result recordset add a couple of columns and loop the results and add the values. I know there has to be a better way.
This Oracle query returns row and column positions:
select storage_address,
floor((to_number(substr(storage_address, 13, 3))-1)/9)+1 rowposition,
mod(to_number(substr(storage_address, 13, 3))-1, 9)+1 colpostion
from t
Here is SQLFiddle with test values.
I have a process that needs to select rows from a Table (queued items) each row has a quantity column and I need to select rows where the quantities add to a specific multiple. The mulitple is the order of between around 4, 8, 10 (but could in theory be any multiple. (odd or even)
Any suggestions on how to select rows where the sum of a field is of a specified multiple?
My first thought would be to use some kind of MOD function which I believe in SQL server is the % sign. So the criteria would be something like this
WHERE MyField % 4 = 0 OR MyField % 8 = 0
It might not be that fast so another way might be to make a temp table containing say 100 values of the X times table (where X is the multiple you are looking for) and join on that
I've got a simple table with 300 rows, and after ordering them I want to select rows 11-50. Do I limit by 50 and remove the top 10 rows somehow?
SELECT *
FROM table
ORDER BY somecolumn
LIMIT 10,40
From MySQL's manual:
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1)
The LIMIT syntax includes an offset value, so you'd use:
LIMIT 10, 40
...to get rows 11 - 50, because the initial offset row is zero (not 1).