Count Delimited value in Column - mysql

Please suggest me an SQL Query for the Below
I have Ids of Items Stored in one column like this 1,2,3,4 as Delimited or CSV value.
Now i want count no of items in one particular row in one particular cell.
i.e
If i have 25,45,26,45,46 in a cell in table.
I want the output like 5 which is nothing but the number of items in a row in a cell say (0,0).
Thank you so much for your Replies

Assuming you don't have values like '123,123,' (note the comma at the end) this should work:
SELECT
LENGTH(yourColumn) - LENGTH(REPLACE(yourColumn, ',', '')) + 1 AS numberOfItemsInRow
FROM yourTable;
Find more information here.
But it would really be better to normalize your database!

Related

How do I log the the sum of values from multiple rows to a new row if the value in the column next to it matches

You will need this solve this.
In this sheet, I am trying to take the scores of the people and log them in the "Week Total" Column L as a sum. The names often exist in each column for that days score. I need to take their score for each day next to their name, and log it next to their name in Column L as a total if that makes sense.
So if Bob got a 10 on Monday, a 10 on Tuesday, and X the rest of the days, then I need to log that in Column L next to his name as "20".
I am using the Unique function to log all their names under Column K.
Thank you!
You can use query and combine it with some array notations.
Formula:
=query({{A4:A,B4:B};{C4:C,D4:D};{E4:E,F4:F};{G4:G,H4:H};{I4:I,J4:J}}, "select Col1, sum(Col2) where Col1 is not null group by Col1 label sum(Col2) ''", 0)
Output:
Note:
A duplicate Bob can be seen as you have spaces after Bob on some cells (e.g. Bob ). Make sure to have no spaces with them when inputting to remove duplicates from the formula result, or just use trim before using them. See modified formula before:
Formula with trim:
=query(index({{trim(A4:A),B4:B};{trim(C4:C),D4:D};{trim(E4:E),F4:F};{trim(G4:G),H4:H};{trim(I4:I),J4:J}}), "select Col1, sum(Col2) where Col1 is not null group by Col1 label sum(Col2) ''", 0)
Result:
You're not making your life easier by inputting X since that will be read as text, I'd recommend using 0 instead (if you insist on X then add an IF statement to exclude the Xs).
But what you're looking for is =SUMIF() coupled with some smart Array notation as demonstrated here in the documentation.
Create arrays
You can also create your own arrays in a formula in your spreadsheet by using brackets { }. The brackets allow you to group together values, while you use the following punctuation to determine which order the values are displayed in:
Commas: Separate columns to help you write a row of data in an array. For example, ={1, 2} would place the number 1 in the first cell and the number 2 in the cell to the right in a new column.
Semicolons: Separate rows to help you write a column of data in an array. For example, ={1; 2} would place the number 1 in the first cell and the number 2 in the cell below in a new row.
Note: For countries that use commas as decimal separators (for example €1,00), commas would be replaced by backslashes () when creating arrays.
You can join multiple ranges into one continuous range using this same punctuation. For example, to combine values from A1-A10 with the values from D1-D10, you can use the following formula to create a range in a continuous column: ={A1:A10; D1:D10}.
Adding arrays to existing formulas
You can also use arrays with other existing formulas using brackets in order to organize the returns from your formulas into rows or columns.
For example, ={SUM(A1:A10), SUM(B1:B10)} will produce two values. The first cell will contain the sum of A1 to A10, the cell to the right will contain the sum of B1 to B10.

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.

find_in_set and regex in a select query

Is there any way to check for a regex expression in a comma separated values column?
I have a column named storeId with the following values EMP_0345,00345,OPS and I need to get only the storeid with no alpha numeric characters in it.
I am able to get the valid store_ids with the regex REGEXP '^[0-9]+$' but how do get the values in a comma separated values column?
You violated a dogma of database design: Never ever store more than one value in a single field, if you need to access them separatly
Even if you manage to REGEX your way around this, you will run into massive performance troubles. The correct way to tackle this ist to move the contents of the CSV column into rows of a join table, then simply match against the single values of that join table.
Your database design is flawed - given your current request.
Whether you should (try to) convert that column into several columns in the current (or a different) table, or rather into rows in a different table does primarily depend on whether or not there is some structure in that column's data.
With some inherent structure, you could use something like
SELECT
storeId
, SUBSTRING_INDEX(storeId, ',', 1) AS some_column
, SUBSTRING_INDEX(SUBSTRING_INDEX(storeId, ',', 2), ',', -1) AS store_id
, SUBSTRING_INDEX(storeId, ',', -1) AS another_column
FROM T
WHERE storeId REGEXP '^[^,]+,[0-9]+,[^,]+$'
;
to separate the values (and potentially populate newly added columns). The WHEREclause would allow to differentiate between sets of rows with specific arrangements of values in the column in question.
See in action / more detail: SQLFiddle.
Please comment if and as adjustment / further detail is required, or update your request to provide more detailed input.

Cut mysql data when selecting

I have a table with "unique" values. The problem is that the program, which adds these values also adds 3 different postfixs to the value (2 characters in the end of the value). As a result, I have three variable with three postfixs. So i need get only unique values from bd - somehow sort it out without the last two characters. Are any ideas?
What Camera_id should you return (first,last,maximum,minimum???) if rows have one "unique" value but different Camera_id's. Try something like this:
select
LEFT(camera_name,LENGTH(camera_name)-2), max(camera_id)
from cameras
where site_id=1
group by LEFT(camera_name,LENGTH(camera_name)-2)
Do you want to retrieve the values with the first letter only?
SELECT DISTINCT SUBSTRING(ColumnName, 1,1) a
FROM tablename
ORDER BY a
can you show sample records? it helps a lot when your asking question.

Selecting data within a row of a Column

Ok, I have table called "lookup" which has a column called "name" which contains a bit of data starting with "Quantity for pricing" (the data I need is in the same cell just after Quantity for Pricing). I was wondering if there was any way to pull out that "row", and list all the quantities except for Quantity for Pricing using mysql?
Sorry for the confusion guys, this is what I am dealing with.
I am pretty sure the data I need (the actual quantities) are stored in the same bit that "Quantity for Pricing" is.
You can use substring operation.
SELECT SUBSTRING(name,LENGTH('Quantity for pricing'))
FROM lookup WHERE name LIKE 'Quantity for pricing%';
I think this is what you want:
SELECT REPLACE(name,"Quantity for pricing","") AS name FROM lookup;
This selects all of the rows from the table lookup, returning the value in column name, but excluding the occurence of "Quantity for pricing" in the column value.
You can use REPLACE
SELECT REPLACE(columnToSearch, 'Quantity for pricing ', '') FROM TableToSearch;