Count sequential field values based on date in seperate table using SQL - vba

I have been trying to devise an SQL query in Access 2010 to count the number of sequential field values which are based over 3 tables using fields with unique ID's
Example
Table1: Course
CorID Date
1 01/01/2012
2 01/03/2012
3 01/02/2012
Table 2: Delegate
DelID StaffID CorID Value CounterField
1 17263 2 99 1
2 17263 1 99 2
3 17263 3 99 3
4 17263 65 4 1
5 17263 44 5 1
6 17263 78 5 2
Table 3: Staff
StaffID Surname
1 Test
2 Smith
17263 Jones
The CounterField increases by 1 where the Value field in Table 2 is the same as the previous Value field. There would be a requirement to ensure that the count only uses the the order based on the course table Date field. The delegate table would also contain more DelID fields than listed and will contain different StaffID values as well.
The CounterField in the above table is an example of what I want the query to be able to do.
Is this possible? Thanks in advance.

If you want to count how many rows you have in Delegate that are not duplicate regarding the Value field:
SELECT count(*)
FROM Delegate
WHERE CounterField = 1

Try running a subquery:
SELECT a.DelID, a.StaffID, a.CorID, a.[Value],
(SELECT Count(*) FROM Delegate b
WHERE b.DelID <= a.DelID AND a.[Value]=b.[Value]) As CounterField
FROM Delegate a;

Related

How can I select a table skipping duplicated value postgreSQL

I have a table like this.
id
grade_1
grade_2
createdAt
1
1
1
20220304
2
1
1
20220301
3
4
2
20220228
I want to select the current row(in here, id=1) and a row where the grade's value is different with the row I selected.(in here, id=3)
Like This
id
grade_1
grade_2
createdAt
1
1
1
20220304
3
4
2
20220228
I tried to use subquery but it doesn't really worked for me. Is there any way to skip the duplicated value when selecting table?
You can just do it with group by and a max value to retieve the one you want
SELECT
grade_1,
grade_2,
Max(createdAt)
from
yourTable
Group by
grade_1,
grade_2

dynamically generate columns per date in sql

I have table where number of errors made by an employee is stored.
i_empid error_category error_count date
13 1 1 1-feb-2017
13 2 1 1-feb-2017
13 2 2 3-feb-2017
341 1 1 3-feb-2017
I want result set to group by error category for particular date
error_category error_count 1-feb-2017 2-feb-2017 3-feb-2017
1 2 1 0 1
2 0 1 0 2
How can I achieve this?
I am not sure what database you use, is it MySQL?
I think youre trying to do something like this:
select t.error_category, t.error_count, sum(IF(t.date='1-feb-2017',1,0)) as `1-feb-2017`,
sum(IF(t.date='3-feb-2017',1,0)) as `3-feb-2017`
from <yourTable> t
group by t.error_category
There are different ways to archieve this actually

SQL copy from one table to another with changing ID value

I have two tables:
A
ID VALUE
----------
1 7
2 5
3 44
4 982
5 1
6 0
7 671
B
ID VALUE
---------------
1 6
2 6
3 77
4 22
How do I copy data from #B to #A to get a different ID (one bigger than the MAX in #A)? For example I need to get
ID VALUE
1 7
2 5
3 44
4 982
5 1
6 0
7 671
8 6
9 6
10 77
11 22
Either make it an IDENTITY column which auto-increments, or this:
INSERT INTO A
SELECT b.ID + (SELECT MAX(ID) FROM A) AS ID, b.Value
FROM B
DEMO
The select is slightly different if the ID in table B has gaps. Then those gaps are transferred.
If the ID column in TableA is not already set to auto-increment, do the following command:
ALTER TABLE TableA MODIFY COLUMN ID INT auto_increment
Now you can just insert all the records from TableB into TableA:
INSERT INTO TableA (VALUE)
SELECT VALUE
FROM TableB
It is not a great idea to rely on the business logic in your query to maintain the order of the ID column. Instead, let SQL take care of it for you; it was designed for this purpose.

Matching two variables to create a new ID

I'm trying to create an SQL statement to match either an id number or a postcode and then assign a new id number
What I want to end up with is ‘newid’ that correctly recognizes that the first four records are the same person (even though the postcode for record 2 is different).
record id postcode newid
--------------------------
1 1 1 1
2 1 2 1
3 1 1 1
4 2 1 1
5 3 3 2
Any suggestions would be appreciated greatly.
Going based on your example:
SELECT RECORD,
(SELECT MIN (ID)
FROM users u2
WHERE users.id IN (u2.id, u2.postcode)
OR users.postcode in (u2.id, u2.postcode)
) AS newid
FROM users
This results with the following data:
RECORD NEWID
------------------
1 1
2 1
3 1
4 1
5 3
Here is the SQLFiddle

SQL - Aggregating data in a result set with identical rows and eliminating multiple rows based on one column's value

I have a table that has transactions by employeeID by TransactionTime. Each employee may have multiple transactions that occur at the same time. For example: EmployeeID 1 has 2 transactions at 12. I need to sum the transactions by EmployeeID at each time interval. So for employeeID 1, the new column (TotalTransactionsByTime) result would be 2. Next, if the CODE for a given TransactionTime has a CODE of BAD, I need to exclude all transactions at that time increment. So for EmployeeID 2, I would need to exclude all three transactions from the result set because they have a CODE of 'BAD' which nullifies all transactions at that increment.
MY TABLE
|EmployeeID|TransactionTime|CODE|
1 12 GOOD
1 12 GOOD
1 5 GOOD
2 1 BAD --need to omit all 3 transactions for employeeID 2
2 1 GOOD
2 1 GOOD
3 3 GOOD
3 3 GOOD
A correct result would look like:
|EmployeeID | TransactionTime | CODE | NUMBERTRNS
1 12 GOOD | 2
1 5 GOOD | 1
3 3 GOOD | 2
select mt1.EmployeeID, mt1.TransactionTime, mt1.CODE, count(*) as NUMBERTRNS
from MyTable mt1
where mt1.EmployeeID not in (select EmployeeID from MyTable where CODE = 'BAD')
group by mt1.EmployeeID, mt1.TransactionTime, mt1.CODE