MS ACCESS insert records base on quantity number - winforms

I have 2 table and the relation is [Booking_Num]
table A: [ID],[Booking_Num] [Prod_Name],[Qty_Order]
table B: [ID],[Booking_Num],[Prod_Name],[Date_Out]
when I get insert to table A, should be the table B get many record base on Qty_Order..?
sample:
table A
ID | Booking_Num | Prod_Name | Qty_Order |
01 | BOK001 | Hammer | 4 |
table B
ID | Booking_Num | Prod_Name | Date_Out |
01 | BOK001 | Hammer | Null |
02 | BOK001 | Hammer | Null |
03 | BOK001 | Hammer | Null |
04 | BOK001 | Hammer | Null |
Thanks

Related

Pivoting Data in SQL and Updting it into Destination Table

I am working on a Store Procedure where I need to update records from from table 1 into table 2.
Table1: contains the following 4 columns
----------------------------------------------
| ID | TableName | ColumnName | Value |
----------------------------------------------
| 1 | [UserDetails] | [FinalScore] | 92 |
| 2 | [UserDetails] | [FinalScore] | 89 |
| 3 | [UserDetails] | [FinalScore] | 65 |
| 4 | [UserDetails] | [FinalScore] | 91 |
| 5 | [UserDetails] | [FinalScore] | 76 |
| 1 |[EmployeeDetail]| [ScoreData] | 0.91 |
----------------------------------------------
UserDetails table
-----------------------------------------------------------
| UserID | UserName | ContactNo | FinalScore |
-----------------------------------------------------------
| 1 | John G | +13288992342 | 0 |
| 2 | Leonard J | +14581232342 | 0 |
| 3 | Max R | +17123992342 | 0 |
| 4 | Robert H | +15688992122 | 0 |
| 5 | Jimmy L | +1328996782 | 0 |
-----------------------------------------------------------
Need to load all the data from table1 into the corresponding destination table for large amount of data (30,000 to 60,000) records.
Table1 contains the ID (from ID Column in table1) and FinalScore (from ColumnName Column in table1) in the destination table (from TableName Column in table1) where the value (from Value Column in table1) needs to be loaded.
End Result of UserDetails table after SP execution :
-----------------------------------------------------------
| UserID | UserName | ContactNo | FinalScore |
-----------------------------------------------------------
| 1 | John G | +13288992342 | 92 |
| 2 | Leonard J | +14581232342 | 89 |
| 3 | Max R | +17123992342 | 65 |
| 4 | Robert H | +15688992122 | 91 |
| 5 | Jimmy L | +1328996782 | 76 |
-----------------------------------------------------------
I am not sure how to load the data from "table1" into destination table (userdetails table) for bulk data as update query in a loop is taking very long time to complete.
It's not pivot what you're after but a simple join:
select
a.userid, a.contactno, b.finalscore
from UserDetails a
join table1 b on a.id = b.id
alternatively, if you want to update the old table without creating a new one, you can update join
SQL update query using joins

Can recursion start from a specific record in a table?

I'm trying to calculate depreciation on vehicles. If there is a rebate on a vehicle, I need to stop the depreciation, factor in the rebaste based on the month it look affect, and resume the depreciation calculation.
A vehicle depreciates at a flat rate of 2% every month with 50 months being the point of 100% depreciation. When a rebate appears, I can stop the depreciation, but I don't know how to make it start again from a certain month.
Below is an example of the table's deprecation up to directly before the rebate:
+----------+-------+------------+--------------+------------+------------+
| Vehicle# | month | depDate | Initial Cost | Monthlydep | totaldep |
+----------+-------+------------+--------------+------------+------------+
| 12451 | 1 | 2015-08-01 | 44953.24 | 899.06 | 899.0648 |
| 12451 | 2 | 2015-09-01 | 44953.24 | 899.06 | 1798.1296 |
| ------- | ----- | ----- | ----- | ----- | ----- |
| 12451 | 42 | 2019-01-01 | 44953.24 | 899.06 | 37760.7216 |
| 12451 | 43 | 2019-02-01 | 44953.24 | 899.06 | 38659.7864 |
+----------+-------+------------+--------------+------------+------------+
Then let's say that a rebate comes in this month (2019-03-01) it needs to be factored in and then the depreciation needs to be recalculated from that month onwards the. How do I restart the depreciation from month 43 instead of it going through everything?
For example let's say that we get a rebate in month 44 for $200 dollars. The table should look like something below:
+----------+-------+------------+--------------+------------+------------+
| Vehicle# | month | depDate | Initial Cost | Monthlydep | totaldep |
+----------+-------+------------+--------------+------------+------------+
| 12451 | 43 | 2019-02-01 | 44953.24 | 899.06 | 38659.7864 |
| 12451 | 44 | 2019-03-01 | 44953.24 | 1099.06 | 39758.8464 |
| 12451 | 45 | 2019-04-01 | 44953.24 | 1099.06 | 40857.9064 |
| 12451 | 46 | 2019-05-01 | 44953.24 | 1099.06 | 41956.9664 |
| 12451 | 47 | 2019-06-01 | 44953.24 | 1099.06 | 43056.0264 |
| 12451 | 48 | 2019-06-01 | 44953.24 | 1099.06 | 44155.0864 |
| 12451 | 49 | 2019-06-01 | 44953.24 | 1099.06 | 45254.1464 |
+----------+-------+------------+--------------+------------+------------+
So month 49 would be the final month because the totalDep is equal to or higher than the initial cost
My sample code is below. If you remove the first cte and the join inner join in the top part of the union then that is the working depreciation calculation:
;With cte As( Select bd.[VehicleID]
,Max(bd.[Month]) As month
,Max(DateAdd(DAY,1,EOMONTH(DepreciationReportDate,-1))) As DepreciationReportDate
,Max(bd.MonthlyDepreciation) As MonthlyDepreciation
,Max(bd.AdjustedPurchaseCost) As AdjustedPurchaseCost
,Max(AccumulatedDepreciation) As AccumulatedDepreciation
From Work.dbo.DepreciationSchedule bd
Group By bd.VehicleID
)
,cte_CreateRows As
(
Select bd.[VehicleID]
,bd.[Month]
,DATEADD(DAY,1,EOMONTH(bd.DepreciationReportDate,-1)) As DepreciationReportDate
,bd.MonthlyDepreciation
,bd.AdjustedPurchaseCost
,bd.AccumulatedDepreciation
From Work.dbo.DepreciationSchedule bd
Inner Join cte cte
On cte.VehicleID = bd.VehicleID
And cte.month = bd.Month
Union All
Select bd.[VehicleID]
,[Month] = Cast(cr.[Month]+1 As int)
,DATEADD(DAY,1,EOMONTH(DateAdd(Month, 1, cr.DepreciationReportDate),-1)) As DepreciationReportDate
,bd.MonthlyDepreciation
,bd.AdjustedPurchaseCost
,AccumulatedDepreciation = cr.AccumulatedDepreciation + cr.MonthlyDepreciation
From Work.dbo.DepreciationSchedule bd
Inner Join cte_CreateRows cr On bd.[VehicleID] = cr.[VehicleID]
Where cr.AccumulatedDepreciation < cr.AdjustedPurchaseCost
And DateAdd(Month,1, DateAdd(DAY,1,EOMONTH(cr.DepreciationReportDate,-1))) < DATEADD(DAY,1,EOMONTH(GetDate(),-1))
)
Select a.VehicleID
,a.Month
,a.DepreciationReportDate
,Cast(a.MonthlyDepreciation As Decimal(12,2)) As 'Monthly Depreciation Expense'
,a.AdjustedPurchaseCost
,a.AccumulatedDepreciation
From [cte_CreateRows] As a
Order By a.VehicleID, a.Month

Alter a column with data to an existing table

I have an SQL table illustrated below but with much more rows (around 500):
Current Table
+----------------+--------+
| EmployeeID | EMP |
+----------------+--------+
| 01 | val |
+----------------+--------+
| 02 | val |
+----------------+--------+
| 03 | val |
+----------------+--------+
| 04 | val |
+----------------+--------+
and an following Excel sheet with the same data except the additional OtherID column:
Excel Sheet
+------------+----------------+--------+
| OtherID | EmployeeID | EMP |
+------------+----------------+--------+
| 001 | 01 | val |
+------------+----------------+--------+
| 002 | 02 | val |
+------------+----------------+--------+
| 003 | 03 | val |
+------------+----------------+--------+
| 004 | 04 | val |
+------------+----------------+--------+
How can I update the original table (without deleting and creating a new one) by adding the missing OtherID column and make sure that the OutherID is related to the EmployeeID?
Desired result in SQL
+------------+----------------+--------+
| OtherID | EmployeeID | EMP |
+------------+----------------+--------+
| 001 | 01 | val |
+------------+----------------+--------+
| 002 | 02 | val |
+------------+----------------+--------+
| 003 | 03 | val |
+------------+----------------+--------+
| 004 | 04 | val |
+------------+----------------+--------+
Thanks.
1) Insert Excel Sheet Data to Dummy Table. (ex: BulkInsert)
2) Use the next code:
Alter table Originaltable
ADD OtherID datatype;
go
UPDATE Originaltable
SET Originaltable.OtherID = DummyTable.OtherID
FROM OriginaltableINNER JOIN DummyTable
ON Originaltable.id = DummyTable.id

Group By with Multiple Unique Constraints Condition

I need to fetch data from below mentioned table Sizes where data will be unique by Code,ArtId and Size OR unique By Code,ArtId and SizeIndex (i.e There are two unique constraints).How I can get unique records in single select statement using group by .
ArtId | SizeIndex | Size | Description | Code
001 | 000000000001111 | X | TEST | 01
002 | 000000000001111 | XL | NULL | 02
003 | 000000000001111 | L | NULL | 03
004 | 000000000009999 | SL | TEST2 | 04
005 | 000000000009999 | ML | LIGHT | 05
006 | 000000000009999 | M | Filter element,Air | 06
Your help will be highly appreciated
SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE FROM
(SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE
FROM TABLE GROUP BY CODE,ARTID,SIZE
UNION
SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE
FROMM TABLE GROUP BY CODE,ARTID,SIZEINDEX)A

Access db loop - for each record in one table create array of records in another table

Is it possible to create a nested looping query in Access DB that will update a third table?
I have a master (header) table:
------------------------
masters
------------------------
num | modality | cost |
------------------------
01 | thing | 23.00 |
02 | thing | 42.00 |
03 | thing | 56.00 |
04 | apple | 11.00 |
05 | apple | 17.00 |
and a temporary table containing detail info that I'll need to create a third (actual) details table which will key off of the masters table
here's a sample of the temp details table.
----------------------------------
temps
----------------------------------
modelnumber | modality | priceEa |
----------------------------------
| 123 | thing | 1.00 |
| 234 | apple | 2.00 |
| 345 | apple | 3.00 |
| 456 | apple | 4.00 |
| 567 | thing | 5.00 |
Basically, I need to loop through every record in the masters table.
Outer loop:
For each record in the masters table, grab the modality.
Inner loop:
Then for each record in the temps table, where the modalities match, create a record in the details table (and in the process, do some calculations based on temps.priceEa and masters.cost).
This should create (masters * temps) number of new records in the details table for every record in the masters table.
the details table, should end up looking like
----------------------------------------------------------
details
----------------------------------------------------------
num | modelnumber | modality | priceEa | adjustedCost |
----------------------------------------------------------
| 01 | 123 | thing | 1.00 | (do calc here)
| 01 | 567 | thing | 5.00 | (do calc here)
| 02 | 123 | thing | 1.00 | (do calc here)
| 02 | 567 | thing | 5.00 | (do calc here)
| 03 | 123 | thing | 1.00 | (do calc here)
| 03 | 567 | thing | 5.00 | (do calc here)
| 04 | 234 | apple | 2.00 | (do calc here)
| 04 | 345 | apple | 3.00 | (do calc here)
| 04 | 456 | apple | 4.00 | (do calc here)
| 05 | 234 | apple | 2.00 | (do calc here)
| 05 | 345 | apple | 3.00 | (do calc here)
| 05 | 456 | apple | 4.00 | (do calc here)
...etc
SELECT m.num, t.modelnumber, m.modality, t.priceea
into myNewTempTable
from masters m inner join temp t on m.modality = t.modality
order by m.num, t.modelnumber

Resources