Calculate total of some fields in mysql? - mysql

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)

Related

SQL sum of two line if equal

I have a table like this:
REFERENCE QUANTITY
AS400 0
AS400 30000
AB123 500
CA031 560
CA031 25
I need to have, in sql, the total quantity if the rows are the same.
For ex:
AS400 30000
AB123 500
CA031 585
You could using group by like below query
select REFERENCE, sum(QUANTITY)
from tableA
group by REFERENCE
If do you want to get the quantites by reference from ps_product_attribute, the code which is working on Ps1.7.6 :
select `reference`, sum(`quantity`)
from `ps_product_attribute`
group by `reference`

how to perform addition of column values in mySql?

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.

Finding count of unique value before a character

I have a some entries in database table rows as follows.
101 - 1
101 - 2
101 - 3
102 - 1
102 - 2
102 - 3
103
I need to get the result of SELECT Query for count as '3' since there are 101 and 102 are the only number before the -.
So is there any way to find the unique value in db table columns before a character?
EDIT : I have entries even without the - .
In case your entries have always the format you have provided us, you just have to find the position of the '-' character, split the values, get the first n characters and count the distinct values
This works for SQL Server, otherwise informs us about what DBMS you are using or replace the functions with the ones of your DBMS on your own
SELECT COUNT(DISTINCT SUBSTRING(val,0,CHARINDEX('-', val))) from YourTable
create table T1
(
id int primary key identity,
col1 varchar(20)
)
insert into T1 values('101 - 1'),('101 - 2'),('101 - 3'),('102 - 1'),('102 - 2'),('102 - 3')
select SUBSTRING(col1,0,CHARINDEX(' ',col1)) as 'Value',count(*) as 'Count' from T1 group by SUBSTRING(col1,0,CHARINDEX(' ',col1))

generating count data using group by in mysql

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`

Unable to apply WHERE/AND on MySQL table with 2 columns on MAMP

I thought I had a very simple query to perform, but I can't seem to make it work.
I have this table with 2 columns:
version_id trim_id
1 15
1 25
1 28
1 30
1 35
2 12
2 25
2 33
2 48
3 11
3 25
3 30
3 32
I am trying to get any version-id's that have say a sub-set of trim_id's. Let's say all version_id's that have trim_id's 25 and 30. My obvious attempt was :
SELECT * FROM table WHERE trim_id=25 AND trim_id=30
I was expecting to have version_id 1 and 3 as a result, but instead I get nothing.
I am working with the latest version of MAMP, which has some odd behavior, like in this case it just tells me its 'LOADING' and never gives me an error message or something. But that's normally the case when there is no data to return.
This is InnoDB, if that helps.
Thanks for your input.
Your query does not work because you are using AND and the trim_id cannot have two different values at the same time, so you need to apply Relational Division to get the result.
You will need to use something similar to the following:
SELECT version_id
FROM yourtable
WHERE trim_id in (25, 30)
group by version_id
having count(distinct trim_id) = 2
See SQL Fiddle with Demo.
This will return the version_id values that have both 25 and 30. Then if you wanted to include additional columns in the final result, you can expand the query to:
select t1.version_id, t1.trim_id
from yourtable t1
where exists (SELECT t2.version_id
FROM yourtable t2
WHERE t2.trim_id in (25, 30)
and t1.version_id = t2.version_id
group by t2.version_id
having count(distinct t2.trim_id) = 2);
See SQL Fiddle with Demo
SELECT *
FROM table
WHERE trim_id IN(25,30)