I have bellow snippet table structure.In this table there is one row count_set that have two values first is 1 and second is 1 here i have to make the sum of this value the sum answer will be 2.
I tried with bellow query but not getting sum value its getting sum with blank value.
so can any one tell me how to do this.
$sql_chk_current_sum_count = "SELECT sum(count_set) FROM wp_lead_count WHERE
end_date = $end_date";
$final_sum= $wpdb->get_results($sql_chk_current_sum_count) or die(mysql_error());
Try this:
SELECT sum(count_set) FROM wp_lead_count WHERE
date_format(end_date,'%Y-%m-%d') = date_format($end_date,'%Y-%m-%d');
Related
I've been struggling to build a query that calculate the sum of column called 'TIDAL_VOLUME' with respect to date value that's coming from another table.
Please see the content of the Table_1:
Please see the content of the Table_2:
Note: TIDAL_VOLUME might have NULL as well.
Now, the start time for O2_Device value 'Endotracheal tube' is '2013-08-06 08:10:05' for same HADM_ID and SUBJECT_ID. and end time is whenever new O2_Device value comes in. In this case which is 'Nasal cannula'. Which means start time for 'Endotracheal tube' is '2013-08-06 08:10:05' and end time is '2013-08-06 10:15:05' for HADM_ID = 1 and SUBJECT_ID = 100.
Using that start time and end time criteria, I have to look for TIDAL_VALUE in Table_2. In this example it's 700, 800. Ans for TIDAL_VOLUME is 1500.
Please see the resultant output look like this:
Thanks in advance.
If you can add End_Time to the first table, you can use BETWEEN when you join the tables.
SELECT t1.HADM_ID, t1.Subject_ID, t1.ChartTime, SUM(t2.tidal_volume) AS tidal_volume
FROM Table_1 AS t1
JOIN Table_2 AS t2
ON t1.HADM_ID = t2.HADM_ID
AND t1.Subject_ID = t2.Subject_ID
AND t2.ChartTime BETWEEN t1.ChartTime AND t1.End_Time
GROUP BY t1.HADM_ID, t1.Subject_ID, t1.ChartTime
I am trying to find the sum of the same id values.
$tournament = Tournament::find($id);
$topTen = DB::table('match_scoreboard_batting')
->join('matches','match_scoreboard_batting.match_id','=','matches.id')
->where('matches.tournament_id',$id)
->select('match_scoreboard_batting.player_id','match_scoreboard_batting.runs')
->sum('match_scoreboard_batting.runs');
echo "<pre>";
print_r($topTen);
I know how to get the sum of all id values. But how can I get the sum of the same id values?
Please try this query.
$topTen = DB::table('match_scoreboard_batting')
->join('matches','match_scoreboard_batting.match_id','=','matches.id')
->where('matches.tournament_id',$id)
->select('match_scoreboard_batting.player_id', \DB::raw(' sum(match_scoreboard_batting.runs) as runs'));
You can use :
from laravel docs
DB::raw('SUM(price) as total_sales')
hope it will work for you.
Use sum() would cause to group by primary key (I guess here is player_id) automatically. If you want to get sum() of each match, tournament, or player you are supposed to add ->groupBy('some_id') in your query.
My current query looks like this:
SELECT * FROM `Entrys` WHERE `Timestamp` > 1469308755 AND `Timestamp` < 1469308765 AND (`Exchange` = 1 OR `Exchange` = 2) AND `Market` = 1
It gives me the following result:
For each timestamp value (1469308756 and 1469308761) , I want to calculate the spreads between the prices like this:
For each timestamp-value
For each Exchange
For each Other-Exchange
Divide Exchanges price where type = 2 by Other-Exchanges price where type = 1
This is the best I can explain it. If you don't understand it please leave a comment and I'll try to explain it better.
With the above data it should work like this:
Format: EnId.Field
943.Price / 940.Price
944.Price / 939.Price
961.Price / 985.Price
962.Price / 957.Price
The calculated numbers should be the output of the query.
My SQL skills are way to low for that. Is it even possible to do such calculation inside a single query?
You need to use a JOIN. A JOIN is useful when you want data from two different rows to be referenced in the same expression (either an expression in the WHERE clause or an expression in the select-list).
So you need to join a row to another row that satisfies these conditions:
the same timestamp
a different exchange
the first row is type=2 and the second row is type=1
When you're doing a self-join like this one, you must use table aliases. I will use "numerator" and "denominator" for the two rows.
SELECT numerator.timestamp, numerator.price / denominator.price AS spread
FROM Entrys AS numerator
JOIN Entries AS denominator ON numerator.timestamp=denominator.timestamp
AND numerator.exchange <> denominator.exchange
AND numerator.type = 2 AND denominator.type = 1;
second = Trade.with_currency(currency).h24.order('id desc').limit(1).offset(1).try(:price)
first = Trade.with_currency(currency).h24.order('id desc').first.try(:price)
I want to get the price of the first and the second row. The first one is working but I have problems to get the second row. With above query I get nil back.
when i do the query for the second row directly in mysql it is working:
SELECT `trades`.* FROM `trades` WHERE `trades`.`currency` = 5 AND (created_at > '2014-01-11 16:20:06') ORDER BY id desc LIMIT 1 OFFSET 1;
prev = Trade.with_currency(currency).order('id desc').limit(1).offset(1).first.try(:price)
this is the solution - I added to select the first result offsetting by 1.
If you just need the values:
Trade.with_currency(currency).h24.order('id desc').pluck(:price).first(2)
The pluck method fetches just the values and first(2) is used to pull the first two values.
with offset(1) add first in the statement
it will go like
second = Trade.with_currency(currency).h24.order('id desc').limit(1).offset(1).first.try(:price)
I'm working with Hibernate and I want to obtain a substraction from two values fetched from two diffeent subqueries but I am gettin the following error:
SQLGramarEception: could not execute query
SELECT termQuery.term.id,
(SELECT COALESCE(MIN(termResult2.position),101) FROM termQuery2.results termResult2 WHERE termResult2.url.hostname MEMBER subscription.account.domains) as **prevPosition**,
(SELECT COALESCE(MIN(termResult.position),101) FROM termQuery.results termResult WHERE termResult.url.hostname MEMBER subscription.account.domains) as **currentPosition**,
prevPosition - currentPosition as change --<<<<<<
FROM TermSubscription subscription, TermQuery termQuery, TermQuery termQuery2
WHERE subscription.account.id=1197
AND termQuery.term=subscription.term
AND termQuery.provider.id=2
AND termQuery.queryDate.yearWeek = '201242'
AND termQuery2.term=termQuery.term
AND termQuery2.provider.id=termQuery.provider.id
AND termQuery2.queryDate.yearWeek = '201241'
ORDER BY subscription.term.id, termQuery.queryDate.yearWeek
I know that it is not possible to substract those two values like that but I don't know the way to do it. So, what would be the way?
Thanks
select only the 3 other values, and while iterating on the results of the query, subtract the currentPosition from the prevPosition, in Java.