SFMC SQL Data Join - sql

Im badly in need of a way on how to merge almost identical records.
These set of records consist of customerID ItemID Location ItemName DeliveryTime.
Ex:
CName |cID |ItemID| Location| ItemName | DeliveryTime | Delivery time2 |
Artemys|123456| 321 | US | Mango | March 31,2022|
Artemys|123456| 321 | US | Mango | April 30 2022|
as you can see the only difference is the delivery time in the second record below. What i need is, put the delivery time of the second record join in the first record as delivery time 2 using SQL Automation.
Example record

Related

SQL Server: create rows with info from multiple tables having the same column name

I am doing an integration with a customer's ERP. The database tables have a normalization so that the columns that have the same name throughout any table, must have the same data type.
With this premise, I would like to generate a SQL, or a stored procedure that drags data from several source tables in a given order always matching the column names, to 2 target tables. As it is highly probable that the ERP vendor will add new columns without notifying my department, I need the columns to be obtained dynamically.
All this is to generate a single record in a table (in this case, the head data of a purchase to a supplier), and several rows in another table (the items of the purchase).
My idea is to have an auxiliary table where I put the information coming from my system, and then, execute that SQL/procedure to consolidate the information into the ERP purchase tables.
Let's take an example.
My tables would have information similar to this
(Purchase header)
ExternalOrderId | SupplierCode | PurchaseDate | PurchaseStatus | FiscalYear | Series
--------------------------------------------------------------------------------------------
ABCD | 00001 | 2021-12-11 12:00:00 | DRAFT | 2021 | S
(Purchase items)
ExternalOrderId | ArticleCode | ItemOrder | Units
--------------------------------------------------
ABCD | 1234 | 1 | 2
ABCD | 2345 | 5 | 4
ABCD | 3456 | 10 | 10
ABCD | 1234 | 15 | 3 (very important, same article can be repeated multiple times in one purchase)
.....
ABCD | 9999 | 100 | 10
Very important step is to take fiscal year, series and number from a table of counters. The counter should be incremented after the process.
Example of table "Counters" (note that there may be several numbers for one type depending on the series and the exercise):
Type | FiscalYear | Series | LastNumber
----------------------------------------------------
SupplierPurchase | 2021 | S | 26
SupplierPurchase | 2021 | A | 60
SupplierPurchase | 2021 | B | 15
SaleOrder | 2021 | S | 19
SaleOrder | 2021 | X | 200
Table "Accounting data".
SupplierCode | AdditionalColumn1 | AdditionalColumn2 | AdditionalColumn3
-------------------------------------------------------------------------
00001 | AC1A | AC2A | AC3A
Table "Company data".
SupplierCode | AdditionalColumn2 | AdditionalColumn3 | AdditionalColumn4
-------------------------------------------------------------------------
00001 | AC2B | AC3B | AC4B
Table "Supplier data".
SupplierCode | AdditionalColumn3 | AdditionalColumn5
-----------------------------------------------------
00001 | AC3C | AC5C
In this case the result should be something like this: for the columns with the same name, the data coming from the last table read should be kept. For example, AdditionalColumn1, will have the value of the first table (AC1A) because is the only table with that column name, and in the case of AdditionalColumn3, the data from the last one (AC3C).
The final result should look something like this:
Purchase Header
FiscalYear | Series | Number | SupplierCode | AdditionalColumn1 | AdditionalColumn2 | AdditionalColumn3 | AdditionalColumn4 | AdditionalColumn5 | PurchaseStatus | PurchaseDate | ExternalPurchaseID
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2021 | S | 27 | 00001 | AC1A | AC2B | AC3C | AC4B | AC5C | DRAFT | 2021-10-11 12:00:00:00 | ABCD
Note that the purchase number is 27, because in the counters table the last number used for the series "S" was 26. After creating this row, the counter must be set to 27.
In the case of the purchase items, it would be the same, obtaining the data from:
The purchase header created in the previous step.
Data from the Articles table
Data from another table with additional information about the articles.
The data from the purchase items table that I generated earlier.
But in this case, instead of being a single record, it will be a record for each item that I reflect in my auxiliary table, matching the info by the item's "ArticleCode".
I could do all this through programmed code, but I would like to abstract from the programming language and include all this in the database logic, to make a very fast, transactional process that can be retried in case of failure. Besides, as I said, they will be dynamic columns, since the ERP provider will be able to create new columns. In this way, I will not have to worry about having to escape the information of possible unicode characters and I will be sure that the data types are respected at all times.
It would be nice if i can get a boolean flag marked on my auxiliary table to indicate that the purchase has been consolidated correctly.
Thanks in advance
EDIT
As #JeroenMostert said in one response this question is too vague. The purpose of my question is to know how to use the column names obtained, for example from INFORMATION_SCHEMA.COLUMNS, from a table A and use them in a query, but only the ones that intersect with the columns of a table B, and do it several times with several tables so that I can generate the header of the purchase. And then use the same process (and the resulting data) to generate the purchase rows.

SQL How to take a list of items and create a new table based on date fields

I'm hoping someone can help me with a query. I'm quite a beginner and I'm not looking for someone to write code for me, as I'd rather learn how to do this, but I'm not sure how to word a search to help me do what I need to do, so if I can get pointers on the search parameters I should be using that would be great.
I have a table of data :
Room Number | User ID | Start Date | End Date
-----------------------------------------------
RM001 | AD215 | 01/08/2018 | 04/12/2018
-----------------------------------------------
RM001 | AQ110 | 05/12/2018 | 02/04/2019
-----------------------------------------------
RM002 | AD215 | 05/12/2018 | 02/04/2019
What I have to show is monthly availability of rooms ( from the 1st Wednesday of the month to the Tuesday before the 1st Wednesday of the next month ) based on the query date to get the following, which details each room, the user and the dates they are using the room :
Room Number | 01/08/2018 | 05/09/2018 | 03/10/2018 | 07/11/2018 | 05/12/2018
| 04/09/2018 | 02/10/2018 | 06/11/2018 | 04/12/2018 | 01/01/2019
----------------------------------------------------------------------------
RM001 | AD215 | AD215 | AD215 | AD215 | AQ110
| 01/08/2018 | 01/08/2018 | 01/08/2018 | 01/08/2018 | 05/12/2018
| 04/12/2018 | 04/12/2018 | 04/12/2018 | 04/12/2018 | 02/04/2019
----------------------------------------------------------------------------
RM002 | Vacant | Vacant | Vacant | Vacant | AD215
| | | | | 05/12/2018
| | | | | 02/04/2019
----------------------------------------------------------------------------
I've searched on creating custom column headers but there doesn't seem to be a way to create based on the query and using this as part of the query to pull the data. Nor can I find a way to make the custom headers the 1st Wednesday of the month and the Tuesday before the 1st Wednesday of the next month and using this as part of the query.
I'm able to do this in Excel by using INDEX and MATCH, but it's taking so long to calculate and I'd prefer to find a way to do this at source, so I can run as often as I like.
Has anyone got any guidance on search parameters to use to create such a query or know of a resource that details examples I can learn from please?
Some thoughts:
Your table shows data that exists (e.g. booking for RM001 in 1st August) and data that doesn't (e.g. no booking for RM002 in 1st August). With SQL you would usually select only data that exists. In your case bookings (one result row per room and booked month). It is your app or website that would care about the layout, i.e. put the data in your grid. This is how I would do it.
You can turn rows into columns in SQL - provided you know the columns beforehand. If it is always five months you want to show for instance, then this would be possible. You'd use the pivot keyword if available or conditional aggregation for this. With pivot it is possible to create column titles based on your data; with conditional aggregation it is not.
You can even create the 'Vacent' cells in SQL. You would probably select all months and cross join with all rooms. This would give you all your "cells" (i.e. 5 x 2 = 10 rows in your example). You would then outer join your bookings to fill cells with bookings.
Is it possible to have multiple bookings in a month? E.g. AD215 booking th first half, AD216 booking the second half? If so, you must think about how to display this.
You get the first Wednesday of a month by looking at the first day of the month, seeing which day it is and adding zero to six days to get to the first Wednesday.

Get sequential dates in query

I am attempting to query a Postgres database to retrieve records for a specific customer that are from dates that are adjacent to the current day.
So for example:
If today is Wednesday, it will pull the records from Monday and Tuesday if they exist.
If today is Wednesday, it will not pull a record from Monday if Tuesday does not have a record.
If pulling a record from last Wednesday, it will pull Tuesday and Thursday if they exist.
The database is currently structured with a Dates table that contains:
Id----------|------DateTime--------
And the Id has a foreign key relationship with the Customers table containing:
CustomerId-------|-----DateId(fk)-----|----other columns.....
I have thought to simply query the database if the date exists before and after the queried date (and pull that record if exists), and then loop such that a success will query the next date before or after..... but that seems horribly inefficient.
Is there a better method to obtain these records?
EDIT:
Example Dates table:
Id | DateTime
1 | '2018-01-23 16:19:17.600305'
2 | '2018-01-24 00:03:11.213492'
3 | '2018-01-25 03:10:14.911771'
Sample Customers table:
Id | DateId | CustomerId | Location1 | Location2 | Transport
66 | 2 | 2 | Market | Library | Car
67 | 2 | 3 | Bookstore | Library | Car
68 | 3 | 3 | Pool | Town | Car
69 | 3 | 3 | Pool | Town | Bus
Join the tables and use the lag and lead window functions with a window clause like OVER (ORDER BY dates.datetime).

Editing a row in a database table affects all previous records that query that information. How should prior versions be stored/managed?

I’ve been working on a Windows Form App using vb.net that retrieves information from a SQL database. One of the forms, frmContract, queries several tables, such as Addresses, and displays them in various controls, such as Labels and DataGridViews. Every year, the customer’s file is either renewed or expired, and I’m just now realizing that a change committed to any record today will affect the information displayed for the customer in the past. For example, if we update a customer’s mailing address today, this new address will show up in all previous customer profiles. What is the smartest way to avoid this problem without creating separate rows in each table with the same information? Or to put it another way, how can versions of a customer’s profile be preserved?
Another example would be a table that stores customer’s vehicles.
VehicleID | Year | Make | Model | VIN | Body
---------------------------------------------------------------
1 | 2005 | Ford | F150 | 11111111111111111 | Pickup
2 | 2001 | Niss | Sentra | 22222222222222222 | Sedan
3 | 2004 | Intl | 4700 | 33333333333333333 | Car Carrier
If today vehicle 1 is changed from a standard pickup to a flatbed, then if I load the customer contract from 2016 it will also show as flatbed even though back then it was a pickup truck.
I have a table for storing individual clients.
ClientID | First | Last | DOB
---------|----------|-----------|------------
1 | John | Doe | 01/01/1980
2 | Mickey | Mouse | 11/18/1928
3 | Eric | Forman | 03/05/1960
I have another table to store yearly contracts.
ContractID | ContractNo | EffectiveDate | ExpirationDate | ClientID (foreign key)
-----------|------------|---------------|-------------------|-----------
1 | 13579 | 06/15/2013 | 06/15/2014 | 1
2 | 13579 | 06/15/2014 | 06/15/2015 | 1
3 | 24680 | 10/05/2016 | 10/05/2017 | 3
Notice that the contract number can remain the same across different periods. In addition, because the same vehicle can be related to multiple contracts, I use a bridge table to relate individual vehicles to different contracts.
Id | VehicleID | ContractID <-- both foreign keys
---|-----------|------------
1 | 1 | 1
2 | 3 | 1
3 | 1 | 2
4 | 3 | 2
5 | 2 | 3
6 | 2 | 2
When frmContract is loaded, it queries the database and displays information about that particular contract year. However, if Vehicle 1 is changed from pickup to flatbed right now, then all the previous contract years will also show it as a flatbed.
I hope this illustrates my predicament. Any guidance will be appreaciated.
Some DB systems have built-in temporal features so you can keep audit history of rows. Check to see if your DB has built-in support for this.

Matching disambiguating data to existing duplicate records

I have a table called transactions that has the ledger from a storefront. Let's say it looks like this, for simplicity:
trans_id | cust | date | num_items | cost
---------+------+------+-----------+------
1 | Joe | 4/18 | 6 | 14.83
2 | Sue | 4/19 | 3 | 8.30
3 | Ann | 4/19 | 1 | 2.28
4 | Joe | 4/19 | 4 | 17.32
5 | Sue | 4/19 | 3 | 8.30
6 | Lee | 4/19 | 2 | 9.55
7 | Ann | 4/20 | 1 | 2.28
For the credit card purchases, I subsequently get an electronic ledger that has the full timestamp. So I have a table called cctrans with date, time, cust, cost, and some other info. I want to add a column trans_id to the cctrans table, that references the transactions table.
The update statement for this is simple enough, except for one hitch: I have an 11 AM transaction from Sue on 4/19 for $8.30 and a 3 PM transaction from Sue on 4/19 for $8.30 that are the same in the transactions table except for the trans_id field. I don't really care which record of the cctrans table gets linked to trans_id 2 and which one gets linked to trans_id 5, but they can't both be assigned the same trans_id.
The question here is: How do I accomplish that (ideally in a way that also works when a customer makes the same purchase three or four times in a day)?
The best I have so far is to do:
UPDATE cctrans AS cc
SET trans_id = t.trans_id
WHERE cc.cust = t.cust AND cc.date = t.date AND cc.cost = t.cost
And then fix them one-by-one via manual inspection. But obviously that's not my preferred solution.
Thanks for any help you can provide.