Hi Guys,
I have a requirements to look at the dependancies between group Customer table and Customers table ( Parent and Child relationships. The current tables are not scalable and difficult to extract information. There is no foreign key between group customer and Customer table.
@CustomerGrp tbl
The Master Client is recorded in the @CustomerGrp table above
• Master client contains a range of sub clients. Eg. (1-52 ranges)
• Sub-Clients cannot be associated with a contract
• Only Master Client will be associated to contract
@Customers tbl
• Associated Clients are renamed or capture to parent table (@Customer tbl)
• The Clients will have their contracts
Could you guys help me either to create a link between the two tables for better reporting. Please see my code below:
SQL Scripts for sample data below:
--create sample data
IF OBJECT_ID('tempdb.dbo.#CustomerGrp', 'U') IS NOT NULL DROP TABLE #CustomerGrp;
IF OBJECT_ID('tempdb.dbo.#Customers', 'U') IS NOT NULL DROP TABLE #Customers;
Create table #CustomerGrp
(
[GroupID] [int] NOT NULL,
[GroupMasterCustKey] varchar NOT NULL,
[GroupFromCustKey] varchar NOT NULL ,
[GroupToCustKey] varchar NOT NULL
)
INSERT #CustomerGrp ([GroupID],[GroupMasterCustKey],[GroupFromCustKey], [GroupToCustKey])
Select '1082', 'XLV001', 'XLVET001', 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1082', 'XLV001', ' XLVET001' , 'XLVET048'
union all
Select '1146', 'XLV001', ' XLVET050' , 'XLVET057'
union all
Select '1146', 'XLV001', ' XLVET050' , 'XLVET057'
union all
Select '1146', 'XLV001', ' XLVET050' , 'XLVET057'
union all
Select '1146', 'XLV001', ' XLVET050' , 'XLVET057'
union all
Select '1146', 'XLV001', ' XLVET050' , 'XLVET057'
union all
Select '1146', 'XLV001', ' XLVET050' , 'XLVET057'
union all
Select '1146', 'XLV001', ' XLVET050' , 'XLVET057'
union all
Select'1146', 'XLV001', ' XLVET050' , 'XLVET057'
--create sample data
Create table #Customers
(
CustomerID int NOT NULL,
CustomerKey varchar(100) NOT NULL PRIMARY KEY,
[CreatedDate] datetime null,
AssociateClient varchar(20) NULL
)
INSERT #Customers (CustomerID,CustomerKey, CreatedDate, AssociateClient, CompanyName)
Select 104506, 'XLV001', '2015-10-09', 'XLVET001-XLVET052'
Below is currently how we query the table:
SELECT CG.GroupId,C.CustomerKey
,CG.GroupMasterCustKey AS MasterCustomerKey
,C.CustomerID AS ClientID
,[GroupFromCustKey]
,[GroupToCustKey]
,C.AssociateClient
FROM #CustomerGrp AS CG WITH (NOLOCK)
INNER JOIN #Customers AS C WITH (NOLOCK)
ON C.CustomerKey >= CG.GroupFromCustKey
AND C.CustomerKey <= CG.GroupToCustKey