I'm struggling to implement an INSERT statement that get's is values from a
CTE. What I want to do is identify zip/crrt combinations at meet minimum
qty specifications, and then insert into a table the recid values of those
record destined to the identify zip/crrts. While the syntax below is
incorrect, hopefully it conveys what I am trying to accomplish.
Can someone assist? Your time and consideration is much appreciated.
don v nielsen
Note: CTAS would work in this scenario. But I cannot figure out that
syntax, as well.
insert into vo_crrt_pieces (recid)
values (
with
-- controls from carrier route processing
pkg_controls AS (
SELECT * FROM d_pkg WHERE pkg_level = 'CRD'
)
-- select pieces that meeting pkg and pkg_level minimums
, min_pkgs_met AS (
SELECT
a.zip
, a.crrt
, count(1) pkg_pieces
, sum(CASE WHEN c.selective THEN 1 ELSE 0 END) sel_pieces
, sum(CASE WHEN NOT c.selective THEN 1 ELSE 0 END) non_pieces
FROM
addresses [a]
, pkg_controls [b]
JOIN versions [c] ON c.ver_id = a.version_id
WHERE trim(crrt) <> ''
GROUP BY
a.zip,
a.crrt
HAVING
count(1) > b.min_pkg
AND sum(CASE WHEN NOT c.selective THEN 1 ELSE 0 END) > b.min_pieces
)
SELECT recid
FROM addresses [c]
JOIN min_pkgs_met [a] ON a.zip = c.zip AND a.crrt = a.crrt
)