Access Event Driven Macro - ms-access

Table1:
ID Name XCount
1 Bob 12
2 Jack 13
Table2:
ID XCount YCount
1 14 1
2 22 15
When i insert a new record (on this example as a third record) to the Table1 then;
if that third record's XCount value is equal to some row's XCount Field on the Table2;
Than that rows YCount field will be updated as ycount=ycount+1 on the Table2.
Input: Table 1
ID Name XCount
3 George 14
Output: Table2:
Id Xcount YCount
1 14 2
2 22 15
I tried a couple of times with "After Insert Macro" but can't handle it. Can anyone help me to overcome this situation?

Related

Swap column values across 2 different tables

I have two tables and need to swap the values of a column in each table - I can do this when they are in the same table but when I try to do this with different tables then the second value is already overwritten so gets lost.
For example:
table1
id user_id currency col2 col3......
1 1 10 Bob 2018-04-16
2 2 150 Tom 2018-05-17
3 3 60 Phil 2018-06-04
4 4 125 Jon 2017-12-01
5 5 35 Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 USD 16 Active
2 2 USD 17 Active
3 3 GBP 21 Left
4 4 CAD 16 Active
5 5 AUD 19 Active
I need these to look like:
table1
id user_id currency col2 col3......
1 1 USD Bob 2018-04-16
2 2 USD Tom 2018-05-17
3 3 GBP Phil 2018-06-04
4 4 CAD Jon 2017-12-01
5 5 AUD Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 10 16 Active
2 2 150 17 Active
3 3 60 21 Left
4 4 125 16 Active
5 5 35 19 Active
I tried:
UPDATE table1 t1, table2 t2
SET t1.currency=t2.salary, t2.salary=t1.currency
WHERE t1.user_id=t2.user_id;
but this does not work (currency gets set correctly but not the salary), is it possible to do?
Swap two columns values between two tables looked like a possible solution but the solution is changing table names as all the columns need swopped whereas I only need single columns swapped.
I believe you'll need to use a mix of both DDL and DML to do this.
First off you'll need to rename one of the columns to be swapped and add a column to hold the new value:
alter table table1 change currency salary int;
alter table table1 add currency varchar(3) after salary;
then update each table independently:
update table1 t1, table2 t2
set t1.currency = t2.salary
where t1.user_id = t2.user_id;
update table1 t1, table2 t2
set t2.salary = t1.salary
where t1.user_id = t2.user_id;
and finally remove the extra column:
alter table table1 drop salary;

I'm trying to create dynamic SQL in MySQL

I use MySql and I have some problems with it ^^
Let's say I have two tables which will be "FirstTable" and "SecondTable"
First table (ID isn't the primary key)
ID IdCust Ref
1 300 123
1 300 124
2 302 345
And the second (ID isn't the primary key)
ID Ref Code Price
1 123 A 10
1 123 Y 15
2 124 A 14
3 345 C 18
[EDIT] The column "Stock" in the final result is equals to "ID" in the second table
The column "Stock", "Code" and "Price" can have x values, so I don't know it, in advance...
I make some research in stackoverflow, but I only find post where people use "count(case when ..."
Like this one : MySQL pivot row into dynamic number of columns
For my problem, I cannot use it, because i can't know in advance the value of reference
I'm trying to produce the following output:
The result I want
[EDIT] Result in TXT
ID IdCust Ref StockA Code1 Price1 StockB Code2 Price2
1 300 123 1 A 10 1 Y 15
1 300 124 2 A 14
2 300 345 3 C 18

Is it possible to display this data horizontally in SSRS?

I have a table that that is being joined by another table that looks like this:
Id Score Total
1 10 30
1 7 30
1 13 30
2 14 27
2 10 27
2 3 27
I want to be able to display this data like this in SSRS:
Id 1 2 3 Total
1 10 7 13 30
2 14 10 3 27
Can this be done and how?
You can do this by using a matrix.
You can add a row identifier for each id in your dataset (assuming you can modify the dataset, as you joined 2 tables). Below code is for SQL Server (T-SQL).
Select Id, Score, row_number() over (partition by id order by score) ident
from table
Output:
Id Score Ident
1 10 1
1 7 2
1 13 3
2 14 1
2 10 2
2 3 3
No need of the Total field, you can add it in matrix (Right Click on ColumnGroup>Add Total>After).
Use the above query in Matrix as shown below.

Get the maximum field value of different row's between different tables and its id's

So we got four tables T1,T2,T3,T4
Each table has four columns, id,1,x,2 (id is primary key auto incremented in all tables) and all of them are populated with numbers (40-50 rows each).
How can I compare the first field of each table and column (and same for all the remaining fields) and display the maximum field for each column and its id?
For example, compare column 1/field 1 of T1 with column 1/field 1 of T2 and column 1/field 1 of T3 display the maximum and its id and then do the same for column 2/field 1 OF T1/T2/T3/T4/ etc etc and each of their respective fields for all 40 rows ?
I have read about UNION and JOINS but I don't know how to do this.
Any help would be appreciated.
TABLE 1
ID | 1 | X | 2 |
1 10 20 30
2 5 45 6
3 3 11 12
4 0 14 23
TABLE 2
ID | 1 | X | 2 |
1 100 200 300
2 50 405 60
3 30 101 102
4 0 104 203
ETC ETC
we need to compare the 10 of table 1 column 1 row 1 id=1 with 100 of table 2 row/column/id=1 then the same for x and 2 column.

how to find overlapping record with between date

I am using MySQL, I have following table structure
Id id2 classId sectionId validFrom validTill
------------------------------------------------------
1 1 5 13 2016-01-01 2016-03-30
2 1 5 22 2016-01-15 2016-03-30
3 1 5 23 2016-01-15 2016-04-29
4 1 5 13 2016-04-01 2016-04-30
9 10 6 24 2016-01-17 2016-02-05
10 10 6 25 2016-01-23 2016-02-05
11 10 6 24 2016-01-31 2016-02-05
My SQL statement is
SELECT count(*) as timeCount FROM TimeTableClassSection a
WHERE classId=5 AND sectionId=13 AND ((a.ValidFrom BETWEEN '2016-01-18' AND '2016-01-24') OR (a.ValidTill BETWEEN '2016-01-18' AND '2016-01-24'))
Its returning timeCount = 0. But it should return 1 as record with Id=1 falls between this date range ('2016-01-18' AND '2016-01-24')
I am trying to achieve, find out any overlapping record for particular classId & sectionId between provided date range.
If classId=5 and sectionId=13 has validFrom=2016-01-01 validTill=2016-03-30 exist, then any date range between this date range ('2016-01-18' AND '2016-01-24') should throw this record as count.
If I give date range 2015-12-25 to 2016-09-20 then record count should = 1
If I give date range 2016-2-1 to 2016-02-20 then record count should = 1
If I give date range 2016-2-1 to 2016-09-20 then record count should = 1
What wrong I am doing here ... all date format is in YYYY-MM-DD
You are only checking if the boundaries are within the date range, but you do not check whether the data range is within the boundaries. You should extend your where criteria:
...AND ((a.ValidFrom BETWEEN '2016-01-18' AND '2016-01-24')
OR (a.ValidTill BETWEEN '2016-01-18' AND '2016-01-24')
OR (a.ValidFrom<'2016-01-18' AND a.ValidTill>'2016-01-24'))