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]
Related
So I have string that should contains "Object.Name" once in a row , if I see it ,I have to get the value after "=" character. If doesn't match it anywhere in the string i should move hardcoded value.
Here is example of the string:
Object.Name=ASDD||Product.Name=DSA
Product.Name=QWE||Object.Name=WSXS
Storage.Name=12345||Object.Name=WERR||Product.Name=QAZ
I know that I should use case for that but doesn't know how to proceed the string
case
when (match the string ) then (value after the "=")
else (hardcoded value)
end
In Oracle, you can use:
SELECT value,
CASE
WHEN start_pos = 0
THEN NULL
ELSE SUBSTR(
'||' || value || '||',
start_pos + LENGTH('||Object.Name='),
end_pos - start_pos - LENGTH('||Object.Name=')
)
END AS object_name
FROM (
SELECT value,
INSTR(
'||' || value || '||',
'||Object.Name='
) AS start_pos,
INSTR(
'||' || value || '||',
'||',
INSTR('||' || value || '||', '||Object.Name=')+LENGTH('||Object.Name=')
) AS end_pos
FROM table_name
)
Which, for the sample data:
CREATE TABLE table_name (value) AS
SELECT 'Object.Name=ASDD||Product.Name=DSA' FROM DUAL UNION ALL
SELECT 'Product.Name=QWE||Object.Name=WSXS' FROM DUAL UNION ALL
SELECT 'Storage.Name=12345||Object.Name=WERR||Product.Name=QAZ' FROM DUAL;
Outputs:
VALUE
OBJECT_NAME
Object.Name=ASDD||Product.Name=DSA
ASDD
Product.Name=QWE||Object.Name=WSXS
WSXS
Storage.Name=12345||Object.Name=WERR||Product.Name=QAZ
WERR
db<>fiddle here
Since you changed the tags, in MySQL:
SELECT value,
CASE
WHEN start_pos = 0
THEN NULL
ELSE SUBSTRING(
CONCAT('||', value, '||'),
start_pos + LENGTH('||Object.Name='),
end_pos - start_pos - LENGTH('||Object.Name=')
)
END AS object_name
FROM (
SELECT value,
LOCATE(
'||Object.Name=',
CONCAT('||', value, '||')
) AS start_pos,
LOCATE(
'||',
CONCAT('||', value, '||'),
LOCATE('||Object.Name=', CONCAT('||', value, '||'))
+ LENGTH('||Object.Name=')
) AS end_pos
FROM table_name
) t
db<>fiddle here
I need help, I want to ask, I send emails in two SQL tables as a body of the email. However, when the first table has no results, sends a blank email to me, even though the results of the second table exists.
The problem is with #body = #html1
Thank you very much
IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[qdatum2]'))
DROP VIEW [dbo].[qdatum2]
GO
-- vytvorenie
CREATE VIEW qdatum2 AS
SELECT(A.Firma) AS FIRMA, A.ID, (A.OBEC) AS OBEC, (A.Ulice) AS Ulica, A.Obec2 as 'Obec dod.', A.Ulice2 AS 'Ulica dod.',
VP.IDS AS REGION,
CAST(SUM(CASE WHEN F.RelTpFak = 2 THEN -F.Kc2 ELSE F.Kc2 END) AS DECIMAL(36,2)) AS OBRAT, MAX(F.DATUM) AS MAXDATUM, MIN(F.DATUM) AS MINDATUM
FROM (StwPh_35760532_2015.dbo.AD as A INNER JOIN StwPh_35760532_2015.dbo.sVPULpol AS VP ON A.RefVPrRegion = VP.ID)
inner JOIN StwPh_35760532_2015.dbo.FA AS F ON A.ID = F.RefAD
WHERE DATEADD(MONTH, 6, F.Datum) > GETDATE()
GROUP BY A.ID, a.Firma, A.Obec, A.Ulice, A.Obec2, A.Ulice2, VP.IDS
union all
SELECT(AD.Firma) AS FIRMA, AD.ID, (AD.OBEC) AS OBEC, (AD.Ulice) AS Ulica, AD.Obec2 as 'Obec dod.', AD.Ulice2 AS 'Ulica dod.',
sVPULpol.IDS AS REGION,
CAST(SUM(CASE WHEN FA.RelTpFak = 2 THEN -FA.Kc2 ELSE FA.Kc2 END) AS DECIMAL(36,2)) AS OBRAT, MAX(FA.DATUM) AS MAXDATUM, MIN(FA.DATUM) AS MINDATUM
FROM (AD INNER JOIN sVPULpol ON AD.RefVPrRegion = sVPULpol.ID) inner JOIN FA ON AD.ID = FA.RefAD
WHERE DATEADD(MONTH, 6, FA.Datum) > GETDATE()
GROUP BY AD.ID, ad.Firma, ad.Obec, ad.Ulice,AD.Obec2,AD.Ulice2, sVPULpol.IDS
go
IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[qdatum1]'))
DROP VIEW [dbo].[qdatum1]
GO
-- vytvorenie
CREATE VIEW qdatum1 AS
select (AD.Firma) AS FIRMA, AD.ID, (AD.OBEC) AS OBEC, (AD.Ulice) AS Ulica, AD.Obec2 as 'Obec dod.', AD.Ulice2 AS 'Ulica dod.', qdatum2.REGION,
SUM(qdatum2.OBRAT) as OBRAT, max(qdatum2.MAXDATUM) as MAXDATUM, MIN(qdatum2.MINDATUM) AS MINDATUM
from AD INNER JOIN qdatum2 ON AD.ID = qdatum2.ID
group by ad.id, ad.Firma, ad.Obec, ad.Ulice, ad.Obec2, ad.Ulice2, qdatum2.region
go
IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[qdatum]'))
DROP VIEW [dbo].[qdatum]
GO
-- vytvorenie
CREATE VIEW qdatum AS
SELECT qDATUM1.*
FROM qdatum1
WHERE DATEADD(DAY, 30, qdatum1.MAXDATUM) < getdate()
and qdatum1.REGION like '%OR%'
and qdatum1.OBRAT > 50
GO
DECLARE #html varchar(max)
SET #html = '<html>'
+ '<H3><font id="Text" style="color: Green;">PREHLAD AKTIVNYCH ZAKAZNIKOV BEZ ODBERU ZA POSLEDNYCH 30 DNI </H3>'
+ '<table border="1" bordercolor="#3300FF" style="background-color:#DDF8CC" width="70%" cellpadding=3 cellspacing=3>'
+ '<tr><font color="Green"><th>Firma</th><th>Obec</th><th>Ulica</th><th>Obec dod.</th><th>Ulica dod.</th><th>Obrat v € za posledných 6 mesiacov</th><th>Posledna. Faktura</th>
</tr>' +
CAST (( SELECT
td = qdatum.FIRMA, ' ',
td = qdatum.OBEC, ' ',
td = CASE WHEN qdatum.Ulica IS NULL THEN '''' ELSE qdatum.Ulica END, ' ',
td = CASE WHEN qdatum.[Obec dod.] IS NULL THEN '''' ELSE qdatum.[Obec dod.] end , ' ',
td = CASE WHEN qdatum.[Ulica dod.] IS NULL THEN '''' ELSE qdatum.[Ulica dod.] END, ' ',
td = qdatum.OBRAT, ' ',
td = CONVERT(VARCHAR(11),qdatum.MAXDATUM, 106)
from StwPh_35760532_2016.dbo.qdatum
where qdatum.REGION like '%B1OR%'
order by qdatum.MAXDATUM
FOR XML PATH ('tr'),ELEMENTS
) AS VARCHAR(MAX))
+
'<table>'
DECLARE #html1 varchar(max)
SET #html1 = '<html>'
+ '<H3><font id="Text" style="color: Green;">PREHLAD KOMUNIKÁCIE SO ZÁKAZNÍKOM REGION B1OR </H3>'
+ '<table border="1" bordercolor="#3300FF" style="background-color:#DDF8CC" width="70%" cellpadding=3 cellspacing=3>'
+ '<tr><font color="Green"><th>Firma</th><th>Obec</th><th>Ulica</th><th>Obec dod.</th><th>Ulica dod.</th><th>Posledna navsteva</th><th>Posledne volanie</th></tr>' +
CAST( (SELECT
td = AD.Firma , ' ',
td = AD.Obec, ' ',
td = CASE WHEN AD.Ulice IS NULL THEN '''' ELSE AD.Ulice END, ' ',
td = CASE WHEN AD.Obec2 IS NULL THEN '''' ELSE AD.Obec2 END, ' ',
td = CASE WHEN AD.Ulice2 IS NULL THEN '''' ELSE AD.Ulice2 END, ' ',
td = CASE WHEN CONVERT(VARCHAR (11), AD.VPrnavsteva, 106) IS NULL THEN '''' ELSE CONVERT(VARCHAR (11), AD.VPrnavsteva, 106) END, ' ',
td = CASE WHEN CONVERT(VARCHAR(11), AD.VPrvolane, 106) IS NULL THEN '''' ELSE CONVERT(VARCHAR(11), AD.VPrvolane, 106) END
FROM StwPh_35760532_2016.dbo.AD INNER JOIN StwPh_35760532_2016.dbo.sVPULpol ON AD.RefVPrRegion = sVPULpol.ID
WHERE AD.VPrnavsteva < DATEADD(DAY, -30 , GETDATE()) AND AD.VPrvolane < DATEADD(DAY, -30 , GETDATE())
AND AD.RefAD IS NULL
AND sVPULpol.IDS like '%B1OR%'
ORDER BY VPrnavsteva DESC
FOR XML PATH ('tr'),ELEMENTS
) AS VARCHAR(MAX))
+
'</table>'
+ #html
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'LACI_ADMINISTRATOR',
--#recipients = 'emailxx',
#copy_recipients = 'emailxy',
--#blind_copy_recipients = 'emailxxy',
#subject ='B1OR report',
#body = #html1,
#body_format = 'HTML'
Try using this:
DECLARE #html varchar(max)
SET #html = '<html>'
+ '<H3><font id="Text" style="color: Green;">PREHLAD AKTIVNYCH ZAKAZNIKOV BEZ ODBERU ZA POSLEDNYCH 30 DNI </H3>'
+ '<table border="1" bordercolor="#3300FF" style="background-color:#DDF8CC" width="70%" cellpadding=3 cellspacing=3>'
+ '<tr><font color="Green"><th>Firma</th><th>Obec</th><th>Ulica</th><th>Obec dod.</th><th>Ulica dod.</th><th>Obrat v € za posledných 6 mesiacov</th><th>Posledna. Faktura</th>
</tr>' +
ISNULL(CAST (( SELECT
td = qdatum.FIRMA, ' ',
td = qdatum.OBEC, ' ',
td = CASE WHEN qdatum.Ulica IS NULL THEN '''' ELSE qdatum.Ulica END, ' ',
td = CASE WHEN qdatum.[Obec dod.] IS NULL THEN '''' ELSE qdatum.[Obec dod.] end , ' ',
td = CASE WHEN qdatum.[Ulica dod.] IS NULL THEN '''' ELSE qdatum.[Ulica dod.] END, ' ',
td = qdatum.OBRAT, ' ',
td = CONVERT(VARCHAR(11),qdatum.MAXDATUM, 106)
from StwPh_35760532_2016.dbo.qdatum
where qdatum.REGION like '%B1OR%'
order by qdatum.MAXDATUM
FOR XML PATH ('tr'),ELEMENTS
) AS VARCHAR(MAX)), '')
+
'<table>'
DECLARE #html1 varchar(max)
SET #html1 = '<html>'
+ '<H3><font id="Text" style="color: Green;">PREHLAD KOMUNIKÁCIE SO ZÁKAZNÍKOM REGION B1OR </H3>'
+ '<table border="1" bordercolor="#3300FF" style="background-color:#DDF8CC" width="70%" cellpadding=3 cellspacing=3>'
+ '<tr><font color="Green"><th>Firma</th><th>Obec</th><th>Ulica</th><th>Obec dod.</th><th>Ulica dod.</th><th>Posledna navsteva</th><th>Posledne volanie</th></tr>' +
ISNULL(CAST( (SELECT
td = AD.Firma , ' ',
td = AD.Obec, ' ',
td = CASE WHEN AD.Ulice IS NULL THEN '''' ELSE AD.Ulice END, ' ',
td = CASE WHEN AD.Obec2 IS NULL THEN '''' ELSE AD.Obec2 END, ' ',
td = CASE WHEN AD.Ulice2 IS NULL THEN '''' ELSE AD.Ulice2 END, ' ',
td = CASE WHEN CONVERT(VARCHAR (11), AD.VPrnavsteva, 106) IS NULL THEN '''' ELSE CONVERT(VARCHAR (11), AD.VPrnavsteva, 106) END, ' ',
td = CASE WHEN CONVERT(VARCHAR(11), AD.VPrvolane, 106) IS NULL THEN '''' ELSE CONVERT(VARCHAR(11), AD.VPrvolane, 106) END
FROM StwPh_35760532_2016.dbo.AD INNER JOIN StwPh_35760532_2016.dbo.sVPULpol ON AD.RefVPrRegion = sVPULpol.ID
WHERE AD.VPrnavsteva < DATEADD(DAY, -30 , GETDATE()) AND AD.VPrvolane < DATEADD(DAY, -30 , GETDATE())
AND AD.RefAD IS NULL
AND sVPULpol.IDS like '%B1OR%'
ORDER BY VPrnavsteva DESC
FOR XML PATH ('tr'),ELEMENTS
) AS VARCHAR(MAX)),'')
+
'</table>'
+ #html
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'LACI_ADMINISTRATOR',
--#recipients = 'emailxx',
#copy_recipients = 'emailxy',
--#blind_copy_recipients = 'emailxxy',
#subject ='B1OR report',
#body = #html1,
#body_format = 'HTML'
I added ISNULL on your CAST(SELECT) so that your variable will not turn to null if SELECT doesn't return data.
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '8:45 AM' to data type int.
My Script is
Declare #SpecimenId bigint = 0, #script varchar(MAX);
Declare #StartTime int = (Select Convert(int,SUBString(START_TIME,0,CHARINDEX(':', START_TIME)) - 12) From APPT
Where APPT._ID=112601)
--select #StartTime
Set #script =
'SELECT distinct PTLAST_NAME + '', '' + PT.FIRST_NAME As PATIENT_NAME,
dbo.FormatDate(PT.DOB,''MM/dd/yyyy'') As DOB,
(Convert(varchar, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END)) + ''/'' +
Convert(varchar, (DATEDIFF(m, DATEADD(yy, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END), DOB), APPT_DATE) - CASE WHEN DAY(DOB) > DAY(APPT_DATE) THEN 1 ELSE 0 END)) + ''/'' +
Convert(varchar, (DATEDIFF(d, (DATEADD(m, (DATEDIFF(m, DATEADD(yy, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END), DOB), APPT_DATE) - CASE WHEN DAY(DOB) > DAY(APPT_DATE) THEN 1 ELSE 0 END), (DATEADD(yy, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END), DOB)))), APPT_DATE)))) As PatAge,
PT.Org_Id, PT.ADDRESS1 As PatAddress,
PT.LAST_NAME As LastName,PT.FIRST_NAME As FirstName,
PT.MIDDLE_NAME As MiddleName,
PHY.LAST_NAME + '', '' + PHY.FIRST_NAME As Physician,
dbo.FormatDate(APPT.APPT_DATE,''MM/dd/yyyy'') As CollectedDate,
(Case when SUBString(START_TIME,0,CHARINDEX('':'', START_TIME)) < 12 Then Convert(varchar,START_TIME) + Convert(varchar,'' AM'') ELSE' +Convert(varchar,#StartTime)+ '+ SUBString(START_TIME,3,6) + '' PM'' END) As CollectedTime,
(Case When PT.GENDER =''1'' Then ''M'' Else ''F'' End) As Gender,PT.CITY,LST.State_Short_Name As State ' +
(Case When #SpecimenId = 0 Then ', 0 As SpeciId' Else ', (Case When Tbl_Pat.Speci_Id > 0 Then Tbl_Pat.Speci_Id Else '''' End) As SpeciId' End) +
' FROM PT INNER JOIN
APPT ON PT.PAT_ID = APPT.PAT_ID INNER JOIN
PHY ON APPT.PHY_ID = PHY.PHY_ID INNER JOIN
LST ON PT.STATE = LST.STATE_ID ' +
(Case When #SpecimenId = 0 Then '' Else 'LEFT OUTER JOIN Tbl_Pat ON APPT.APPT_ID = Tbl_Pat.Speci_ApptId' End)
+ ' WHERE (APPT.APPT_ID = 112601) ' +
(Case When #SpecimenId = 0 Then '' Else 'AND (Tbl_Pat.Speci_Id = ' + Convert(varchar, #SpecimenId) + ')' End)
exec (#script)
Can Anyone help or correct my script please
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))
)
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