Skip to content

Instantly share code, notes, and snippets.

@keithf4
Created November 30, 2015 21:26
Show Gist options
  • Save keithf4/549779a468a76e5f81d6 to your computer and use it in GitHub Desktop.
Save keithf4/549779a468a76e5f81d6 to your computer and use it in GitHub Desktop.
"broken" view
--
-- TOC entry 2690 (class 1259 OID 17197646)
-- Name: sku_average_costs_in_our_possession; Type: TABLE; Schema: admin_views;
CREATE TABLE sku_average_costs_in_our_possession (
sku_id integer,
id integer,
product_id integer,
created_at timestamp with time zone,
updated_at timestamp with time zone,
size character varying(100),
location character varying(12),
upc_code character varying(13),
deactivated_at timestamp with time zone,
scn bigint,
origin character(1),
product_look_id integer,
msrp_price numeric(10,2),
sale_price numeric(10,2),
vendor_bar_code character varying(50),
shipping_surcharge numeric(10,2),
force_ground_shipping smallint,
is_webstore_sku smallint,
webstore_sku_id integer,
foreign_msrp_price numeric(10,2),
foreign_currency character varying(40),
weight numeric(10,2),
length numeric(10,2),
width numeric(10,2),
height numeric(10,2),
is_exit_inventory boolean,
is_giveaway boolean,
extra_weight numeric(10,2),
vendor_id bigint,
is_sellable boolean,
primary_size_chart_column_id bigint,
size_id bigint,
ship_alone_qty integer,
sku_attribute_set_id bigint,
ship_method_id integer,
vendor_style_number character varying(50),
vendor_color character varying(100),
data_source_key character varying(100),
country_of_origin_id bigint,
total_cost numeric(10,2),
total_average_cost numeric(10,2),
total_units bigint,
physical_cost numeric(10,2),
physical_average_cost numeric(10,2),
physical_units bigint,
virtual_cost numeric(10,2),
virtual_average_cost numeric(10,2),
virtual_units bigint,
consignment_cost numeric(10,2),
consignment_average_cost numeric(10,2),
consignment_units bigint
);
ALTER TABLE ONLY sku_average_costs_in_our_possession REPLICA IDENTITY NOTHING;
CREATE RULE "_RETURN" AS
ON SELECT TO sku_average_costs_in_our_possession DO INSTEAD SELECT skus.id AS sku_id,
skus.id,
skus.product_id,
skus.created_at,
skus.updated_at,
skus.size,
skus.location,
skus.upc_code,
skus.deactivated_at,
skus.scn,
skus.origin,
skus.product_look_id,
skus.msrp_price,
skus.sale_price,
skus.vendor_bar_code,
skus.shipping_surcharge,
skus.force_ground_shipping,
skus.is_webstore_sku,
skus.webstore_sku_id,
skus.foreign_msrp_price,
skus.foreign_currency,
skus.weight,
skus.length,
skus.width,
skus.height,
skus.is_exit_inventory,
skus.is_giveaway,
skus.extra_weight,
skus.vendor_id,
skus.is_sellable,
skus.primary_size_chart_column_id,
skus.size_id,
skus.ship_alone_qty,
skus.sku_attribute_set_id,
skus.ship_method_id,
skus.vendor_style_number,
skus.vendor_color,
skus.data_source_key,
skus.country_of_origin_id,
(sum(units.base_price))::numeric(10,2) AS total_cost,
(avg(units.base_price))::numeric(10,2) AS total_average_cost,
count(DISTINCT units.id) AS total_units,
(sum(units.base_price) FILTER (WHERE (NOT units.is_virtual)))::numeric(10,2) AS physical_cost,
(avg(units.base_price) FILTER (WHERE (NOT units.is_virtual)))::numeric(10,2) AS physical_average_cost,
count(DISTINCT units.id) FILTER (WHERE (NOT units.is_virtual)) AS physical_units,
(sum(units.base_price) FILTER (WHERE units.is_virtual))::numeric(10,2) AS virtual_cost,
(avg(units.base_price) FILTER (WHERE units.is_virtual))::numeric(10,2) AS virtual_average_cost,
count(DISTINCT units.id) FILTER (WHERE units.is_virtual) AS virtual_units,
(sum(units.base_price) FILTER (WHERE units.is_consignment))::numeric(10,2) AS consignment_cost,
(avg(units.base_price) FILTER (WHERE units.is_consignment))::numeric(10,2) AS consignment_average_cost,
count(DISTINCT units.id) FILTER (WHERE units.is_consignment) AS consignment_units
FROM (public.skus
LEFT JOIN public.units ON (((units.webstore_sku_id = skus.id) AND (units.unit_disposition_id = 1))))
GROUP BY skus.id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment