salix/db/routines/vn/views/lastTopClaims.sql

39 lines
955 B
MySQL
Raw Normal View History

2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost`
SQL SECURITY DEFINER
VIEW `vn`.`lastTopClaims`
AS SELECT `s`.`itemFk` AS `itemFk`,
`i`.`longName` AS `itemName`,
`it`.`name` AS `itemTypeName`,
COUNT(0) AS `claimsNumber`,
round(
sum(
`cb`.`quantity` * `s`.`price` * (1 - (`c`.`responsibility` - 1) / 4) * (100 - `s`.`discount`) / 100
),
2
) AS `claimedAmount`,
round(
sum(
`cb`.`quantity` * `s`.`price` * (100 - `s`.`discount`) / 100
),
2
) AS `totalAmount`
FROM (
(
(
(
`vn`.`claim` `c`
JOIN `vn`.`claimBeginning` `cb` ON(`cb`.`claimFk` = `c`.`id`)
)
JOIN `vn`.`sale` `s` ON(`s`.`id` = `cb`.`saleFk`)
)
JOIN `vn`.`item` `i` ON(`i`.`id` = `s`.`itemFk`)
)
JOIN `vn`.`itemType` `it` ON(`it`.`id` = `i`.`typeFk`)
)
WHERE `c`.`created` >= `util`.`VN_CURDATE`() + INTERVAL -1 week
GROUP BY `s`.`itemFk`
HAVING `claimedAmount` > 100
AND `claimsNumber` > 2
OR `claimsNumber` > 4
ORDER BY COUNT(0) DESC