Calculate Total Number of Row's Value [duplicate] - mysql

This question already has answers here:
mysql count the sum of all rows
(6 answers)
Closed 6 years ago.
I have a SQL table that looks like this:
id | value
-----------------------
1 | 5
2 | 5
3 | 6
I want to count the value of every rows.
In this example the output should be 16.
What query should I use to count the total value of the rows?

You can learn more about COUNT() Function there.
https://www.w3schools.com/sql/sql_func_count.asp

Related

I want to do a query in mySQL from horizontal to vertical [duplicate]

This question already has answers here:
MySQL transpose columns sum into rows with column names as a value [duplicate]
(1 answer)
MySQL converting columns to rows
(1 answer)
Closed 4 months ago.
I have this table:
ColumnA
ColumnB
ColumnC
ColumnD
1
2
1
3
3
4
3
4
I want a to do a select like this:
Column id
sum
ColumnA_
4
ColumnB_
6
ColumnC_
4
ColumnD_
7
The porpuse of this is to get a column with the comumn id and another with the sum of each column
I've tried to get the sum of the columns but I couldn't get it like vertically
SELECT SUM(ColumnA ) as ColumnA_, SUM(ColumnB ) as ColumnB_, SUM(ColumnC ) as ColumnC_, SUM(ColumnD ) as ColumnD_ FROM table_name
and I get this (not what I want):
ColumnA_
ColumnB_
ColumnC_
ColumnD_
4
6
4
7

Retrieve different data from one column in MySQL [duplicate]

This question already has answers here:
Count boolean field values within a single query
(4 answers)
Closed 4 months ago.
Suppose a table have a column with values like:
column
3
1
1
3
4
1
2
I want to retrieve that how many times there is 1 in column and how many times > 1.
Is there anyway to do this within one query?
I want the data to be fetched like this:
one | greater_than_one
3 | 4
Thanks in advance.
Simple:
select sum(col1=1) as one ,
sum(col1>1) as greater_than_one
from test_tbl;
https://dbfiddle.uk/348f5YOK

How to auto insert data into database using the values from another table? [duplicate]

This question already has answers here:
What is a good pratice to store row counts in another table?
(4 answers)
Trigger update other table with count
(1 answer)
Closed 9 months ago.
I have 2 tables.
this is the incomes table
income_id date_income total_amount
1 2022-05-22 14
2 2022-05-22 15
3 2022-05-21 20
4 2022-05-21 25
and this is the total_income table
total_inc_id income_id date_income total total_income_amount
1 1 2022-05-22 2 29
However I want to count rows with the same date from table1 and store inside table 2 like above automatically whenever i add income. How can i achieve this?

MySQL query several records from certain date range [duplicate]

This question already has answers here:
What is the most straightforward way to pad empty dates in sql results (on either mysql or perl end)?
(9 answers)
Closed 6 years ago.
here's my recordset:
id date
-----------------------
1 2017-01-11
2 2017-01-12
3 2017-01-14
4 2017-01-15
4 2017-01-16
i'd like to query all records within the date range 2017-01-14 to 2017-01-16
currently i'm using:
SELECT * FROM foo WHERE (date='2017-01-14' OR date='2017-01-15' OR date='2017-01-16')
is there a better way (which would be probably faster for bigger ranges)?
thanks
PS: i'm aware i could use:
SELECT * FROM foo WHERE date >= '2017-01-14' AND date <= '2017-01-16'
but the problem is that i don't want "gaps" between each day.
SELECT *
FROM foo
WHERE date BETWEEN '2017-01-14' AND '2017-01-16';

Does anyone know how to Stuff in groups? [duplicate]

This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 9 years ago.
I am trying to use the stuff function in MS SQL to stuff certain info. Here is the example:
Number Value
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
I would like to stuff the column so that only one record will display as following:
Value Number
1 1,2,3
2 1,2,3
3 1,2
Please note that there are a like n-Numbers and n-Values.
You can use GROUP_CONCAT for this. For example:
SELECT `Value`, GROUP_CONCAT(DISTINCT `Number` ORDER BY `Number`)
FROM `yourTable`
GROUP BY `Value`