How to remove duplicates - mysql

I have two tables
test
and test1
test
id formula
1 A12+C32+D+X
2 K/Y
test1
id Code
6 A12
7 C32
100 A1
10 D
12 X
13 K
14 Y
How can I update formula(text formlas) filed in the table test to get
id formula
1 [6]+[7]+[10]+[12]
2 [13]/[14]
Itry the following script sqlfield but it doesn't rerun the correct result . It returns
RFORMULA
[6]+C32+D+X//need to remove it
[13]/Y//need to remove it
[13]/[14]//the best result
[100]2+[7]+[10]+[12]
[13]/Y//need to remove it
K/[14]//need to remove it
[6]+[7]+[10]+[12]//the best result
I am working on it for hours , any idea ?

Try by using the following:
SQL Fiddle
WITH a AS
( SELECT DISTINCT A.id group_id,
Split.A.value('.', 'VARCHAR(100)') AS Data
FROM
(SELECT id,
CAST ('<M>' + REPLACE(REPLACE(REPLACE(REPLACE(formula, '/', '/</M><M>'), '+','+</M><M>'), '-','+</M><M>'),'*','+</M><M>') + '</M>' AS XML) AS Data
FROM test
) AS A CROSS APPLY Data.nodes ('/M') AS Split(a)
),
b AS
(SELECT group_id,
REPLACE(LEFT(Data, LEN(Data)-1), t.[Code], concat(t.[id],RIGHT(Data,1))) yy,
REPLACE(Data, t.[Code], t.[id])uu
FROM a
LEFT JOIN test1 t
ON t.Code =LEFT(Data, LEN(Data)-1)
OR (t.Code=Data)
)
SELECT group_id,
REPLACE(REPLACE(REPLACE(REPLACE(stuff(
(SELECT uu FROM b WHERE newtable.group_id=group_id FOR XML PATH('')
), 1,1,''), 'uu', ''), '</>', ''),'<', ''), '>', '')
FROM b newtable
GROUP BY group_id
Here, I have also specified multiplication and subtraction, in case they appear in your expression column. You can edit it accordingly if there exist some other ones:
CAST ('<M>' + REPLACE(REPLACE(REPLACE(REPLACE(formula, '/', '/</M><M>'), '+','+</M><M>'), '-','+</M><M>'),'*','+</M><M>') + '</M>' AS XML) AS Data

Related

mysql move the last N characters of each line of data to the next line

I want move the last 3 characters of each line of data to the next line, the first line fill with xxx.
example:
now i have TableOne, i want get TableTwo, thanks!
[update]
mysql version is 5.7.22, not support lag function
If your version of MySql/MariaDB supports window functions and you have already created TableTwo you can insert the new rows like this:
insert into TableTwo(id, num)
select
id,
concat(
coalesce(lag(right(num, 3)) over (order by id), 'xxx'),
coalesce(left(num, 2), '')
) num
from (
select * from TableOne
union all
select max(id) + 1, null from TableOne
) t;
See the demo.
Without window functions you can do it with a self join:
insert into TableTwo(id, num)
select
t.id,
concat(
coalesce(right(t1.num, 3), 'xxx'),
coalesce(left(t.num, 2), '')
) num
from (
select * from TableOne
union all
select max(id) + 1, null from TableOne
) t left join TableOne t1
on t1.id = t.id - 1;
See the demo.
Results:
> id | num
> -: | :----
> 1 | xxxab
> 2 | cde01
> 3 | 23456
> 4 | 789

Split values then resolve the values to a name

I need to be able to do something with my column (below) that can contain multiple values. The 'HearAboutEvent' column has multiple values separated by a comma. Each one of these values corresponds to an entry in another table. So the value of 11273 will equal facebook, 11274 will mean radio, and 11275 will mean commercial.
The data I am working with looks like this:
weather ID MemberID SubscriptionID DateEntered ParticipatedBefore ParticipatedBeforeCities WeatherDependent NonRefundable TShirtSize HearAboutEvent
Yes 24 18 1 2013-12-19 0 NULL 10950 10952 10957 11273, 11274, 11275
I am able to do the proper join to resolve the value of 'weather', note it is the first column and the 8th column.
This is the query I have created so far to resolve the values of WeatherDependent:
SELECT CFS1.Name as 'weather', *
FROM FSM_CustomForm_693 t
LEFT JOIN FSM_CustomFormSelectOptions CFS1 ON CFS1.ID = t.WeatherDependent
where t.ID = 24
Ultimately I need to have the data look like this:
weather ID MemberID SubscriptionID DateEntered ParticipatedBefore ParticipatedBeforeCities WeatherDependent NonRefundable TShirtSize HearAboutEvent
Yes 24 18 1 2013-12-19 0 NULL 10950 10952 10957 Facebook, radio, commercial
Things I think you could use to accomplish this are:
A Split TVF FUNCTION - http://msdn.microsoft.com/en-us/library/ms186755.aspx
CROSS APPLY - http://technet.microsoft.com/en-us/library/ms175156.aspx
STUFF & FOR XML PATH - http://msdn.microsoft.com/en-us/library/ms188043.aspx & http://msdn.microsoft.com/en-us/library/ms190922.aspx
Going one step further, you need something like this:
Excuse my profuse use of sub queries.
CREATE FUNCTION dbo.Split (#sep char(1), #s varchar(512))
RETURNS table
AS
RETURN (
WITH Pieces(pn, start, stop) AS (
SELECT 1, 1, CHARINDEX(#sep, #s)
UNION ALL
SELECT pn + 1, stop + 1, CHARINDEX(#sep, #s, stop + 1)
FROM Pieces
WHERE stop > 0
)
SELECT pn,
SUBSTRING(#s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
FROM Pieces
)
GO
SELECT
O.A,O.B,O.C,O.D,O.E,O.F,O.G,O.H,O.I,O.J,O.Stuffed
FROM (
SELECT
*
,STUFF((
SELECT ', ' + Name
FROM (
SELECT
V.*
,Y.Name
FROM (
SELECT
'Yes' AS A
,24 AS B
,18 AS C
,1 AS D
,'2013-12-19' AS E
,0 AS F
,NULL AS G
,10950 AS H
,10952 AS I
,10957 AS J
,'11273, 11274, 11275' AS K
)
AS V
CROSS APPLY dbo.Split(',',REPLACE(K,' ','')) AS P
JOIN (
SELECT 11273 AS Id , 'Facebook' AS Name UNION ALL
SELECT 11274 AS Id , 'radio' AS Name UNION ALL
SELECT 11275 AS Id , 'commercial' AS Name
)Y ON y.Id = p.s) ExampleTable
FOR XML PATH('')
), 1, 1, '' )
AS [Stuffed]
FROM (
SELECT
V.*
FROM (
SELECT
'Yes' AS A
,24 AS B
,18 AS C
,1 AS D
,'2013-12-19' AS E
,0 AS F
,NULL AS G
,10950 AS H
,10952 AS I
,10957 AS J
,'11273, 11274, 11275' AS K
)
AS V
CROSS APPLY dbo.Split(',',REPLACE(K,' ','')) AS P
JOIN (
SELECT 11273 AS Id , 'Facebook' AS Name UNION ALL
SELECT 11274 AS Id , 'radio' AS Name UNION ALL
SELECT 11275 AS Id , 'commercial' AS Name
)Y ON y.Id = p.s
)Z
) O
GROUP BY O.A,O.B,O.C,O.D,O.E,O.F,O.G,O.H,O.I,O.J,O.K,O.Stuffed

select from sql database with avg function

I have a database as shown below
id , name , var1
and I want to write a sql query like this:
select name
from table
where last var1 > avg of var1 s of each name
Notice that i want to select between names that last var1 is greater than avrage of var1s of each name
i write this code :
select name
from table
where var1>(select avg(var1) from table ) limit 0 , 1
but this code gets avrage from all var1s and I dont know whether this works or not!
for example we have these data:
1 , John , 32
2 , John , 21
3 , Mike , 22
4 , John , 11
5 , Mike , 5
6 , Mike , 45
=> for John , we have: 32+21+11 /3 =21.3 , but the last data is 11 , so John shouldnt be chosen
=> for Mike , avrage of var1 is 24 , and last row for Mike is 45 that is greater than the avrage , so Mike should be chosen.
Can anyone help me?
SELECT y.* FROM
your_table y
JOIN
(
SELECT name, AVG(var1) AS av, MAX(id) AS mx
FROM your_table
GROUP BY name
) tab
ON y.name = tab.name
AND y.id = tab.mx
AND y.var1 > tab.av
Here is the code at SQL Fiddle
[EDIT]:
Based on your latest requirement, what you want to accomplish is LIMIT N within group, which can be done with the following query:
SET #N := 2;
SELECT * FROM
(
SELECT (#rownumber:= #rownumber + 1) AS rn, yt.*
FROM your_table yt,(SELECT #rownumber:= 0) nums
ORDER BY name, id
) k
JOIN
(
SELECT t.name, MAX(rn) AS MaxRN FROM
(
SELECT (#rownumber:= #rownumber + 1) AS rn, yt.*
FROM your_table yt,(SELECT #rownumber:= 0) nums
ORDER BY name, id
) t
GROUP BY name
) l
ON k.rn <= l.MaxRN AND k.rn > l.MaxRN - #N
Here #N variable holds number of records we want to select within each group
Check the code at SQL Fiddle
Now in an outer query we can take the avg of the resultset returned by above.
Let me know if you could accomplish what you wanted with my inputs.

SQL statement to insert repetitive data

current situation is to add below value of A01, B03, Z11 and X21 in repetitive way in field code for 400 hundreds row of data in table BabyCode.
Above is current table - without value in 'Code" column
Above is to be updated table - repetitive value is added in 'Code' column
You can do this:
INSERT INTO BabyCode
SELECT Codes.Code
FROM
(
SELECT id
FROM
(
SELECT t3.digit * 100 + t2.digit * 10 + t1.digit + 1 AS id
FROM TEMP AS t1
CROSS JOIN TEMP AS t2
CROSS JOIN TEMP AS t3
) t
WHERE id <= 400
) t,
(
SELECT 1 AS ID, 'A01' AS Code
UNION ALL
SELECT 2, 'B03'
UNION ALL
SELECT 3, 'Z11'
UNION ALL
SELECT 4, 'X21'
) codes;
But you will need to define a temp table, to use as an anchor table:
CREATE TABLE TEMP (Digit int);
INSERT INTO Temp VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
SQL Fiddle Demo
This will insert 400 hundred rows of the values A01, B03, Z11, and X21, into the code column in the table BabyCode.
You could put the four values into a virtual table identical to that used in #Mahmoud Gamal's answer, and, if the ID values in your table start at 1 and are sequential (have neither gaps nor duplicates), you could use the following method to join to the virtual table and update the target's Code column:
UPDATE YourTable t
INNER JOIN (
SELECT 1 AS ID, 'A01' AS Code
UNION ALL SELECT 2, 'B03'
UNION ALL SELECT 3, 'Z11'
UNION ALL SELECT 4, 'X21'
) x
ON (t.ID - 1) MOD 4 + 1 = x.ID
SET t.Code = x.Code
;
Otherwise you could use variables to assign 1, 2, 3, 4 sequentially to every row of your table, then you would be able join to the virtual table using those values:
UPDATE YourTable t
INNER JOIN (
SELECT ID, #rnk := CASE WHEN #rnk = 4 THEN 0 ELSE #rnk END + 1 AS rnk
FROM YourTable
CROSS JOIN (SELECT #rnk := 0) x
ORDER BY ID
) r ON t.ID = r.ID
INNER JOIN (
SELECT 1 AS ID, 'A01' AS Code
UNION ALL SELECT 2, 'B03'
UNION ALL SELECT 3, 'Z11'
UNION ALL SELECT 4, 'X21'
) x
ON r.rnk = x.ID
SET t.Code = x.Code
;
Both queries can be played with at SQL Fiddle:
Method 1
Method 2

Mysql group_concat with sums also inside

I have a table of different attributes I want to sum, and then group concatenate them into a JSON string to make it easier to send over network. Here's a simplified table:
t1
type amount
'atr1' 10
'atr2' 10
'atr1' 17
'atr3' 20
'atr3' 4
I tried something like
select concat('{',
group_concat(
(select concat('"', type, '":', sum(amount)) from t1 group by type)
),
'}')
but failed.
I want to end up with '{"atr1":27,"atr2":10,"atr3":24}'
Try this query -
SELECT CONCAT('{', GROUP_CONCAT(c1), '}') FROM (
SELECT CONCAT('"', type, '":', SUM(amount)) c1 FROM t1 GROUP BY type
) t
something like
select
group_concat(concat('"', type, '":', TheSum))
FROM
(
SELECT SUM(amount) AS TheSum,type
FROM t1
GROUP BY type
) T