SQL query MLM database with fixed level - sql-server

I've got a database to be developed for a MLM (Multi Level Marketing) company. So, there's one customers table, each customer has a unique ID called CustomerId, and all customers can have customers under them can make N customers down to 1 customer.
All customers are added to the same table tblCustomer and for each customer there's a another table tblCustomerUplink where I add customer's customerId under whom that customer is.
tblCustomerUplink structure is look like below:
Our system have a fixed 5 level where check customer's level based on refer count and allocate level accordingly.
Suppose:
Level ReferLimit Commission
Level1 5 5
Level2 10 3
Level3 15 2

Related

Does row level security works if null value found in cube model

I have created one sales cube which has country wise sales table and country table and security group table like below.
SALES TABLE:
country sales_amount
India 50000
UK 50000
NULL 50000
COUNTRY NAME:
country security_group_id
India S1
UK S2
SECURITY TABLE:
User_name security_group_id
ABC S1
XYZ S2
I am trying to restrict the data country wise,Here I got some issue what if country column has null value and how to handle row level security.
Here,Two users are unable to access data which has gl_country NULL.
How to handle in such that cases.
The NULL value of the country column in table SALES won't affect the execution of row level security.
Row level security restrict data based on which windows user loggs in and in our case the related condition columns are User_name and security_group_id.
For more details about Row level security, please refer:
Dynamic row level security with Analysis services tabular model

Creating a global ID for every row in multiple tables for notifications

I'm designing what is essentially an accounting/retailing application for MS SQL Server 2016. I have ~75 core data object types, each in their own respective table (users, organizations, invoices, payments, etc.).
What I need to create is a notification system,
e.g. "Steve paid invoice 1234", "Bob purchased product XYZ", etc. What I was going to create was a "notification types" table:
NotificationTypes:
id message
11 "{0} paid invoice {1]"
12 "{0} purchased product {1]"
...
and then have two corresponding tables that store each notification event's info, and then the values for that notification message. So for example for "Steve paid invoice 1234":
NotificationEvent:
id notificationType (FK) occured
21 11 2016-01-01 00:00:00
NotificationEventValues:
id notificationEvent (FK) XXXXX
31 21 (FK reference to Steve in users table)
32 21 (FK reference to invoice 1234 in invoices table)
Now since I can't create generic foreign keys for NotificationEventValues.XXXXX, I was going to have a single 'dataObjects' table that has FK columns for all 75 data types I have, with only one of the 'data type' columns having a value per row.
This way, every instance of a data object in my database has a unique ID I can reference in the notification field - which will mean a huge table given it has a unique ID for basically every row in the other 75 tables. The other downside is it means for every user, invoice, any 'data object', I'm wasting significant amounts of space since space will be reserved for ID references for the other 74 null-valued columns (since they're fixed size IDs and not variable).
ASK:
Is there a better way to achieve my 'global' identifier across all the tables? Or a better way to handle the notification system to avoid this problem?
Create triggers in all table that will insert a record in a table say KeyMaster with one column key which will also be the primry key whenever a record is inserted in any table. The value inserted in this table will be in the format _. For example if a record is inserted in user table with id 1 then the record inserted in KeyMaster table will be 'user_1'. Similarly if a record is inserted in invoice table with id 1 then the recor inserted in KeyMaster table will be 'invoice_1'.
In your NotificationEventValues table you will need only three columns id,notificationEvent and key (FK reference to KeyMaster table).
For getting corresponding record you need to write query similar to below:
select *
from NotificationEventValues n
inner join users u on 'user_' + u.id = n.key

DataStage recursion

I have to find a leader of a group and update employee's leader. I am not sure how to proceed with this in DataStage.
I have an employee table as shown below
Emp_id mgr_id leader_id
1 100 400
101 201 500
3 202 600
I get a file to update employee table when an employee changes group. Change code = CHG means it is a job/group change.
I do an equi join between file and employee table and can update manager id. At the same time, I need to find a leader. I need to get all the employees who report to that top level leader and use as the leader id's for every employee.
File:
emp_id mgr_id chg_cd
1 102 CHG
101 301 CHG
File Row 1: There is change in manager for emp_id = 1; need to update mgr_id, leader_id in employee table
File Row 2: There is change in manager for emp_id = 102, need to change mgr_id and leader_id for in employee table
Can you please suggest me on how to proceed with this in DataStage?
ok this problem requires a solution with recursion. As DataStage has no way to do it (if the levels between managers and leaders are variable).
So load the data into a database table and use recursive SQL to query it - this will provide you the solution you are asking for.
Example:
Extract all leaders with their business units they manage inculding different levels) with the recursive SQL statement and use this data in da DataStage lookup to enrich the file data.

SSIS: Lookup data and merge again

Somehow I have a feeling that this should be an easy one - but I cannot see how to fix this in a SSIS package.
I have two tables: Order and CustomerMapping. My CustomerMapping table is not containing all my customers, but only customers that have changed Id and CustomerName.
The definitions of the tables are:
Order:
Id (int)
CustomerId (int)
CustomerName (nvarchar(50))
CustomerMapping:
Id (int)
ObsoleteId (int)
CustomerName (nvarchar(50))
ObsoleteCustomerName (nvarchar(50))
Data in Order table is:
Id CustomerId CustomerName
1 100 Customer 1
2 101 Customer 2
3 102 Customer 3
Data in CustomerMapping:
Id ObsoleteId CustomerName ObsoleteCustomerName
20 100 New Customer 1 Customer 1
21 101 New Customer 2 Customer 2
I want to use the tools provided by the SSIS package to create what I would do like this in SQL:
SELECT o.Id,
CustomerId = ISNULL(cm.Id, o.CustomerId),
CustomerName = ISNULL(cm.CustomerName, o.CustomerName)
FROM Order AS o
LEFT JOIN CustomerMapping AS cm ON o.CustomerId = cm.ObsoleteId
The result of above query is
Id CustomerId CustomerName
1 20 New Customer 1
2 21 New Customer 2
3 102 Customer 3
I now want to take this result-set and save into a new table.
I know that I can just take the above SQL and do what I want (which might be what I end up doing), but somehow I believe I can make a lookup, merge and/or merge join... But I must admit I cannot really see how I do this without splitting the lookup into two and then having to save from each new "thread".
The above is rather simplified... I have 4 columns which I have to compare and then do some other stuff with the whole lot, before saving it into a new table again, which is why I want to keep a single "thread".
Edit: image added:

Database schema for end user report designer

I'm trying to implement a feature whereby, apart from all the reports that I have in my system, I will allow the end user to create simple reports. (not overly complex reports that involves slicing and dicing across multiple tables with lots of logic)
The user will be able to:
1) Select a base table from a list of allowable tables (e.g., Customers)
2) Select multiple sub tables (e.g., Address table, with AddressId as the field to link Customers to Address)
3) Select the fields from the tables
4) Have basic sorting
Here's the database schema I have current, and I'm quite certain it's far from perfect, so I'm wondering what else I can improve on
AllowableTables table
This table will contain the list of tables that the user can create their custom reports against.
Id Table
----------------------------------
1 Customers
2 Address
3 Orders
4 Products
ReportTemplates table
Id Name MainTable
------------------------------------------------------------------
1 Customer Report #2 Customers
2 Customer Report #3 Customers
ReportTemplateSettings table
Id TemplateId TableName FieldName ColumnHeader ColumnWidth Sequence
-------------------------------------------------------------------------------
1 1 Customer Id Customer S/N 100 1
2 1 Customer Name Full Name 100 2
3 1 Address Address1 Address 1 100 3
I know this isn't complete, but this is what I've come up with so far. Does anyone have any links to a reference design, or have any inputs as to how I can improve this?
This needs a lot of work even though it’s relatively simple task. Here are several other columns you might want to include as well as some other details to take care of.
Store table name along with schema name or store schema name in additional column, add column for sorting and sort order
Create additional table to store child tables that will be used in the report (report id, schema name, table name, column in child table used to join tables, column in parent table used to join tables, join operator (may not be needed if it always =)
Create additional table that will store column names (report id, schema name, table name, column name, display as)
There are probably several more things that will come up after you complete this but hopefully this will get you in the right direction.

Resources