Skip to content

Commit ef07904

Browse files
Merge pull request #59 from Ritik574-coder/dbt_branch
data cleaning and data transformation
2 parents 4287da0 + d000846 commit ef07904

1 file changed

Lines changed: 164 additions & 10 deletions

File tree

explore_database/inventory/inventory.sql

Lines changed: 164 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ SELECT
231231
category
232232
FROM bronze.inventory_snapshots
233233
WHERE category IS NULL
234-
OR category = ''
235-
OR category != TRIM(category) ;
234+
OR category = ''
235+
OR category != TRIM(category) ;
236236

237237
SELECT DISTINCT
238238
category
@@ -253,8 +253,8 @@ SELECT
253253
category COLLATE Latin1_General_CS_AS as category ,
254254
COUNT(*) as total_count
255255
FROM bronze.inventory_snapshots
256-
GROUP BY category COLLATE Latin1_General_CS_AS
257-
ORDER BY total_count DESC ;
256+
GROUP BY category COLLATE Latin1_General_CS_AS
257+
ORDER BY total_count DESC ;
258258

259259
-- category cleaning and standardization
260260
WITH category_analysis AS
@@ -279,8 +279,140 @@ SELECT
279279
category COLLATE Latin1_General_CS_AS as category ,
280280
COUNT(*) as total_count
281281
FROM category_analysis
282-
GROUP BY category COLLATE Latin1_General_CS_AS
283-
ORDER BY total_count DESC ;
282+
GROUP BY category COLLATE Latin1_General_CS_AS
283+
ORDER BY total_count DESC ;
284+
285+
--=============================================================================================
286+
--=============================== stock_on_hand column cleaning ===============================
287+
--=============================================================================================
288+
-- stock_on_hand data overview
289+
SELECT
290+
stock_on_hand
291+
FROM bronze.inventory_snapshots ;
292+
293+
--stock_on_hand data profiling
294+
SELECT
295+
stock_on_hand
296+
FROM bronze.inventory_snapshots
297+
WHERE stock_on_hand IS NULL
298+
OR stock_on_hand < 0
299+
OR TRY_CONVERT(INT, stock_on_hand) IS NULL ;
300+
301+
--Stock_on_hand cleaning and standardization
302+
SELECT
303+
CASE
304+
WHEN TRY_CONVERT(INT, stock_on_hand) IS NULL OR stock_on_hand < 0 THEN NULL
305+
ELSE TRY_CONVERT(INT, stock_on_hand)
306+
END AS stock_on_hand
307+
FROM bronze.inventory_snapshots
308+
WHERE stock_on_hand IS NULL ;
309+
310+
--=============================================================================================
311+
--=============================== stock_reserved column cleaning ==============================
312+
--=============================================================================================
313+
-- stock_reserved data overview
314+
SELECT
315+
stock_reserved
316+
FROM bronze.inventory_snapshots ;
317+
318+
-- stock_reserved data profiling
319+
SELECT
320+
stock_reserved
321+
FROM bronze.inventory_snapshots
322+
WHERE stock_reserved IS NULL
323+
OR stock_reserved < 0
324+
OR TRY_CONVERT(INT, stock_reserved) IS NULL ;
325+
326+
-- stock_reserved cleaning and standardization
327+
SELECT
328+
CASE
329+
WHEN stock_reserved < 0 OR TRY_CONVERT(INT, stock_reserved) IS NULL THEN NULL
330+
ELSE TRY_CONVERT(INT, stock_reserved)
331+
END as stock_reserved
332+
FROM bronze.inventory_snapshots
333+
WHERE stock_reserved IS NULL ;
334+
335+
--=============================================================================================
336+
--============================= stock_available column cleaning ===============================
337+
--=============================================================================================
338+
-- stock_available data overview
339+
SELECT
340+
stock_available
341+
FROM bronze.inventory_snapshots
342+
343+
-- stock_available data profilint
344+
SELECT
345+
stock_available
346+
FROM bronze.inventory_snapshots
347+
WHERE stock_available IS NULL
348+
OR stock_available < 0
349+
OR TRY_CONVERT(INT, stock_available) IS NULL ;
350+
351+
-- null count in stock_available
352+
SELECT
353+
COUNT(*) as null_count
354+
FROM bronze.inventory_snapshots
355+
WHERE stock_available IS NULL ;
356+
357+
-- not null count in stock_available
358+
SELECT
359+
stock_available
360+
FROM bronze.inventory_snapshots
361+
WHERE stock_available IS NOT NULL ;
362+
363+
-- stock_available cleaning and standardization
364+
SELECT
365+
CASE
366+
WHEN stock_available IS NULL THEN stock_on_hand - stock_reserved
367+
WHEN TRY_CONVERT(INT, stock_available) IS NULL THEN NULL
368+
ELSE TRY_CONVERT(INT, stock_available)
369+
END as stock_available
370+
FROM bronze.inventory_snapshots ;
371+
372+
--=============================================================================================
373+
--=============================== reorder_level column cleaning ===============================
374+
--=============================================================================================
375+
-- reorder_level data overview
376+
SELECT
377+
reorder_level
378+
FROM bronze.inventory_snapshots ;
379+
380+
--reorder_level data profiling
381+
SELECT
382+
reorder_level
383+
FROM bronze.inventory_snapshots
384+
WHERE reorder_level IS NULL
385+
OR reorder_level < 0
386+
OR TRY_CONVERT(INT, reorder_level) IS NULL ;
387+
388+
-- reorder_level cleaning and standardization
389+
SELECT
390+
CASE
391+
WHEN reorder_level IS NULL OR reorder_level < 0 OR TRY_CONVERT(INT, reorder_level) IS NULL THEN NULL
392+
ELSE TRY_CONVERT(INT, reorder_level)
393+
END as reorder_level
394+
FROM bronze.inventory_snapshots ;
395+
396+
--=============================================================================================
397+
--=============================== unit_cost column cleaning ===================================
398+
--=============================================================================================
399+
SELECT
400+
unit_cost
401+
FROM bronze.inventory_snapshots
402+
403+
--=============================================================================================
404+
--================================== unit_price column cleaning ===============================
405+
--=============================================================================================
406+
SELECT
407+
unit_price
408+
FROM bronze.inventory_snapshots
409+
410+
--=============================================================================================
411+
--=============================== inventory_value column cleaning ============================
412+
--=============================================================================================
413+
SELECT
414+
inventory_value
415+
FROM bronze.inventory_snapshots
284416

285417
--#############################################################################################
286418
--############################## EMPLOYEE CLEAN DATA ##########################################
@@ -329,13 +461,35 @@ SELECT TOP (1000)
329461
ELSE 'Unknown'
330462
END AS category
331463

332-
,[stock_on_hand]
333-
,[stock_reserved]
334-
,[stock_available]
335-
,[reorder_level]
464+
,CASE
465+
WHEN TRY_CONVERT(INT, stock_on_hand) IS NULL OR stock_on_hand < 0 THEN NULL
466+
ELSE TRY_CONVERT(INT, stock_on_hand)
467+
END AS stock_on_hand
468+
469+
,CASE
470+
WHEN stock_reserved < 0 OR TRY_CONVERT(INT, stock_reserved) IS NULL THEN NULL
471+
ELSE TRY_CONVERT(INT, stock_reserved)
472+
END as stock_reserved
473+
474+
,CASE
475+
WHEN stock_available IS NULL THEN stock_on_hand - stock_reserved
476+
WHEN TRY_CONVERT(INT, stock_available) IS NULL THEN NULL
477+
ELSE TRY_CONVERT(INT, stock_available)
478+
END as stock_available
479+
480+
,CASE
481+
WHEN reorder_level IS NULL OR reorder_level < 0 OR TRY_CONVERT(INT, reorder_level) IS NULL THEN NULL
482+
ELSE TRY_CONVERT(INT, reorder_level)
483+
END as reorder_level
484+
336485
,[unit_cost]
486+
337487
,[unit_price]
488+
338489
,[inventory_value]
490+
339491
,[warehouse_location]
492+
340493
,[store_id]
494+
341495
FROM [bronze].[inventory_snapshots]

0 commit comments

Comments
 (0)