subquery - MySql: Calculate a value based on multiple sub-queries -
i have query calculates value based on several sub-queries. sub-queries based on specific timestamp. calculate value records many different timestamps. can't quite figure out how it.
the basic formula (s11s4createsessionreqrcvd - s11s4createsessionrespaccsent) * 100 / s11s4createsessionrespaccsent
select ((((select ref_data test.sgw_s5s11 timestamp = "2013-08-21 00:00:06" , ref_type = "s11s4createsessionreqrcvd" ) - (select ref_data test.sgw_s5s11 timestamp = "2013-08-21 00:00:06" , ref_type = "s11s4createsessionrespaccsent")) * 100) / (select ref_data test.sgw_s5s11 timestamp = "2013-08-21 00:00:06" , ref_type = "s11s4createsessionreqrcvd")) mycalc
i don't know how paste in table here's sample of data in csv format
here's sample of data. (i don't know how put table in here it's in csv format)
mykey,timestamp,ref_type,ref_data 1016101,"2013-08-21 00:00:06",s5s8createsessionreqsent,128042907 1016102,"2013-08-21 00:00:06",s5s8createsessionrespaccrcvd,127088838 1016103,"2013-08-21 00:00:06",s5s8createsessionresprejrcvd,615553 1016104,"2013-08-21 00:00:06",s5s8createbearerreqrcvd,10047 1016105,"2013-08-21 00:00:06",s5s8createbearerrespaccsent,9932 1016106,"2013-08-21 00:00:06",s5s8createbearerresprejsent,103 1016107,"2013-08-21 00:00:06",s11s4createsessionreqrcvd,128255390 1016108,"2013-08-21 00:00:06",s11s4createsessionrespaccsent,127114539 1016109,"2013-08-21 00:00:06",s11s4createsessionresprejsent,713325 1016110,"2013-08-21 00:00:06",s11s4createbearerreqsent,10028 1016111,"2013-08-21 00:00:06",s11s4createbearerrespaccrcvd,9932 1016112,"2013-08-21 00:00:06",s11s4createbearerresprejrcvd,42
any appreciated!!!
this query awkward because have stored attributes on separate rows. design called entity-attribute-value, , it's bad idea relational database.
the following query might little more efficient , easier write:
select (eav.reqrcvd - eav.accsent) * 100 / eav.accsent mycalc ( select timestamp, max(if(ref_type='s11s4createsessionreqrcvd', ref_data)) reqrcvd, max(if(ref_type='s11s4createsessionrespaccsent', ref_data)) accsent test.sgw_s5s11 timestamp = '2013-08-21 00:00:06' group timestamp ) eav;
ps: use single quotes '
string , date literals, not double quotes "
. in standard sql, double quotes delimiting table , column identifiers. mysql treats 2 types of quotes same default, subject sql_mode , won't behave same way if ever use brand of rdbms. it's develop habit of using quotes in standard way.
Comments
Post a Comment