Add two values ​in mysql - mysql

I enter in the database the value with this format 1 000.00.
I want to add two values ​​of this format, for example 1 000.00 + 4 000.00 like this:
CAST(SUM(1 000.00 + 4 000.00) AS DECIMAL (15 , 2 )) AS Pago
the returned value is 5 and should be 5 000.00

Sum 2 values and finally add a space
-- MySQL
SELECT INSERT((SUM(CAST(REPLACE('1 000.00', ' ', '') AS decimal(15, 2)) + CAST(REPLACE('4 000.00', ' ', '') AS decimal(15, 2)))), 2, 0, ' ') sum_total
Please check url https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=b8ab1e7c4c1d2ab596fbc1fe58754cf8
Add these two values in a table
SELECT INSERT((SUM(CAST(REPLACE(apago, ' ', '') as decimal(15, 2)))), 2, 0, ' ') pago FROM test;
Please check this url https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=65fa6af1bedd4a1acce01dc0ea700dd6

I resolved it this way. First I removed the space in the string like this:
SELECT Id, Total, Metodo, REPLACE(Pago, ' ', '') AS Pago
FROM dados.Orcamento
Then I added the string:
SELECT B.Id, B.Total, B.Metodo, CAST(SUM(B.Pago) AS DECIMAL (15 , 2 )) AS Pago
FROM
(SELECT Id, Total, Metodo, REPLACE('Pago', ' ', '') AS Pago
FROM dados.Orcamento) AS B GROUP BY B.Id, B.Metodo, B.Total ORDER BY B.Id ASC LIMIT 1
So the 5000.00 returned

Related

counting unexpected Chars when trying to find occurrence of the words in phpmyadmin

I have a table that contains different length of sentences from one word to 40 words, and I want to count each word separately and how many times it occurred in that table,
but whenever the sentence is containing just one word it prints unexpected Char. for some reasons any ideas why ?
this is a demo of my DB
DB demo
and this is the code
create table messages(sent varchar(200), verif int);
insert into messages values
('HI' , null),
('HI alex how are you' , null),
('bye' , null);
select * from messages;
UPDATE messages set sent = TRIM(sent);
UPDATE messages set sent = REGEXP_REPLACE(sent,' +',' ')
with recursive cte as (
select
substring(concat(sent, ' '), 1, locate(' ', sent)) word,
substring(concat(sent, ' '), locate(' ', sent) + 1) sent
from messages
union all
select
substring(sent, 1, locate(' ', sent)) word,
substring(sent, locate(' ', sent) + 1) sent
from cte
where locate(' ', sent) > 0
)
select row_number() over(order by count(*) desc, word) wid, word, count(*) freq
from cte
group by word
order by wid
out put of the code
wid word freq
1 2
2 HI 2
3 alex 1
4 are 1
5 bye 1
6 how 1
7 you 1
expected output
wid word freq
1 HI 2
2 alex 1
3 are 1
4 bye 1
5 how 1
6 you 1
Your problem is in these lines:
substring(concat(sent, ' '), 1, locate(' ', sent)) word,
substring(concat(sent, ' '), locate(' ', sent) + 1) sent
When sent doesn't contain a space, locate(' ', sent) returns 0 and substring returns an empty string, which is what you are seeing counted in your output. To fix that, use concat(sent, ' ') in place of sent:
substring(concat(sent, ' '), 1, locate(' ', concat(sent, ' '))) word,
substring(concat(sent, ' '), locate(' ', concat(sent, ' ')) + 1) sent
For your sample data this gives:
wid word freq
1 HI 2
2 alex 1
3 are 1
4 bye 1
5 how 1
6 you 1
Demo on dbfiddle

How to count the occurrence of specific words in a Mysql db row?

I'm quite new to MySQL. I require to count the number of occurrences of specific words in a MySQL table/columns.
I came across the following code through another post #Raging Bull, which only provides the count of one specific word.
SELECT name,(CHAR_LENGTH(description)-
CHAR_LENGTH(REPLACE(description,' is ','')))/CHAR_LENGTH(' is ') AS
TotalCount
FROM TableName
GROUP BY name
Could someone help me to align it for multiple words to be counted. For example, I want to count "is", "as", "was", "how".
MySQL is not designed for this sort of thing. However, you can just add the values together. I thing the correct method is like this;
select ( (length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' is ', '')) / length(' is ') +
(length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' as ', '')) / length(' as ') +
(length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' was ', '')) / length(' was ') +
(length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' how ', '')) / length(' how ')
)
from t ;
Note the use of spaces at the beginning and end to capture words at the beginning and end of the description. Also, this assumes that only spaces are used for separating words.
Try using LENGTH instead of CHAR_LENGTH and truncate spaces from String, e.g.:
SELECT name,
ROUND (
(
LENGTH(description)
- LENGTH(REPLACE(description, "is", ""))
) / LENGTH("is")
) AS count
FROM TableName
update
To count multiple words, you can write simillar ROUND queries and add them together, e.g.:
SELECT name,
SELECT(
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "is", "")) ) / LENGTH("is")) +
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "This", "")) ) / LENGTH("This")) +
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "That", "")) ) / LENGTH("That"))
) AS `count`
FROM TableName
update 2
Here's the query to get the counts as individual columns:
SELECT name,
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "is", "")) ) / LENGTH("is")) AS 'is count',
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "this", "")) ) / LENGTH("this")) AS 'this count',
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "by", "")) ) / LENGTH("by")) AS 'by count'
FROM TableName
update 3
Below is the query to get aggregated counts for words:
SELECT 'is', SUM(ROUND((LENGTH(description)- LENGTH(REPLACE(description, "is", "")) ) / LENGTH("is"))) AS `count` FROM TableName
UNION
SELECT 'this', SUM(ROUND((LENGTH(description)- LENGTH(REPLACE(description, "this", "")) ) / LENGTH("this"))) AS `count` FROM TableName
UNION
SELECT 'by', SUM(ROUND((LENGTH(description)- LENGTH(REPLACE(description, "by", "")) ) / LENGTH("by"))) AS `count` FROM TableName
Here's the SQL Fiddle.
select count(adm_no) from class_manager
Try that where adm_no is the column name and class_manager is the table

How can I use a MySQL stored procedure to count and sort a pipe-delimited field?

I need to create a stored procedure that will return the total count for each unique value encountered in a pipe-delimited field, roughly in the format in figure 2. Fortunately, the values will only ever be "value1", "value2", or "value3" if that makes things any simpler. I had originally planned to output the data and sort it in PHP by exploding the data and looping through for string matches, but there are extraordinary circumstances that require me to use a stored procedure and I've not used stored procedures before.
The data I'm interpreting is in the format shown below, where the 'valuelist' field is a pipe-delimited field containing many values. I need to find out how many times the values occurred each for a given date.
(figure 1 below) What the data looks like in the database.
id, date, valuelist
1, 2017-01-01, value1|value2|value3
2, 2017-01-01, value1|value2
3, 2017-01-01, value1
So a query of select date, valuelist from db.table where id = 1; will return 2017-01-01, value1|value2|value3.
(figure 2 below) Desired count output representing how many times each value occurred on a specific date. For example, using the data from figure 1, if we ask about 2017-01-01, the output should look something like this.
value1: 3
value2: 2
value3: 1
Assuming you are converting 3|2|1 to separate rows of 3, 2, 1; MySQL (or pretty much any SQL-based RDBMS) does not lend itself well to that kind of output.
Without going into actual procedure code, in a procedure you could:
Store value in a local variable
Use a more general purpose stored function (should be easy to find one around here) to "split" the string on your delimiter |; or "loop" through your string using a "get n-th" function to split it iteratively.
Store values into a temp table
Use a SELECT on the temp table as your procedure's resultset.
Take a look at this:
Detailed as given sample data:
SELECT date,fval as allValue,count(fval) as ValueCount from
(select date,fval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select 1 as id, '2017-01-01' as date, 'value1|value2|value3' as valuelist union all
select 2, '2017-01-01', 'value1|value2' union all
select 3, '2017-01-01', 'value1') as a)as a) as a
union all
select date,sval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select 1 as id, '2017-01-01' as date, 'value1|value2|value3' as valuelist union all
select 2, '2017-01-01', 'value1|value2' union all
select 3, '2017-01-01', 'value1') as a)as a) as a
UNION all
select date,tval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select 1 as id, '2017-01-01' as date, 'value1|value2|value3' as valuelist union all
select 2, '2017-01-01', 'value1|value2' union all
select 3, '2017-01-01', 'value1') as a)as a) as a) as a
where fval !='' GROUP BY fval,date
final Query:
SELECT date,fval as allValue,count(fval) as ValueCount from
(select date,fval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select * from yourTable) as a)as a) as a
union all
select date,sval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select * from yourTable as a)as a) as a
UNION all
select date,tval from
(select date,SUBSTRING_INDEX(rplace, ' ', 1)fval,SUBSTRING_INDEX(MID(rplace,8,LENGTH(rplace)), ' ', 1)sval,SUBSTRING_INDEX(MID(rplace,15,LENGTH(rplace)), ' ', 1)tval from
(select id, date, valuelist,REPLACE(valuelist,'|',' ') as rplace from
(select * from yourTable) as a)as a) as a) as a
where fval !='' GROUP BY fval,date
Result:
2017-01-01 value1 3
2017-01-01 value2 2
2017-01-01 value3 1

MySQL Split string by character mutli-part

I want to add a temporal column to a query.
I have a table example_ip_db, that contains s1,s2,s3,s4,e1,e2,e3,e4,cc rows.
These s1,s2,s3... rows means start of ip range, and end of ip range for the e1,e2,e3.... In case that for example the ip range where 1.1.1.1 to 2.2.2.2 for country China (CN), these will be the data:
Starting rows s1,s2,s3,s4 will equals 1. (s1.s2.s3.s4/1.1.1.1)
Ending rows e1,e2,e3,e4 will equals 2. (e1.e2.e3.e4/2.2.2.2)
The cc row will equal CN.
What I want is to add a column county to a non existing example_ip_db table/row/data. Something like the generated column with the count(*) as 'amount' query.
How can I split the IP for this?
You use SUBSTRING() function in mysql.
For example.
mysql> SELECT SUBSTRING('habonytest', -3);
-> est
mysql> SELECT SUBSTRING('habonytest', -5, 2);
-> yt
mysql> SELECT SUBSTRING('habonytest', FROM -5 FOR 4);
-> ytes
Also mysql has SUBSTRING_INDEX() function.
example:
mysql> SELECT SUBSTRING_INDEX('myid#domain.com', '#', -1);
-> domain.com
mysql> SELECT SUBSTRING_INDEX('myid#domain.com', '#', 1);
-> myid
See it more detail
Thank you! (#Star_Man)
Solved:
SELECT IP, SUBSTRING_INDEX( IP, '.', 1 ) AS part1,
SUBSTRING_INDEX( SUBSTRING_INDEX( IP, '.', 2 ) , '.' , -1 ) AS part2,
SUBSTRING_INDEX( SUBSTRING_INDEX( IP, '.' , -2 ) , '.', 1 ) AS part3,
SUBSTRING_INDEX( IP, '.' , -1 ) AS part4,
(SELECT `cc`
FROM `example_ip_db`
WHERE `s1` <= part1
AND `e1` >= part1
AND `s2` <= part2
AND `e2` >= part2
AND `s3` <= part3
AND `e3` >= part3
AND `s4` <= part4
AND `e4` >= part4
) AS CC
FROM `example_saved_ip_from`
WHERE 1

SQL Query to display unique records based on certain conditions

[I have gone through a large number of questions before posting this question.]
I have a table which contains 4 fields. It is ClientId, ClientName,ClientAddress, ClientCity.
Now, I have an autocomplete textbox control where I need to fetch & display client name.
The problem is that in our database we have same client from the same city from different address.
Now the requirement given to me is that the user should be able to see "ClientName" or "ClientName + ClientCity" or "ClientName+ClientCity+ClientAddress" to make it easy for user to select the client.
It means that I need to add a column to the query till it makes it unique.
I am sure there must be some simple solution to this which I am not getting for past 2 days now.
As shown in below sample data, If I show only "D" as a client name to the end user, he will be confused as in which client "D" he has to select. So we need to concatenate city and address to make it unique.
I am expecting an output as below.
You can use COUNT() OVER() for this:
;WITH CTE AS(
SELECT *,
ByName = COUNT(*) OVER(PARTITION BY ClientName),
ByCity = COUNT(*) OVER(PARTITION BY ClientName,ClientCity)
FROM Client
)
SELECT
CASE
WHEN ByName = 1 AND ByCity = 1 THEN ClientName
WHEN ByName > 1 AND ByCity = 1 THEN ClientName + ', ' + ClientCity
WHEN ByName > 1 AND ByCity > 1 THEN ClientName + ', ' + ClientCity + ', ' + ClientAddress
END
FROM CTE
ORDER BY ClientID
RESULT
Client
--------------------------------------------------------
A
B
C, New York
D, London, LSE Houghton Streen London WC2A 2AE
D, London, Hard Rock Cafe London 150 Old Park Lane
F
C, Paris
See SQL Fiddle.
If you are using SQL Server, you can try the string concatenation using "+" operator as follows
select
ClientName + ', ' + ClientCity + ', ' + ClientAddress as ClientData,
Concat(ClientName, ', ', ClientCity, ', ', ClientAddress) Client
from client
The second concatenation is built using SQL CONCAT() funtion will work on SQL Server 2012 and later
For the conditional data following SELECT statement can help,
select
-- ClientName + ', ' + ClientCity + ', ' + ClientAddress as ClientData,
-- Concat(ClientName, ', ', ClientCity, ', ', ClientAddress) ClientDtata2,
client =
case when count(*) over (partition by ClientName) = 1 then ClientName
else
case when count(*) over (partition by ClientName, ClientCity) = 1 then CONCAT(ClientName, ', ' , ClientCity)
else Concat(ClientName, ', ', ClientCity, ', ', ClientAddress)
end
end
from client
TRY THIS,
Declare #t table (clientid int identity(1,1),clientname varchar(50),clientCITY varchar(50)
,clientaddress varchar(200))
insert into #t values ('A','PARIS','DFSDFSDF'), ('C','NEW YORK','DFSDFSDF')
,('C','PARIS','WEQWEQWE'),('D','LONDON','QWE342'),('D','LONDON','21DXCXZC')
;With CTE as
(select *,ROW_NUMBER()over(partition by clientname order by clientid)rn
,ROW_NUMBER()over(partition by clientname,ClientCity order by clientid)rn1
from #T A
)
--select * from cte
select clientname+','+ clientCITY
from cte A WHERE
EXISTS(SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN>1 AND RN1<=1)
UNION ALL
select clientname+','+ clientaddress
from cte A WHERE
EXISTS(SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN1>1)
UNION ALL
select clientname
from cte A WHERE not
EXISTS (SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN>1 )