38 lines
1.0 KiB
SQL
38 lines
1.0 KiB
SQL
WITH tickets AS(
|
|
SELECT id, packages, addressFk, weight
|
|
FROM ticket
|
|
WHERE refFk='E12400228'
|
|
) , weight AS(
|
|
SELECT weight
|
|
FROM tickets
|
|
WHERE weight
|
|
LIMIT 1
|
|
), volume AS(
|
|
SELECT SUM(volume) volume
|
|
FROM tickets t
|
|
JOIN vn.saleVolume sv ON sv.ticketFk = t.id
|
|
), intrastat AS(
|
|
SELECT GROUP_CONCAT(DISTINCT ir.description ORDER BY ir.description SEPARATOR '. ' ) intrastat
|
|
FROM tickets t
|
|
JOIN vn.sale s ON t.id = s.ticketFk
|
|
JOIN vn.item i ON i.id = s.itemFk
|
|
JOIN vn.intrastat ir ON ir.id = i.intrastatFk
|
|
)SELECT SUM(t.packages),
|
|
a.incotermsFk,
|
|
ic.name incotermsName,
|
|
w.weight,
|
|
ca.fiscalName customsAgentName,
|
|
ca.street customsAgentStreet,
|
|
ca.nif customsAgentNif,
|
|
ca.phone customsAgentPhone,
|
|
ca.email customsAgentEmail,
|
|
CAST(v.volume AS DECIMAL (10,2)) volume,
|
|
i.intrastat
|
|
FROM tickets t
|
|
JOIN vn.address a ON a.id = t.addressFk
|
|
JOIN vn.incoterms ic ON ic.code = a.incotermsFk
|
|
LEFT JOIN vn.customsAgent ca ON ca.id = a.customsAgentFk
|
|
JOIN volume v
|
|
JOIN intrastat i
|
|
LEFT JOIN weight w ON TRUE
|