Show modified strings that appear more than once - mysql

I got a Query which cuts off domain suffixes for every row, e.g. google.com -> google or google.co.uk -> google
The query is as follows
SELECT id,domain,
CASE
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))
END as Keydomain
FROM sites
Now I want to display all modified domains that occur more than once. How can I do that? Thanks for helping me out ;)

Just add
GROUP BY Keydomain
HAVING COUNT(*) > 1
to your query.
EDIT:
Could you tell me if there is a way to list the complete domains one by one with your addition?
SELECT * FROM
(
SELECT
CASE
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))
END as Keydomain
FROM sites
GROUP BY Keydomain
HAVING COUNT(*) > 1
) d1
INNER JOIN
(
SELECT id, domain,
CASE
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))
END as Keydomain
FROM sites
) d2
ON d1.Keydomain = d2.Keydomain

Related

Is there a way to sum only integer from rows and then sum strings without integer

I have this table
------------------------------------------
id | Machines
------------------------------------------
1. | 1 Truck
------------------------------------------
2. | 1 Bobcat
------------------------------------------
3. | 2 Platform
------------------------------------------
4. | Telehender
------------------------------------------
5. | Teodolit, 3 Platform
------------------------------------------
6. | 2 Tractor
------------------------------------------
7. | NULL
------------------------------------------
Result: | 11
------------------------------------------
I want to sum firstly integers (1+1+2+3+2), then SUM Count of values without Integer (Telehender and Teodolit = 2) and skip NULL values..
The result of this table need to be 11.
I am using this query
SELECT Sum((Char_length(machines) - Char_length(Replace(machines, ',','')) + 1)) AS ukupno
FROM izvestaji
WHERE projekatid='8'
AND datum='2019-10-03'
But I get the Result = 9.
Is there a way to make that query?
Bad bad database design... :/
However , here it it a solution in which i suppose you have no more than 3 elements
into machines field
SELECT
sum(case
when length(machines)-length(replace(machines,',','')) = 0
then if( CAST(machines AS UNSIGNED) = 0, 1, cast(machines as unsigned))
when length(machines)-length(replace(machines,',','')) = 1
then ( if( CAST(SUBSTRING_INDEX(machines, ',', 1) AS UNSIGNED) = 0, 1, cast(SUBSTRING_INDEX(machines, ',', 1) as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') as unsigned)))
when length(machines)-length(replace(machines,',','')) = 2
then ( if( CAST(SUBSTRING_INDEX(machines, ',', 1) AS UNSIGNED) = 0, 1, cast(SUBSTRING_INDEX(machines, ',', 1) as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') as unsigned)))
end) AS ukupno
FROM izvestaji
#WHERE projekatid='8'
#AND datum='2019-10-03'
you can obtain other cases from 2 to M including cases like this
when length(machines)-length(replace(machines,',','')) = M
then ( if( CAST(SUBSTRING_INDEX(machines, ',', 1) AS UNSIGNED) = 0, 1, cast(SUBSTRING_INDEX(machines, ',', 1) as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') as unsigned))) +
...
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', j),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', j),','),'') as unsigned))) +
...
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', M),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', M),','),'') as unsigned)))
if you see case 1 or case 2 i think you can understand it.
You can use combinations of sign(), abs(), substr(), instr() and length() functions, and can filter the integer values by string + 0 formula :
select sum( int_value_1 + int_value_2 ) +
sum( case when int_value_1 = 0 or int_value_2 = 0 then
sign( length(before_comma) ) + abs( sign( after_comma+0 ) - 1 )
end) as total
from
(
select substr( Machines, 1, instr(Machines,',') - 1 ) + 0 as int_value_1,
substr( Machines, instr(Machines,',')+1, length(Machines) ) as int_value_2,
substr( Machines, 1, instr(Machines,',') - 1 ) as before_comma,
substr( Machines, instr(Machines,',')+1, length(Machines) ) as after_comma
from izvestaji
) i;
Demo

How to show multiple condition with mysql

I have a problem with with my query. I have this data : Check this out here is full data and my table. http://sqlfiddle.com/#!9/f440d7/1
Please i hope you can help me.
My expected result should be like this :
|PartID | IdMesin |ket1 |ket2 |Ket3|
|---------- |-------- |---------------- |---------| |
|AELE-00071-XX-KGX|BS 150U |- |1.00 Tes1|- |
|GCXX-MX070-XX-KGX|BS 320F |0.50 Tes3, 2.00 Tes4|0.50 Tes2|- |
I hope you can help me to fix this problem. Anyhelp can be appreciated.
case when should be used in group_concat:
SELECT partid,
idmesin,
COALESCE(GROUP_CONCAT(CASE WHEN idshift = '1' THEN CONCAT(ROUND(
TIME_TO_SEC(
MAKETIME(
durasi + 0,
SUBSTRING_INDEX(durasi, 'Jam ', - 1) + 0, 0)) / 3600, 2), ' ',
keterangandowntime) end), '-') ket1,
COALESCE(GROUP_CONCAT(CASE WHEN idshift = '2' THEN CONCAT(ROUND(
TIME_TO_SEC(
MAKETIME(
durasi + 0,
SUBSTRING_INDEX(durasi, 'Jam ', - 1) + 0, 0)) / 3600, 2), ' ',
keterangandowntime) end), '-') ket2,
COALESCE(GROUP_CONCAT(CASE WHEN idshift = '3' THEN CONCAT(ROUND(
TIME_TO_SEC(
MAKETIME(
durasi + 0,
SUBSTRING_INDEX(durasi, 'Jam ', - 1) + 0, 0)) / 3600, 2), ' ',
keterangandowntime) end), '-') ket3
FROM trans_lhpdtdw
WHERE divisiid = 'IJ001'
AND tanggal = '2017-11-20'
AND idlocation LIKE '%3%'
GROUP BY partid,
idmesin
See demo in SQLFiddle.

SQL - Extract a numeric part of a variable length string

Here is an example:
[U_TipTon]=118.7->59.35;[U_Haulge]=428.28->214.14
I need to extract just 118.7->59.35 as Tipton, and 428.28->214.14 in another column as U_Haulage.
The length of the string is variable as well as the posision os my pattern word.
I am trying with Patindex but I cannot find the way.
In MySQL there's SUBSTRING_INDEX, which extracts a substring based on a delimiter:
select
substring_index(substring_index(x, '[U_TipTon]=', -1), ';', 1) as TipTon
,substring_index(substring_index(x, '[U_Haulge]=', -1), ';', 1) as Haulge
from
(
select '[U_TipTon]=118.7->59.35;[U_Haulge]=428.28->214.14' as x
) as dt
Edit:
In MS SQL Server it's more complicated:
select
substring(xHaulge, 1, charindex(';', xHaulge + ';')-1) as Haulge,
substring(xTipTon, 1, charindex(';', xTipTon + ';')-1) as TipTon
from
(
select
case when charindex('[U_Haulge]=', x) > 0
then substring(x, charindex('[U_Haulge]=', x) + len('[U_Haulge]='), 8000)
else ''
end as xHaulge,
case when charindex('[U_TipTon]=', x) > 0
then substring(x, charindex('[U_TipTon]=', x) + len('[U_TipTon]='), 8000)
else ''
end as xTipTon
from
(
select '[U_TipTon]=118.7->59.35;[U_Haulge]=428.28->214.14' as x
) as dt
) as dt
The solution was:
case
when charindex('Haulge',t.xField) > 0 then
substring( t.xField , charindex('Haulge',t.xField) + 8,case when charindex(';',substring( t.xField , charindex('Haulge',t.xField) + 8,LEN(t.xField))) = 0
then LEN(t.xField)
else charindex(';',substring( t.xField , charindex('Haulge',t.xField) + 8,LEN(t.xField)))-1 end )
else '-' end [Haul Price]
,case
when charindex('Tipton',t.xField) > 0 then
substring( t.xField , charindex('TipTon',t.xField) + 8,case when charindex(';',substring( t.xField , charindex('Tipton',t.xField) + 8,LEN(t.xField))) = 0
then LEN(t.xField)
else charindex(';',substring( t.xField , charindex('Tipton',t.xField) + 8,LEN(t.xField)))-1 end )
else '-' end [Tip Price]
,case
when charindex('AItmPr',t.xField) > 0 then
substring( t.xField , charindex('AItmPr',t.xField) + 8,case when charindex(';',substring( t.xField , charindex('AItmPr',t.xField) + 8,LEN(t.xField))) = 0
then LEN(t.xField)
else charindex(';',substring( t.xField , charindex('AItmPr',t.xField) + 8,LEN(t.xField)))-1 end )
else '-' end [Additional Price]

Strange behavior in SQL query

Good evening to all,
as the subject, I have a query in which parameters can be used, preceded by the # character, in order to do the calculations. In order to test this query using MySQL Front. The problem is that if you launch the query the first time, I expected returns data, but if you run it again, I do not return anything. I also tried using Toad, but from the first attempt did not return anything. Thank you in advance.
SELECT(
SUM(
PzProd1 +
PzProd2 +
PzProd3 +
PzProd4 +
PzProd5 +
PzProd6
)) AS Pezzi_Prodotti,
PSLC.PSLC_Prod.LineaID as linea,
PSLC.PSLC_Prod.SezioneID,
PSLC.PSLC_Prod.Desc_Prod,
#cod_spezz := PSLC.PSLC_Prod.cod_spezz_1,
#l_spezz := REPLACE(FORMAT((SUBSTRING_INDEX(
PSLC.PSLC_Prod.lung_Spezz_1, '± ', 1) / 1000) ,2) , ',' , '.'
) as lunghezza_Spezzone,
REPLACE(FORMAT( #MtScrap := SUM(MtScrap) , 0) , ',' , '.') as MtScarto,
REPLACE(FORMAT((#MtScrap / #l_spezz),0), ',' , '.') as ScartoInPezzi ,
REPLACE(FORMAT(SUM((PzProd1 + PzProd2 + PzProd3 + PzProd4 + PzProd5 + PzProd6) * #l_spezz) ,0), ',' , '.') as MT_Prodotti,
REPLACE(FORMAT( #xxx := (
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_1 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_1 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_2 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_2 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_3 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_3 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_4 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_4 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_5 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_5 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.AvailableCausa_6 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.AvailableTotTime_6 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_1 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_1 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_2 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_2 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_3 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_3 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_4 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_4 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_5 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_5 , 0)) +
SUM(IF(PSLC.PSLC_CAUSALI.Causa_6 NOT LIKE ('%sezione%'),PSLC.PSLC_CAUSALI.Total_Time_6 , 0))),0), ',' , '.') as tempi,
#a := SAMPLE.products.vel_linea as Vel,
REPLACE(FORMAT(((#xxx * #a) / #l_spezz) ,0), ',' , '.') as MinFermoInPezzi
FROM PSLC.PSLC_Prod
JOIN PSLC.PSLC_CAUSALI ON (
(PSLC.PSLC_Prod.SezioneID = PSLC.PSLC_CAUSALI.IDSezione) and
(PSLC.PSLC_prod.giorno = PSLC.PSLC_CAUSALI.DataStartPrg) AND (
(PSLC.PSLC_prod.Cod_Spezz_1 = PSLC.PSLC_CAUSALI.IDSpezzone) OR
(PSLC.PSLC_prod.Cod_Spezz_2 = PSLC.PSLC_causali.IDSpezzone) OR
(PSLC.PSLC_prod.Cod_Spezz_3 = PSLC.PSLC_causali.IDSpezzone) OR
(PSLC.PSLC_prod.Cod_Spezz_4 = PSLC.PSLC_CAUSALI.IDSpezzone) OR
(PSLC.PSLC_prod.Cod_Spezz_5 = PSLC.PSLC_causali.IDSpezzone) OR
(PSLC.PSLC_prod.Cod_Spezz_6 = PSLC.PSLC_causali.IDSpezzone) ) AND
PSLC.PSLC_prod.ID_Prog = PSLC.PSLC_CAUSALI.ID_Prog)
JOIN sample.products ON (
PSLC.PSLC_Prod.SezioneID = sample.products.sku)
WHERE (PSLC.PSLC_Prod.giorno BETWEEN '2014-05-05' AND '2014-05-09') AND
(PSLC.PSLC_Prod.SezioneID = PSLC.PSLC_CAUSALI.IDSezione) AND (
((PSLC.PSLC_prod.Cod_Spezz_1 = #cod_spezz) OR
(PSLC.PSLC_prod.Cod_Spezz_2 = #cod_spezz) OR
(PSLC.PSLC_prod.Cod_Spezz_3 = #cod_spezz) OR
(PSLC.PSLC_prod.Cod_Spezz_4 = #cod_spezz) OR
(PSLC.PSLC_prod.Cod_Spezz_5 = #cod_spezz) OR
(PSLC.PSLC_prod.Cod_Spezz_6 = #cod_spezz))
)

How do I prevent NaN from appearing in my reports?

The following code returns a NaN in situations where there are no records. How do I prevent this from being displayed in the report? A 0 would be preferred.
=FormatNumber(
((
(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
) / (
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
))
, 0)
I think the formula below should get it. I haven't tested this, so I might be missing a parenthesis. The problem is likely coming from a divide by zero when all entries are null. This catches that and sets the divisor to 1 in that case.
=FormatNumber(
(
(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 0) * Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
)
/
(
IIF((
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
) = 0,
1,
(
CInt(Code.NullSafeSplit(Fields!AvgLOSC1.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC2.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC3.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC4.Value, 1)) +
CInt(Code.NullSafeSplit(Fields!AvgLOSC5.Value, 1))
)
)
, 0)