How can I add data from table to other in SQL? - sql

I have 2 departments and created a new one, I want to update employee department but if that employees id equals one of id's listed in other table.
Employees
empid empname deptname
------------------------
01 john dept1
02 bill dept2
03 alex dept1
.
.
.
80 tomas dept1
New_depts_employees_id
empid
-----
02
05
45
18
20
34
78
80
55
32
If employee's id is inside the second table his depname will become 'dept3'
How can I write code make this process in SQL language (I using MS Access).

Do you want sql? You can use update and exists as follows:
Update employees
Set dept_name = 'dept3'
Where exists (select 1 from New_depts_employees_id n where n.emp_id = employees.emp_id)

Open new query constructor.
Add both tables to it.
Drag Employees.empid and drop it onto New_depts_employees_id.empid - a link occures. Not needed if the link is created automatically.
Change query type to UPDATE.
Set "Column to update" to Employees.deptname.
Set "Value to set" to 'dept3'.
Click "Execute".
You may save this query and convert static 'dept3' value to query parameter for future use from external application. Or you may open query constructor in SQL Mode and copy query text from it for external use.

Related

Case Statement with 2 columns

If Dr's name is SoinSo, then make column "Clinic Number Column" say "SO" instead of "06"
This is just a select statement not an actual change to the database.
Not sure how to code this in SQL to get this specific output.
This is the current output:
Dr Name Column | Clinic Number Column
---------------+---------------------
Doe 06
SoinSo 06
James 06
This is the desired Output:
Dr Name Column | Clinic Number Column
---------------+---------------------
Doe 06
SoinSo SO
James 06
I've tried this, but couldn't find any documentation online about doing a CASE statement for 2 columns:
When stf.DrName='SoInSo' then pc.ClinicNumberColumn='SO'
You can get the desired output from following (not tested):
SELECT
stf.DrName
,CASE WHEN stf.DrName='SoInSo' THEN 'SO' ELSE pc.ClinicNumberColumn END AS "CLINIC"
FROM
TABLE

MS Access - Continuous Form Select with Dropdown from Another Table

I've been using Databasedevelopment.co.uk's excellent example on how to do a continuous form select with a invisible button overlaying a checkbox to assign employees to a specific shift. I'd like to make it so that said continuous form also has a dropdown of the different Paycodes so that when they are selected I can use a combobox to indicate "Regular Pay, Overtime, etc....". I'm running into a wall because with the query as-is from the example, the recordset for the Paycode field is not updateable.
Messing with the primary key for the employee's table fixes the paycode issue but prevents the selection code from working properly.
I'm a bit out of my depth here, what's the easiest way to accomplish this?
SELECT CAT.EmployeeID, CAT.FirstName, CAT.LastName, ASGN_TEMP.ShiftNum, ASGN.PayCode, IIf(ASGN_TEMP.[ShiftNum] Is Null,0,-1) AS IsSelected
FROM tblEmployees AS CAT
LEFT JOIN (SELECT ASGN.EmployeeID, ASGN.ShiftNum, ASGN.PayCode FROM tblAssignedEmployees AS ASGN
WHERE ASGN.ShiftNum = Forms!frmMainMenu![txtShiftNum]) AS ASGN_TEMP
ON CAT.EmployeeID = ASGN_TEMP.EmployeeID;
Paycode is a static table with an ID, a Paycode and a description and would only correspond with each record in "tblAssignedEmployee". That is to say, there is no relationship between the employee or the shift with what Paycodes are available, I'd just like a second table for ease of updates.
---EDIT---
Table: Employees
ID
EmployeeID
Firstname
LastName
1
1234
Bob
Jones
2
9999
Mary
Sue
Table: AssignedEmployees
ID
EmployeeID
ShiftNum
PayCode
1
1234
1
OT
2
9999
2
Regular
3
1234
2
OT
Table: PayCodes
ID
PayCode
Desc
1
Regular
Regular Pay
2
OT
Overtime

How to create/handle complex join conditions in Analysis Services "Tabular" Model between two SCD Type2 tables with 1:M relationship?

I am new to Analysis Services and working on a Tabular model design. Need experts advice/suggestion on the best approach and how to handle below scenarios.
Assuming, I have below two tables Department and Employee where I am storing data for different customers(tenants) and there’s one-to-many (1:M) relationship between these two tables (i.e. one department can have 1 or more employees)
However, let’s say both the tables are SCD Type 2 i.e. storing history with effective and termination dates. There are no constraints, indexes etc. created on these tables at database level.
Department table:
cust_id dept_id dept_name efctv_dt trmntn_dt Dept_key
1001 D1 IT 12-01-2018 12-31-9999 1001D1
1001 D2 HR 01-01-2019 12-31-9999 1001D2
1002 D3 Admin 02-01-2019 02-28-2019 1002D3
1002 D3 HR+Admin 03-01-2019 12-31-9999 1002D3
1002 D4 Finance 02-01-2019 12-31-9999 1002D4
Employee table:
cust_id emp_id emp_name dept_id efctv_dt trmntn_dt Emp_key
1001 E1 XYZ D1 01-01-2019 01-31-2019 1001D1
1001 E1 XYZ-A D1 02-01-2019 12-31-9999 1001D1
1001 E2 ABC D2 02-01-2019 12-31-9999 1001D2
1002 E3 AXBYCZ D3 03-01-2019 03-31-2019 1002D3
1002 E3 AXBYCZ D4 04-01-2019 12-31-9999 1002D4
1002 E4 DEFG D4 04-01-2019 12-31-9999 1002D4
Columns cust_id & dept_id can be concatenated together as a separate column in both the tables as a key field and used as a join between both the tables.
Department Key=Concatenate(department[cust_id], department[dept_id] )
Employee Key=Concatenate(employee[cust_id], employee[dept_id] )
Example key output values= 1001D1, 1001D2, 1002D3, 1002D4
Now let’s say we have following reporting requirements, i.e.
To filter on Date Ranges (in visualization) - assuming there's another date dimension table with all dates & hierarchy
1) When no specific date range or filter selected - show all current active employee & departments names (where, trmntn_dt = 12-31-9999)
So, expected output is:
Emp Name Dept Name
XYZ-A IT
ABC HR
AXBYCZ Finance
2) When reporting for a specific month example - Jan-2019 - show all employees & department names active as of that month.
So, expected output is:
Emp Name Dept Name
XYZ IT
3) When reporting for a specific Quarter example - Q1-2019 - show all employees & department names active as of that quarter.
So, expected output is:
Emp Name Dept Name
XYZ-A IT
ABC HR
AXBYCZ HR+Admin
However, the join condition in AS Tabular model with 1:M relationship between these two tables would fail because the rows are not unique in the Department table (rows for D3), which is on the one side of the relationship.
If you include efctv_dt or trmntn_dt also in the concatenated join condition in both the tables as key for joining i.e.
Department Key=Concatenate(department[cust_id], department[dept_id] ) & Concatenate(department[efctv_dt],””))
Employee Key=Concatenate(employee[cust_id], employee[dept_id] ) & Concatenate(employee[efctv_dt],””))
Example key output values= 1001D112-01-2018, 1001D201-01-2019…
However, though now the rows would be unique since we don’t expect same row twice on the same day (unless some ETL issues like process ran twice on the same day etc.)
AS tabular model doesn’t allow to create complex join/condition (like in SAP BO Universe) so that we could add below condition when joining these two SCD type 2 tables as below which might help solve some of the requirements.
dept.cust_id = emp.cust_id
And dept.dept_id = emp.dept_id
And ( calendar_date is between efctv_dt and trmntn_dt
Or
trmntn_dt = ’12-31-9999’
)
I think for creating/calculating any measure value is still doable with lots of examples available online on DAX, but what about with just the dimensional attributes ?
Is this the right approach ? How to handle these?

oracle - sql query select max from each base

I'm trying to solve this query where i need to find the the top balance at each base. Balance is in one table and bases are in another table.
This is the existing query i have that returns all the results but i need to find a way to limit it to 1 top result per baseID.
SELECT o.names.name t.accounts.bidd.baseID, MAX(t.accounts.balance)
FROM order o, table(c.accounts) t
WHERE t.accounts.acctype = 'verified'
GROUP BY o.names.name, t.accounts.bidd.baseID;
accounts is a nested table.
this is the output
Name accounts.BIDD.baseID MAX(T.accounts.BALANCE)
--------------- ------------------------- ---------------------------
Jerard 010 1251.21
john 012 3122.2
susan 012 3022.2
fin 012 3022.2
dan 010 1751.21
What i want the result to display is calculate the highest balance for each baseID and only display one record for that baseID.
So the output would look only display john for baseID 012 because he has the highest.
Any pointers in the right direction would be fantastic.
I think the problem is cause of the "Name" column. since you have three names mapped to one base id(12), it is considering all three records as unique ones and grouping them individually and not together.
Try to ignore the "Name" column in select query and in the "Group-by" clause.
SELECT t.accounts.bidd.baseID, MAX(t.accounts.balance)
FROM order o, table(c.accounts) t
WHERE t.accounts.acctype = 'verified'
GROUP BY t.accounts.bidd.baseID;

Merge rows with same username in SSIS

I have data origination from Active Directory in a flat file that i need to export to SQL server using SSIS. My challenge is that I want to do all the operations in SSIS and have the data that is exported into the database as the final output. My flat file has several rows bearing the same username that need to be combined into one row, and then concatenating the data in one column as in my illustration below:
Username Office LocationID Dept
-------- ---------- ---------- -----
1. btan HQ 01 Acct
2. cvill South 04 HR
3. cvill North 02 HR
4. btan East 03 Acct
5. cvill West 05 HR
6. lkays HQ 01 Legal
My output should be as follows and it should all be done using SSIS:
Username LocationID Dept
-------- ---------- -----
1. btan 01, 03 Acct
2. cvill 04, 02, 05 HR
6. lkays 01 Legal
Any help will be very much appreciate.
I support the prior suggestions that this is a bad data model, and I also support the SQL (non SSIS) solution. However if you must follow this path despite our warnings, take a look at the SSIS Pivot operator. You'll need to concatenate the resulting columns into one column.
Something like this will get you a comma delimited list of the IDs
SELECT Username, STUFF(IDList, 1,2,'') AS LocationID, Dept
FROM TableName T OUTER APPLY
(
SELECT ', ' + LocationID [text()]
FROM TableName
WHERE UserName = T.UserName
FOR XML PATH('')
) T2(IDList)