7 Commits

Author SHA1 Message Date
Ankit Malik 75fc088c68 22-06-2026 2nd commit 2026-06-22 13:22:16 +05:30
Ankit Malik e1bd387849 22-06-2026 1st commit 2026-06-22 13:08:26 +05:30
Ankit Malik 2ead99553f 22-06-2026 1st commit 2026-06-22 12:05:25 +05:30
Ankit Malik 1ba1545ea8 19-06-2026 2nd commit 2026-06-19 18:15:27 +05:30
Ankit Malik 97b22ea0f8 19-06-2026 1st commit 2026-06-19 16:51:03 +05:30
Ankit Malik d60740ae48 18-06-2026 1st commit 2026-06-18 18:16:50 +05:30
Ankit Malik 9cd4135d7b 17-06-2026 1st commit 2026-06-17 15:40:13 +05:30
38 changed files with 3141 additions and 7267 deletions
+9 -12
View File
@@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import polars as pl import polars as pl
from datetime import date, datetime, timedelta
from log import log from log import log
@@ -38,10 +39,7 @@ def delete_rows(
WHERE {where_clause} WHERE {where_clause}
""" """
log.info( log.info(f"_ _ _ _ Deleting Data from ClickHouse for {table_name} _ _ _ _")
"Deleting from %s",
table_name,
)
client.command(query) client.command(query)
@@ -74,14 +72,12 @@ def delete_existing_data(
if table_name in mid_tables and mids : if table_name in mid_tables and mids :
mids_str = ",".join( mids_str = ",".join(str(mid) for mid in mids)
map(str, mids)
)
delete_rows( delete_rows(
client, client,
table_name, table_name,
f"MID IN ({mids_str})", f"Mid IN ({mids_str})",
) )
return return
@@ -96,8 +92,9 @@ def delete_existing_data(
client, client,
table_name, table_name,
f""" f"""
toMonth(visit_date) = {run_date.month} project_id = 40148
AND toYear(visit_date) = {run_date.year} AND toMonth(visit_date) = toMonth(toDate('2026-06-18'))
AND toYear(visit_date) = toYear(toDate('2026-06-18'))
""", """,
) )
@@ -147,8 +144,8 @@ def delete_existing_data(
client, client,
table_name, table_name,
f""" f"""
toDate(attendance_date) toDate(visit_date) BETWEEN toDate('{run_date - timedelta(days=15) }') AND toDate('{run_date}')
= toDate('{run_date}') =
""", """,
) )
-1301
View File
File diff suppressed because it is too large Load Diff
-164
View File
@@ -1,164 +0,0 @@
WITH SOS_BASE AS
(
SELECT
sm.CountryName,
sc.MID,
sm.RegionName,
sm.StateName,
sm.CityName,
Em.SupervisorName,
em.EmpId,
Em.EmpName AS EmployeeName,
Em.DesignationName AS Designation,
sm.StoreId,
CONVERT(varchar, sc.VisitDate, 101) AS VisitDate,
sm.StoreCode,
sm.StoreName,
sm.Address,
sm.StoreTypeid,
sm.ChannelId,
sm.ChainName,
MSD.SOSDefinitionName,
CASE
WHEN ISNULL(TS.SOSHeaderTable,'')='Master_Category' THEN 'Category'
WHEN ISNULL(TS.SOSHeaderTable,'')='Master_SubCategory' THEN 'SubCategory'
WHEN ISNULL(TS.SOSHeaderTable,'')='Master_Brand' THEN 'Brand'
WHEN ISNULL(TS.SOSHeaderTable,'')='Master_SubBrand' THEN 'SubBrand'
END AS SOSHeaderDeatils,
TS.SOSHeaderName,
TS.SOSHeaderValue AS SOSHeaderID,
'Header_Image' AS HDR1,
CASE
WHEN ISNULL(TSC.SOSChildTable,'')='Master_Category' THEN 'Category'
WHEN ISNULL(TSC.SOSChildTable,'')='Master_SubCategory' THEN 'SubCategory'
WHEN ISNULL(TSC.SOSChildTable,'')='Master_Brand' THEN 'Brand'
WHEN ISNULL(TSC.SOSChildTable,'')='Master_SubBrand' THEN 'SubBrand'
END AS SOSChildDeatils,
TSC.SOSChildName,
TSC.SOSChildValue AS SOSChildID,
TSC.ChildTotalFacing,
TS.SOSHeaderFacing,
TSC.ChildSelfFacing,
(
SELECT TOP 1 SOSTarget
FROM OneApp_KelloggsMT.dbo.Mapping_StoreShareOfShelfTarget a
WHERE a.SOSDefinitionId = MSD.SOSDefinitionId
AND a.StoreId = sm.StoreId
AND a.FromDate <= sc.VisitDate
AND a.ToDate >= sc.VisitDate
) AS SOSTarget,
CASE
WHEN ISNULL(SHI.SOSHeaderImage,'') = ''
THEN ''
ELSE
'https://kimt1.parinaam.in/Upload/SOSImages/'
+ SHI.SOSHeaderImage
END AS SOSHeaderImg
FROM OneApp_KelloggsMT.dbo.T_ShareOfShelfHeader ts
INNER JOIN OneApp_KelloggsMT.dbo.T_StoreCoverage sc
ON ts.MID = sc.MID
INNER JOIN OneApp_KelloggsMT.dbo.vw_StoreDetail sm
ON sc.StoreId = sm.StoreId
INNER JOIN OneApp_KelloggsMT.dbo.vw_Employee_Detail Em
ON sc.EmpId = Em.EmpId
INNER JOIN OneApp_KelloggsMT.dbo.T_ShareOfShelfChild tsc
ON ts.SOSId = tsc.SOSId
INNER JOIN OneApp_KelloggsMT.dbo.Master_ShareOfShelfDefinition msd
ON msd.SOSDefinitionId = tsc.SOSDefinitionId
LEFT JOIN OneApp_KelloggsMT.dbo.T_ShareOfShelfHeaderImages SHI
ON ts.SOSId = SHI.SOSId
AND ts.SOSHeaderValue = SHI.SOSHeaderValue
LEFT JOIN OneApp_KelloggsMT.dbo.T_ShareOfShelfChildImages SCI
ON tsc.SOSId = SCI.SOSId
AND tsc.SOSChildValue = SCI.SOSChildValue
WHERE Em.EmpName NOT LIKE 'test%'
AND sc.MID IN ({mid_list})
),
SOS_PIVOT AS
(
SELECT *
FROM SOS_BASE
PIVOT
(
MIN(SOSHeaderImg)
FOR HDR1 IN ([Header_Image])
) pvt
)
SELECT
'40148' AS ProjectId,
MID,
EmpId AS employee_id,
StoreId AS store_id,
VisitDate AS visit_date,
StoreTypeid AS storetype_id,
ChannelId AS channel_id,
SOSDefinitionName,
SOSHeaderDeatils,
SOSHeaderName,
SOSHeaderID,
SOSChildDeatils,
SOSChildName,
SOSChildID,
SOSHeaderFacing,
ChildTotalFacing,
ChildSelfFacing,
SOSTarget,
GETDATE() AS update_date,
'SP-Pius' AS update_by
FROM SOS_PIVOT
GROUP BY
CountryName,
MID,
RegionName,
StateName,
CityName,
SupervisorName,
EmpId,
EmployeeName,
Designation,
StoreId,
VisitDate,
StoreCode,
StoreName,
Address,
StoreTypeid,
ChannelId,
ChainName,
SOSDefinitionName,
SOSHeaderDeatils,
SOSHeaderName,
SOSHeaderID,
SOSChildDeatils,
SOSChildName,
SOSChildID,
ChildTotalFacing,
SOSHeaderFacing,
ChildSelfFacing,
SOSTarget
ORDER BY
RegionName,
StateName,
CityName,
VisitDate;
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
-276
View File
@@ -1,276 +0,0 @@
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "polars>=0.20.0",
# "pyarrow>=18.0.0",
# "sqlalchemy>=2.0.0",
# "pyodbc>=5.0.0",
# "clickhouse-connect>=0.7.0",
# "clickhouse-sqlalchemy>=0.3.2",
# "pyyaml>=6.0.3",
# "python-dotenv>=1.0.0",
# ]
# ///
from __future__ import annotations
import sys
from datetime import date, datetime, timedelta
import polars as pl
import yaml
from log import log
from clickhouse_task.create_table import create_clickhouse_table
from clickhouse_task.delete_task import (
delete_existing_data,
truncate_table,
)
from clickhouse_task.load_table import load_to_clickhouse
from db_con.connection import (
build_sql_server_engine,
build_clickhouse_engine,
get_clickhouse_client,
)
from mids import (
MID_TABLE_COV,
MID_TABLE_COV1,
)
from masters.dimensions import *
from masters.bridge import *
from kpi.facts import *
# ==========================================================
# Helpers
# ==========================================================
def table_exists(
client,
table_name: str,
) -> bool:
return bool(
client.command(
f"EXISTS TABLE {table_name}"
)
)
def get_dataframe(
fn_name: str,
fetch_by: str,
sql_engine,
mids,
run_date,
):
fn = globals()[fn_name]
if fetch_by == "mids":
return fn(sql_engine, mids)
if fetch_by == "run_date":
return fn(sql_engine, run_date)
return fn(sql_engine)
# ==========================================================
# Main
# ==========================================================
def main():
log.info("=" * 80)
log.info("Hello from data-move Python data pipeline!")
# ------------------------------------------------------
# Run Date
# ------------------------------------------------------
if len(sys.argv) > 1:
run_date = datetime.strptime(
sys.argv[1],
"%Y-%m-%d",
).date()
else:
run_date = date.today() - timedelta(days=1)
log.info(
"Pipeline Run Date: %s",
run_date,
)
# ------------------------------------------------------
# Connections
# ------------------------------------------------------
log.info(
"Connecting to databases..."
)
sql_engine = build_sql_server_engine()
clickhouse_engine = build_clickhouse_engine()
client = get_clickhouse_client()
log.info(
"Database connections established"
)
# ------------------------------------------------------
# Delete Keys
# ------------------------------------------------------
mids = MID_TABLE_COV(
sql_engine,
run_date,
)
emp_visit_df = MID_TABLE_COV1(
sql_engine,
run_date,
)
# ------------------------------------------------------
# Config
# ------------------------------------------------------
with open(
"t.yml",
"r",
) as file:
config = yaml.safe_load(file)
# ------------------------------------------------------
# Process Tables
# ------------------------------------------------------
for table in config["tables"]:
table_name = table["name"]
operation = table["operation"]
fetch_by = table["fetch_by"]
table_type=table["type"]
log.info("=" * 80)
log.info(
"Processing Table: %s",
table_name,
)
try:
# ------------------------------------------
# Fetch Data
# ------------------------------------------
fn_name = f"fetch_{table_name}"
df = get_dataframe(
fn_name=fn_name,
fetch_by=fetch_by,
sql_engine=sql_engine,
mids=mids,
run_date=run_date,
)
if df.is_empty():
log.warning(
"%s returned no rows",
table_name,
)
continue
log.info(
"Fetched %s rows",
len(df),
)
# ------------------------------------------
# Create Table If Missing
# ------------------------------------------
exists = table_exists(
client,
table_name,
)
if not exists:
log.info(
"Creating table %s",
table_name,
)
create_clickhouse_table(
df=df,
table_name=table_name,
clickhouse_engine=clickhouse_engine,
)
# ------------------------------------------
# Existing Table Logic
# ------------------------------------------
else:
if operation == "DELETE+INSERT":
truncate_table(
client,
table_name,
)
else:
delete_existing_data(
client=client,
table_name=table_name,
run_date=run_date,
mids=mids,
emp_visit_df=emp_visit_df,
)
# ------------------------------------------
# Load Data
# ------------------------------------------
load_to_clickhouse(
client=client,
table_name=table_name,
df=df,
)
log.info(
"%s loaded successfully (%s rows)",
table_name,
len(df),
)
except Exception:
log.exception(
"Failed processing table %s",
table_name,
)
raise
log.info("=" * 80)
log.info("Pipeline Completed Successfully")
log.info("=" * 80)
if __name__ == "__main__":
main()
+57 -73
View File
@@ -14,7 +14,7 @@
from __future__ import annotations from __future__ import annotations
from time import sleep
import sys import sys
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
@@ -43,9 +43,9 @@ from mids import (
MID_TABLE_COV1, MID_TABLE_COV1,
) )
from masters.dimensions import * from src.bridge import *
from masters.bridge import * from src.fact import *
from kpi.facts import * from src.dim import *
# ========================================================== # ==========================================================
@@ -64,53 +64,6 @@ def table_exists(
) )
def get_dataframe(
fn_name: str,
fetch_by: str,
sql_engine,
mids,
run_date,
):
fn = globals()[fn_name]
if fetch_by == "mids":
return fn(sql_engine, mids)
if fetch_by == "run_date":
return fn(sql_engine, run_date)
return fn(sql_engine)
def get_empids_clickhouse_OQAD(
client,
table_name: str = "OQaD",
) -> pl.DataFrame:
if not table_exists(client, table_name):
log.warning(f"Table '{table_name}' does not exist.")
return pl.DataFrame(
schema={
"EmpId": pl.Int64,
"VisitDate": pl.Date,
}
)
query = f"""
SELECT DISTINCT
employee_id AS EmpId,
toDate(visit_date) AS VisitDate
FROM {table_name}
"""
# ClickHouse -> PyArrow -> Polars
arrow_table = client.query_arrow(query)
return pl.from_arrow(arrow_table)
# ========================================================== # ==========================================================
# Main # Main
# ========================================================== # ==========================================================
@@ -166,14 +119,8 @@ def main():
sql_engine, sql_engine,
run_date, run_date,
) )
qf=fetch_quiz_empids()
db_df = get_empids_clickhouse_OQAD(client)
matched = qf.join(
db_df,
on=["EmpId", "VisitDate"],
how="inner",
)
# ------------------------------------------------------ # ------------------------------------------------------
# Config # Config
# ------------------------------------------------------ # ------------------------------------------------------
@@ -188,19 +135,14 @@ def main():
# ------------------------------------------------------ # ------------------------------------------------------
# Process Tables # Process Tables
# ------------------------------------------------------ # ------------------------------------------------------
for table in config["tables"]: for table in config["tables"]:
table_name = table["name"] table_name = table["name"]
operation = table["operation"] operation = table["operation"]
fetch_by = table["fetch_by"] fetch_by = table["fetch_by"]
table_type=table["type"] table_type=table["type"]
log.info("=" * 80) log.info("=" * 80)
log.info( log.info(f"Processing Table: {table_name} | Table type is -: {table_type} | Based on -{fetch_by} and operation is used -{operation} " )
"Processing Table: %s",
table_name,
)
try: try:
@@ -208,15 +150,17 @@ def main():
# Fetch Data # Fetch Data
# ------------------------------------------ # ------------------------------------------
fn_name = f"fetch_{table_name}"
df = get_dataframe( log.info(f"Fetching Data from sql server for table-: {table_name} ..............")
fn_name=fn_name, fetch_list=["mids" ,"run_date", "reason_id"]
fetch_by=fetch_by, if fetch_by in fetch_list :
sql_engine=sql_engine, fn_name = f"fetch_{table_name}"
mids=mids, fn = globals()[fn_name]
run_date=run_date, df=fn(sql_engine, table_name , table_type, mids, run_date)
) else:
df = fetch_data(sql_engine ,table_name,table_type)
log.info(f"Fetched total row -: {len(df)} from sql server for table-:{table_name} ...........!!!")
if df.is_empty(): if df.is_empty():
@@ -267,6 +211,8 @@ def main():
table_name, table_name,
) )
elif operation =="ONLY_INSERT" :
continue
else: else:
delete_existing_data( delete_existing_data(
@@ -280,7 +226,7 @@ def main():
# ------------------------------------------ # ------------------------------------------
# Load Data # Load Data
# ------------------------------------------ # ------------------------------------------
log.info("_ _ _ _Inserting data into clickhouse db from sql server_ _ _ _")
load_to_clickhouse( load_to_clickhouse(
client=client, client=client,
table_name=table_name, table_name=table_name,
@@ -308,4 +254,42 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
for attempt in range(3):
try:
main() main()
with open("Pipeline_config.yml", "r") as f:
config = yaml.safe_load(f)
config["pipeline"]["last_successful_run_date"] = str(date.today())
with open("Pipeline_config.yml", "w") as f:
yaml.safe_dump(config, f, sort_keys=False)
log.info(
f"Pipeline completed successfully. "
f"last_successful_run_date={date.today()}"
)
break
except Exception as e:
with open("Pipeline_config.yml", "r") as f:
config = yaml.safe_load(f)
config["pipeline"]["run_date"] = str(date.today())
with open("Pipeline_config.yml", "w") as f:
yaml.safe_dump(config, f, sort_keys=False)
if attempt == 2:
raise
log.warning(
f"Pipeline failed. Retry {attempt + 1}/3. Error: {e}"
)
sleep(5)
-64
View File
@@ -1,64 +0,0 @@
import os
# import pyarrow
import sys
import logging
from datetime import date, timedelta
import polars as pl
from sqlalchemy import create_engine, text
from sqlalchemy.engine import Engine, URL
import clickhouse_connect
from dotenv import load_dotenv
from log import log
from clickhouse_task.create_table import *
from db_con.connection import *
from mids import *
def fetch_mapping_storevisibility(
engine: Engine,
run_date: date
) -> pl.DataFrame:
sql = f"""
SELECT DISTINCT
40148 AS project_id,
Z.StoreId AS store_id,
Z.VisibilityDefinitionId AS visibility_definition_id,
Z.FromDate AS from_date,
Z.ToDate AS to_date
FROM OneApp_KelloggsMT.dbo.Mapping_StoreVisibility Z
WHERE CAST(Z.FromDate AS DATE) <= '{run_date}'
AND CAST(Z.ToDate AS DATE) >= '{run_date}'
AND Z.VisibilityDefinitionId IN
(
SELECT DISTINCT VisibilityDefinitionId
FROM OneApp_KelloggsMT.dbo.Master_VisibilityDefinition
WHERE MenuId = 22
)
"""
log.info(
f"Fetching Mapping Store Visibility for {run_date}"
)
df = pl.read_database(
query=sql,
connection=engine
)
log.info(
f"Fetched {len(df):,} Mapping Store Visibility records"
)
return df
-515
View File
@@ -1,515 +0,0 @@
import os
# import pyarrow
import sys
import logging
from datetime import date, timedelta
import polars as pl
from sqlalchemy import create_engine, text
from sqlalchemy.engine import Engine, URL
import clickhouse_connect
from dotenv import load_dotenv
from log import log
from clickhouse_task.create_table import *
from db_con.connection import *
from mids import *
#PROJECT_ID = 40148
p=40148
def fetch_Store_Master(engine: Engine) -> pl.DataFrame:
sql = """
SELECT
RegionName AS region,
StateId AS state_id,
StateName AS state,
CityId AS city_id,
CityName AS city,
CityCode AS cpm_city_id,
ChannelId AS channel_id,
ChannelName AS channel,
DistributorId AS distributor_id,
Distributor AS distributor_name,
ChainId AS keyaccount_id,
ChainName AS keyaccount,
StoreUniqueCode AS insight_store_id,
StoreCode AS client_store_code,
Latitude AS latitude,
Longitude AS longitude,
StoreCategoryId AS store_category_id,
StoreCategory AS store_category,
StoreTypeId AS store_type_id,
StoreType AS store_type,
StoreClassId AS store_classification_id,
StoreClass AS store_classification,
StLayerFourId,
StoreId AS store_id,
StoreName AS store_name,
Address AS address
FROM OneApp_KelloggsMT.dbo.vw_storedetail
"""
log.info("Fetching Store Master data")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(
f"Fetched {len(df):,} stores"
)
return df
def fetch_SKU_Master(engine: Engine) -> pl.DataFrame:
sql = """
SELECT
CM.CategoryId AS category_id,
CM.CategoryCode AS category_code,
CM.CategoryName AS category_name,
SCA.SubCategoryId AS sub_category_id,
SCA.SubCategoryCode AS sub_category_code,
SCA.SubCategoryName AS sub_category_name,
BR.BrandId AS brand_id,
BR.BrandCode AS brand_code,
BR.BrandName AS brand_name,
SB.SubBrandId AS sub_brand_id,
SB.SubBrandCode AS sub_brand_code,
SB.SubBrandName AS sub_brand_name,
P.ProductId AS product_id,
P.ProductName AS product_name,
P.ProductCode AS product_code,
P.MRP AS mrp,
FL.FlavourId AS flavour_id,
FL.Flavour AS flavour,
P.Grammage AS grammage,
P.ProductSequence AS product_sequence,
P.CaseSize AS case_size,
MC.Company AS company_name,
MC.IsCompetitor AS is_competitor,
P.PTR AS ptr
FROM OneApp_KelloggsMT.dbo.Master_Product P
RIGHT JOIN OneApp_KelloggsMT.dbo.Master_Flavour FL
ON P.FlavourId = FL.FlavourId
RIGHT JOIN OneApp_KelloggsMT.dbo.Master_SubBrand SB
ON P.SubBrandId = SB.SubBrandId
RIGHT JOIN OneApp_KelloggsMT.dbo.Master_Brand BR
ON SB.BrandId = BR.BrandId
RIGHT JOIN OneApp_KelloggsMT.dbo.Master_SubCategory SCA
ON BR.SubCategoryId = SCA.SubCategoryId
RIGHT JOIN OneApp_KelloggsMT.dbo.Master_Category CM
ON SCA.CategoryId = CM.CategoryId
RIGHT JOIN OneApp_KelloggsMT.dbo.Master_Company MC
ON MC.CompanyId = BR.CompanyId
"""
log.info("Fetching SKU Master data")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(
f"Fetched {len(df):,} SKU Master rows"
)
return df
def fetch_display_master(engine: Engine) -> pl.DataFrame:
"""
Fetch Display Master data.
Source: Master_Display
Target: display_master
"""
sql = """
SELECT
DisplayId AS display_id,
DisplayCode AS display_code,
DisplayName AS display_name,
DisplayRefImage AS display_ref_url
FROM OneApp_KelloggsMT.dbo.Master_Display
"""
log.info("Fetching Display Master data")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(
f"Fetched {len(df):,} Display Master records"
)
return df
def fetch_Employee_Master(engine: Engine) -> pl.DataFrame:
"""
Fetch Employee Master data.
Source: vw_Employee_Detail + Mapping_PositionUser + Master_Position
Target: Employee_Master
"""
sql = """
SELECT
RegionId AS region_id,
RegionName AS region,
StateId AS state_id,
StateName AS state,
CityId AS city_id,
CityName AS city,
A.EmpId AS employee_id,
EmpName AS employee_name,
Gender AS gender,
A.DesignationId AS designation_id,
DesignationName AS designation,
SupervisorId AS manager_id,
SupervisorName AS manager_name,
JoinDate AS employee_joining_date,
ResignDate AS employee_resign_date,
C.PositionCode AS position_code,
EmpCode AS employee_legacy_code,
RIGHTNAME AS employee_role,
CASE
WHEN RIGHTNAME IN ('Client', 'Client HO')
THEN 'NON CPM'
ELSE 'CPM'
END AS employee_type
FROM OneApp_KelloggsMT.dbo.vw_Employee_Detail A
LEFT JOIN
(
SELECT DISTINCT
PositionId,
EmpId
FROM OneApp_KelloggsMT.dbo.Mapping_PositionUser
WHERE DATEDIFF(DAY, FromDate, GETDATE()) >= 0
AND DATEDIFF(DAY, ToDate, GETDATE()) <= 0
) B
ON A.EmpId = B.EmpId
LEFT JOIN
(
SELECT
PositionId,
PositionCode
FROM OneApp_KelloggsMT.dbo.Master_Position
) C
ON B.PositionId = C.PositionId
"""
log.info("Fetching Employee Master data")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(
f"Fetched {len(df):,} Employee Master records"
)
return df
def fetch_Employee_Master(engine: Engine) -> pl.DataFrame:
"""
Fetch Employee Master data.
Source: vw_Employee_Detail + Mapping_PositionUser + Master_Position
Target: Employee_Master
"""
sql = """
SELECT
RegionId AS region_id,
RegionName AS region,
StateId AS state_id,
StateName AS state,
CityId AS city_id,
CityName AS city,
A.EmpId AS employee_id,
EmpName AS employee_name,
Gender AS gender,
A.DesignationId AS designation_id,
DesignationName AS designation,
SupervisorId AS manager_id,
SupervisorName AS manager_name,
JoinDate AS employee_joining_date,
ResignDate AS employee_resign_date,
C.PositionCode AS position_code,
EmpCode AS employee_legacy_code,
RIGHTNAME AS employee_role,
CASE
WHEN RIGHTNAME IN ('Client', 'Client HO')
THEN 'NON CPM'
ELSE 'CPM'
END AS employee_type
FROM OneApp_KelloggsMT.dbo.vw_Employee_Detail A
LEFT JOIN
(
SELECT DISTINCT
PositionId,
EmpId
FROM OneApp_KelloggsMT.dbo.Mapping_PositionUser
WHERE DATEDIFF(DAY, FromDate, GETDATE()) >= 0
AND DATEDIFF(DAY, ToDate, GETDATE()) <= 0
) B
ON A.EmpId = B.EmpId
LEFT JOIN
(
SELECT
PositionId,
PositionCode
FROM OneApp_KelloggsMT.dbo.Master_Position
) C
ON B.PositionId = C.PositionId
"""
log.info("Fetching Employee Master data")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(
f"Fetched {len(df):,} Employee Master records"
)
return df
def fetch_Master_VisibilityReason(engine: Engine) -> pl.DataFrame:
"""
Source:
Master_VisibilityReason
Target:
Master_VisibilityReason
"""
sql = """
SELECT DISTINCT
40148 AS project_id,
MenuId AS menu_id,
VisibilityReasonId AS reason_id,
VisibilityReason AS reason
FROM OneApp_KelloggsMT.dbo.Master_VisibilityReason
"""
log.info("Fetching Master Visibility Reason data")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(
f"Fetched {len(df):,} Master Visibility Reason records"
)
return df
def fetch_Master_VisibilityDefinition(engine: Engine) -> pl.DataFrame:
"""
Source:
OneApp_KelloggsMT.dbo.Master_VisibilityDefinition
Target:
Master_VisibilityDefinition
"""
sql = """
SELECT DISTINCT
40148 AS project_id,
VisibilityDefinitionId AS visibility_definition_id,
VisibilityDefinitionName AS visibility_definition_name,
GETDATE() AS create_date,
'SP-Pius' AS create_by
FROM OneApp_KelloggsMT.dbo.Master_VisibilityDefinition
"""
log.info("Fetching Master Visibility Definition data")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(
f"Fetched {len(df):,} Master Visibility Definition records"
)
return df
def fetch_Master_Salesterritorylayer(
engine: Engine
) -> pl.DataFrame:
"""
Source:
Master_SalesTerritoryLayerOne
Master_SalesTerritoryLayerTwo
Master_SalesTerritoryLayerThree
Master_SalesTerritoryLayerFour
Target:
Master_Salesterritorylayer
"""
sql = """
SELECT DISTINCT
40148 AS project_id,
D.StLayerOneId AS st_layer_one_id,
D.StLayerOneName AS st_layer_one_name,
C.StLayerTwoId AS st_layer_two_id,
C.StLayerTwoName AS st_layer_two_name,
B.StLayerThreeId AS st_layer_three_id,
B.StLayerThreeName AS st_layer_three_name,
A.StLayerFourId AS st_layer_four_id,
A.StLayerFourName AS st_layer_four_name,
GETDATE() AS create_date,
'SP-Pius' AS create_by
FROM OneApp_KelloggsMT.dbo.Master_SalesTerritoryLayerFour A
INNER JOIN OneApp_KelloggsMT.dbo.Master_SalesTerritoryLayerThree B
ON A.StLayerThreeId = B.StLayerThreeId
INNER JOIN OneApp_KelloggsMT.dbo.Master_SalesTerritoryLayerTwo C
ON B.StLayerTwoId = C.StLayerTwoId
INNER JOIN OneApp_KelloggsMT.dbo.Master_SalesTerritoryLayerOne D
ON C.StLayerOneId = D.StLayerOneId
"""
log.info("Fetching Master Sales Territory Layer data")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(
f"Fetched {len(df):,} Master Sales Territory Layer records"
)
return df
def fetch_coverage_remarks(engine: Engine ) -> pl.DataFrame:
"""
Source:
OneApp_KelloggsMT.dbo.Master_NonWorkingReason
Target:
coverage_remarks
"""
sql = """
SELECT
40148 AS project_id,
ReasonId AS reason_id,
Reason AS reason_remarks
FROM OneApp_KelloggsMT.dbo.Master_NonWorkingReason
"""
log.info("Fetching Coverage Remarks")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(
f"Fetched {len(df):,} Coverage Remark records"
)
return df
-48
View File
@@ -72,51 +72,3 @@ def MID_TABLE_COV1(
def fetch_quiz_empids(engine: Engine, run_date : date) -> pl.DataFrame:
sql_template = f"""
WITH MID_TABLE_COV1 AS
(
SELECT EmpId, VisitDate
FROM OneApp_KelloggsMT.dbo.T_OQAD
WHERE CreateDate >= {run_date}
AND CreateDate < DATEADD(DAY,1,'{run_date}')
UNION ALL
SELECT EmpId, VisitDate
FROM OneApp_KelloggsMT.dbo.T_OQAD
WHERE UpdateDate >= {run_date}
AND UpdateDate < DATEADD(DAY,1, '{run_date}')
),
QUIZ AS
(
SELECT Distinct E.EmpId as empid
, CONVERT(VARCHAR,DQ.VisitDate) AS VisitDate
FROM OneApp_KelloggsMT.dbo.T_OQAD DQ INNER JOIN
OneApp_KelloggsMT.dbo.vw_Employee_Detail E ON DQ.EmpId = E.EmpId inner join
OneApp_KelloggsMT.dbo.Master_OQAD_Question QU on DQ.QuestionId= qu.QuestionId inner join
OneApp_KelloggsMT.dbo.Master_OQAD_Category qc on qu.QuestionCategoryId= qc.QuestionCategoryId
where e.EmpName not like 'test%' and e.RightId in (6)
and (E.ResignDate is null or E.ResignDate>=''+CONVERT(VARCHAR,'{run_date}')+'') AND E.EmpName NOT LIKE '%TEST%'
AND DQ.EmpId IN (SELECT EmpId FROM MID_TABLE_COV1 A WHERE
DQ.EmpId=A.EmpId AND CONVERT(date,VisitDate)=CONVERT(date,A.VisitDate) )
) select * from quiz
"""
sql = sql_template.format(
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching quiz_empids data for EMPID and Visitid")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} total empid and visitdate fetched for OQAD from SQL Server")
return df
+110
View File
@@ -0,0 +1,110 @@
from pathlib import Path
import polars as pl
from sqlalchemy import Engine
from datetime import date , timedelta
from log import log
from db_con.connection import (
build_sql_server_engine,
build_clickhouse_engine,
get_clickhouse_client,
)
def fetch_mapping_storevisibility(
sql_engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
run_date = run_date + timedelta(days=1)
client= get_clickhouse_client()
def table_exists(
client,
table_name: str,
) -> bool:
return bool(
client.command(
f"EXISTS TABLE {table_name}"
)
)
def get_reason_ids_mapping_storevisibility(
client,
run_date: date,
table_name: str = "mapping_storevisibility",
) -> list[int] :
if not table_exists(client, table_name):
log.warning(f"Table '{table_name}' does not exist. During collecting store_ids")
return [0]
query = f"""
SELECT DISTINCT StoreId
FROM mapping_storevisibility
WHERE toDate(Fromdate) <= toDate('{run_date + timedelta(days= 1)}')
AND toDate(Todate) >= toDate('{run_date + timedelta(days= 1)}')
AND project_Id = '40148'
"""
# ClickHouse -> PyArrow -> Polars
arrow_table = client.query_arrow(query)
df= pl.from_arrow(arrow_table)
list=df["reason_id"].to_list()
return list
def fetch_data(
engine: Engine,
table_name: str,
table_type: str,
run_date: date,
store_id: list[int]
) -> pl.DataFrame:
log.info(f"Fetching data from sql server for Master table......")
store_id_list = ",".join(str(sid) for sid in store_id)
sql_file = Path("src") / "sql" / f"bridge" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
store_id_list=store_id_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching in progress .... ")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
store_id=get_reason_ids_mapping_storevisibility(client, run_date, "mapping_storevisibility")
df=fetch_data(engine=sql_engine,
table_name=table_name,
table_type=table_type,
run_date=run_date,
store_id=store_id,
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
+138
View File
@@ -0,0 +1,138 @@
from pathlib import Path
import polars as pl
from sqlalchemy import Engine
from datetime import date , timedelta
from log import log
from db_con.connection import (
build_sql_server_engine,
build_clickhouse_engine,
get_clickhouse_client,
)
def fetch_data(
engine: Engine,
table_name: str,
table_type: str
) -> pl.DataFrame:
log.info(f"Fetching data from sql server for Master table......")
sql_file = Path("src") / "sql" / f"dim" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format( )
log.info(f"Fetching in progress .... ")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
def fetch_coverage_remarks(
sql_engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
client= get_clickhouse_client()
def table_exists(
client,
table_name: str,
) -> bool:
return bool(
client.command(
f"EXISTS TABLE {table_name}"
)
)
def get_reason_ids_coverage_remarks(
client,
table_name: str = "coverage_remarks",
) -> list[int] :
if not table_exists(client, table_name):
log.warning(f"Table '{table_name}' does not exist. During collecting reason_ids")
return [0]
query = f"""
SELECT DISTINCT
reason_id
FROM {table_name}
"""
# ClickHouse -> PyArrow -> Polars
arrow_table = client.query_arrow(query)
df= pl.from_arrow(arrow_table)
list=df["reason_id"].to_list()
return list
def fetch_data(
engine: Engine,
table_name: str,
table_type: str,
reason_ids: list[int]
) -> pl.DataFrame:
log.info(f"Fetching data from sql server for Master table......")
resaon_id_list = ",".join(str(rid) for rid in reason_ids)
sql_file = Path("src") / "sql" / f"dim" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
resaon_id_list=resaon_id_list
)
log.info(f"Fetching in progress .... ")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
reason_ids=get_reason_ids_coverage_remarks(client, "coverage_remarks")
df=fetch_data(engine=sql_engine,
table_name=table_name,
table_type=table_type,
reason_ids=reason_ids,
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
+684 -38
View File
@@ -1,11 +1,693 @@
from pathlib import Path from pathlib import Path
import polars as pl import polars as pl
from sqlalchemy import Engine from sqlalchemy import Engine
from datetime import date from datetime import date , timedelta
from log import log from log import log
from db_con.connection import (
build_sql_server_engine,
build_clickhouse_engine,
get_clickhouse_client,
)
# def fetch_data(
# engine: Engine,
# table_name: str,
# table_type: str,
# mids: list[int],
# run_date: date
# ) -> pl.DataFrame:
# if not mids:
# log.warning("No MIDs — nothing to fetch.")
# return pl.DataFrame()
# mid_list = ",".join(str(mid) for mid in mids)
# sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
# with open(sql_file, "r", encoding="utf-8") as f:
# sql_template = f.read()
# sql = sql_template.format(
# mid_list=mid_list,
# run_date=run_date.strftime("%Y-%m-%d")
# )
# log.info(f"Fetching data for {len(mids):,} MIDs")
# df = pl.read_database(
# query=sql,
# connection=engine
# )
# log.info(f"Fetched {len(df):,} rows from SQL Server")
# return df
def fetch_SOS_OneApp( engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
if not mids:
log.warning("No MIDs — nothing to fetch.")
return pl.DataFrame()
log.info(f" Start Fetching data for these {len(mids):} MIDs ")
mid_list = ",".join(str(mid) for mid in mids)
log.info(f"Start Fetching data for these {len(mids):} MIDs or based on this date {run_date}")
sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
mid_list=mid_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching data for {len(mids):,} MIDs")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
def fetch_additional_visibility( engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
if not mids:
log.warning("No MIDs — nothing to fetch.")
return pl.DataFrame()
log.info(f" Start Fetching data for these {len(mids):} MIDs ")
mid_list = ",".join(str(mid) for mid in mids)
log.info(f"Start Fetching data for these {len(mids):} MIDs or based on this date {run_date}")
sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
mid_list=mid_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching data for {len(mids):,} MIDs")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
def fetch_OQaD(
sql_engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
client= get_clickhouse_client()
def table_exists(
client,
table_name: str,
) -> bool:
return bool(
client.command(
f"EXISTS TABLE {table_name}"
)
)
def fetch_quiz_empids(engine: Engine, run_date : date) -> pl.DataFrame:
sql_template = f"""
WITH MID_TABLE_COV1 AS
(
SELECT EmpId, VisitDate
FROM OneApp_KelloggsMT.dbo.T_OQAD
WHERE CreateDate >= {run_date}
AND CreateDate < DATEADD(DAY,1,'{run_date}')
UNION ALL
SELECT EmpId, VisitDate
FROM OneApp_KelloggsMT.dbo.T_OQAD
WHERE UpdateDate >= {run_date}
AND UpdateDate < DATEADD(DAY,1, '{run_date}')
),
QUIZ AS
(
SELECT Distinct E.EmpId as empid
, CONVERT(date,DQ.VisitDate) AS visitdate
FROM OneApp_KelloggsMT.dbo.T_OQAD DQ INNER JOIN
OneApp_KelloggsMT.dbo.vw_Employee_Detail E ON DQ.EmpId = E.EmpId inner join
OneApp_KelloggsMT.dbo.Master_OQAD_Question QU on DQ.QuestionId= qu.QuestionId inner join
OneApp_KelloggsMT.dbo.Master_OQAD_Category qc on qu.QuestionCategoryId= qc.QuestionCategoryId
where e.EmpName not like 'test%' and e.RightId in (6)
and (E.ResignDate is null or E.ResignDate>=''+CONVERT(VARCHAR,'{run_date}')+'') AND E.EmpName NOT LIKE '%TEST%'
AND DQ.EmpId IN (SELECT EmpId FROM MID_TABLE_COV1 A WHERE
DQ.EmpId=A.EmpId AND CONVERT(date,VisitDate)=CONVERT(date,A.VisitDate) )
) select * from quiz
"""
sql = sql_template.format(
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching quiz_empids data for EMPID and Visitid")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} total empid and visitdate fetched for OQAD from SQL Server")
return df
def get_empids_clickhouse_OQAD(
client,
table_name: str = "OQaD",
) -> pl.DataFrame:
if not table_exists(client, table_name):
log.warning(f"Table '{table_name}' does not exist.")
return pl.DataFrame(
schema={
"empid": pl.Int64,
"visitdate": pl.Date,
}
)
query = f"""
SELECT DISTINCT
employee_id AS empid,
visit_date AS visitdate
FROM {table_name}
"""
# ClickHouse -> PyArrow -> Polars
arrow_table = client.query_arrow(query)
return pl.from_arrow(arrow_table)
qf=fetch_quiz_empids(sql_engine,run_date)
db_df = get_empids_clickhouse_OQAD(client)
matched = qf.join(
db_df,
on=["empid", "visitdate"],
how="inner",
)
if matched.is_empty():
empids=[0]
log.warning(
"%s Matched df in OQaD returned no rows",
table_name,
)
else:
empids=matched["empid"].to_list()
log.info(f"Fetched {len(empids):,} matched empids fetched for OQAD ")
def fetch_data( def fetch_data(
engine: Engine,
table_name: str,
table_type: str,
empids: list[int],
run_date: date
) -> pl.DataFrame:
empid_list = ",".join(str(empid) for empid in empids)
sql_file = Path("src") / "sql" / "fact" / f"{table_name}.sql"
log.info(f"Exists: {sql_file.exists()}")
log.info(f"Path: {sql_file.resolve()}")
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
empid_list=empid_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching data for {len(empids):,} EMPIDs")
log.info("Fetching OQaD data for run_date=%s", run_date)
df = pl.read_database(
query=sql,
connection=engine,
)
log.info("fn name is fetch_OQad ------Fetched %s rows", len(df))
return df
df=fetch_data( engine=sql_engine,
table_name=table_name,
table_type=table_type,
empids=empids,
run_date=run_date
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
# def fetch_OQaD(
# engine: Engine,
# table_name: str,
# table_type: str,
# empids: list[int],
# run_date: date
# ) -> pl.DataFrame:
# empid_list = ",".join(str(empid) for empid in empids)
# sql_file = Path("src") / "sql" / "fact" / f"{table_name}.sql"
# log.info(f"Exists: {sql_file.exists()}")
# log.info(f"Path: {sql_file.resolve()}")
# with open(sql_file, "r", encoding="utf-8") as f:
# sql_template = f.read()
# sql = sql_template.format(
# empid_list=empid_list,
# run_date=run_date.strftime("%Y-%m-%d")
# )
# log.info(f"Fetching data for {len(empids):,} EMPIDs")
# log.info("Fetching OQaD data for run_date=%s", run_date)
# df = pl.read_database(
# query=sql,
# connection=engine,
# )
# log.info("fn name is fetch_OQad ------Fetched %s rows", len(df))
# return df
def fetch_Coverage( engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
if not mids:
log.warning("No MIDs — nothing to fetch.")
return pl.DataFrame()
log.info(f"Start Fetching data for these {len(mids):} MIDs or based on this date {run_date}")
mid_list = ",".join(str(mid) for mid in mids)
sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
mid_list=mid_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching data for {len(mids):,} MIDs")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
def fetch_Survey( engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
if not mids:
log.warning("No MIDs — nothing to fetch.")
return pl.DataFrame()
mid_list = ",".join(str(mid) for mid in mids)
log.info(f"Start Fetching data for these {len(mids):} MIDs or based on this date {run_date}")
sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
mid_list=mid_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching data for {len(mids):,} MIDs")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
def fetch_Login( engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
# if not mids:
# log.warning("No MIDs — nothing to fetch.")
# return pl.DataFrame()
mid_list = ",".join(str(mid) for mid in mids)
log.info(f"Start Fetching data for these {len(mids):} MIDs or based on this date {run_date}")
sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
# mid_list=mid_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching data for {len(mids):,} MIDs")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
def fetch_Stock_Details( engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
if not mids:
log.warning("No MIDs — nothing to fetch.")
return pl.DataFrame()
mid_list = ",".join(str(mid) for mid in mids)
log.info(f"Start Fetching data for these {len(mids):} MIDs or based on this date {run_date}")
sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
mid_list=mid_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching data for {len(mids):,} MIDs")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
# def fetch_Coverage( engine: Engine,
# table_name: str,
# table_type: str,
# mids: list[int],
# run_date: date
# ) -> pl.DataFrame:
# if not mids:
# log.warning("No MIDs — nothing to fetch.")
# return pl.DataFrame()
# mid_list = ",".join(str(mid) for mid in mids)
# log.info(f"Start Fetching data for these {len(mids):} MIDs or based on this date {run_date}")
# sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
# with open(sql_file, "r", encoding="utf-8") as f:
# sql_template = f.read()
# sql = sql_template.format(
# mid_list=mid_list,
# run_date=run_date.strftime("%Y-%m-%d")
# )
# log.info(f"Fetching data for {len(mids):,} MIDs")
# df = pl.read_database(
# query=sql,
# connection=engine
# )
# log.info(f"Fetched {len(df):,} rows from SQL Server")
# return df
def fetch_Attendance(
engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date | None = None,
days_back: int = 15
) -> pl.DataFrame:
"""
Fetch attendance source data.
Default:
end_date = yesterday
start_date = yesterday - 15 days
"""
if run_date is None:
run_date = date.today() - timedelta(days=1)
start_date = run_date - timedelta(days=days_back)
sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
start_date=start_date.strftime("%Y-%m-%d"),
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(
f"Fetching Attendance data from {start_date} to {run_date}"
)
df = pl.read_database(
query=sql,
connection=engine
)
log.info(
f"Fetched {len(df):,} attendance rows "
f"for {df['employee_id'].n_unique():,} employees"
)
return df
def fetch_Journey_Plan( engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
if not mids:
log.warning("No MIDs — nothing to fetch.")
return pl.DataFrame()
log.info(f" Start Fetching data for these {len(mids):} MIDs ")
mid_list = ",".join(str(mid) for mid in mids)
log.info(f"Start Fetching data for these {len(mids):} MIDs or based on this date {run_date}")
sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
mid_list=mid_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching data for {len(mids):,} MIDs")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
def fetch_PaidVisibility( engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
if not mids:
log.warning("No MIDs — nothing to fetch.")
return pl.DataFrame()
log.info(f" Start Fetching data for these {len(mids):} MIDs ")
mid_list = ",".join(str(mid) for mid in mids)
log.info(f"Start Fetching data for these {len(mids):} MIDs or based on this date {run_date}")
sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
mid_list=mid_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching data for {len(mids):,} MIDs")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
def fetch_Web_Logins( engine: Engine,
table_name: str,
table_type: str,
mids: list[int],
run_date: date
) -> pl.DataFrame:
if not mids:
log.warning("No MIDs — nothing to fetch.")
return pl.DataFrame()
log.info(f" Start Fetching data for these {len(mids):} MIDs ")
mid_list = ",".join(str(mid) for mid in mids)
log.info(f"Start Fetching data for these {len(mids):} MIDs or based on this date {run_date}")
sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
mid_list=mid_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching data for {len(mids):,} MIDs")
df = pl.read_database(
query=sql,
connection=engine
)
log.info(f"Fetched {len(df):,} rows from SQL Server")
return df
def fetch_Promotion(
engine: Engine, engine: Engine,
table_name: str, table_name: str,
table_type: str, table_type: str,
@@ -19,7 +701,7 @@ def fetch_data(
mid_list = ",".join(str(mid) for mid in mids) mid_list = ",".join(str(mid) for mid in mids)
sql_file = Path("sql") / f"{table_type}" / f"{table_name}.sql" sql_file = Path("src") / "sql" / f"{table_type.lower()}" / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f: with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read() sql_template = f.read()
@@ -39,39 +721,3 @@ def fetch_data(
log.info(f"Fetched {len(df):,} rows from SQL Server") log.info(f"Fetched {len(df):,} rows from SQL Server")
return df return df
def fetch_OQaD(
engine: Engine,
table_name: str,
table_type: str,
empids: list[int],
run_date: date
) -> pl.DataFrame:
empid_list = ",".join(str(empid) for empid in empids)
sql_file = Path("sql") / table_type / f"{table_name}.sql"
with open(sql_file, "r", encoding="utf-8") as f:
sql_template = f.read()
sql = sql_template.format(
empid_list=empid_list,
run_date=run_date.strftime("%Y-%m-%d")
)
log.info(f"Fetching data for {len(empids):,} EMPIDs")
log.info("Fetching OQaD data for run_date=%s", run_date)
df = pl.read_database(
query=sql,
connection=engine,
)
log.info("Fetched %s rows", len(df))
return df
@@ -0,0 +1,11 @@
with mapping_storevisibility
(Project_Id,StoreId,VisibilityDefinitionid,Fromdate,Todate,CreateDate,CreateBy)
AS (
select DISTINCT '40148' as Project_Id,StoreId,VisibilityDefinitionid,Fromdate,Todate,getdate(),'SP-Pius'
FROM OneApp_KelloggsMT.dbo.mapping_storevisibility z WHERE
convert(date,FROMDATE,101)<=convert(Date,getdate(),101) AND CONVERT(DATE,ToDate,101)>=convert(Date,getdate(),101)
AND z.VisibilityDefinitionid IN
(SELECT DISTINCT VisibilityDefinitionid FROM OneApp_KelloggsMT.dbo.MASTER_VISIBILITYDEFINITION WHERE MENUID=22 )
AND z.StoreId NOT IN ({store_id_list})
)
select * from mapping_storevisibility
+17
View File
@@ -0,0 +1,17 @@
with Employee_Master (project_id,region_id,region,state_id,state,city_id,
city,employee_id,employee_name,gender,designation_id,designation,manager_id,
manager_name,employee_joining_date,employee_resign_date,position_code,employee_legacy_code,employee_role,EMPLOYEE_TYPE)
as (
select '40148' as ProjectId, RegionId,
RegionName,StateId,StateName, CityId,CityName,
a.EmpId,EmpName,Gender,a.DesignationId,DesignationName,SupervisorId,SupervisorName,JoinDate,ResignDate,c.PositionCode ,EmpCode ,RIGHTNAME,
case when RIGHTNAME in ('Client','Client HO') Then 'NON CPM' else 'CPM' END AS EMPLOYEE_TYPE
from OneApp_KelloggsMT.dbo.vw_Employee_Detail a Left join (Select distinct PositionId, EmpId from OneApp_KelloggsMT.dbo.Mapping_PositionUser b
Where DATEdiff(d,FromDate, GETDATE())>= 0 and DATEDIFF(d,ToDate,getdate())<=0) b on a.EmpId=b.EmpId
LEFT join (select PositionId,PositionCode from OneApp_KelloggsMT.dbo.Master_Position) c on b.PositionId=c.PositionId
where 1=1
-- and EmpId not in (
--select employee_id from [dbo].[Employee_Master] where project_id='40148')
)
select * from Employee_Master
@@ -0,0 +1,14 @@
with Master_Salesterritorylayer(ProjectId,StLayerOneId,StLayerOneName,StLayerTwoId,StLayerTwoName,StLayerThreeId,StLayerThreeName,StLayerFourId,
StLayerFourName,CreateDate,CreateBy)
as (
select DISTINCT '40148' as Project_Id,d.StLayerOneId,d.StLayerOneName,c.StLayerTwoId,c.StLayerTwoName ,
b.StLayerThreeId,b.StLayerThreeName,a.StLayerFourId,a.StLayerFourName ,
getdate(),'SP-Pius'
FROM OneApp_KelloggsMT.dbo.[Master_SalesTerritoryLayerFour] A
INNER JOIN OneApp_KelloggsMT.dbo.[Master_SalesTerritoryLayerThree]B ON
A.StLayerThreeId=B.StLayerThreeId
INNER JOIN OneApp_KelloggsMT.dbo.[Master_SalesTerritoryLayerTWo]C ON
B.StLayerTwoId=C.StLayerTwoId
INNER JOIN OneApp_KelloggsMT.dbo.[Master_SalesTerritoryLayerOne]D ON
C.StLayerOneId=D.StLayerOneId)
select * from Master_Salesterritorylayer
@@ -0,0 +1,5 @@
with Master_VisibilityDefinition(Project_Id,VisibilityDefinitionId,VisibilityDefinitionName,CreateDate,CreateBy)
as (
select DISTINCT '40148' as Project_Id,VisibilityDefinitionid,VisibilityDefinitionname,getdate(),'SP-Pius'
FROM OneApp_KelloggsMT.dbo.Master_VisibilityDefinition)
select * from Master_VisibilityDefinition
+7
View File
@@ -0,0 +1,7 @@
with Master_VisibilityReason (ProjectId,MenuId,ReasonId,Reason,CreateDate,Createby)
as (
select DISTINCT '40148' as ProjectId,MenuId,VisibilityReasonId,VisibilityReason,getdate(),'SP-Pius'
FROM OneApp_KelloggsMT.dbo.Master_VisibilityReason
)
select * from Master_VisibilityReason
+18
View File
@@ -0,0 +1,18 @@
with SKU_Master (project_id,category_id,category_code,Category_name,sub_category_id,sub_category_code,sub_category_name,brand_id,
brand_code,brand_name,sub_brand_id,sub_brand_code,sub_brand_name,product_id,product_name,product_code,mrp,flavour_id,
flavour,grammage,product_sequence,case_size,company_name,is_competitor,ptr,update_date,update_by)
as (
Select '40148' as ProjectId, cm.CategoryId,
cm.CategoryCode,cm.CategoryName,sca.SubCategoryId,sca.SubCategoryCode,sca.SubCategoryName,br.BrandId,br.BrandCode,br.BrandName
,sb.SubBrandId,sb.SubBrandCode,sb.SubBrandName,p.ProductId,p.ProductName,p.ProductCode,p.Mrp,fl.FlavourId,
fl.Flavour,p.Grammage,p.ProductSequence,p.CaseSize,mc.Company,mc.IsCompetitor,p.ptr,GETDATE(),'MB'
from OneApp_KelloggsMT.dbo.Master_Product p Right join
OneApp_KelloggsMT.dbo.Master_Flavour fl on p.FlavourId= Fl.FlavourId Right join
OneApp_KelloggsMT.dbo.Master_subbrand sb on p.SubBrandId= sb.SubBrandId Right join
OneApp_KelloggsMT.dbo.Master_Brand br on sb.BrandId = br.BrandId Right join
OneApp_KelloggsMT.dbo.Master_SubCategory sca on br.SubCategoryId= sca.SubCategoryId Right join
OneApp_KelloggsMT.dbo.Master_Category cm on sca.CategoryId= cm.CategoryId Right join
OneApp_KelloggsMT.dbo.Master_Company mc on mc.companyId=br.companyId )
select * from SKU_Master
+14
View File
@@ -0,0 +1,14 @@
with Store_Master (project_id,
region_id,region,state_id,state,city_id,city,cpm_city_id,channel_id,channel,distributor_id,distributor_name, keyaccount_id, keyaccount,
insight_store_id,client_store_code,latitude,longitude,store_category_id,store_category,store_type_id,store_type,store_classification_id,store_classification, StLayerFourId,
store_id,store_name,address)
AS (
select '40148',RegionId,RegionName,StateId,StateName,CityId,
CityName,CityCode,ChannelId,ChannelName,DistributorId,Distributor,ChainId,ChainName,StoreUniqueCode,StoreCode,Latitude,
Longitude, StoreCategoryId,StoreCategory,StoreTypeId,
StoreType,StoreClassId,StoreClass,StLayerFourId,StoreId,
StoreName,Address from OneApp_KelloggsMT.dbo.vw_storedetail where 1=1
--and storeid not in (
--select store_id from Store_Master where project_id='40148')
)
select * from Store_Master
+8
View File
@@ -0,0 +1,8 @@
with coverage_remarks
(project_id,reason_id,reason_remarks)
as(
select
'40148' ,reasonid,reason from OneApp_KelloggsMT.dbo.master_nonworkingreason
where reasonid not in ( {resaon_id_list})
)
select * from coverage_remarks
+8
View File
@@ -0,0 +1,8 @@
with display_master (project_id,display_id,display_code,display_name,display_ref_url,created_date,
created_by) as (
select '40148',displayid,displaycode,displayname,displayrefImage,getdate(),'Pius'
from OneApp_KelloggsMT.dbo.Master_display
-- and ProductId not in (
--select product_id from [dbo].[SKU Master] where project_id='40148')
)
select * from display_master
+134
View File
@@ -0,0 +1,134 @@
with Emp_cte as
(Select T1.*, ISNULL(ps.PositionCode ,'') PositionCode,FromDate,ToDate from(
select a.*,b.date from
(Select rm.RegionId, rm.RegionName as Region, st.StateId, st.StateName , cm.CityId, cm.CityName
, em2.Id as [Area Manager Id], em2.EmployeeName AS [Area Manager]
, em1.Id as [Supervisor Id], isnull(em1.EmployeeName,'') as [Supervisor Name]
, em.Id as [Employee Id], isnull(em.EmployeeName,'') as [Employee Name]
, dm.DesignationId, dm.DesignationName as Designation
, CAST(EM.ResignDate AS DATE) as DOR, CAST(EM.JoinDate AS DATE) as DOJ, em.LegacyCode
--,ISNULL(ps.PositionCode ,'') PositionCode--, FromDate, ToDate
from
OneApp_KelloggsMT.dbo.AspNetUsers em inner join
OneApp_KelloggsMT.dbo.AspNetUsers em1 on em1.Id = em.ManagerId inner join
OneApp_KelloggsMT.dbo.AspNetUsers em2 on em2.Id = em1.ManagerId inner join
OneApp_KelloggsMT.dbo.Master_City cm on em.CityId= cm.CityId inner join
OneApp_KelloggsMT.dbo.Master_State st on cm.StateId= st.StateId inner join
OneApp_KelloggsMT.dbo.Master_Region rm on st.RegionId= rm.RegionId inner join
OneApp_KelloggsMT.dbo.Master_Designation dm on Em.DesignationId= dm.DesignationId
where em.RightId in (6) and em.EmployeeName not like '%test%' and
(Em.ResignDate is null or Em.ResignDate>=CAST('{start_date}' AS DATE)) and CONVERT(date,em.JoinDate,101)<=CONVERT(date,CAST('{run_date}' AS DATE),101)
--and em.Id=2290
)a
cross join (select distinct DATE from DBO.GET_ALL_DAYS(CAST('{start_date}' AS DATE),CAST('{run_date}' AS DATE))) b
) AS T1 LEFT JOIN
(Select distinct EmpId, PositionId, Date,FromDate,ToDate from
(Select distinct EmpId,PositionId,FromDate,ToDate from OneApp_KelloggsMT.dbo.Mapping_PositionUser
Where convert(Date,FromDate)<= CONVERT(date,CAST('{run_date}' AS DATE),101) and convert(Date,ToDate)>=CONVERT(date,CAST('{start_date}' AS DATE),101)) as m Inner Join
(Select Date from OneApp_KelloggsMT.dbo.Master_Calender Where 1=1 and Date between CONVERT(date,CAST('{start_date}' AS DATE),101) and CONVERT(date,CAST('{run_date}' AS DATE),101))
cal on cal.Date >= m.FromDate AND cal.Date <= m.ToDate) pu
on T1.[Employee Id]=pu.EmpId and T1.date= pu.date Left Join
OneApp_KelloggsMT.dbo.Master_Position ps WITH (NOLOCK) ON pu.PositionId=ps.PositionId
) ,
Attendance
(project_id,
employee_id,position_code,legacy_code,supervisor_id,date_of_join,date_of_resign,visit_date,parinaam_attendance,
update_date,update_by,Unique_Id)
AS (
select '40148', emp.[Employee Id], Case When emp.DATE <= ToDate and emp.DATE>= FromDate
then emp.PositionCode Else '' End as PositionCode ,emp.LegacyCode, emp.[Supervisor Id] ,emp.DOJ,emp.DOR,
emp.DATE, case when emp.DATE>=emp.DOR then 'R' WHEN emp.DATE< emp.DOJ THEN '-'
WHEN b.COL IS NULL AND (emp.DATE<=emp.DOR OR emp.DOR IS NULL )AND (emp.DATE>=emp.DOJ OR emp.DOJ IS NULL) THEN 'W' ELSE b.COL
END AS Attendance,GETDATE(),'SP-Pius',CAST('40148' AS VARCHAR) + '_' + CAST(emp.[Employee Id] AS VARCHAR)
from Emp_cte as emp
left join
(select a.*
FROM
(
SELECT A.DATE,TT.[MERCHANDISER CD],TT.VisitDate,TT.DOJ1,TT.DOR1,TT.COL from DBO.GET_ALL_DAYS(CAST('{start_date}' AS DATE),CAST('{run_date}' AS DATE)) a
left join
(
SELECT distinct [EMPLOYEE ID] AS [MERCHANDISER CD], VISITDATE
, CASE WHEN COL=1 THEN 'H' WHEN COL=2 THEN 'L' WHEN COL=4 THEN 'M' WHEN COL=5 THEN 'CO' WHEN COL=6 THEN 'WO'
WHEN COL=7 THEN 'P' WHEN COL=10 THEN 'EH' WHEN COL=8 THEN 'HD' WHEN COL IS NULL THEN 'A' END AS COL
, DOJ AS DOJ1, DOR AS DOR1
FROM
(
SELECT DOJ, DOR, Region, [AREA MANAGER], SUPERVISOR_CD, [SUPERVISOR], City, [EMPLOYEE ID], MERCHANDISER, VISITDATE
, REMARKS, COL, ROW_NUMBER()OVER (PARTITION BY [EMPLOYEE ID], VISITDATE ORDER BY COL DESC,Intime, VISITDATE, ROW ) AS COL1
FROM
(
SELECT DOJ, DOR, Region, [AREA MANAGER], SUPERVISOR_CD, [SUPERVISOR], City, [EMPLOYEE ID], MERCHANDISER, InTime, VISITDATE
, REMARKS, COL, ROW_NUMBER()OVER (PARTITION BY [EMPLOYEE ID], VISITDATE ORDER BY COL) AS ROW
FROM
(
SELECT RM.RegionName as Region, em2.EmployeeName AS [Area Manager]
, em1.Id as Supervisor_cd, em1.EmployeeName AS [Supervisor]
, CM.CityName as City, EM.Id as [Employee Id], em.EmployeeName AS Merchandiser
,JP.VisitDate as VisitDate, sc.InTime
,ISNULL((SELECT top 1 case
when d.ReasonId IS NULL OR d.ReasonId =0 then '' else REASON+' - ' +
case when replace(replace(D.REMARK,CHAR(13),''),CHAR(10),' ')<>'' then replace(replace(D.REMARK,CHAR(13),''),CHAR(10),' ')
else replace(replace(D.REMARK,CHAR(13),''),CHAR(10),' ') end end as REASON FROM OneApp_KelloggsMT.dbo.T_StoreCoverage D
INNER JOIN OneApp_KelloggsMT.dbo.Master_NonWorkingReason N ON N.ReasonId = D.ReasonId
WHERE D.ISDEL = 0
AND D.EmpId = JP.EmpId AND D.StoreId = JP.StoreId AND d.VisitDate = JP.VisitDate),'') AS Remarks
, CASE WHEN (Select Top 1 EmpId FROM OneApp_KelloggsMT.dbo.T_StoreCoverage SC WHERE JP.EmpId = SC.EmpId AND JP.StoreId = SC.StoreId AND JP.VisitDate = SC.VisitDate
AND SC.ReasonId=11) >=1 THEN 1
ELSE CASE WHEN(Select Top 1 EmpId FROM OneApp_KelloggsMT.dbo.T_StoreCoverage SC WHERE JP.EmpId= SC.EmpId AND JP.StoreId= SC.StoreId AND JP.VisitDate= SC.VisitDate
AND SC.ReasonId=2) >=1 THEN 2
ELSE CASE WHEN(Select Top 1 EmpId FROM OneApp_KelloggsMT.dbo.T_StoreCoverage SC WHERE JP.EmpId= SC.EmpId AND JP.StoreId= SC.StoreId AND JP.VisitDate= SC.VisitDate
AND SC.ReasonId=6) >=1 THEN 4
ELSE CASE WHEN(Select Top 1 EmpId FROM OneApp_KelloggsMT.dbo.T_StoreCoverage SC WHERE JP.EmpId= SC.EmpId AND JP.StoreId= SC.StoreId AND JP.VisitDate= SC.VisitDate
AND SC.ReasonId=12) >=1 THEN 5
ELSE CASE WHEN(Select Top 1 EmpId FROM OneApp_KelloggsMT.dbo.T_StoreCoverage SC WHERE JP.EmpId= SC.EmpId AND JP.StoreId= SC.StoreId AND JP.VisitDate= SC.VisitDate
AND SC.ReasonId=13) >=1 THEN 6
ELSE CASE WHEN(Select Top 1 EmpId FROM OneApp_KelloggsMT.dbo.T_StoreCoverage SC WHERE JP.EmpId= SC.EmpId AND JP.StoreId= SC.StoreId AND JP.VisitDate= SC.VisitDate
AND SC.ReasonId=7) >=1 THEN 8
ELSE CASE WHEN(Select Top 1 EmpId FROM OneApp_KelloggsMT.dbo.T_StoreCoverage SC WHERE JP.EmpId= SC.EmpId AND JP.StoreId= SC.StoreId AND JP.VisitDate= SC.VisitDate
AND SC.ReasonId=26) >=1 THEN 10
ELSE CASE WHEN(Select Top 1 EmpId FROM OneApp_KelloggsMT.dbo.T_StoreCoverage SC WHERE JP.EmpId= SC.EmpId AND JP.StoreId= SC.StoreId AND JP.VisitDate= SC.VisitDate
AND SC.ReasonId IN(0,1,3,4,5,8,9,10,14,15,17,18,19,20,21,22,24)) >=1 THEN 7
END END END END END END END END AS COL, em.ResignDate as DOR, em.JoinDate as DOJ
FROM OneApp_KelloggsMT.dbo.Mapping_JourneyPlan JP LEFT OUTER JOIN
OneApp_KelloggsMT.dbo.T_StoreCoverage SC ON JP.StoreId= SC.StoreId AND JP.EmpId= SC.EmpId AND JP.VisitDate= SC.VisitDate INNER JOIN
OneApp_KelloggsMT.dbo.Master_Store SM ON JP.StoreId= SM.StoreId INNER JOIN
OneApp_KelloggsMT.dbo.AspNetUsers em ON JP.EmpId= em.Id inner join
OneApp_KelloggsMT.dbo.AspNetUsers em1 on em1.Id = em.ManagerId inner join
OneApp_KelloggsMT.dbo.AspNetUsers em2 on em2.Id = em1.ManagerId inner join
OneApp_KelloggsMT.dbo.Master_City cm on em.CityId= cm.CityId inner join
OneApp_KelloggsMT.dbo.Master_State st on cm.StateId= st.StateId inner join
OneApp_KelloggsMT.dbo.Master_Region rm on st.RegionId= rm.RegionId --Left Join
WHERE EM.EMPLOYEENAME not like 'teststore%' AND JP.VisitDate BETWEEN CAST('{start_date}' AS DATE) AND CAST('{run_date}' AS DATE)
) AS T1
) AS T2
) AS T
WHERE COL1=1
) AS TT
on a.date=tt.visitdate)A)b
on emp.[Employee Id]=b.[MERCHANDISER CD] and (
emp.DOR IS NULL
OR emp.DATE >= emp.DOR
) )
select * from Attendance where
parinaam_attendance !='-' and
project_id=40148
AND visit_date > date_of_join
order by employee_id, visit_date
+101 -157
View File
@@ -1,164 +1,108 @@
WITH SOS_BASE AS
(
SELECT
sm.CountryName,
sc.MID,
sm.RegionName,
sm.StateName,
sm.CityName,
Em.SupervisorName,
em.EmpId,
Em.EmpName AS EmployeeName,
Em.DesignationName AS Designation,
sm.StoreId,
CONVERT(varchar, sc.VisitDate, 101) AS VisitDate,
sm.StoreCode,
sm.StoreName,
sm.Address,
sm.StoreTypeid,
sm.ChannelId,
sm.ChainName,
MSD.SOSDefinitionName, with Executed as
(select JP.mid, CASE WHEN isnull(B.MID,'')<>'' then 'Y'
ELSE 'N' END AS COVERED,
CASE WHEN isnull(C.MID,'')<>'' then 'Y'
ELSE 'N' END AS EXECUTED,isnull(NR.REASON,'') AS REASON
CASE from OneApp_KelloggsMT.dbo.T_StoreCoverage JP with (nolock) left join
WHEN ISNULL(TS.SOSHeaderTable,'')='Master_Category' THEN 'Category' (SELECT jp.mid FROM OneApp_KelloggsMT.dbo.T_StoreCoverage jp with (nolock)
WHEN ISNULL(TS.SOSHeaderTable,'')='Master_SubCategory' THEN 'SubCategory' inner join OneApp_KelloggsMT.dbo.Mapping_JourneyPlan B
WHEN ISNULL(TS.SOSHeaderTable,'')='Master_Brand' THEN 'Brand' ON JP.STOREID=B.STOREID AND JP.EMPID=B.EMPID AND CONVERT(VARCHAR,JP.VISITDATE,101)=CONVERT(VARCHAR,B.VISITDATE,101)
WHEN ISNULL(TS.SOSHeaderTable,'')='Master_SubBrand' THEN 'SubBrand' AND JP.ReasonId in (0,1,3,9,10,19,20))b
END AS SOSHeaderDeatils, on JP.MID=b.MID
TS.SOSHeaderName, left join
TS.SOSHeaderValue AS SOSHeaderID, (SELECT jp.mid FROM OneApp_KelloggsMT.dbo.T_StoreCoverage jp with (nolock)
inner join OneApp_KelloggsMT.dbo.Mapping_JourneyPlan B
ON JP.STOREID=B.STOREID AND JP.EMPID=B.EMPID AND CONVERT(VARCHAR,JP.VISITDATE,101)=CONVERT(VARCHAR,B.VISITDATE,101)
AND JP.ReasonId in (0,19))c
on JP.MID=c.MID
left join
OneApp_KelloggsMT.dbo.Master_NonWorkingReason nr on JP.ReasonId=nr.ReasonId
Where 1=1 AND JP.MID in ({mid_list})
'Header_Image' AS HDR1,
CASE
WHEN ISNULL(TSC.SOSChildTable,'')='Master_Category' THEN 'Category'
WHEN ISNULL(TSC.SOSChildTable,'')='Master_SubCategory' THEN 'SubCategory'
WHEN ISNULL(TSC.SOSChildTable,'')='Master_Brand' THEN 'Brand'
WHEN ISNULL(TSC.SOSChildTable,'')='Master_SubBrand' THEN 'SubBrand'
END AS SOSChildDeatils,
TSC.SOSChildName,
TSC.SOSChildValue AS SOSChildID,
TSC.ChildTotalFacing,
TS.SOSHeaderFacing,
TSC.ChildSelfFacing,
(
SELECT TOP 1 SOSTarget
FROM OneApp_KelloggsMT.dbo.Mapping_StoreShareOfShelfTarget a
WHERE a.SOSDefinitionId = MSD.SOSDefinitionId
AND a.StoreId = sm.StoreId
AND a.FromDate <= sc.VisitDate
AND a.ToDate >= sc.VisitDate
) AS SOSTarget,
CASE
WHEN ISNULL(SHI.SOSHeaderImage,'') = ''
THEN ''
ELSE
'https://kimt1.parinaam.in/Upload/SOSImages/'
+ SHI.SOSHeaderImage
END AS SOSHeaderImg
FROM OneApp_KelloggsMT.dbo.T_ShareOfShelfHeader ts
INNER JOIN OneApp_KelloggsMT.dbo.T_StoreCoverage sc
ON ts.MID = sc.MID
INNER JOIN OneApp_KelloggsMT.dbo.vw_StoreDetail sm
ON sc.StoreId = sm.StoreId
INNER JOIN OneApp_KelloggsMT.dbo.vw_Employee_Detail Em
ON sc.EmpId = Em.EmpId
INNER JOIN OneApp_KelloggsMT.dbo.T_ShareOfShelfChild tsc
ON ts.SOSId = tsc.SOSId
INNER JOIN OneApp_KelloggsMT.dbo.Master_ShareOfShelfDefinition msd
ON msd.SOSDefinitionId = tsc.SOSDefinitionId
LEFT JOIN OneApp_KelloggsMT.dbo.T_ShareOfShelfHeaderImages SHI
ON ts.SOSId = SHI.SOSId
AND ts.SOSHeaderValue = SHI.SOSHeaderValue
LEFT JOIN OneApp_KelloggsMT.dbo.T_ShareOfShelfChildImages SCI
ON tsc.SOSId = SCI.SOSId
AND tsc.SOSChildValue = SCI.SOSChildValue
WHERE Em.EmpName NOT LIKE 'test%'
AND sc.MID IN ({mid_list})
) , ) ,
Coverage
(project_id,Mid,
store_id,employee_id,visit_date,in_time,out_time,duration_in_minutes,
is_covered,is_executed,reason_remarks,storetype_id,
supervisor_id,coverage_type,distance_in_meters,reasonId,camera_allow,update_date,
update_by,Unique_Id)
AS (
Select '40148' as ProjectId,JP.MID, sm.StoreId,JP.EmpId as EmpId,
Convert(VARCHAR,Jp.VisitDate) AS VisitDate
, Isnull((Select Top 1 Case When Convert(Varchar(15),Convert(Time,
Convert(Datetime,InTime,101)),100) = 'Null' Then '' Else Convert(Varchar(15),Convert(Time,
Convert(Datetime,InTime,101)),100)End From OneApp_KelloggsMT.dbo.T_StoreCoverage Where EmpId = Jp.EmpId And StoreId = Jp.StoreId
And VisitDate = Jp.VisitDate),'') As [In Time]
, Isnull((Select Top 1 Case When Convert(Varchar(15),Convert(Time,
Convert(Datetime,OutTime,101)),100) = 'Null' Then '' Else Convert(Varchar(15),Convert(Time,
Convert(Datetime,OutTime,101)),100)End From OneApp_KelloggsMT.dbo.T_StoreCoverage
Where EmpId = Jp.EmpId And StoreId = Jp.StoreId
And Convert(Date,VisitDate) = Jp.VisitDate),'') As [Out Time]
,
Isnull((Select Top 1 Case When convert(varchar,outtime) like '%00:00:00.00%' THEN 0
Else Case When outtime<InTime THEN 0 ELSE DATEDIFF(SS,CONVERT(DATETIME, InTime)
, CONVERT(DATETIME, outtime))/60 END END From
OneApp_KelloggsMT.dbo.T_StoreCoverage sc
Where sc.EmpId = Jp.EmpId And sc.StoreId = Jp.StoreId
And Convert(Date,sc.visitdate,101) = convert(date,Jp.VisitDate,101)), '') As duration_in_minute,
-- Isnull((Select Top 1 case when min(convert(varchar,InTime,108))='00:00:00' OR max(convert(varchar,CheckOutTime,108))='00:00:00'
-- then '00:00:00' else Cast(Datediff(Ss,Min(Convert(Varchar,Convert(Datetime,InTime),108)),
-- Max(Convert(Varchar,Convert(Datetime, CheckOutTime),108)) ) /3600 As Varchar(2))+':'+ Cast((Datediff(Ss,Min(Convert(Varchar,Convert(Datetime,InTime),108))
-- ,Max(Convert(Varchar,Convert(Datetime, CheckOutTime),108)) ) /60 )%60 As Varchar(2)) +':'+ Cast(Datediff(Ss,Min(Convert(Varchar,Convert(Datetime,InTime),108)),
-- Max(Convert(Varchar,Convert(Datetime, CheckOutTime),108)) ) %60 As Varchar(2)) end as time_taken From OneApp_KelloggsMT.dbo.T_StoreCoverage sc left join
--OneApp_KelloggsMT.dbo.T_StoreCheckOut UD ON UD.EmpId = sc.EmpId AND CONVERT(VARCHAR,UD.CheckOutDate,101) = sc.VisitDate AND sc.StoreId = UD.StoreId
-- Where sc.EmpId = Jp.EmpId And sc.StoreId = Jp.StoreId
-- And VisitDate = Jp.VisitDate),'') As [Time Taken]
Case When (Select Top 1 EmpId From OneApp_KelloggsMT.dbo.T_StoreCoverage Sc LEFT join
OneApp_KelloggsMT.dbo.Master_NonWorkingReason nw on Sc.ReasonId= nw.ReasonId
Where EmpId = Jp.EmpId And StoreId = Jp.StoreId And VisitDate = Jp.VisitDate
And (sc.ReasonId =0 Or nw.ForCoverage=1) ) >0 Then 'Y'
Else 'N' End As Covered
, isnull(Exe.Executed,'') as Executed
,Isnull((Select Top 1 Case
When D.ReasonId Is Null Or D.ReasonId =0 Then '' Else Reason+' - ' +
Case When Replace(Replace(D.Remark,Char(13),''''),Char(10),' ')<>'' Then Replace(Replace(D.Remark,Char(13),''),Char(10),' ')
Else Replace(Replace(D.Remark,Char(13),''),Char(10),' ') End End As Reason
From OneApp_KelloggsMT.dbo.T_StoreCoverage D
Inner Join OneApp_KelloggsMT.dbo.Master_NonWorkingReason N On N.ReasonId = D.ReasonId
Where D.Isdel = 0
And D.EmpId = Jp.EmpId And D.StoreId = Jp.StoreId And D.VisitDate = Jp.VisitDate),'''') As [Detailed Remarks],
sm.StoreTypeId,Em.SupervisorId,
Case When jp.Deviation=0 Then 'Planned' When jp.Deviation=1 Then 'Adhoc'
When jp.Deviation=2 Then 'Beat Plan' When jp.Deviation=3 Then 'Non Merchandised'
When jp.Deviation=4 Then 'Add New Store' When jp.Deviation=5 Then 'Non Program' else '' End as [PJP Status]
,Isnull((Select Top 1 Case When (Sc.Latitude=0.00000000 Or Sc.Longitude Is Null) Then 0 Else Case When (sm.Latitude=0.00000000 Or sm.Latitude Is Null) Then 0
Else Case When (sm.Longitude=0.00000000 Or sm.Longitude Is Null) Then 0
Else SQRT(POWER(69.1 * ( Sc.Latitude - Sm.Latitude),2) + POWER(69.1 * ( Sm.Longitude - Sc.Longitude ) * COS(Sc.Latitude / 57.3), 2))*1000
End End End As [Distance In Mtr] From OneApp_KelloggsMT.dbo.T_StoreCoverage Sc Where Sc.Isdel=0 And Sc.EmpId= Jp.EmpId
And Sc.StoreId= Jp.StoreId And Sc.VisitDate= Jp.VisitDate),'') As [Distance In Mtr]
,ISNULL(CAST (JP.ReasonId AS VARCHAR),'0'),sm.CameraAllow,
GETDATE(),'SP-Pius' ,
CAST('40148' AS VARCHAR) + '_' + CAST(SM.storeid AS VARCHAR)
+ '_' + CAST(EM.EMPID AS VARCHAR)
FROM OneApp_KelloggsMT.dbo.T_StoreCoverage JP with (nolock) Inner Join
Executed Exe on Jp.MID= Exe.MID Inner Join
OneApp_KelloggsMT.dbo.vw_StoreDetail sm on Jp.StoreId= sm.StoreId Inner Join
OneApp_KelloggsMT.dbo.vw_Employee_Detail Em on JP.EmpId= Em.EmpId
left join
OneApp_KelloggsMT.dbo.T_StoreCoveragePositionPivot pv with (nolock)
on jp.mid=pv.MID
Where 1=1 and em.UserName not like 'test%' AND JP.MID in ({mid_list} )
SOS_PIVOT AS
(
SELECT *
FROM SOS_BASE
PIVOT
(
MIN(SOSHeaderImg)
FOR HDR1 IN ([Header_Image])
) pvt
) )
select * from Coverage
SELECT
'40148' AS ProjectId,
MID,
EmpId AS employee_id,
StoreId AS store_id,
VisitDate AS visit_date,
StoreTypeid AS storetype_id,
ChannelId AS channel_id,
SOSDefinitionName,
SOSHeaderDeatils,
SOSHeaderName,
SOSHeaderID,
SOSChildDeatils,
SOSChildName,
SOSChildID,
SOSHeaderFacing,
ChildTotalFacing,
ChildSelfFacing,
SOSTarget,
GETDATE() AS update_date,
'SP-Pius' AS update_by
FROM SOS_PIVOT
GROUP BY
CountryName,
MID,
RegionName,
StateName,
CityName,
SupervisorName,
EmpId,
EmployeeName,
Designation,
StoreId,
VisitDate,
StoreCode,
StoreName,
Address,
StoreTypeid,
ChannelId,
ChainName,
SOSDefinitionName,
SOSHeaderDeatils,
SOSHeaderName,
SOSHeaderID,
SOSChildDeatils,
SOSChildName,
SOSChildID,
ChildTotalFacing,
SOSHeaderFacing,
ChildSelfFacing,
SOSTarget
ORDER BY
RegionName,
StateName,
CityName,
VisitDate;
+10
View File
@@ -0,0 +1,10 @@
with Journey_Plan
(project_id,store_id,employee_id,visit_date,process_id,CreateDate,CreateBy,UpdateDate,
UpdateBy)
AS (
select
'40148' ,storeid,EmpId,cast(visitdate as DATE),Deviation,GETDATE(),'Pius',GETDATE(),'Pius' from OneApp_KelloggsMT.dbo.mapping_journeyplan
where MONTH(VisitDate) = MONTH(CAST('{run_date}' AS DATE)) AND Year(VisitDate)=Year(CAST('{run_date}' AS DATE))
AND EmpId NOT IN ( SELECT ID FROM OneApp_KelloggsMT.dbo.ASPNETUSERS
WHERE USERNAME LIKE 'TEST%'))
select * from Journey_Plan;
+81
View File
@@ -0,0 +1,81 @@
with employee_detail AS
(
SELECT rm.RegionId, rm.RegionName, st.StateId, st.StateName,
cm.CityId, cm.CityName,
em1.Id AS [Supervisor Code], ISNULL(em1.EmployeeName, '') AS Supervisor,
em.Id AS [Employee code], ISNULL(em.EmployeeName, '') AS [Employee Name],
dm.DesignationId, dm.DesignationName AS Designation
FROM OneApp_KelloggsMT.dbo.AspNetUsers em
INNER JOIN OneApp_KelloggsMT.dbo.AspNetUsers em1 ON em1.Id = em.ManagerId
INNER JOIN OneApp_KelloggsMT.dbo.Master_City cm ON em.CityId = cm.CityId
INNER JOIN OneApp_KelloggsMT.dbo.Master_State st ON cm.StateId = st.StateId
INNER JOIN OneApp_KelloggsMT.dbo.Master_Region rm ON st.RegionId = rm.RegionId
INNER JOIN OneApp_KelloggsMT.dbo.Master_Designation dm ON Em.DesignationId = dm.DesignationId
WHERE em.RightId IN (6)
AND (Em.ResignDate IS NULL OR CONVERT(DATE, Em.ResignDate, 101) >= CAST('{run_date}' AS DATE))
AND em.EmployeeName NOT LIKE '%test%'
),
Login (
project_id,
employee_id, login_date, login_time, process_id,
first_store_in_time, last_store_out_time, update_date, update_by,
Unique_Id
) AS (
SELECT '40148' AS ProjectId,
Em.[Employee Code],
[Login Date],
[Login Time],
'0',
[1st Store In Time],
[Last Store Out Time],
GETDATE(),
'SP-Pius',
CAST(40148 AS VARCHAR) + '_' + CAST(Em.[Employee Code] AS VARCHAR)
FROM employee_detail Em
LEFT OUTER JOIN
(
SELECT SupervisorId AS [Supervisor Code],
SupervisorName,
EmpId AS [Employee Code],
EmpName,
DesignationName,
[Login Date],
[Login Time],
[1st Store In Time],
[Last Store Out Time],
AppVersion AS [App Version]
FROM (
SELECT Em.SupervisorId,
Em.SupervisorName,
Em.EmpId,
Em.EmpName,
Em.DesignationName,
CONVERT(VARCHAR, ud.LoginDate, 101) AS [Login Date],
CONVERT(VARCHAR, ud.InTime, 108) AS [Login Time],
ud.AppVersion,
ROW_NUMBER() OVER (
PARTITION BY ud.Empid, CONVERT(VARCHAR, ud.LoginDate, 101)
ORDER BY ud.LoginDate ASC
) AS col,
(SELECT MIN(CONVERT(NVARCHAR, sc.InTime, 108))
FROM OneApp_KelloggsMT.dbo.T_StoreCoverage sc
WHERE sc.Isdel = 0
AND sc.EmpId = ud.EmpId
AND CONVERT(VARCHAR, sc.VisitDate, 101) = CONVERT(VARCHAR, ud.LoginDate, 101)
) AS [1st Store In Time],
(SELECT MAX(CONVERT(NVARCHAR, OutTime, 108))
FROM OneApp_KelloggsMT.dbo.T_StoreCoverage SC
WHERE SC.Isdel = 0
AND SC.EmpId = UD.EmpId
AND CONVERT(VARCHAR, SC.VisitDate, 101) = CONVERT(VARCHAR, UD.LoginDate, 101)
) AS [Last Store Out Time]
FROM OneApp_KelloggsMT.dbo.T_DeviceLogin ud
INNER JOIN OneApp_KelloggsMT.dbo.vw_Employee_Detail Em ON ud.EmpId = Em.EmpId
WHERE CAST(ud.LoginDate AS DATE) = CAST('{run_date}' AS DATE) -- fixed: direct date comparison
) AS T1
WHERE T1.col = 1
) AS T ON Em.[Employee code] = T.[Employee Code]
WHERE T.[Login Date] IS NOT NULL
)
SELECT * FROM Login;
+81
View File
@@ -0,0 +1,81 @@
WITH MID_TABLE_COV1 AS
(
SELECT EmpId, VisitDate
FROM OneApp_KelloggsMT.dbo.T_OQAD
WHERE CreateDate >= {run_date}
AND CreateDate < DATEADD(DAY,1,'{run_date}')
UNION ALL
SELECT EmpId, VisitDate
FROM OneApp_KelloggsMT.dbo.T_OQAD
WHERE UpdateDate >= {run_date}
AND UpdateDate < DATEADD(DAY,1, '{run_date}')
),
QUIZ AS
(
SELECT DISTINCT
E.EmpId,
E.EmpName,
E.SupervisorId,
E.SupervisorName,
E.DesignationName,
E.CityName,
E.StateName,
E.RegionName,
CAST(DQ.VisitDate AS DATE) AS VisitDate,
DQ.QuestionId,
DQ.AnswerId,
QC.QuestionCategoryId,
QC.QuestionCategory
FROM OneApp_KelloggsMT.dbo.T_OQAD DQ
INNER JOIN OneApp_KelloggsMT.dbo.vw_Employee_Detail E
ON DQ.EmpId = E.EmpId
INNER JOIN OneApp_KelloggsMT.dbo.Master_OQAD_Question QU
ON DQ.QuestionId = QU.QuestionId
INNER JOIN OneApp_KelloggsMT.dbo.Master_OQAD_Category QC
ON QU.QuestionCategoryId = QC.QuestionCategoryId
WHERE E.EmpName NOT LIKE '%TEST%'
AND E.RightId = 6
AND (
E.ResignDate IS NULL
OR CAST(E.ResignDate AS DATE) >= '{run_date}'
)
AND EXISTS
(
SELECT 1
FROM MID_TABLE_COV1 A
WHERE A.EmpId = DQ.EmpId
AND CAST(A.VisitDate AS DATE) = CAST(DQ.VisitDate AS DATE)
)
)
SELECT
40148 AS project_id,
Q.EmpId AS employee_id,
0 AS process_id,
Q.VisitDate AS visit_date,
Q.QuestionCategoryId AS question_category_id,
Q.QuestionCategory AS question_category,
QM.QuestionId AS question_id,
QM.Question AS question,
ISNULL(QA.AnswerId,0) AS answer_id,
ISNULL(QA.Answer,'') AS answer,
CASE
WHEN QA.AnswerId IS NULL THEN 'Not Answer'
WHEN QA.RightAnswer = 1 THEN 'Y'
WHEN QA.RightAnswer IS NULL THEN 'Not Answer'
ELSE 'N'
END AS correct_answer,
GETDATE() AS update_date,
'SP-Pius' AS update_by
FROM QUIZ Q
INNER JOIN OneApp_KelloggsMT.dbo.Master_OQAD_Question QM
ON Q.QuestionId = QM.QuestionId
LEFT JOIN OneApp_KelloggsMT.dbo.Master_OQAD_Answer QA
ON Q.AnswerId = QA.AnswerId
where Q.EmpId not in ({empid_list})
+56
View File
@@ -0,0 +1,56 @@
with PaidVisibility(Mid,
project_id,store_id,employee_id,visit_date,supervisor_id,channel_id,chain_id,storetype_id,Menuid,MenuName,
Visibility_Id,
Visibility_Name,
Visibility_definition_id,Visibility_definition_name,
Visibility_deatils,Visibility_deatils_id,Visibility_value_name,
present,REASONID,reason,VisibilityQuestion,VisibilityAnswer,image1,image2,update_date,update_by)
AS (
Select sc.MID, '40148' Projectid,sc.StoreId,Em.EmpId,sc.VisitDate,Em.SupervisorId,sm.ChannelId,sm.ChainId,sm.StoreTypeId, 0 as menuid ,'' as menuname,
ts.visibilityid,mv.Visibilityname,msd.VisibilityDefinitionId,
MSD.VisibilityDefinitionName, case when isnull(TS.VisibilityTable,'')='Master_Category' then 'Category'
when isnull(TS.VisibilityTable,'')='Master_SubCategory' then 'SubCategory'
when isnull(TS.VisibilityTable,'')='Master_Brand' then 'Brand'
when isnull(TS.VisibilityTable,'')='Master_SubBrand' then 'SubBrand'
end as PaidVisibilityDeatils ,ts.visibilityvalue[Paid_visiValueID],
CASE when isnull(TS.VisibilityTable,'')='Master_Category' THEN (SELECT a.CategoryName from OneApp_KelloggsMT.dbo.Master_Category a where ts.VisibilityValue=a.CategoryId )
when isnull(TS.VisibilityTable,'')='Master_SubCategory' THEN (SELECT a.SubCategoryName from OneApp_KelloggsMT.dbo.Master_SubCategory a where ts.VisibilityValue=a.SubCategoryId )
when isnull(TS.VisibilityTable,'')='Master_Brand' THEN (SELECT a.BrandName from OneApp_KelloggsMT.dbo.Master_Brand a where ts.VisibilityValue=a.BrandId )
when isnull(TS.VisibilityTable,'')='Master_SubBrand' THEN (SELECT a.SubBrandName from OneApp_KelloggsMT.dbo.Master_SubBrand a where ts.VisibilityValue=a.SubBrandId ) end as
[Paid_VisiValueName],case when ts.Present=0 then 'N' else 'Y' end Present,
CASE WHEN TS.PRESENT=0 THEN TS.VISIBILITYREASONID ELSE '' END AS VISIBILITYREASONID ,
case when ts.Present=0 then isnull(mnp.VisibilityReason,'') else '' end Reason,
ISNULL( MVQ.VisibilityQuestionName,'') AS Question, ISNULL(TVQ.VisibilityAnswerName,'') AS Answer ,
case when ts.present=0 then '' else
case when isnull(SHI.VisibilityImage1,'')='' then '' else 'https://kimt1.parinaam.in/Upload/PaidVisibilityImages/'+SHI.VisibilityImage1
end end as PaidVisibilityImg1 ,case when isnull(SHI.VisibilityImage2,'')='' then '' else 'https://kimt.parinaam.in/Upload/PaidVisibilityImages/'+SHI.VisibilityImage2
end as PaidVisibilityImg2 ,GETDATE(),'SP-Pius'
from
OneApp_KelloggsMT.dbo.T_Visibility ts WITH (NOLOCK) Inner join
OneApp_KelloggsMT.dbo.T_StoreCoverage sc WITH (NOLOCK) on ts.mid= sc.mid Inner Join
OneApp_KelloggsMT.dbo.vw_StoreDetail sm on sc.StoreId= sm.StoreId Inner Join
OneApp_KelloggsMT.dbo.vw_Employee_Detail Em on sc.EmpId= Em.EmpId
inner join
OneApp_KelloggsMT.dbo.Master_VisibilityDefinition msd WITH (NOLOCK) on
msd.VisibilityDefinitionId=ts.VisibilityDefinitionId --and msd.menuid=ts.menuid
--inner join OneApp_KelloggsMT.dbo.Master_menu MM WITH (NOLOCK) ON ts.menuid=mm.menuid and msd.menuid=mm.menuid
left join OneApp_KelloggsMT.DBO. master_visibility mv WITH (NOLOCK) on ts.visibilityid=mv.visibilityid
LEFT join
OneApp_KelloggsMT.dbo.T_VisibilityImages SHI WITH (NOLOCK) ON ts.VId=SHI.VId
left join
OneApp_KelloggsMT.dbo.Master_VisibilityReason mnp WITH (NOLOCK) on ts.VisibilityReasonId=mnp.VisibilityReasonId
LEFT JOIN OneApp_KelloggsMT.dbo.T_VisibilityStock TVS WITH (NOLOCK) ON TS.VID=TVS.VID
LEFT JOIN OneApp_KelloggsMT.dbo.T_VisibilityQuestion TVQ WITH (NOLOCK) ON TS.Vid=TVQ.Vid
LEFT JOIN OneApp_KelloggsMT.dbo.Master_VisibilityQuestion MVQ WITH (NOLOCK) on TVQ.VisibilityQuestionId=MVQ.VisibilityQuestionId
Where 1=1 and Em.EmpName not like 'test%'
--and sc.visitdate between '10/01/2023' and '10/31/2023'
AND sc.MID in ({mid_list})
)
select * from PaidVisibility
+43
View File
@@ -0,0 +1,43 @@
with Promotion (Mid,
project_id,store_id,employee_id,visit_date,supervisor_id,channel_id,chain_id,storetype_id,promo_definition_id,promo_definition_name,
promotion_deatils,promotion_deatils_id,promotion_value_name,
present,reason,PromoQuestion,PromoAnswer,image1,image2,update_date,update_by)
as (
Select
sc.MID, '40148' Projectid,sc.StoreId,Em.EmpId,sc.VisitDate,Em.SupervisorId,sm.ChannelId,sm.ChainId,sm.StoreTypeId,msd.PromoDefinitionId,
MSD.PromoDefinitionName, case when isnull(TS.PromoTable,'')='Master_Category' then 'Category'
when isnull(TS.PromoTable,'')='Master_SubCategory' then 'SubCategory'
when isnull(TS.PromoTable,'')='Master_Brand' then 'Brand'
when isnull(TS.PromoTable,'')='Master_SubBrand' then 'SubBrand'
end as PromotionDeatils ,
TS.PromoValue[PromotionDeatilsId],
CASE when isnull(TS.PromoTable,'')='Master_Category' THEN (SELECT a.CategoryName from OneApp_KelloggsMT.dbo.Master_Category a where ts.PromoValue=a.CategoryId )
when isnull(TS.PromoTable,'')='Master_SubCategory' THEN (SELECT a.SubCategoryName from OneApp_KelloggsMT.dbo.Master_SubCategory a where ts.PromoValue=a.SubCategoryId )
when isnull(TS.PromoTable,'')='Master_Brand' THEN (SELECT a.BrandName from OneApp_KelloggsMT.dbo.Master_Brand a where ts.PromoValue=a.BrandId )
when isnull(TS.PromoTable,'')='Master_SubBrand' THEN (SELECT a.SubBrandName from OneApp_KelloggsMT.dbo.Master_SubBrand a where ts.PromoValue=a.SubBrandId ) end as
[PromoValueName],case when ts.Present=0 then 'N' else 'Y' end Present,
case when ts.Present=1 then '' else isnull(mnp.PromoReason,'') end as Reason, ISNULL(mpq.PromoQuestionName,'') AS Question,
ISNULL(tpq.PromoAnswerName,'') AS Answer ,
case when isnull(SHI.PromoImage1,'')='' then '' else 'https://kimt1.parinaam.in/Upload/PromotionImages/'+SHI.PromoImage1
end as Image1 ,case when isnull(SHI.PromoImage2,'')='' then '' else 'https://kimt1.parinaam.in/Upload/PromotionImages/'+SHI.PromoImage2
end as Image2 ,GETDATE(),'SP-Pius'
from
OneApp_KelloggsMT.dbo.T_Promotion ts Inner join
OneApp_KelloggsMT.dbo.T_StoreCoverage sc on ts.mid= sc.mid Inner Join
OneApp_KelloggsMT.dbo.vw_StoreDetail sm on sc.StoreId= sm.StoreId Inner Join
OneApp_KelloggsMT.dbo.vw_Employee_Detail Em on sc.EmpId= Em.EmpId
inner join
OneApp_KelloggsMT.dbo.Master_PromotionDefinition msd on
msd.PromoDefinitionId=ts.PromoDefinitionId
LEFT join
OneApp_KelloggsMT.dbo.T_PromotionImages SHI ON ts.PId=SHI.PId
left join
OneApp_KelloggsMT.dbo.Master_PromotionReason mnp on ts.PromoReasonId=mnp.PromoReasonId
left join OneApp_KelloggsMT.dbo.t_promotionquestion tpq on ts.PId=tpq.PId
left join OneApp_KelloggsMT.dbo.Master_PromotionQuestion mpq on tpq.PromoQuestionId=mpq.PromoQuestionId
Where 1=1 and Em.EmpName not like 'test%'
AND sc.MID in ({mid_list}) )
select * from Promotion
+70
View File
@@ -0,0 +1,70 @@
with SOS_OneApp ( project_id,Mid,
employee_id,store_id,visit_date,storetype_id,channel_id,SOSDefinitionName,SOSHeaderDeatils,SOSHeaderName,SOSHeaderID,SOSChildDeatils,SOSChildName,SOSChildID,
SOSHeaderFacing,ChildTotalFacing,ChildSelfFacing,SOSTarget,update_date,update_by)
as (
Select '40148' AS ProjectId,MID,
EmpId
,StoreId,
VisitDate,StoreTypeid,ChannelId ,
--CameraAllow,
SOSDefinitionName,SOSHeaderDeatils,SOSHeaderName,[SOSHeaderID] ,SOSChildDeatils , SOSChildName,SOSChildID,SOSHeaderFacing,ChildTotalFacing,
ChildSelfFacing ,SOSTarget,getdate(),'SP-Pius' from
(
Select sm.CountryName,SC.MID, sm.RegionName, sm.StateName, sm.CityName, Em.SupervisorName, em.EmpId as EmpId
, Em.EmpName as EmployeeName, Em.DesignationName as Designation, sm.StoreId,convert(varchar,sc.VisitDate,101)VisitDate, sm.StoreCode, sm.StoreName,
sm.Address, sm.StoreTypeid ,sm.ChannelId,sm.ChainName,
MSD.SOSDefinitionName, case when isnull(TS.SOSHeaderTable,'')='Master_Category' then 'Category'
when isnull(TS.SOSHeaderTable,'')='Master_SubCategory' then 'SubCategory'
when isnull(TS.SOSHeaderTable,'')='Master_Brand' then 'Brand'
when isnull(TS.SOSHeaderTable,'')='Master_SubBrand' then 'SubBrand'
end as SOSHeaderDeatils ,
TS.SOSHeaderName,TS.SOSHeaderValue[SOSHeaderID], 'Header'+'_'+'Image' as HDR1,
case when isnull(tsc.SOSChildTable,'')='Master_Category' then 'Category'
when isnull(tsc.SOSChildTable,'')='Master_SubCategory' then 'SubCategory'
when isnull(tsc.SOSChildTable,'')='Master_Brand' then 'Brand'
when isnull(tsc.SOSChildTable,'')='Master_SubBrand' then 'SubBrand'
end as SOSChildDeatils ,
TSC.SOSChildName,TSC.SOSChildValue[SOSChildID],TSC.ChildTotalFacing,ts.SOSHeaderFacing,TSC.ChildSelfFacing
,
(select top 1 SOSTarget from OneApp_KelloggsMT.dbo.Mapping_StoreShareOfShelfTarget a where a.SOSDefinitionId=msd.SOSDefinitionId and
a.Storeid=sm.StoreId and a.fromdate<=sc.visitdate and a.todate>=sc.visitdate ) as SOSTarget,
case when isnull(SHI.SOSHeaderImage,'')='' then '' else 'https://kimt1.parinaam.in/Upload/SOSImages/'+SHI.SOSHeaderImage
end as SOSHeaderImg
--,case when isnull(SCI.SOSChildImage,'')='' then '' else 'https://di1.parinaam.in/Upload/SOSImages/'+SCI.SOSChildImage
--end as SOSChildImage
from
OneApp_KelloggsMT.dbo.T_ShareOfShelfHeader ts Inner join
OneApp_KelloggsMT.dbo.T_StoreCoverage sc on ts.mid= sc.mid Inner Join
OneApp_KelloggsMT.dbo.vw_StoreDetail sm on sc.StoreId= sm.StoreId Inner Join
OneApp_KelloggsMT.dbo.vw_Employee_Detail Em on sc.EmpId= Em.EmpId
inner join
OneApp_KelloggsMT.dbo.T_ShareOfShelfChild tsc on ts.sosid=tsc.SOSId
inner join
OneApp_KelloggsMT.dbo.Master_ShareOfShelfDefinition msd on
msd.SOSDefinitionId=tsc.SOSDefinitionId
LEFT join
OneApp_KelloggsMT.dbo.T_ShareOfShelfHeaderImages SHI ON ts.SOSId=SHI.SOSId AND TS.SOSHeaderValue=SHI.SOSHeaderValue
LEFT JOIN
OneApp_KelloggsMT.dbo.T_ShareOfShelfChildImages SCI ON TSC.SOSId=SCI.SOSId AND TSC.SOSChildValue=SCI.SOSChildValue
Where 1=1 AND EM.EMPNAME NOT LIKE 'test%' AND sc.mid IN ({mid_list})
-- AND sc.VisitDate BETWEEN '09/18/2023' AND '09/21/2023'
--and sc.StoreId not in (
--select store_id from SOS_OneApp a where a.project_id=40148 and a.store_id=sc.StoreId
--and a.employee_id=sc.EmpId and convert(date,sc.VisitDate,101)=convert(date,a.visit_date,101) )
) as A
Pivot
(
min(SOSHeaderImg)
for HDR1 in([Header_Image])
) as pvt1
GROUP BY CountryName,MID, RegionName, StateName, CityName, SupervisorName, EmpId
, EmployeeName, Designation, StoreId,VisitDate,StoreCode, StoreName,
Address, StoreTypeid ,ChannelId,ChainName,
SOSDefinitionName,SOSHeaderDeatils,SOSHeaderName,[SOSHeaderID] ,SOSChildDeatils ,
SOSChildName,SOSChildID,ChildTotalFacing,SOSHeaderFacing,ChildSelfFacing,SOSTarget )
select * from SOS_OneApp
+29
View File
@@ -0,0 +1,29 @@
with Stock_Details (project_id,
Mid,supervisor_id,employee_id,store_id,
visitdate,storetype_id,store_category_id,product_id,MSL, MBQ,Stock_Qty,
damagedstock,loststock,expirystock,skuavailability,update_date,update_by,StockType)
AS (
Select '40148' AS ProjectId,sc.MID,Em.SupervisorId,Em.EmpId,sm.StoreId,sc.VisitDate,sc.StoreTypeId,sm.StoreCategoryId,
vp.ProductId,ts.MSL,mp.MBQ, ISNULL(ts.OpeningStock,0)+ISNULL(TS.middaystock,0),0,0,0,
Case WHEN ISNULL(ts.OpeningStock,0)+ISNULL(TS.middaystock,0)>=1THEN 'Y' ELSE 'N' END AS
skuavailability, GETDATE(),'SP-Pius','Parinaam'
from
OneApp_KelloggsMT.dbo.T_Stock ts Inner join
OneApp_KelloggsMT.dbo.T_StoreCoverage sc on ts.mid= sc.mid Inner Join
OneApp_KelloggsMT.dbo.vw_StoreDetail sm on sc.StoreId= sm.StoreId Inner Join
OneApp_KelloggsMT.dbo.vw_Employee_Detail Em on sc.EmpId= Em.EmpId
inner join OneApp_KelloggsMT.dbo.vw_product vp on ts.ProductId=vp.ProductId inner JOIN
(SELECT MBQ,ProductId,StateId,ChainId,StoreTypeId,StoreCategoryId,StoreClassId FROM OneApp_KelloggsMT.dbo.Mapping_ProductAssortment where FromDate<='{run_date}' and ToDate>='{run_date}') mp on mp.StateId=sm.StateId and
mp.ChainId=sm.ChainId
and mp.StoreTypeId=sm.StoreTypeId
and mp.StoreCategoryId=sm.StoreCategoryId
and mp.StoreClassId=sm.StoreClassId
and mp.ProductId=ts.ProductId
Where Em.EmpName not like 'test%' AND sc.MID in ({mid_list})
)
select * from Stock_Details
+68
View File
@@ -0,0 +1,68 @@
WITH QUIZ AS
(
SELECT
SC.MID,
SC.StoreId,
EM.EmpId,
EM.EmpName,
EM.SupervisorId,
EM.SupervisorName,
EM.DesignationName,
CAST(SC.VisitDate AS DATE) AS VisitDate,
DQ.QuestionId,
QU.Question,
DQ.Answer,
QC.CategoryId,
QC.Category,
MSS.SubCategoryId,
MSS.SubCategory,
DQ.ImageUrl
FROM OneApp_KelloggsMT.dbo.T_SURVEY DQ
INNER JOIN OneApp_KelloggsMT.dbo.T_StoreCoverage SC
ON DQ.MID = SC.MID
INNER JOIN OneApp_KelloggsMT.dbo.Master_SurveyQuestion QU
ON DQ.QuestionId = QU.QuestionId
INNER JOIN OneApp_KelloggsMT.dbo.Master_SurveySubCategory MSS
ON QU.SubCategoryId = MSS.SubCategoryId
INNER JOIN OneApp_KelloggsMT.dbo.Master_SurveyCategory QC
ON MSS.CategoryId = QC.CategoryId
INNER JOIN OneApp_KelloggsMT.dbo.vw_Employee_Detail EM
ON SC.EmpId = EM.EmpId
INNER JOIN OneApp_KelloggsMT.dbo.Master_Survey MS
ON DQ.SurveyId = MS.SurveyId
AND QU.SurveyId = MS.SurveyId
WHERE EM.EmpName NOT LIKE 'test%'
AND SC.MID IN ({mid_list})
)
SELECT
40148 AS Project_Id,
Q.MID AS Mid,
Q.SupervisorId AS SuperVisorId,
Q.EmpId AS EmpID,
SM.StoreId,
Q.VisitDate,
SM.StoreTypeId,
SM.ChainId,
Q.CategoryId,
Q.Category,
Q.SubCategoryId,
Q.SubCategory,
Q.QuestionId,
Q.Question,
Q.Answer,
GETDATE() AS CreateDate,
'SP-Pius' AS CreateBy
FROM QUIZ Q
INNER JOIN OneApp_KelloggsMT.dbo.vw_StoreDetail SM
ON Q.StoreId = SM.StoreId
+23
View File
@@ -0,0 +1,23 @@
with Web_Logins (project_id,
supervisor_id,supervisor_name,emp_id,employee_name,designation,date,time,activity_name,activity_type,right_name,CreateDate,CreateBy)
as (
Select Distinct '40148' as ProjectId
, EM1.Id AS SupervisorId, EM1.EmployeeName AS Supervisor
, EM.Id AS EmployeeId , EM.EmployeeName as Employee
, DM.DesignationName as Designation
, cast( CONVERT(date,AL.Date) as Date) AS Date,CONVERT(NVARCHAR,AL.Date ,108) AS Time
, AL.Thread as ActivityName, AL.Level as ActivityType, R.RightName,GETDATE(),'Pius'
FROM OneApp_KelloggsMT.dbo.T_User_Activity_Log AL LEFT JOIN
OneApp_KelloggsMT.dbo.AspNetUsers EM ON AL.Logger=EM.USERNAME INNER JOIN
OneApp_KelloggsMT.dbo.AspNetUsers EM1 ON EM1.Id = EM.ManagerId INNER JOIN
OneApp_KelloggsMT.dbo.AspNetUsers EM2 ON EM2.Id = EM1.ManagerId INNER JOIN
OneApp_KelloggsMT.dbo.Master_Designation DM ON EM.DesignationId = DM.DesignationId INNER JOIN
OneApp_KelloggsMT.dbo.Master_City CM ON EM.CityId = CM.CityId INNER JOIN
OneApp_KelloggsMT.dbo.Master_State ST ON ST.StateId = cm.StateId INNER JOIN
OneApp_KelloggsMT.dbo.Master_Region RM ON RM.RegionId = ST.RegionId INNER JOIN
OneApp_KelloggsMT.dbo.Right_Master R ON EM.RightId=R.RightId
Where 1=1 and CONVERT(date,AL.Date,101) = CONVERT(DATE,CAST('{run_date}' AS DATE),101)
)
select * from Web_Logins
ORDER BY employee_name
+31
View File
@@ -0,0 +1,31 @@
WITH
additional_visibility (project_id,Mid,
emp_id,store_id,storetype_id,channel_id,chain_id,camera_allowed,visit_date,is_present,brand_id,
display_id,Remarks,image_url,created_date,created_by)
as (
SELECT '40148' AS ProjectId,ts.mid, em.EmpId as EmpId
, sm.StoreId,sm.storetypeid,sm.channelid,sm.chainid,case when sm.cameraallow=1 then 'Y' ELSE 'N' END AS
cameraallow , convert(varchar,sc.VisitDate,101)VisitDate,
case when ts.Present=0 then 'N' else 'Y' end Present,
isnull(MB.BrandId,'')[BrandId],
isnull(MD.DisplayId,'')[DisplayId],ISNULL(TS.REMARK,'')[Remark],
case when isnull(ts.ImageUrl,'')='' then '' else 'https://kimt1.parinaam.in/Upload/VisibilityImages/'+ts.ImageUrl
end as AdditionalImageUrl ,GETDATE(),'Pius'
from
OneApp_KelloggsMT.dbo.T_AdditionalVisibility ts Inner join
OneApp_KelloggsMT.dbo.T_StoreCoverage sc on ts.mid= sc.mid Inner Join
OneApp_KelloggsMT.dbo.vw_StoreDetail sm on sc.StoreId= sm.StoreId Inner Join
OneApp_KelloggsMT.dbo.vw_Employee_Detail Em on sc.EmpId= Em.EmpId
left join
OneApp_KelloggsMT.dbo.Master_Brand MB ON TS.BrandId=MB.BrandId
LEFT JOIN
OneApp_KelloggsMT.dbo.Master_Display MD ON TS.DisplayId=MD.DisplayId
Where Em.EmpName not like 'test%' and ts.Present=1 and sc.mid in ({mid_list})
)
select * from additional_visibility;
+6 -6
View File
@@ -62,32 +62,32 @@ tables:
- name: Store_Master - name: Store_Master
type: DIMENSION type: DIMENSION
operation: DELETE+INSERT operation: DELETE+INSERT
fetch_by: none fetch_by: master
- name: SKU_Master - name: SKU_Master
type: DIMENSION type: DIMENSION
operation: DELETE+INSERT operation: DELETE+INSERT
fetch_by: none fetch_by: master
- name: display_master - name: display_master
type: DIMENSION type: DIMENSION
operation: DELETE+INSERT operation: DELETE+INSERT
fetch_by: none fetch_by: master
- name: Employee_Master - name: Employee_Master
type: DIMENSION type: DIMENSION
operation: DELETE+INSERT operation: DELETE+INSERT
fetch_by: none fetch_by: master
- name: coverage_remarks - name: coverage_remarks
type: DIMENSION type: DIMENSION
operation: DELETE+INSERT operation: DELETE+INSERT
fetch_by: none fetch_by: reason_id
- name: mapping_storevisibility - name: mapping_storevisibility
type: BRIDGE type: BRIDGE
operation: DELETE+INSERT operation: ONLY_INSERT
fetch_by: run_date fetch_by: run_date
- name: Master_VisibilityReason - name: Master_VisibilityReason
+109
View File
@@ -0,0 +1,109 @@
tables:
# - name: SOS_OneApp
# type: FACT
# operation: INSERT
# fetch_by: mids
# # - name: OQaD
# # type: FACT
# # operation: INSERT
# # fetch_by: run_date
# - name: additional_visibility
# type: FACT
# operation: INSERT
# fetch_by: mids
# - name: Coverage
# type: FACT
# operation: INSERT
# fetch_by: mids
# - name: Survey
# type: FACT
# operation: INSERT
# fetch_by: mids
# - name: Login
# type: FACT
# operation: INSERT
# fetch_by: run_date
# - name: Stock_Details
# type: FACT
# operation: INSERT
# fetch_by: mids
# - name: Attendance
# type: FACT
# operation: DELETE+INSERT
# fetch_by: run_date
# - name: Journey_Plan
# type: FACT
# operation: INSERT
# fetch_by: run_date
# - name: PaidVisibility
# type: FACT
# operation: INSERT
# fetch_by: mids
# - name: Web_Logins
# type: FACT
# operation: INSERT
# fetch_by: run_date
# - name: Store_Master
# type: DIMENSION
# operation: DELETE+INSERT
# fetch_by: master
# - name: coverage_remarks
# type: DIMENSION
# operation: DELETE+INSERT
# fetch_by: reason_id
# - name: SKU_Master
# type: DIMENSION
# operation: DELETE+INSERT
# fetch_by: master
# - name: display_master
# type: DIMENSION
# operation: DELETE+INSERT
# fetch_by: master
# - name: Employee_Master
# type: DIMENSION
# operation: DELETE+INSERT
# fetch_by: master
# - name: mapping_storevisibility
# type: BRIDGE
# operation: ONLY_INSERT
# fetch_by: run_date
# - name: Master_VisibilityReason
# type: DIMENSION
# operation: DELETE+INSERT
# fetch_by: none
# - name: Master_VisibilityDefinition
# type: DIMENSION
# operation: DELETE+INSERT
# fetch_by: none
# - name: Master_Salesterritorylayer
# type: DIMENSION
# operation: DELETE+INSERT
# fetch_by: none
- name: Promotion
type: FACT
operation: INSERT
fetch_by: mids