| 1 | -- преглед на сите активни продукти
|
|---|
| 2 | CREATE OR REPLACE VIEW vw_active_products AS
|
|---|
| 3 | SELECT
|
|---|
| 4 | p.product_id,
|
|---|
| 5 | p.title,
|
|---|
| 6 | p.price,
|
|---|
| 7 | p.currency,
|
|---|
| 8 |
|
|---|
| 9 | CASE
|
|---|
| 10 | WHEN p.quantity <= 5 THEN 1
|
|---|
| 11 | ELSE 0
|
|---|
| 12 | END AS low_stock,
|
|---|
| 13 |
|
|---|
| 14 | ( SELECT pi.image_url
|
|---|
| 15 | FROM productimages pi
|
|---|
| 16 | WHERE pi.product_id = p.product_id
|
|---|
| 17 | ORDER BY pi.image_id
|
|---|
| 18 | LIMIT 1) AS thumbnail,
|
|---|
| 19 |
|
|---|
| 20 | --za filter
|
|---|
| 21 | u.user_id AS seller_id,
|
|---|
| 22 | c.category_id,
|
|---|
| 23 | p.location
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | FROM product p
|
|---|
| 27 | JOIN category c ON c.category_id = p.category_id
|
|---|
| 28 | JOIN appuser u ON u.user_id = p.seller_id
|
|---|
| 29 | WHERE p.is_active = 1 AND p.quantity > 0 AND u.is_active = 1;
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | -------------------------------------------------------
|
|---|
| 33 | -- детален преглед на избран продукт
|
|---|
| 34 | DROP VIEW vw_product_details;
|
|---|
| 35 |
|
|---|
| 36 | CREATE OR REPLACE VIEW vw_product_details AS
|
|---|
| 37 |
|
|---|
| 38 | WITH product_rating_reviews AS (
|
|---|
| 39 | SELECT r.product_id,
|
|---|
| 40 | COALESCE(avg(r.rating), 0::numeric) AS average_rating,
|
|---|
| 41 | count(r.review_id) AS total_reviews
|
|---|
| 42 | FROM review r
|
|---|
| 43 | GROUP BY r.product_id
|
|---|
| 44 | ),
|
|---|
| 45 |
|
|---|
| 46 | product_images AS (
|
|---|
| 47 | SELECT pi.product_id,
|
|---|
| 48 | array_agg(pi.image_url ORDER BY pi.image_id) AS images,
|
|---|
| 49 | min(pi.image_url::text) AS thumbnail
|
|---|
| 50 | FROM productimages pi
|
|---|
| 51 | GROUP BY pi.product_id
|
|---|
| 52 | )
|
|---|
| 53 |
|
|---|
| 54 | SELECT
|
|---|
| 55 | p.product_id,
|
|---|
| 56 | p.title,
|
|---|
| 57 | p.description,
|
|---|
| 58 | p.price,
|
|---|
| 59 | p.currency,
|
|---|
| 60 | p.created_at,
|
|---|
| 61 | p.location,
|
|---|
| 62 | CASE
|
|---|
| 63 | WHEN p.quantity <= 5 THEN 1
|
|---|
| 64 | ELSE 0
|
|---|
| 65 | END AS low_stock,
|
|---|
| 66 |
|
|---|
| 67 | c.name,
|
|---|
| 68 |
|
|---|
| 69 | u.username AS seller_username,
|
|---|
| 70 | u.first_name,
|
|---|
| 71 | u.last_name,
|
|---|
| 72 |
|
|---|
| 73 | --od gore od expressions
|
|---|
| 74 | ps.average_rating,
|
|---|
| 75 | ps.total_reviews,
|
|---|
| 76 | i.images,
|
|---|
| 77 | i.thumbnail
|
|---|
| 78 |
|
|---|
| 79 | FROM product p
|
|---|
| 80 | JOIN category c ON c.category_id = p.category_id
|
|---|
| 81 | JOIN appuser u ON u.user_id = p.seller_id
|
|---|
| 82 | LEFT JOIN product_rating_reviews ps ON ps.product_id = p.product_id
|
|---|
| 83 | LEFT JOIN product_images i ON i.product_id = p.product_id;
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | -----------------------------------------------------
|
|---|
| 87 | -- преглед на reviews за конкретен продукт
|
|---|
| 88 | DROP VIEW vw_product_reviews
|
|---|
| 89 |
|
|---|
| 90 | CREATE OR REPLACE VIEW vw_product_reviews AS
|
|---|
| 91 | SELECT
|
|---|
| 92 | r.review_id,
|
|---|
| 93 | r.product_id,
|
|---|
| 94 | p.title,
|
|---|
| 95 | r.rating,
|
|---|
| 96 | r.comment,
|
|---|
| 97 | r.created_at,
|
|---|
| 98 |
|
|---|
| 99 | r.buyer_id,
|
|---|
| 100 | ub.username AS buyer_username,
|
|---|
| 101 | r.seller_id
|
|---|
| 102 |
|
|---|
| 103 | FROM review r
|
|---|
| 104 | JOIN product p ON p.product_id = r.product_id
|
|---|
| 105 | JOIN appuser ub ON ub.user_id = r.buyer_id;
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | -------------------------------------------------------------
|
|---|
| 109 | --------------------------------------------------------------
|
|---|
| 110 | -- преглед на листата со омилени продукти на корисникот.
|
|---|
| 111 | CREATE OR REPLACE VIEW vw_favorites AS
|
|---|
| 112 | SELECT
|
|---|
| 113 | f.user_id,
|
|---|
| 114 |
|
|---|
| 115 | p.product_id,
|
|---|
| 116 | p.title,
|
|---|
| 117 | p.price,
|
|---|
| 118 | p.currency,
|
|---|
| 119 |
|
|---|
| 120 | CASE
|
|---|
| 121 | WHEN p.quantity <= 5 THEN 1
|
|---|
| 122 | ELSE 0
|
|---|
| 123 | END AS low_stock,
|
|---|
| 124 |
|
|---|
| 125 | ( SELECT pi.image_url
|
|---|
| 126 | FROM productimages pi
|
|---|
| 127 | WHERE pi.product_id = p.product_id
|
|---|
| 128 | ORDER BY pi.image_id
|
|---|
| 129 | LIMIT 1) AS thumbnail
|
|---|
| 130 |
|
|---|
| 131 | FROM favorites f
|
|---|
| 132 | JOIN product p ON f.product_id = p.product_id;
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | explain analyze
|
|---|
| 136 | SELECT *
|
|---|
| 137 | FROM vw_favorites
|
|---|
| 138 | where user_id = 5;
|
|---|
| 139 |
|
|---|
| 140 | --------------------------------------------------------
|
|---|
| 141 | ------------------------------------------------------------
|
|---|
| 142 | -- преглед на сите ставки во кошничката на корисникот.
|
|---|
| 143 | CREATE OR REPLACE VIEW vw_cart_items AS
|
|---|
| 144 | SELECT
|
|---|
| 145 | ci.cart_id, --filter
|
|---|
| 146 | ci.cart_item_id,
|
|---|
| 147 |
|
|---|
| 148 | ci.product_id,
|
|---|
| 149 | p.title,
|
|---|
| 150 | p.price AS current_price,
|
|---|
| 151 | p.currency,
|
|---|
| 152 | ci.quantity,
|
|---|
| 153 | ci.price_at_time,
|
|---|
| 154 |
|
|---|
| 155 | (ci.quantity * ci.price_at_time) AS subtotal,
|
|---|
| 156 |
|
|---|
| 157 | (
|
|---|
| 158 | SELECT image_url
|
|---|
| 159 | FROM productimages pi
|
|---|
| 160 | WHERE pi.product_id = p.product_id
|
|---|
| 161 | ORDER BY image_id
|
|---|
| 162 | LIMIT 1
|
|---|
| 163 | ) AS thumbnail
|
|---|
| 164 |
|
|---|
| 165 | FROM cartitems ci
|
|---|
| 166 | JOIN product p ON p.product_id = ci.product_id
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 | ----------------------------------------------------
|
|---|
| 170 | -- сумиран приказ на кошничката.
|
|---|
| 171 | drop view vw_cart_summary;
|
|---|
| 172 |
|
|---|
| 173 | CREATE OR REPLACE VIEW vw_cart_summary AS
|
|---|
| 174 | SELECT
|
|---|
| 175 | c.cart_id,
|
|---|
| 176 | c.created_at,
|
|---|
| 177 |
|
|---|
| 178 | c.user_id, --filter
|
|---|
| 179 |
|
|---|
| 180 | c.total_price AS cart_total,
|
|---|
| 181 |
|
|---|
| 182 | COUNT(ci.cart_item_id) AS total_items,
|
|---|
| 183 | SUM(ci.quantity) AS total_quantity
|
|---|
| 184 |
|
|---|
| 185 | FROM cart c
|
|---|
| 186 | JOIN cartitems ci ON ci.cart_id = c.cart_id
|
|---|
| 187 |
|
|---|
| 188 | GROUP BY c.cart_id;
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 | -------------------------------------------------
|
|---|
| 193 | -------------------------------------------------
|
|---|
| 194 | -- преглед на сите продукти кои припаѓаат на одредена нарачка
|
|---|
| 195 | drop view vw_order_items
|
|---|
| 196 |
|
|---|
| 197 | CREATE OR REPLACE VIEW v_order_items AS
|
|---|
| 198 | SELECT
|
|---|
| 199 | oi.order_id, -- filter
|
|---|
| 200 | oi.order_item_id,
|
|---|
| 201 |
|
|---|
| 202 | oi.product_id,
|
|---|
| 203 | p.title,
|
|---|
| 204 | p.price AS current_price,
|
|---|
| 205 | p.currency,
|
|---|
| 206 | oi.quantity,
|
|---|
| 207 | oi.price_at_time,
|
|---|
| 208 |
|
|---|
| 209 | (oi.quantity * oi.price_at_time) AS subtotal,
|
|---|
| 210 |
|
|---|
| 211 | (
|
|---|
| 212 | SELECT image_url
|
|---|
| 213 | FROM productimages pi
|
|---|
| 214 | WHERE pi.product_id = p.product_id
|
|---|
| 215 | ORDER BY image_id
|
|---|
| 216 | LIMIT 1
|
|---|
| 217 | ) AS thumbnail
|
|---|
| 218 |
|
|---|
| 219 | FROM orderitems oi
|
|---|
| 220 | JOIN product p ON p.product_id = oi.product_id;
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 | --------------------------------------------------------
|
|---|
| 224 | -- сумиран приказ на нарачка
|
|---|
| 225 | CREATE OR REPLACE VIEW v_order_summary AS
|
|---|
| 226 | SELECT
|
|---|
| 227 | o.order_id,
|
|---|
| 228 | o.created_at AS order_date,
|
|---|
| 229 | o.status AS order_status,
|
|---|
| 230 | o.total_price,
|
|---|
| 231 |
|
|---|
| 232 | u.first_name || ' ' || u.last_name AS buyer_name,
|
|---|
| 233 | ua.street || ' ' || ua.house_number AS street_address,
|
|---|
| 234 | ua.city,
|
|---|
| 235 |
|
|---|
| 236 | c.name AS carrier_name,
|
|---|
| 237 | s.tracking_number,
|
|---|
| 238 |
|
|---|
| 239 | (SELECT COUNT(*) FROM orderitems oi WHERE oi.order_id = o.order_id) AS total_items,
|
|---|
| 240 | (SELECT SUM(quantity) FROM orderitems oi WHERE oi.order_id = o.order_id) AS total_quantity
|
|---|
| 241 |
|
|---|
| 242 | FROM "order" o
|
|---|
| 243 | JOIN appuser u ON u.user_id = o.buyer_id
|
|---|
| 244 | JOIN shipment s ON s.order_id = o.order_id
|
|---|
| 245 | JOIN carriers c ON c.carrier_id = s.carrier_id
|
|---|
| 246 | JOIN useraddress ua ON ua.user_address_id = s.user_address_id;
|
|---|
| 247 |
|
|---|
| 248 | ---------------------------------------------------------------------------
|
|---|
| 249 | ---------------------------------------------------------------------------
|
|---|
| 250 | -- dashboard приказ со статистики за продуктите на одреден продавач
|
|---|
| 251 | CREATE OR REPLACE VIEW v_seller_dashboard AS
|
|---|
| 252 | WITH active_package AS (
|
|---|
| 253 | SELECT DISTINCT ON (seller_id)
|
|---|
| 254 | seller_id,
|
|---|
| 255 | package_id
|
|---|
| 256 | FROM UserPackages
|
|---|
| 257 | WHERE CURRENT_TIMESTAMP BETWEEN start_date AND end_date
|
|---|
| 258 | ORDER BY seller_id, end_date DESC
|
|---|
| 259 | )
|
|---|
| 260 | SELECT
|
|---|
| 261 | u.user_id AS seller_id,
|
|---|
| 262 | u.first_name,
|
|---|
| 263 | u.last_name,
|
|---|
| 264 | COUNT(DISTINCT pr.product_id) AS total_products,
|
|---|
| 265 | SUM(oi.quantity) AS total_sales,
|
|---|
| 266 | SUM(oi.quantity * oi.price_at_time) AS total_revenue,
|
|---|
| 267 | ROUND(AVG(r.rating), 2) AS avg_rating,
|
|---|
| 268 | pck.name AS package_name
|
|---|
| 269 | FROM AppUser u
|
|---|
| 270 | LEFT JOIN Product pr ON pr.seller_id = u.user_id
|
|---|
| 271 | LEFT JOIN OrderItems oi ON oi.product_id = pr.product_id
|
|---|
| 272 | LEFT JOIN Review r ON r.product_id = pr.product_id
|
|---|
| 273 | LEFT JOIN active_package ap ON ap.seller_id = u.user_id
|
|---|
| 274 | LEFT JOIN Package pck ON pck.package_id = ap.package_id
|
|---|
| 275 | WHERE u.is_verified = 1
|
|---|
| 276 | GROUP BY u.user_id, u.first_name, u.last_name, pck.name;
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 | EXPLAIN ANALYZE
|
|---|
| 281 | SELECT *
|
|---|
| 282 | FROM v_seller_dashboard
|
|---|
| 283 | WHERE seller_id = 120824;
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 | --------------------------------------------------------------
|
|---|
| 287 | -- преглед на кориснички профил
|
|---|
| 288 | drop view v_user_profile
|
|---|
| 289 |
|
|---|
| 290 | CREATE OR REPLACE VIEW v_user_profile as
|
|---|
| 291 |
|
|---|
| 292 | WITH primary_location AS (
|
|---|
| 293 | SELECT user_id, city || ', ' || country AS location
|
|---|
| 294 | FROM UserAddress
|
|---|
| 295 | WHERE is_primary = 1
|
|---|
| 296 | ),
|
|---|
| 297 | user_orders AS (
|
|---|
| 298 | SELECT buyer_id, COUNT(*) AS total_orders
|
|---|
| 299 | FROM "order"
|
|---|
| 300 | GROUP BY buyer_id
|
|---|
| 301 | )
|
|---|
| 302 | SELECT
|
|---|
| 303 | u.user_id,
|
|---|
| 304 | u.first_name,
|
|---|
| 305 | u.last_name,
|
|---|
| 306 | u.email,
|
|---|
| 307 | u.phone_number,
|
|---|
| 308 | pl.location AS primary_location,
|
|---|
| 309 | uo.total_orders,
|
|---|
| 310 | u.created_at
|
|---|
| 311 | FROM AppUser u
|
|---|
| 312 | LEFT JOIN primary_location pl ON pl.user_id = u.user_id
|
|---|
| 313 | LEFT JOIN user_orders uo ON uo.buyer_id = u.user_id;
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 | explain analyze
|
|---|
| 318 | SELECT * FROM v_user_profile
|
|---|
| 319 | WHERE user_id = 600;
|
|---|
| 320 |
|
|---|