CREATE OR REPLACE VIEW vgCEC AS (SELECT CEC.cecData, CEC.cecNumb, CecPrat.cpOrder, CecPrat.peID FROM CEC INNER JOIN CecPrat ON CEC.cecID = CecPrat.cecID);
CREATE OR REPLACE VIEW vgFabbricato AS (SELECT LinkFabInt.peID, FabInt.fiDesc AS Fabbricato FROM LinkFabInt INNER JOIN FabInt ON LinkFabInt.fiID = FabInt.fiID WHERE (LinkFabInt.lfiType = 0 AND LinkFabInt.lfiDef > 0));
CREATE OR REPLACE VIEW vgIntervento AS (SELECT LinkFabInt.peID, FabInt.fiDesc AS Intervento, LinkFabInt.lfiDef FROM LinkFabInt INNER JOIN FabInt ON LinkFabInt.fiID = FabInt.fiID WHERE (LinkFabInt.lfiDef > 0 AND LinkFabInt.lfiType = 1));
CREATE OR REPLACE VIEW vgIntestatario AS (SELECT LinkAnag.peID, Anagrafiche.anRagSoc AS Intestatario FROM LinkAnag INNER JOIN Anagrafiche ON LinkAnag.anID = Anagrafiche.anID WHERE (LinkAnag.laType = 0 AND LinkAnag.laDef > 0));
CREATE OR REPLACE VIEW vgLastIntPres AS (SELECT Integrazioni.peID, MAX(IntPres.ipData) AS LastIntPres FROM Integrazioni INNER JOIN IntPres ON Integrazioni.intID = IntPres.intID GROUP BY Integrazioni.peID);
CREATE OR REPLACE VIEW vgProgettista AS (SELECT LinkAnag.peID, Anagrafiche.anRagSoc AS Progettista FROM LinkAnag INNER JOIN Anagrafiche ON LinkAnag.anID = Anagrafiche.anID WHERE (LinkAnag.laType = 1 AND LinkAnag.laDef > 0));
CREATE OR REPLACE VIEW vgTotOneri AS (SELECT peID, SUM(euCosto *(onSegno * -2 + 1) - asCcImp) AS Costo, SUM(euUrb1 * (onSegno * -2 + 1) - asU1Imp) AS Urb1, SUM(euUrb2 * (onSegno * -2 + 1) - asU2Imp) AS Urb2, SUM(euSmaltRif * (onSegno * -2 + 1) - asSrImp) AS Smalt, SUM(euSistAmb * (onSegno * -2 + 1) - asSaImp) AS Amb FROM Oneri GROUP BY peID);
CREATE OR REPLACE VIEW vgVia AS (SELECT Vie.viaID, CONCAT(Toponimi.tpvDesc, ' ', Vie.viaDesc) AS Via FROM Vie INNER JOIN Toponimi ON Vie.tpvID = Toponimi.tpvID);
CREATE OR REPLACE VIEW vgViaTop AS (SELECT Vie.viaID, CONCAT(Vie.viaDesc, ', ', IFNULL(Toponimi.tpvDesc, '')) AS Via FROM Vie LEFT JOIN Toponimi ON Vie.tpvID = Toponimi.tpvID);
CREATE OR REPLACE VIEW vgZone AS (SELECT GpeZone.znID, CONCAT(GpeZone.znCod, ' - ', GpeZone.znDesc) AS Zona, GpeZone.gwView FROM GpeZone);
CREATE OR REPLACE VIEW vgPosLastData AS (SELECT Posizioni.peID AS peID, MAX(Posizioni.ppData) AS lastPosData FROM Posizioni GROUP BY Posizioni.peID);
CREATE OR REPLACE VIEW vgPosLastId AS (SELECT Posizioni.peID AS peID, MAX(Posizioni.ppID) AS lastPosID FROM (Posizioni JOIN vgPosLastData ON (((Posizioni.peID = vgPosLastData.peID) AND (Posizioni.ppData = vgPosLastData.lastPosData)))) GROUP BY Posizioni.peID);
CREATE OR REPLACE VIEW vgPosLast AS (SELECT Posizioni.peID AS peID, UserTab.utDesc AS Posizione FROM ((Posizioni LEFT JOIN UserTab ON ((Posizioni.utID = UserTab.utID))) JOIN vgPosLastId ON (((Posizioni.peID = vgPosLastId.peID) AND (Posizioni.ppID = vgPosLastId.lastPosID)))));
CREATE OR REPLACE VIEW vgPosLastFlt AS (SELECT Posizioni.peID AS peID, Posizioni.utID AS utID FROM (Posizioni JOIN vgPosLastId ON (((Posizioni.ppID = vgPosLastId.lastPosID) AND (Posizioni.peID = vgPosLastId.peID)))));
