I have one table named tripdetails:
table structure:
**trip no** **invoice no** **total balance**
111 1 500
111 2 800
i want to add these two invoices total balance...means 500+800=1300....
SELECT SUM(total_balance) as invoice_total FROM tripdetails
We have to use aggregate functions to do sum of a column.There is a pre-defined function which helps to obtain your desired output.
Syntax :
select sum(column_name) as alias
from table_name
SELECT trip_no,SUM(total_balance) as total
FROM tripdetails
WHERE trip_no = 111
GROUP BY trip_no;
This should do it. It selects both rows with trip_no 111, then groups them and selects the sum of balances.
Related
I have a table like this
item_id item_quantity
----------------------
324 2
432 23
543 12
879 3
The item_id field is not auto-increment but it is unique.
The inputs of the query will be some item_id values and the result should be sum of those in item_quantity.
For instance, if the inputs are 324, 543, then the query should return 14 (2 + 12)
Is it possible to do this at mysql level?
Try using in :
SELECT SUM(item_quantity)
FROM table
WHERE item_id in (324,543)
This works on MSSQL, not sure about MySQL, though:
SELECT SUM(item_quantity) FROM MyTable WHERE item_id in (324, 543)
EDIT: OK, others got the same answer. So I guess it also works on MySQL.
If all you need is the sum of the item_quantity, all you need to do is:
Select Sum(item_quantity)
From Table
Where item_id In (324, 543)
From this post, enter link description here
I would like to improve the query
SELECT `BetType`,
count(`BetType`) AS COUNT,
sum(`BetAmount`) AS BetAmountTotal,
sum(`Payout`) AS PayoutTotal
FROM `betdb`
LEFT JOIN `matchdb` ON `betdb`.`MatchID` = `matchdb`.`MatchID`
WHERE `betdb`.`MatchID`=135
GROUP BY `BetType`
thanks to Sadikhasan, who helped on this query
I would like to add another row showing the totals of the columns
BetType Count BetAmount Total Payout Total
Handi 2 60000 950000
Homerun Count 4 10000 0
Total 6 70000 950000
this seems to be needing another SELECT statement but how would I put another row explicitly showing the "Total" string and getting the sum of the previously used columns with Aggregate Functions?
You can use WITH ROLLUP modifier to GROUP BY, which will give you another row with totals, but the column you group on (BetType) will show NULL for that row. But nothing stops you from using COALESCE() to replace that NULL with 'Total' string.
SELECT COALESCE(`BetType`,'Total') AS BetType,
COUNT(*) AS `Count`,
sum(BetAmount) AS BetAmountTotal,
sum(Payout) AS PayoutTotal
FROM betdb
WHERE betdb.MatchID=135
GROUP BY BetType WITH ROLLUP
I have for example a query with return something as it
route value
1 3
2 2
3 4
4 5
5 1
then I need to put in 2 textbox the max and the min route so in sql this would be
select top 1 route from table where value=(select max(value) from table)
I add a image done in excel, how this would be.
I believe this is so easy but I dont have idea how to get it.
I got using expression, this was extactly expression
="Route "+
Convert.ToString (
Lookup(max(fields!value.Value),fields!value.Value ,fields!route.Value,"mydataset")
)
changing max for min, for the other...
thanks everyone.
I believe the query you're looking for would be:
With Min_Max_CTE as (
Select MIN(value) as Min_Value
, MAX(value) as Max_Value
From Table
)
Select Top 1 'Min' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Min_Value
Union All
Select Top 1 'Max' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Max_Value
Order by Type desc --This will put the Min Route first followed by the Max Route
Then, put that query into a dataset, and then create a tablix and use the Type, route, and value fields to return the minimum route and the maximum route. It should end up being set up just like your excel section with the min and max routes above.
You can do this SSRS by using a couple of separate tables. Your example data:
And two tables in the Designer:
Since the tables only have header rows, only the first row in the table will be displayed.
To make sure we get the MAX and MIN values in the two tables, each table needs to order its Dataset appropriately, i.e. by Value by descending and ascending respectively.
MAX table:
MIN table:
Which gives your expected result:
I have data like
column
1
1
57
57
57
1
1
57
57
I need to generate count data like
count
2
3
2
2
using mysql query. I have tried group by but it groups all the values also i couldn't group by any other fields. Is there any easy way to achieve this?
find the screenshot in this link
Update:
My end goal is to get the time spend by the user on each course . like the user who has ID 1 have spend time from 1177924991 to 1177925038 ( here 1177925038 is one second minus the next course visit time) on course id 1
Use this query:
SELECT COUNT(*)
FROM `tablename`
GROUP BY `info`
try using
SELECT COUNT(columnname)
FROM `tablename`
GROUP BY columnname
This will do the needful.
UPDATE
I am getting following result set on running this query :
Use This Query:
Select `course`, count(*) from `tablename`
group by `course`
I need to query in MS Access the difference in value of a column to 8 and only if it is greater than 8.
So if I have a column of numbers 1-10, I want to query the sum of all the value's differences from 8. So the result of the query for the below column would be 3. (9-8)+(10-8)
SELECT Sum(([time1]-8)+([time2]-8)+([time3]-8)+([time4]-8)+([time5]-8)+([time6]-8)+([time7]-8)+([time8]-8)+([time9]-8)+([time10]-8)+([time11]-8)+([time12]-8)+([time13]-8)+([time14]-8)+([time15]-8)+([time16]-8)+([time17]-8)+([time18]-8)+([time19]-8)+([time20]-8)+([time21]-8)+([time22]-8)) AS Total
FROM tblTimeTracking
WHERE (((Month(([Day])))=Month(Now()))) AND ([time1]>8 AND[time2]>8 AND[time3]>8 AND[time4]>8 AND[time5]>8 AND[time6]>8 AND[time7]>8 AND[time8]>8 AND[time9]>8 AND[time10]>8 AND[time11]>8 AND[time12]>8 AND[time13]>8 AND[time14]>8 AND[time15]>8 AND[time16]>8 AND[time17]>8 AND[time18]>8 AND[time19]>8 AND[time20]>8 AND[time21]>8 AND[time22]) ;
Thanks,
How about:
SELECT Sum([Value]-8) As SumOfVal
FROM table
WHERE [Value]>8
Edit re complete change in original question.
It is not clear what you want
SELECT Sum(([time1]-8)+([time2]-8) ...
WHERE [time1]>8 And Time2>8 ...
Time1>8 will exclude nulls, but if that is not what you are doing, you will need to consider:
Nz([time1],0) + ...
Edit re comments
Something like:
SELECT Sum(times) FROM
(SELECT IIf(Time1>8,Time1-8,Time1) As times FROM Table
UNION ALL
SELECT IIf(Time2>8,Time2-8,Time2) As times FROM Table) As b
As b is an alias: Access SQL
UNION / UNION ALL: View a unified result from multiple queries with a union query