Looping thru one table and assign ID to another - sql

I have table of Clients (id, clientName), and table of tasks (id, taskName).
Lets say that client table have about 200 rows and tasks about 700 000 rows.
I need to assign every client about 300 tasks every day (with Job).
My idea is to make third table ClientXrefTask (idxref, fkClient, fkTask, date), and every night to fill that table with data
(I need to have assign history so I choose third table)
Is there a better way to do this other then cursos (iterate thru clients, and insert in ClientXref table) ?

You can add fkClient and date to the tasks table if those tasks are not getting changed once assigned.

Related

MS Access - Counting Occurrences of a word in multiple columns

I have a database with a couple tables that tracks personnel errors that require rework by another person. Basically, a person on the job could rework up to 10 different work packages by other people throughout their shift. To make it easy, I just have columns in the table for rework_1/original_worker_1/rework_comment_1 (repeated up to 10) and the person who had to rework it. All of my worker's names are in a separate table so I can add people and my forms update dynamically with their names. What I want to do is this:
Pull a person from my worker's name table.
Search for all occurrences of their name in another table in in column original_worker_X (where X is 1 - 10).
Output the values: Workers Name / How Many Times I found it in the original_worker_X columns.
From here I would need to make a bar graph so that each person's name had a bar with how many times someone had to rework something they did originally.
If I could do this with PHP and MySQL I would be in the money because I could brute force something with some PHP variables, queries, and loops but I am an access novice at best! I appreciate any help you wizards can provide.
Table 1:
Table 2:
Expected Output Numbers:
so i will suggest you do the following
Create a new table,lets say table 3 with three fields
A. ID, pkey, auto number
B. original_worker, text field
C. Person_doing_rework, text field
You will need ten insert statements that will insert each of the original worker 1-10, as well as person doing re-work , this is to a normalise table
Currently, the design of your table is a bit crude, and having a select statement with group by columns numbering 10 is not achievable
Below are samples of the insert statements
INSERT INTO Table3 (original_worker,Person_doing_rework)
SELECT original_worker1,Person_doing_rework
FROM table2 where isnotNull(original_worker1)
INSERT INTO Table3 (original_worker,Person_doing_rework)
SELECT original_worker2,Person_doing_rework
FROM table2 where isnotNull(original_worker2)
replicate this for original_worker3 to original_worker10
Third step
You need a delete statement that will delete all from table 3, this is to ensure that the records from table 3 is not duplicated, since we don't have a pkey/fkey relationship between table 2 and 3
Fourth step
Place all the queries into a macro in the following order
A. Delete query to run first
B. Insert queries to run next
Fifth step
Add a msgbox in the macro, that will run last, this is to inform you that all the other macro steps, i.e A and B above has successfully run.
Sixth step
You can now have a select statement from table 3 that can count the number of times an original workers' work is re worked upon, because you now have two main fields in table 3, one for original_work, and two for Person_reworked.
So any time you want to find out how many times some ones work has been re worked upon, you have to just click the macro button, this will run all the queries and put values you need in the table 3, after which you can view the details via the query in step 6.
SELECT original_worker, Count(Person_doing_rework), FROM table3 GROUP BY original_worker;

How I check existing record and avoid redundant data

I have two tables, both are situated at different server and belongs to different schema.
Table 1 : SALARY_DETAIL_REPORT_[monthYearSuffix]-Same Replica of Table 2.
Table 2: XXMPCD_SALARY_DETAIL_TABLE-Contains employee's salary related data,each employee having multiple pay code like HOUSE RENT ALLOW,DEARNESS ALLOW, NPS-Company Contri(Earning), BASIC PAY, NET EARNING, GROSS DEDUCTION, GROSS EARNING, GROSS DEDUCTION, NET EARNING, BASIC PAY, GRADE PAY.
So Employee code repeated multiple times and hence we are not able to maintain primary key or unique index.
Suppose 1000 records pushed in TABLE 2 which I need to copy exactly in my TABLE 1. This is handle by my SPRING service class a GUI is available in which we simply click on migrate button and services running in background fetch the data from TABLE 2 and insert the same in the TABLE 1.
There is two column inside TABLE 2
1.PICK_DATE
2.IS_DATA_PICKED
If both of these data contain null that simply means that data is not migrated into our TABLE 1, we updated the same columns after successful migration by acknowledging, so the next time the data will not be available for migration.
PROBLEM
Now suppose we migrated the 1000 records from TABLE 2 to TABLE 1.
What I done, I go back to my SQL DEVELOPER, choose 3 random records and set their PICK_DATE & IS_DATA_PICKED to NULL.
Now I migrated the same another time and those 3 records inserted again.
That means replication of 3 records, 1000 records becomes 1003.
Now what I want to check:
If same data existed, than record should be updated not inserted, means overwrite.
TABLE 1:SALARY_DETAIL_REPORT_092018
SALARY_REPORT_ID
EMP_NAME
EMP_CODE
PAY_CODE
PAY_CODE_NAME
AMOUNT
PAY_MODE
PAY_CODE_DESC
YYYYMM
REMARK
EMP_ID
PRAN_NUMBER
PF_NUMBER
PRAN_NO
ATTOFF_EMPCODE
REFERENCE_ID
**
TABLE 2:XXMPCD_SALARY_DETAIL_TABLE
EMP_NAME
EMP_CODE
PAY_CODE
AMOUNT
PAY_MODE
PAY_CODE_NAME
YYYYMM
REMARK
PUSH_DATE
PICK_DATE
IS_DATA_PICKED
ERROR_MESG
REFERENCE_ID
PRAYAS_ERP_ORG_ID
ERP_ORG_ID
PF_NUM
PRAN_NO
VERIFIED_BY

How to add a column for each day in sql?

I'm trying to make a attendance management system for my college project.
I'm planning to createaone table for each month.
Each table will have
OCT(Roll_no int ,Name varchar, (dates...) bool)
Here dates will be from 1 to 30 and store boolean for present or absent.
Is this a good way to do it?
Is there a way to dynamically add a column for each day when the data was filled.
Also, how can I populate data according to current day.
Edit : I'm planning to make a UI which will have only two options (Present, absent) corresponding to each fetched roll no.
So, roll nos. and names are already going to be in the table. I'll just add status (present or absent) corresponding to each row in table for each date.
I would use Firebase. Make a node with a list of users. Then inside the uses make a attendance node with time-stamps for attended days. That way it's easier to parse. You also would leave room for the ability to bind data from other tables to users as well as the ability to add additional properties to each user.
Or do the SQL equivalent which would be make a table list of users (names and user properties) with associated keys (Primary keys in the user table with Foreign keys in the attendance table) that contained an attendance column that would hold an array of time-stamps representing attended days.
Either way, your UI would then only have to process timestamps and be able to parse through them with dates.
Though maybe add additional columns as years go so it wouldnt be so much of a bulk download.
Edit: In your case you'd want the SQL columns to be by month letting you select whichever month you'd like. For your UI, on injecting new attendance you'd simply add a column to the table if it does not already exist and then continue with the submission. On search/view you'd handle null results (say there were 2 months where no one attended at all. You'd catch any exceptions and continue with your display.)
Ex:
User
Primary Key - Name
1 - Joe
2 - Don
3 - Rob
Attendance
Foreign Key - Dates Array (Oct 2017)
1 - 1508198400, 1508284800, 1508371200
2 - 1508284800
3 - 1508198400, 1508371200
I'd agree with Gordon. This is not a good way to store the data. (It might be a good way to present it). If you have a table with the following columns, you will be able to store the data you want:
role_no (int)
Name (varchar)
Date (Date)
Present (bool)
If you want to then pull out the data for a particular month, you could just add this into your WHERE clause:
WHERE DATEPART(mm, [Date]) = 10 -- for October, or pass in a parameter
Dynamically adding columns is going to be a pain in the neck and is also quite messy

MSSQL give new primary key in table 1 and update corresponding keys in table 2

I have a product and product_detail table pair that I need to copy the data from and update the primary key. Essentially what I'm trying to do is copy last night's data from both tables, give them new primary keys so they don't clash with the current data and insert back into the table with some of the information updated - pk/fk, update_date, and two fields flagged as something different.
I can't make changes to the tables, so I can't use update cascade. We have a file that does an End of Day batch and inserts the data into the tables. We also have a file that is updated every time a transaction happens during the day, so what I thought I could do is copy last nights data, update the keys so they didn't clash, and update that new set of data with whatever comes in from the file during the day. The way it is now, our users have to wait until the end of the day to see where we are. With updating during the day, they can see the balance as the day progresses.
I believe I will have to grab the information out of the main table, get a new pk, update the other info, and pass that new pk to the second table to replace the original fk it has and do it row by row.
Am I heading in the right direction?

Automatically creating new rows based on a value

I am trying to create a number of rows in a table based on the value of record in a different table.
i.e Table A has two columns NumberID, Number and TableB has MonthID, Month, Amount
When TableA.Number gets a value of say 5 I want TableB to be updated with 5 new rows. The values of TableB.Month and TableB.Amount will then be entered manually. Can this be done?
Depending upon what database you are using, this can be done using triggers.
Alternately, you could do this in the application layer.