sum MySQL column with spaces in name - mysql

How do I just simply sum a column in my MySQL database? I thought:
SELECT sum(Disbursement Amount) FROM payments
but this doesn't work. Says I have a syntax error near 'Amount)
I just need to know how to SUM ONE column of data and I can take it from there
I've searched for a couple days and have found nothing of use to me. I'm also brand spanking new to MySQL so I'm sure that's why ;)

If you have a space in your column name (Disbursement Amount), then you need to use backticks to enclose the column name:
SELECT sum(`Disbursement Amount`) as Total
FROM payments;
My suggestion would be to not use spaces in your tables and column names. If you want to have a separator between the two words, you can use an underscore:
SELECT sum(Disbursement_Amount) as Total
FROM payments;

Related

MYSQL select all record if contains specific number

I have a small problem, I have a table like this:
id|name|group|date_created
1|Volvo|1,3|06-04-2020 10:00:00
2|Audi|3|06-04-2020 10:00:00
etc....
Now I wish I could get all the records that have the value 1 inside the group column.
I tried LIKE "%1%", but I don't think it's a good query. Can you address me?
SELECT id FROM cars WHERE group LIKE '%1%'
The problem with your query is that it would wrongly match '1' against a list like '45,12,5' for example.
One method is to add commas on both ends before searching:
where concat(',', `group`, ',') like '%,1,%';
But in MySQL, it is much more convenient to use string function find_in_set(), whose purpose is just what you are looking for, ie search for a value in a comma-separated list:
select id from cars where find_in_set('1', `group`) > 0
Notes:
you should fix your data model, and have a separated table to store relationship between ids and groups, with each tuple on a separate row. Related reading: Is storing a delimited list in a database column really that bad?
group is a reserved word in MySQL, so not a good choice for a column name (you would need to surround it with backticks everytime you use it, which is error-prone)

How to add the numbers in comma separated string of numbers contained in a MySQL column?

I have a MySQL column which contains a string of scores separated by a semi-colon eg: "5;21;24;25;26;28;117".
This column was created not by design, but by collecting the values from multiple rows in a table using GROUP_CONCAT and GROUP BY. The original data arrived as a spreadsheet with multiple rows with the ID value.
I can use a select clause with REPLACE function to replace the ; with a +.
SELECT values, REPLACE(values,";","+") AS score FROM [table_name] WHERE 1
values score
5;21;24;25;26;28;117 5+21+24+25+26+28+117
However what I need is the sum of: 5+21+24+25+26+28+117 to get a total of 246.
Is there any way to do this in MySQL without using some other scripting language?
The SELECT clause shows me a string of numbers joined with the + symbol.
Am looking for a way to evaluate that string to give me the result: 246
UPDATE:
As I was framing my question, I did more research and came up with this link which solves my problem:
(https://dba.stackexchange.com/questions/120747/evaluate-a-string-value-as-a-computed-expression-in-an-sql-statement-sthg-like).
Am keeping this question and the link to the answer here in case it could help other people searching for the same.

Explode and count in SQL

I have column in table views: 165,75,44,458,458,42,45
This column contain user_ids who viewed a link.
I want to explode it by comma delimiter and count the ids
I tried this but it only counts all the character.
SQL:
SELECT LENGTH(views) FROM questions WHERE question_id=1;
This is the PHP version that I want to do in SQL
$viewers_ids = "165,75,44,458,458,42,45";
$num_of_views = count(array_filter(explode(",", $viewers_ids)));
You can count the commas:
select 1 + length(views) - length(replace(views, ',', ''))
That said, you should fix your data structure. You should not be storing multiple numeric ids in a string column -- that is just wrong in many ways. You should be using a junction/association table.
Tried the other way around i.e. to replace all characters other than ,
Select
Length(REGEXP_REPLACE
(views,'[^\,]*',''))+1 from
table
My immediate reaction is that your table structure needs to change - I would expect there to be another table that joins question links and user_ids with a "visit_id" perhaps, and use visit_id as a foreign key in the views table.
My second reaction is that this is a perfect problem for a shell utility - maybe sed/,/ /g then wc -w, or a small awk script.
To do it in SQL, I might try to figure out how to count the number of commas, and add one. This answer gives you a function that will get you the number of commas by replacing them with "", then substracting the length from the length of the unmodified field: Count the number of occurrences of a string in a VARCHAR field?

in mysql my distinct query not working?

in the below image I'm using
SELECT DISTINCT(name),date,reporting,leaving from attendance where date='2016-09-01
and I'm still getting repeating names. Why?
When using DISCTINCT, MySQL uses all columns as grouping factor. If you want group by only one column and get all corresponding column values, use GROUP BY instead
SELECT name, date, reporting, leaving FROM attendance GROUP BY name WHERE ...
Actually your all rows have distinct data apart from Name column if you want only distinct names then you can get it with help of Aggregate functions, you can use MIN or MAX as per your business requirement
SELECT Name,MAX(date),MAX(reporting),MAX(leaving)
FROM attendance
WHERE date='2016-09-01'
GROUP BY Name

Selecting column name for frequency count in sqlite

I have a table in sqlite that contains roughly about 3 billion values (a lot of them will be repeats). It's basically a giant vector of values. I'm trying to calculate the frequency in which values appear in the table by performing this:-
SELECT abs(diffs), count(*) as total FROM mzdiff GROUP by abs(diffs);
abs(diffs) is the name of my column and mzdiff is my table name, but when I try performing the code above it comes up with an error message saying that the column diffs doesn't exist. I know that the naming of my column isn't really ideal for sql, but is there any way I can get around this?
Thanks
The answer to this is not an alias since the column name must be identified before it can be aliased so use the backtick to quote the name and make it a habit to always quote identifiers.
SELECT `abs(diffs)`, count(*) as total FROM `mzdiff` GROUP by `abs(diffs)`;