Microsoft SQL create a summary table based on other group of tables - sql-server

My question is a bit simple, there are many answers to it but I have a question more about the query itself for certain conditions.
I have a table like this :
Client | Date | Employee | Last Record | Trained
JOE | April 2020 | John Doe | May 2019 | TRUE
JOE |February 2020| John Doe | May 2019 | TRUE
JOE | May 2 019 | John Doe | May 2019 | FALSE
Now I watn to make a simple SQL summary table saying :
Client | Date | Inactive | Trained
JOE | April 2020 | 1 | 1
JOE |February 2020 | 1 | 1
JOE | May 2019 | 0 | 0
So basically do a count of Employees grouped by client and date, with the condition that the difference of date and last record is greater than, lets say 1 month and also in another column count the number of employees with a TRUE condition.
So my question is basically that, hwo would I go about creating a summary table where I want to set conditions per column, such as a date difference or if its true in a column.
Before you say Use a view, I need to create this table for performance reason since I am querying the first table which has millions of rows for a report program. However it is simple and better to query instead a table that holds a summary or counts with conditions.

Related

SQL Server - same table for multiple customers

I have a need to manage a dataset for multiple customers - each customer manages a small table to update procedure volumes for the next five years. The table is structured like so:
+-------------+--------+--------+--------+--------+--------+
| | Year 1 | Year 2 | Year 3 | Year 4 | Year 5 |
+-------------+--------+--------+--------+--------+--------+
| Procedure A | 5 | 10 | 14 | 12 | 21 |
+-------------+--------+--------+--------+--------+--------+
| Procedure B | 23 | 23 | 2 | 3 | 4 |
+-------------+--------+--------+--------+--------+--------+
| Procedure C | 5 | 6 | 7 | 8 | 12 |
+-------------+--------+--------+--------+--------+--------+
The values in this table will be managed by each customer via MS PowerApps.
This same structure exists for every single customer. What is the best way to put all of these in one dataset?
Should I just add a column for CUSTOMER ID and just put all the data in there?
The process:
Utilizing PowerApps, a new customer deal will be generated and a row will be added for them in the SQL DB in a customer records table.
Simultaneously, the blank template of the above table should be generated for them.
Now, the customer can interface with this SQL table within PowerApps and add their respective procedure volumes.
The question isn't explained well but:
I would assume all of the customer specific data has at least one column that is the same. For instance CustomerName. You could create your own table with CustomerId, CustomerName, (any other fields you would like to see). If there isn't a concept of CustomerId on the customer's tables, you would have to join them on CustomerName. You could populate your own CustomerId for the new table.
I would be happy to help more if you could clarify the question and show a few examples.

Issue with new table in oracle

I have to add a new table according to some new requirements, the model currently consists of two tables: DETAIL and SUMMARY.
The relation is that every detail has associated one summary, so now I need to add a new table called SUMMARY_ESP, which has the a FK ( SUMMARY) and two more columns, something like this:
ID | SUMMARY_ID | ESP_ID | PRIORITY_ESP | PTY_ID | PRIORITY_PTY
1 | 123 | 34 | 1 | 122 | 1
2 | 123 | 35 | 2 | 111 | 2
3 | 123 | 30 | 3 | null | null
4 | 1111 | 34 | 4 | null | null
Other tables info:
DETAIL TABLE
ID_DET | AMOUNT | DATE | ID_SUMMARY | EXTERNAL_ID
1 | 1000 | 14/05/2018 | 1111 | 4
2 | 2000 | 18/07/2016 | 1111 | 4
3 | 1200 | 11/07/2017 | 123 | 1
4 | 1300 | 21/09/2018 | 123 | 2
SUMMARY TABLE
ID_SUMMARY | PRIORITY| PROFILE | CLASS | AREA
123 | 1 | 1 | 5 | 3
1111 | 2 | 1 | 5 | 3
33 | 3 | 2 | 5 | 9
4 | 4 | 8 | 5 | 10
So according to this, SUMMARY_ID , ESP_ID and PTY_ID are unique, the thing is at some point to know the what is the ESP_ID of certain detail, but since the relation is with SUMMARY table, I have no idea which one was when it was added, so I was asked to create a new column to the DETAIL table called EXTERNAL_ID, so I can know what is the code from the SUMMARY_ESP.
So if the row is the first one, it can be either 24 or 122 in the new column according to some previous logic, but I'm worried about the implications this might have in the future, because somehow I might be duplicating information, also I would need to make some weird logic in order to get the priority depending on whether it's ESP_ID or PTY_ID.
The new table along with SUMMARY are somehow parameters table, their values do not change that often and only the PRIORITY column would change, DETAIL instead is more transactional, and it has insert and update everyday according to some business logic.
I was thinking of adding the ID of the new table as a FK to the DETAIL table, but at the end would be the same, because it'll be hard to maintain and update would be harder, also it's like a circular dependency, so I'm kind of stuck with this , so any kind of help would be really helpful, below the complete model, with the current idea.
Also I can't add those new columns to the table SUMMARY, because there could be more than one associated to the same code in that table and since it's the PK I cant add two rows with the same code.
The relation is that every detail has associated one summary
You need to represent that relationship in your database layout : if you have a 1-N relationship between SUMMARY and DETAIL, you want to create another column in DETAIL that holds the primary key of the SUMMARY record that it is related to.
With this relation in place, you can start from a DETAIL row, relate a row from SUMMARY and identifiy all SUMMARY_ESP records that are linked to it.
Now if you need to uniquely relate a DETAIL record to a SUMMARY_ESP record, then you want to either add a foreign key to SUMMARY_ESP in DETAIL, or the other way around (add a foreign key to DETAIL in SUMMARY_ESP), depending on the way your data flows.

Constraint to prevent overlapping time periods

Temporal tables and time periods are nothing special in the IT world. But somehow it seems my request is rather unique because I cannot find anything useful for it at all.
What I have are two tables, one containing static data and the other one dynamic data for entries of the first table which changes over time. For each row in the second table there are two columns ValidFrom and ValidUntil whereas the latter can be null if there is no planned end of validity.
Simplified, the schema looks like this:
tbStatic
+----+------------+------------+
| Id | Attribute1 | Attribute2 |
+----+------------+------------+
| 1 | foo | bar |
| 2 | baz | foo |
+----+------------+------------+
tbDynamic
+----+------------+---------------------+---------------------+------------+------------+
| Id | tbStaticId | ValidFrom | ValidUntil | Attribute1 | Attribute2 |
+----+------------+---------------------+---------------------+------------+------------+
| 1 | 1 | 2018-01-01 00:00:00 | 2018-01-31 23:59:59 | 1 | 0 |
| 2 | 2 | 2018-04-01 00:00:00 | 2018-04-02 11:59.59 | 2 | 1 |
| 3 | 1 | 2018-02-01 00:00:00 | null | 2 | 1 |
| 4 | 2 | 2018-05-01 00:00:00 | 2018-06-01 00:00:00 | 23 | 15 |
| 5 | 2 | 2018-07-01 01:23:45 | 2018-07-05 23:12:01 | 80 | 12 |
+----+------------+---------------------+---------------------+------------+------------+
As you might have spotted, there is the possibility that we have holes between time periods. What we cannot have is overlapping periods, though. This means it is impossible to have overlapping time periods for the same tbStaticId.
Unfortunately, this is only a requirement until now and although it is enforced in the application using the database, I would prefer having a constraint on the table that prevents new rows to be inserted or existing rows to be updated when they violate this time uniqueness.
As stated, my research up to this point was rather disappointing and that is also the reason I cannot really show any code I've tried yet. The most promising approach I followed yet was to create a function that takes a record or the two time period values and the foreign key as input and determines if they overlap with something else. This function could then be called in a check constraint. But after thinking about the amount of cases to check, I gave up because it seemed unreasonable (especially when considering updates as well, which require additional attention).
So my question is if there is some easy way to constraint time slices in SQL Server, without the use of temporal tables (not available in my SQL Server version)? And if yes, how?

SQL Server - Multiple Identity Ranges in the Same Column

Yesterday, I was asked the same question by two different people. Their tables have a field that groups records together, like a year or location. Within those groups, they want to have a unique ID that starts at 1 and increments up sequentially. Obviously, you could search for MAX(ID), but if these applications have a lot of traffic, they'd need to lock the entire table to ensure the same ID wasn't returned multiple times. I thought about using sequences but that would mean dynamically creating a sequence for each group.
Example 1:
Records created during the year should increment by one and then restart at 1 at the beginning of the next year.
| Year | ID |
|------|----|
| 2016 | 1 |
| 2016 | 2 |
| 2017 | 1 |
| 2017 | 2 |
| 2017 | 3 |
Example 2:
A company has many locations and they want to generate a unique ID for each customer, combining a the location ID with a incrementing ID.
| Site | ID |
|------|----|
| XYZ | 1 |
| ABC | 1 |
| XYZ | 2 |
| XYZ | 3 |
| DEF | 1 |
| ABC | 2 |
One trick that is often under-used is to create a clustered index on Site / ID or Year / ID - BUT Change the order of the ID column to Desc rather than ASC.
This way when you need to scan the CI to get the Next ID value it only needs to check 1 row in the clustered index. I've used this on Multi-Billion Record tables and it runs quite quickly. You can get even better performance by partitioning the table by Site or Year then you'll get the added benefit of partition elimination when you run your MAX(ID) queries.

How to negate or minimize repeating database records(rows) with several "many to many" relationships

I am pretty new to database development and architecture. My only experience has been in college and now my project requires me to use that knowledge, however my project seems a lot more complicated with many more intricacies than what I studied.
A brief overview: My task is to basically turn paper work that was previously done by hand, into a quick computer application, which I will do in Java but thats far off now. I know I will need a database set up to accomplish my task since these reports are frequently edited. The report is a Labor Report. Basically, it shows who was working on a specific job, what days and how many hours on those days, as well as their total hours, pay rate, and total amount.
I believe my current problem lies within the fact that it seems like I'm going to have several "many to many" relationships, perhaps even nested, which is what is throwing my head for a spin as I try to organize information into entity relationship diagrams and tables. (I know that there are normally much more measured and organized stages to development but I don't have that experience and I'm essentially a one man team on this)
Contract Personnel with be selected out of a pool of Employees.
A Labor Contract can have 1 to 10 personnel (For sake of space on the final printed version, jobs requiring more laborers will have another Labor Contract.)
Each personnel must have 1 Title (foreman, mechanic, etc.) These titles can change from job to job. Joe Smith can be a mechanic on job A but a foreman on job B.
Each personnel must also have on record the number of hours they worked on each day of the week; and may have overtime and double overtime. (One Labor Record per week).
I am trying to avoid repeated data, or at least keep it to a minimum but I am struggling on figuring out how to do that in this situation. The tricky thing, at least in my mind, is figuring out how to handle the fact that different employees can work several jobs at once, under different titles, and different pay rates, and recording different types of hours (straight time, OT, double OT) on each day of the week.
Can anyone make suggestions?
I hope that I have supplied adequate information and apologize if I didn't or wasn't detailed enough. Please remember to keep in mind I'm a newbie to this type of work.
First thing, take a deep breath! It looks to me like you have a pretty good handle on this, maybe more than you think! This is not at all to try and design your project, and I'm sure you'll have lots of details to deal with, but maybe this will give an idea of how you might face these many many-to-many relationships swimming around in your head.
EMPLOYEES
---------
emp_id
emp_name
emp_address
JOBS
----
job_id
job_description
EMPLOYEE_JOBS
-------------
ej_id -- primary key
emp_id -- fk to employees table
job_id -- fk to jobs table
ej_title -- employee title for this job
ej_rate -- employee pay rate for this job
EMPLOYEE_JOB_HOURS
------------------
ejh_id -- primary key
ej_id -- fk to employee_jobs table
ejh_date
ejh_normal_hours -- hours worked by the employee on this job on this date, etc.
ejh_overtime_hours
ejh_double_overtime_hours
Following is a basic outline you could use to get started. Your final solution will be different based on your exact needs.
You'll need a table to store contract information. My example just shows a description but I'm sure you'll have much more than that.
contracts
id unsigned int(P)
description varchar(50)
+----+-------------+
| id | description |
+----+-------------+
| 1 | Contract A |
| 2 | Contract B |
| .. | ........... |
+----+-------------+
You'll need a table that links contracts and employees and shows what title the employee has for the given contract. In my example you can see that for Contract A John Q Public is a Foreman and Mary Jane Smith is a Mechanic. For Contract B their titles are reversed, John is a Mechanic and Mary is a Foreman. contract_id and employee_id are foreign keys to their respective tables and together they form the primary key. If it's possible that John and Mary get paid different rates for the same title (for example John get 25.00/hour as Foreman while Mary gets 20.00/hour) you would add a column here instead of using the rate in the titles table.
contracts_employees
contract_id unsigned int(F contracts.id)--\_(P)
employee_id unsigned int(F employees.id)--/
title_id varchar(15)(F titles.id)
+-------------+-------------+----------+
| contract_id | employee_id | title_id |
+-------------+-------------+----------+
| 1 | 1 | Foreman |
| 1 | 2 | Mechanic |
| 2 | 1 | Mechanic |
| 2 | 2 | Foreman |
| ........... | ........... | ........ |
+-------------+-------------+----------+
You'll need a table for employees (you could call this personnel if you prefer). You'll probably store a lot more than just their names...
employees
id unsigned int(P)
first_name varchar(30)
middle_name varchar(30)
last_name varchar(30)
...
+----+------------+-------------+-----------+-----+
| id | first_name | middle_name | last_name | ... |
+----+------------+-------------+-----------+-----+
| 1 | John | Quincy | Public | ... |
| 2 | Mary | Jane | Smith | ... |
| .. | .......... | ........... | ......... | ... |
+----+------------+-------------+-----------+-----+
You'll need a table to track hours worked. I just store a beginning and ending date/time, leaving it up to the application to calculate elapsed time. Your application will also need to ensure there is no overlap for employees - an employee should not be able to be working on more than one contract at any given time. Calculation of overtime and double overtime hours is also up to your application. If an employee's pay rate can change at any time (ie in the middle of a contract) you would want to store the pay rate in this table instead of using the rate from contracts_employees or titles.
hours
id unsigned int(P)
contract_id unsigned int(F contracts.id)
employee_id unsigned int(F employees.id)
beg datetime
end datetime
+----+-------------+-------------+---------------------+---------------------+
| id | contract_id | employee_id | beg | end |
+----+-------------+-------------+---------------------+---------------------+
| 1 | 1 | 1 | 2014-01-01 08:00:00 | 2014-01-01 17:00:00 |
| 2 | 1 | 2 | 2014-01-01 09:00:00 | 2014-01-01 17:30:00 |
| 3 | 1 | 1 | 2014-01-02 09:00:00 | 2014-01-02 10:00:00 |
| 4 | 1 | 2 | 2014-01-02 08:00:00 | 2014-01-02 09:00:00 |
| 5 | 2 | 1 | 2014-01-02 10:00:00 | 2014-01-02 17:30:00 |
| 6 | 2 | 2 | 2014-01-02 09:00:00 | 2014-01-02 15:00:00 |
| .. | ........... | ........... | ................... | ................... |
+----+-------------+-------------+---------------------+---------------------+
And finally a table to store titles and their related pay rates. If employees can be paid different rates for the same title, you wouldn't need the rate column here, instead you would use the rate stored in the contracts_employees table.
titles
id varchar(15)(P)
rate double
+----------+-------+
| id | rate |
+----------+-------+
| Foreman | 20.00 |
| Mechanic | 15.00 |
| ........ | ..... |
+----------+-------+

Resources