I have a table, gainfinal, which consists of three columns: countrycode, year and values.
I want to calculate z values for all the rows.
I did a simple query like this.
SELECT (
`values` - STDDEV( `values` )
) / AVG( `values` ) AS zvalue
FROM `gainfinal`
But the query returned only one row. How can I perform same thing for all the rows?
How about this?
DECLARE #stdev AS FLOAT, #avg AS FLOAT
SELECT #stdev = STDEV([values]), #avg = AVG([values])
FROM gainfinal
SELECT
countrycode
, year
, [values]
, ([values] - #stdev) / #avg AS zvalue
FROM gainfinal
You need a subquery to get the aggregated values:
SELECT (gf.`values` - stats.average) / stats.s AS zvalue
FROM `gainfinal` gf cross join
(select sttdev(values) as s, avg(values) as average from gainfinal) stats;
In most databases, your original query would generate an error, because it is mixing aggregated columns and non-aggregated columns in a query with no group by. The solution is a separate subquery that does the calculation.
Related
I want to get the sum of the two tables.
1-1.hospital_payment_data table
1-2.hospital_payment_data table data
2-1.advenced_payment table
2-2 advenced_payment table data
I want result
However, sending the query as follows results.
SELECT COUNT(IF(treatment_fees_check_division = '선수금',
treatment_fees_check_division, NULL)) AS COUNT,
SUM(CASE WHEN treatment_fees_check_division = '선수금'
THEN treatment_fees_difference END) + SUM(advenced_amount
) AS if_treatment_fees_check_division,
SUM(advenced_amount) AS sum_init_amount
FROM
(
SELECT treatment_fees_check_division, treatment_fees_difference,
init_amount, advenced_amount
FROM hospital_payment_data , advenced_payment
)AS a
bad result
How do I fix the query? I'd like your posture, please.
Is this what you are looking for?
SELECT ( SELECT SUM(...) FROM t1 ... ) +
( SELECT SUM(...) FROM t2 ... ) AS sum_of_tables
This s my first question on SO, so please bear if I am not super clear! I am trying to sum up values of 2 columns from 2 tables. Both table values have a common ID called 'imid'. These imids are further divided as 'pmid' (say 1 imid can have one or multiple pmids). Both tables have different structures. I would want to sum values on 1 column from table1 and another column from table(to use it for a php calculation). When I try JOIN it always gives me a timeout error. Query below.
SELECT F.`imid` AS imid, SUM( F.Impression ) AS si, D.accmgr AS accmgr, D.cmname AS cmname,sum(D.Item_Impression_Cap) AS sim
FROM trasactions F, rawdata D
WHERE F.`imid` = D.`imid`
AND F.EntryDate LIKE '2017-%-%'
GROUP BY D.`imid`, F.`imid` ORDER BY F.`imid` ASC
I get results, but not even close to the correct numbers.(eg) Impression as 6,557,824 instead of 1,233,287 for a particular imid.
Not sure where I am wrong! Any help would be great...
Edit: Thanks for the responses...I managed to write the query...
SELECT F.`imid`, F.si , F.imname,D.Item_Start_Date ,D.sim, D.Item_End_Date, D.accmgr, D.cmname
FROM (SELECT `imid` AS imid, SUM(Impression ) AS si, adname AS imname, EntryDate FROM trasactions GROUP BY imid) F LEFT JOIN
(SELECT imid,Item_Start_Date, Item_End_Date, accmgr AS accmgr, cmname AS cmname, sum(Item_Impression_Cap) AS sim FROM rawdata GROUP BY imid) D ON F.`imid`=D.`imid`
WHERE D.cmname IS NOT NULL
GROUP BY F.`imid`
ORDER BY F.`imid` ASC
Now there is a new question! I am doing some calculations based on the array values derived from the query above...(eg) $pacing = $row['si']/$avgday*100;
Say I am listing the 'Pacing' for all items active. Would it be possible to count values from variables. (eg) Would want to show the count of items where $pacing is less than 100%. Would it even be possible to do that!! Thanks again.
I have two tables, brp_consume_log and brp_transfer_log, I joined both table with query:
SELECT *
FROM brp_consume_log a
JOIN brp_transfer_log b ON b.acct_id = a.acct_id
WHERE a.acct_id ='1657'
result output as:
I would like to show the results in below format:
is it possible?
UPDATE
I've been able to use query below to output results that I want:
SELECT
datetime,
remark,
intake,
consume
FROM
(
SELECT datetime, remark, points_intake intake, NULL consume, 1 ord
FROM brp_transfer_log
WHERE acct_id = '1657'
UNION ALL
SELECT datetime, remark, NULL, point_consume, 2
FROM brp_consume_log
WHERE acct_id = '1657'
) q
ORDER BY datetime DESC, ord
By the way, how do I add another field for balance points and do necessary subtraction or addition from last balance points?
Please advise. thanks!
I am trying to calculate the difference of values list coming from a database.
I would like to achieve it using php or mysql, but I do not know how to proceed.
I have a table named player_scores. One of its rows contains the goals scored.
Ex.
pl_date pl_scores
03/11/2014 18
02/11/2014 15
01/11/2014 10
I would like to echo the difference between the goals scored during the matches played in different dates.
Ex:
pl_date pl_scores diff
03/11/2014 18 +3
02/11/2014 15 +5
01/11/2014 10 no diff
How can I obtain the desired result?
You seem to want to compare a score against the score on a previous row.
Possibly simplest if done using a a sub query that gets the max pl_date that is less than the pl_date for the current row, then joining the results of that sub query back against the player_scores table to get the details for each date:-
SELECT ps1.pl_date, ps1.pl_scores, IF(ps2.pl_date IS NULL OR ps1.pl_scores = ps1.pl_scores, 'no diff', ps1.pl_scores - ps1.pl_scores) AS diff
FROM
(
SELECT ps1.pl_date, MAX(ps2.pl_date) prev_date
FROM player_scores ps1
LEFT OUTER JOIN player_scores ps2
ON ps1.pl_date > ps2.pl_date
GROUP BY ps1.pl_date
) sub0
INNER JOIN player_scores ps1
ON sub0.pl_date = ps1.pl_date
LEFT OUTER JOIN player_scores ps2
ON sub0.prev_date = ps2.pl_date
There are potentially other ways to do this (for example, using variables to work through the results of an ordered sub query, comparing each row with the value stored in the variable for the previous row)
SELECT score FROM TABLE WHERE DATE = TheDateYouWant
$score = $data['score'];
SELECT score FROM TABLE WHERE date = dateYouWant
$difference = $score - $data['score'];
Something like this?
You could use two queries, one to get the value to use in the comparison (in the example below is the smaller number of scores) and the second one to get the records with a dedicated column with the difference:
SELECT MIN(pl_scores);
SELECT pl_date, pl_scores, (pl_scores - minScore) as diff FROM player_scores;
Or, using a transaction (one query execution php side):
START TRANSACTION;
SELECT MIN(Importo) FROM Transazione INTO #min;
SELECT Importo, (Importo - #min) as diff FROM Transazione;
select *,
coalesce(
(SELECT concat(IF(t1.pl_scores>t2.pl_scores,'+',''),(t1.pl_scores-t2.pl_scores))
FROM tableX t2 WHERE t2.pl_date<t1.pl_date ORDER BY t2.pl_date DESC LIMIT 1)
, 'no data' ) as diff
FROM tableX t1
WHERE 1
order by t1.pl_date DESC
I have a SQLite Database with the following structure:
rowid ID startTimestamp endTimestamp subject
1 00:50:c2:63:10:1a 1000 1090 entrance
2 00:50:c2:63:10:1a 1100 1270 entrance
3 00:50:c2:63:10:1a 1300 1310 door1
4 00:50:c2:63:10:1a 1370 1400 entrance
.
.
.
I have prepared a sqlfiddle here: http://sqlfiddle.com/#!2/fe8c6/2
With this SQL-Query i can get the average differences between the endTime and the startTime between one row and the following row, sorted by subject and ID:
SELECT
id,
( MAX(endtimestamp) - MIN(startTimestamp)
- SUM(endtimestamp-startTimestamp)
) / (COUNT(*)-1) AS averageDifference
FROM
table1
WHERE ID = '00:50:c2:63:10:1a'
AND subject = 'entrance'
GROUP BY id;
My problem: To calcute the average value is no problem, that does this query. But how can i
get the standard deviation and the variance of this values?
First finding the time differences of interest by joining the table to itself and grouping by ID, then finding the averages, variances as V(x) = E(x^2) - (E(x))^2 and standard deviation as sqrt(V)gives
SELECT ID, AVG(diff) AS average,
AVG(diff*diff) - AVG(diff)*AVG(diff) AS variance,
SQRT(AVG(diff*diff) - AVG(diff)*AVG(diff)) AS stdev
FROM
(SELECT t1.id, t1.endTimestamp,
min(t2.startTimeStamp) - t1.endTimestamp AS diff
FROM table1 t1
INNER JOIN table1 t2
ON t2.ID = t1.ID AND t2.subject = t1.subject
AND t2.startTimestamp > t1.startTimestamp -- consider only later startTimestamps
WHERE t1.subject = 'entrance'
GROUP BY t1.id, t1.endTimestamp) AS diffs
GROUP BY ID
For formulas that are more complex than simple summation, you have to compute the actual difference values for each record by lookin up the corresponding next start times, like this:
SELECT (SELECT MIN(startTimestamp)
FROM table1 AS next
WHERE next.startTimestamp > table1.startTimestamp
AND ID = '...'
) - endTimestamp AS timeDifference
FROM table1
WHERE nextStartTimestamp IS NOT NULL
AND ID = '...'
Then you can use all the difference values to do the calculations:
SELECT SUM(timeDifference) / COUNT(*) AS average,
AVG(timeDifference) AS moreEfficientAverage,
SUM(timeDifference * timeDifference) / COUNT(*) -
AVG(timeDifference) * AVG(timeDifference) AS variance
FROM (SELECT (SELECT MIN(startTimestamp)
FROM table1 AS next
WHERE next.startTimestamp > table1.startTimestamp
AND next.ID = '...'
) - endTimestamp AS timeDifference
FROM table1
WHERE nextStartTimestamp IS NOT NULL
AND ID = '...')
A number of points:
Your formula for the mean is wrong the correct formula is SUM(endtimestamp-starttimestamp)/COUNT(endtimestamp). I have no idea why you have the MIN/MAX terms. COUNT(*) will count NULL rows and will give the wrong result.
SQLlite has an avg function which finds the mean.
The formula for the variance is SUM((endtimestamp-starttimestamp)*(endtimestamp-starttimestamp)) - AVG(endtimestamp-starttimestamp)*AVG(endtimestamp-starttimestamp)
The standard deviation is the square root of the variance.
In response to the question authors comment, in order to compute the variance the start and end times must be paired with each other through a self join.
Becuase of the absence of a row_number function in SQL lite this is a little inelegant.
SELECT id,
AVG(startTimestamp-endTimestamp) as mean,
SUM((startTimestamp-endTimestamp)^2) - AVG(startTimestamp-endTimestamp)^2 as variance,
SQRT(SUM((startTimestamp-endTimestamp)^2) - AVG(startTimestamp-endTimestamp)^2) as stDev
FROM
(SELECT
t1.id,
t1.endTimestamp,
MIN(t2.startTimestamp) as starttimestamp
FROM table1 t1
INNER JOIN
table1 t2 ON t1.endTimestamp<=t2.startTimestamp
GROUP BY t1.id, t1.endTimestamp) t
GROUP BY id;
See SQL Fiddle