How do I prevent NaN from appearing in my reports? - reporting-services

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)

Related

SQL select calculations

Below are the things I'm using
lahman's baseball -database
2015 SQL version of the database.
Microsoft SQL server management studio v17.7
I'm using "Los Angeles Dodgers" statistics and I'm basing my selects of
the website and pulling from the above database Website for ESPN Los Angeles Dodgers
I'm trying to recreate the website with 7 individual select statements. I'm having issues with my second select statements at the moment, The purpose of the second is to provide a totals row at the bottom like they do on the website.
( So my main question is can someone help with fixing my second select.)
( My second question can i get some assistance on creating 5 other
selects statements that correspond with the image below.)
(1st)
Main Body Select :
SELECT
nameFirst + ' ' + nameLast AS Name,
G AS GP,
AB,
R,
H,
H - B2 - B3 - HR AS S,
B2 AS '2B',
B3 AS '3B',
HR,
RBI,
(((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4)) AS 'TB',
BB,
SO,
SB,
(H * 1.0) / (CASE
WHEN AB = 0 THEN 1
ELSE AB
END) AS 'BA',
(CASE
WHEN (H + BB + HBP) = 0 THEN 0
ELSE ((H + BB + HBP) * 1.0)
END) / (CASE
WHEN (AB + BB + HBP) = 0 THEN 1.0
ELSE ((AB + BB + HBP) * 1.0)
END) AS 'OBP',
(CASE
WHEN (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4)) = 0 THEN 0
ELSE (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4))
END) / (CASE
WHEN AB = 0 THEN 1
ELSE AB
END) AS 'SLG',
(CASE
WHEN (H + BB + HBP) = 0 THEN 0
ELSE ((H + BB + HBP) * 1.0)
END) / (CASE
WHEN (AB + BB + HBP) = 0 THEN 1.0
ELSE ((AB + BB + HBP) * 1.0)
END) +
(CASE
WHEN (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4)) = 0 THEN 0
ELSE (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4))
END) / (CASE
WHEN AB = 0 THEN 1
ELSE AB
END) AS 'OPS'
FROM Players
LEFT OUTER JOIN Batting
ON Players.playerIDpk = Batting.playerID
WHERE teamID = 'LAN'
AND yearID = '2012'
ORDER BY BA DESC
Click Here - For Image of above select output
(2nd)
Total Select:
SELECT
'' AS Total,
'162' AS 'GP',
SUM(AB) AS 'AB',
SUM(R) AS 'R',
SUM(H) AS 'H',
SUM(B2) AS '2B',
SUM(B3) AS '3B',
SUM(HR) AS HR,
SUM(RBI) AS RBI,
SUM((((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4))) AS 'TB',
SUM(BB) AS BB,
SUM(SO) AS 'SO',
SUM(SB) AS 'SB',
AVG((H * 1.0) / (CASE
WHEN AB = 0 THEN 1
ELSE AB
END)) AS 'BA',
AVG((CASE
WHEN (H + BB + HBP) = 0 THEN 0
ELSE ((H + BB + HBP) * 1.0)
END) / (CASE
WHEN (AB + BB + HBP) = 0 THEN 1.0
ELSE ((AB + BB + HBP) * 1.0)
END)) AS 'OBP',
AVG((CASE
WHEN (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4)) = 0 THEN 0
ELSE (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4))
END) / (CASE
WHEN AB = 0 THEN 1
ELSE AB
END)) AS 'SLG',
AVG((CASE
WHEN (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4)) = 0 THEN 0
ELSE (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4))
END) / (CASE
WHEN AB = 0 THEN 1
ELSE AB
END)) AS 'OPS'
FROM Players
LEFT OUTER JOIN Batting
ON Players.playerIDpk = Batting.playerID
WHERE teamID = 'LAN'
AND yearID = '2012'
Here's what my total select returns and its incorrect.
Below is what it should return when i run the select for totals i don't know what i did wrong.
Looking at only the total value for the column BA it is clear that you are not doing the same calculations as them, instead of calculating average values you should calculate the ratio betweens the sum of the columns H and AB
So replace
AVG((H * 1.0) / (CASE
WHEN AB = 0 THEN 1
ELSE AB
END)) AS 'BA',
with
SUM(H) / SUM(AB) AS 'BA'
I haven't checked the other incorrect columns but I assume it's the same issue.

mysql average of best three

I have pieced this together from sites online and it works but not completely, what i need it to do is take the top 3 results and average them but it takes ALL results, can anyone point me in the right direction?
SELECT i.NAME,
e.comp,
Round(Avg(c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6), 2) AS "Average Score",
( CASE
WHEN compID = '7' THEN Concat(Round(Avg(
( (
c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 ) / 400 ) * 100), 2), ' %')
WHEN compID = '5' THEN Concat(Round(Avg(
( (
c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 ) / 600 ) * 100), 2), ' %')
WHEN compID = '3' THEN
Concat(Round(Avg(( ( c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 ) / 600 ) * 100), 2), ' %')
ELSE 'Unspecified'
END ) AS "Average as Percent"
FROM jos_practicedetail c,
jos_comps e,
jos_practice g,
jos_members i
WHERE e.compsid = g.competition
AND g.practiceid = c.practicepid
AND i.memberid = c.competitorid
AND g.typeID = '2'
AND Year(g.pdate) = '2017'
AND (SELECT Count(*)
FROM jos_practicedetail b
WHERE b.competitorid = c.competitorid
AND b.practicepid = c.practicepid
AND ( b.phase1 + b.phase2 + b.phase3 + b.phase4 + b.phase5
+ b.phase6 ) >= (
c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6 )) <= 3
GROUP BY competitorid
HAVING Count(*) > 2
ORDER BY competitorid,
( Avg(c.phase1 + c.phase2 + c.phase3 + c.phase4 + c.phase5
+ c.phase6) ) DESC

How to shorten this MySQL Query (colums use often the same sql parts...)

I am not an MySQL Professinal, but my query works fine
SELECT r.id,
/* Total repurchase price per Ton
* (Quantitiy/t * repurchase price/dry) + Extra Costs
*/
(
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0) +
IF ((r.finanzierung_satz > 0) AND (r.finanzierung_monate > 0) AND SUM(rc.menge / 1000),
((r.finanzierung_satz / 100) / 12) * r.finanzierung_monate * (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0)
)
, 0) ) as repurchase_price,
/* Profit
* (Quantitiy/t * Price) - repurchase_price
*/
(SUM(rc.menge) / 1000 * r.vk) - (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0) +
IF ((r.finanzierung_satz > 0) AND (r.finanzierung_monate > 0) AND SUM(rc.menge / 1000),
((r.finanzierung_satz / 100) / 12) * r.finanzierung_monate * (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0)
)
, 0)
) as profit,
/* Profit Percentage
* Profit / (repurchase_price / 100)
*/
((SUM(rc.menge) / 1000 * r.vk) - (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0) +
IF ((r.finanzierung_satz > 0) AND (r.finanzierung_monate > 0) AND SUM(rc.menge / 1000),
((r.finanzierung_satz / 100) / 12) * r.finanzierung_monate * (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0)
)
, 0)
))
/
((
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0) +
IF ((r.finanzierung_satz > 0) AND (r.finanzierung_monate > 0) AND SUM(rc.menge / 1000),
((r.finanzierung_satz / 100) / 12) * r.finanzierung_monate * (
SUM((rc.menge / 1000) * (rw.preis / (IF (rw.is_zement = 1, 1.25 , 1)) / (1 - (IFNULL(rw.h2o, 0) / 100)))) +
SUM((rc.menge / 1000) * pr.preis) +
IF ((r.extra_1_type > 0), IF ((r.extra_1_type = 1), extra_1 , extra_1 * SUM(rc.menge / 1000)) , 0) +
IF ((r.extra_2_type > 0), IF ((r.extra_2_type = 1), extra_2 , extra_2 * SUM(rc.menge / 1000)) , 0)
)
, 0)
) / 100)
as profit_percentage,
FROM recipe as r
LEFT JOIN recipecomponent as rc ON r.id = rc.recipe_id
LEFT JOIN rawmaterial as rw ON rc.rawmaterial_id = rw.id
LEFT JOIN press as pr ON r.press_id = pr.id
GROUP BY r.id
ORDER BY lieferdatum desc
As you can see, to calculate price, profit, profit percentage, I use often
the same parts... Is it possible to shorten my SQL Query? To make it more
'elegant' :)
Thank you & Best Regars Simon
As a generic answer, if you repeat caculations in your query, you can use derived tables to avoid repetition:
select d.a, d.b, d.a*d.b
from (
select x+y+z as a, d+e+f as b
from yourtable
) as d;

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))
)

Show modified strings that appear more than once

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