Importing new records and updating employee-manager data - sql-update

I have spent a day researching and trying various samples without success in being able to update imported records from a lookup table, however most samples use a straightforward lookup table that has the relevant info, which is not the scenario I have.
I am importing employee records into a Users table. I have created an Import table that contains the employee details as well as the manager Email address for each user. For example: FirstName, LastName, Email, ManagerEmail.
From the Import table the records (excepting ManagerEmail) are then inserted into the Users table. Only once this has been done will the user now have a UserId. The challenge is now to update the newly created User record with the ManagerId (Which would be the Manangers UserId).
I could create a UserId and ManagerId field in the Import table and run a script to update these values after the User records have been created and then use this to update the Users table, however it would be a challenge to update the Users table based only on having the Email and ManagerEmail relationship in the Import table.
Please let me know if my explanation needs further clarification. Any help would be appreciated and I will continue researching in the meantime.

Worked out a solution using a CTE.
WITH
cteImport (ManagerId, Email)
AS
(
SELECT u.UserId, i.Email
FROM Users u
INNER JOIN Import i
ON i.ManagerEmail = u.Email
)
UPDATE Users
SET ManagerId = i.ManagerId
FROM cteImport i
WHERE Users.Email = i.Email
Hope this can help someone else.

Related

Data modelling in dynamo db for Ticket Management System

I am working in Dynamo DB for the first time . My assignment is Ticket Management System where it has 3 entities Department , User and Ticket. The relationship between each entity is.
I have identified the following access patterns
Fetch a Department.
Fetch all users in Department
Fetch a given user in Department
Fetch all Tickets belongs to the Department
Fetch all Tickets assigned to the User
for which i defined the following data model . I am thinking of creating GSI with Tickets as PK and User as SK to do 4 & 5
On a higher level I need to perform 2 updates . I can update the User to which the ticket is assigned and I can update the ticket status as inprogress, resolved . And in the table I have Ticket details as JSON object as below.
I need help from from the experienced people whether my understanding and approach is efficient.
I think you're on the right track. I'd design it as a table with two Global Secondary indexes. The base table looks like this:
The first Global Secondary Index like this (GSI1):
The second Global Secondary Index like this (GSI2):
Now for the why:
This design allows you to easily update the following things:
A user's department
A ticket's status if you know the ticket Id
A ticket's user if you know the ticket Id
A ticket's department if you know the ticket Id
You can get a bunch of information from this model:
Fetch a Department.
Query the base table with the department name or list all departments
Fetch all users in Department
Query GSI 1 with the Department Name and filter the sort Key using begins_with = USER#
Fetch a given user in Department
Sound like you know the UserId, so do a GetItem on the base table. If that's not the case, do the query mentioned in "Fetch all users in Department".
Fetch all Tickets belongs to the Department
Query GSI 1 with the department name as the PK and filter the SK using begins_with = Ticket#
Fetch all Tickets assigned to the User
Query GSI 2 with the user id as the PK and filter the SK using begins_with = Ticket#

How to Create A Dax for RLS?

I have a column named company ID that contains various comapnies with differnet employes in them, I need a DAX Query which will give data A/Q to the Company id, suppose if there are 3 employes inside a company and each of them have a companyid 1 then I they should able to see the reports of each other but they cannot be able to see the reports of comanyid 2 and 3, how this can be achived?
I know I can do this by creating different Roles for each companyid, but how can this be achived if I want this to be built in one particular role???
What you are asking is to implement Dynamic Row Level Security.
Model:
User Table: Table that contains user detail along with the field on which we will apply security(here email field).
Company Table: Table containing company data .
User Company Bridge: Bridge table that contains permission details, for example user x is member of company y and z.
Company Data Table: Measures or transaction information of company that is to be filtered.
Defining RLS(Row Level Security):
In Modelling -> Manage Roles, create a new role on Email of User Table by this DAX query which returns the email id of logged in user.
[Email] = userprincipalname()
Finalizing:
Go to PowerBI Service -> Dataset -> Security and add users to the roles created.
To test the implementation:
Go to Modelling tab of pbix file.
Click on View As Roles.
Check other User checkbox and put an email ID and also check Profile
checkbox.
Now you can see data filtered.
In this manner it becomes easy to maintain roles and security by just modifying the bridge table that stores all permission details.

How can I select records n-at-a-time for multiple users to edit?

I am using a Django backend with postgresql.
Let's say I have a database with a table called Employees with about 20,000 records.
I need to allow multiple users to edit and verify the Area Code field for every record in Employees.
I'd prefer to allow a user to view the records, say, 30 at a time (to reduce burnout).
How can I select 30 records at a time from Employees to send to the front end UI for editing, without letting multiple users edit the same records, or re-selecting a record that has already been verified?
I don't need comments on the content of the database (these are example table and field names).
One way to do this would be to add 2 more fields to your table, say for example assigned_to and verified. You can update assigned_to, which can be a foreign key to the verifying user, when you allow the user to view that Employee. This will create a record preventing the Employee from being chosen twice. assigned_to can also double as a record of who verified this Employee for future reference.
verified could be simply a Boolean field which keeps track if the Employee has already been verified and can be updated when the user confirms the verification
The actual selects can be done like this:
employees = Employee.objects.filter(assigned_to=None, verified=False)[:30]
Then
for emp in employees:
emp.assigned_to = user
emp.save()
Note: This can still potentially cause a race condition if 2 users make this request at exactly the same time. To avoid this, another possibility could be to partition the employee tables into groups for each user with no overlap. This would ensure that no 2 users would ever have the same employees

How to interact with Existing database with Model through template function in Django

I have an existing table called empname in my postgres database
(Projectid,empid,name,Location) as
(1,101,Raj,India),
(2,201,David,USA)
So in the app console it will have like the following
1)Projectid=Textbox
2)Ops =(view,insert,Edit)-Dropdown
Case1:
So if i write project id as 1 and select View Result:It will display all the records for Projectid =1(Here 1 record)
Case2:
If i write projectid as 3 and select insert it will ask for all the inputs like empid,name,address and based on that it will update the table .
Case3:
If i write projectid as 2 and select edit.Then it will show all the field for that id and user can edit any column and can save which will update the records in backend for the existing table
If there is not data found for the respective project id then it will display no records found
Please help me on this as I am stuck up with models
Once you have your models created, the next task should be the form models. I can identify atleast 3 form classes that you will need to create. One to display the information(case 1), another to collect information(case 2) and the last class to edit the information. Wire up the form to the views and add the urls.
A good reference could be a django a user registration form since it will have all the three cases taken care of.http://www.tangowithdjango.com/book17/chapters/login.html

how does otrs save relation between ticket and ldap customer in DB table

I create a ticket for a custmer from LDAP datasource,customer_id and customer_user_id column in ticket table show like below:
customer_id customer_user_id
zhangjq#**.com zhangjq
I think this is the foreign key which OTRS use to associate this ticket with an customer in LDAP,But,When after I update the value of customer_id and customer_user_id column,the customer info also displayed correctly in ticket view page:
Customer Infomation
Firstname: Junqian
Lastname:Zhang
login:zhangjq
Email:zhangjq#**.com
Comment:##2012.09.03
All of these information is read from LDAP.
So,how does otrs save relation between ticket and ldap customer in DB table? or OTRS has other way to manage the relation between ticket and ladp customer?
When setting up your LDAP-Connection for CustomerUser, you can define a CustomerKey. This is the key, OTRS is using to uniquely identify a customer.
When after I update the value of customer_id and customer_user_id column,the customer info also displayed correctly in ticket view page
What are exactly doing? If you are modifying in the SQL-Database, this won't have any impact on the data displayed within OTRS, as this data is cached.
If you are doing modifications in your LDAP, this won't change anything either of the customer-information displayed as this data only gets updated when you (re-)assing a customer to a ticket.