Mysql table view datatype - mysql

I'm trying to change the data type bigint(37) to int(11) here. I've tried several methods CAST or CONVERT but ended unsuccessfully.
How can I change the datatype here in this table VIEW? please. thanks.
CREATE OR REPLACE VIEW gtemp_view AS
SELECT
d1.Week AS Week1,
d2.Week AS Week2,
IF( ABS(d1.dt1-d2.dt1)=0,1,0) +
IF( ABS(d1.dt1-d2.dt2)=0,1,0) +
IF( ABS(d1.dt1-d2.dt3)=0,1,0) +
IF( ABS(d1.dt1-d2.dt4)=0,1,0) +
IF( ABS(d1.dt2-d2.dt1)=0,1,0) +
IF( ABS(d1.dt2-d2.dt2)=0,1,0) +
IF( ABS(d1.dt2-d2.dt3)=0,1,0) +
IF( ABS(d1.dt2-d2.dt4)=0,1,0) +
IF( ABS(d1.dt3-d2.dt1)=0,1,0) +
IF( ABS(d1.dt3-d2.dt2)=0,1,0) +
IF( ABS(d1.dt3-d2.dt3)=0,1,0) +
IF( ABS(d1.dt3-d2.dt4)=0,1,0) +
IF( ABS(d1.dt4-d2.dt1)=0,1,0) +
IF( ABS(d1.dt4-d2.dt2)=0,1,0) +
IF( ABS(d1.dt4-d2.dt3)=0,1,0) +
IF( ABS(d1.dt4-d2.dt4)=0,1,0) AS mcount
FROM gtemp AS d1 , gtemp AS d2 WHERE d1.Week IS NOT NULL AND d2.Week IS NOT NULL AND d1.Week > d2.Week order by Week1 DESC, Week2 DESC limit 200000;

If you don't want see INT37 in view schema you could try using an explict cast as unsigned
CREATE OR REPLACE VIEW gtemp_view AS
SELECT
d1.Week AS Week1,
d2.Week AS Week2,
cast(
(IF( ABS(d1.dt1-d2.dt1)=0,1,0) +
IF( ABS(d1.dt1-d2.dt2)=0,1,0) +
IF( ABS(d1.dt1-d2.dt3)=0,1,0) +
IF( ABS(d1.dt1-d2.dt4)=0,1,0) +
IF( ABS(d1.dt2-d2.dt1)=0,1,0) +
IF( ABS(d1.dt2-d2.dt2)=0,1,0) +
IF( ABS(d1.dt2-d2.dt3)=0,1,0) +
IF( ABS(d1.dt2-d2.dt4)=0,1,0) +
IF( ABS(d1.dt3-d2.dt1)=0,1,0) +
IF( ABS(d1.dt3-d2.dt2)=0,1,0) +
IF( ABS(d1.dt3-d2.dt3)=0,1,0) +
IF( ABS(d1.dt3-d2.dt4)=0,1,0) +
IF( ABS(d1.dt4-d2.dt1)=0,1,0) +
IF( ABS(d1.dt4-d2.dt2)=0,1,0) +
IF( ABS(d1.dt4-d2.dt3)=0,1,0) +
IF( ABS(d1.dt4-d2.dt4)=0,1,0))
AS UNIGNED) mcount
FROM gtemp AS d1 , gtemp AS d2
WHERE d1.Week IS NOT NULL
AND d2.Week IS NOT NULL
AND d1.Week > d2.Week
order by Week1 DESC, Week2 DESC limit 200000;
anyway the cast should impact on performance in several way ..instead the INT37 datatype don't should produce appretiable performance degrade

Related

Aliasing a CASE statement

I would need your help to fix a problem I'm facing.. I'm using a CASE function and aliasing its result by adding "AS 'RESULT'" after its "END".
Event though the query seems to run, I'm still getting the following warnings through phpmyadmin :
Unrecognized keyword (near AS)
Unexpected token (near 'RESULT')
I have no idea on why I'm getting this...I would appreciate your help to highlight my mistake ;-)
SELECT
app_fd_List_of_components.c_component,
app_fd_List_of_orders.c_package,
app_fd_List_of_orders.c_status,
app_fd_List_of_orders.dateCreated,
app_fd_List_of_orders.c_orderId,
app_fd_List_of_orders.Id,
temptablename.datetime AS Int_Date,
temptablename2.datetime AS Prdw3_Date,
#counterStatus := IF(
app_fd_List_of_orders.c_status <> 'Deployment completed',
IF(
temptablename.datetime IS NOT NULL,
'Counting',
'Initializing'
),
'Complete'
) AS 'Counter_status',
#counter := IF(
app_fd_List_of_orders.c_status <> 'Deployment completed',
IF(
temptablename.datetime IS NOT NULL,
(
35 - DATEDIFF(
CURRENT_DATE(), temptablename.datetime)
),
'n/a'
),
DATEDIFF(
temptablename2.datetime,
temptablename.datetime
)
) AS 'Counter',
CASE (WHEN(
(#counterStatus = 'Counting') AND(#counter < 0)
) THEN "black" WHEN(
(#counterStatus = 'Counting') AND(#counter > 5)
) THEN "green" WHEN(
(#counterStatus = 'Counting') AND(-1 < #counter < 5)
) THEN "orange" WHEN(
(#counterStatus = 'Complete') AND(#counter > 35)
) THEN "black" WHEN(
(#counterStatus = 'Complete') AND(#counter < 35)
) THEN "green" ELSE "n/a")
END AS 'RESULT'
FROM
app_fd_List_of_orders
JOIN app_fd_List_of_components ON
app_fd_List_of_orders.c_component = app_fd_list_of_components.id
LEFT JOIN(
SELECT app_form_data_audit_trail.datetime,
app_form_data_audit_trail.data
FROM
app_form_data_audit_trail
WHERE
SUBSTRING_INDEX(
SUBSTRING_INDEX(
app_form_data_audit_trail.data,
'"opdetails":"',
-1
),
'"',
1
) = 'int'
) AS temptablename
ON
app_fd_list_of_orders.c_orderid = SUBSTRING_INDEX(
SUBSTRING_INDEX(
temptablename.data,
'"orderId":"',
-1
),
'"',
1
)
LEFT JOIN(
SELECT app_form_data_audit_trail.datetime,
app_form_data_audit_trail.data
FROM
app_form_data_audit_trail
WHERE
SUBSTRING_INDEX(
SUBSTRING_INDEX(
app_form_data_audit_trail.data,
'"opdetails":"',
-1
),
'"',
1
) = 'prdw3'
) AS temptablename2
ON
app_fd_list_of_orders.c_orderid = SUBSTRING_INDEX(
SUBSTRING_INDEX(
temptablename2.data,
'"orderId":"',
-1
),
'"',
1
)
Have you tried removing the parentheses around the CASE statement?
CASE
WHEN #counterStatus = 'Counting' AND #counter < 0 THEN "black"
WHEN #counterStatus = 'Counting' AND #counter > 5 THEN "green"
WHEN #counterStatus = 'Counting' AND -1 < #counter < 5 THEN "orange"
WHEN #counterStatus = 'Complete' AND #counter > 35 THEN "black"
WHEN #counterStatus = 'Complete' AND #counter < 35 THEN "green"
ELSE "n/a"
END AS 'RESULT'
EDIT: I'm also not sure this is possible:
WHEN #counterStatus = 'Counting' AND -1 < #counter < 5 THEN "orange"
It may need to be written as:
WHEN #counterStatus = 'Counting' AND -1 < #counter AND #counter < 5 THEN "orange"

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

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

SQL Server SELECT statement compare time in db with current time

This is probably a noob question, but i am a little stuck.
I have this long SELECT query :
SELECT
dbo.Dagplanning.GeldigOp ,
dbo.Trips_Bus.Route ,
dbo.Trips_Bus.TripStart AS BeginUur ,
dbo.Places.Naam AS BeginPlaats ,
dbo.Trips_Bus.TripEnd AS EindUur ,
dbo.Trips_Bus.Duty AS Dienst ,
dbo.Trips_Bus.Block ,
dbo.Personeel.Personeelsnummer ,
dbo.Personeel.Naam ,
dbo.Personeel.VoorNaam ,
dbo.Trips_Bus.OpDay ,
dbo.Trips_Bus.PeriodeID
FROM
dbo.Dagplanning ,
dbo.Personeel ,
dbo.Trips_Bus ,
dbo.Places
WHERE
dbo.Trips_Bus.TripStart >= DATEADD(minute, DATEDIFF(minute, -5, GETDATE()), 0)
AND dbo.Trips_Bus.TripStart <= DATEADD(minute, DATEDIFF(minute, 300, GETDATE()), 0)
AND dbo.Personeel.Personeelsnummer = dbo.Dagplanning.Personeelsnummer
AND dbo.Trips_Bus.Duty = ( dbo.Dagplanning.Rol + '+' + REPLICATE('0', 3 - LEN(dbo.Dagplanning.Dienst)) + dbo.Dagplanning.Dienst )
AND dbo.Trips_bus.Opday LIKE '%" + opdag + "%'
AND dbo.Trips_Bus.PeriodeID = '" + periodeid + "'
AND dbo.Dagplanning.GeldigOp = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
AND dbo.Trips_Bus.TripFrom = dbo.Places.Place
ORDER BY
dbo.Trips_Bus.TripStart ASC
It's actually this part that I have a problem with:
WHERE
dbo.Trips_Bus.TripStart >= DATEADD(minute, DATEDIFF(minute, -5, GETDATE()), 0)
AND dbo.Trips_Bus.TripStart <= DATEADD(minute, DATEDIFF(minute, 300, GETDATE()), 0)
This returns nothing, no error but also no data. De data in the table is stored like this example : 21:50:00
Any idea what I am doing wrong? Please be gentle ;-)
Oh, I am completely new at this, so please keep that in mind!

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)