I apologize in advance if there is a similar question out there already. I haven't had any luck finding anything. Basically, I run a small business and have four separate locations. With the current POS system we use, customer data is added/updated independently of each location. This means, for each customer, they have up to 4 customer IDs if they've visited all four locations. The information of course, will mostly constant for all four locations, but not in the following two cases: (1) The customer is new, (2) the customer updates their information at a particular location during their visit. I already figured how to handle new customers. However...
I'm trying write a query in SQL SERVER that automates the following for existing customers:
1. Imports and appends each of the four .CSV files (customer lists) I export from my POS system.
I've already accomplished this step with a simple BULK INSERT
2. Compare this list to what is currently in the customer data and:
a. For customers WITH four records in the newly imported list, I want to update all four records in the existing SQL table to the most current version of the customer's information. See example below:
Month 1: DATA IN SQL TABLE
Location | Cust_ID | Name | Phone Number
----------------------------------------------
0000001 | 12345A | David | 7025551234
0000002 | 12345B | David | 7025551234
0000003 | 12345C | David | 7025551234
0000004 | 12345D | David | 7025551234
Month 2: DATA TO COMPARE TO SQL TABLE
Location | Cust_ID | Name | Phone Number
---------------------------------------------
0000001 | 12345A | David | 7025551234
0000002 | 12345B | David | 7025559999
0000003 | 12345C | David | 7025551234
0000004 | 12345D | David | 7025551234
DESIRED RESULT
Location | Cust_ID | Name | Phone Number
----------------------------------------------
0000001 | 12345A | David | 7025559999
0000002 | 12345B | David | 7025559999
0000003 | 12345C | David | 7025559999
0000004 | 12345D | David | 7025559999
You guys might be thinking, "this guy just needs a new POS software provider." You're probably right, and I will once my wife is on-board with the idea of big change. In the meantime, this is what I'm stuck with. Thank you guys in advance for your help.
Related
I'm not sure whether this is possible with some of the new BigQuery scripting capabilities, UDFs, array/string functions (or anything else!), however I simply can't figure it out.
I'm trying to write the SQL for a view in BigQuery which dynamically defines columns based on query results, similar to a pivot table in a spreadsheet/BI tool (or melt in pandas). I can do this externally in Python or hard-code it using case statements, but I'm sure that a SQL solution to this would be incredibly useful to a huge number of people.
Essentially I'm trying to write a query which would transform a table like this:
year | name | number
-----------------------
1963 | Michael | 9246
1961 | Michael | 9055
1958 | Michael | 9203
1957 | Michael | 9116
1953 | Robert | 9061
1952 | Robert | 9205
1951 | Robert | 9054
1948 | Robert | 9015
1947 | Robert | 10025
1947 | John | 9634
1946 | Robert | 9295
----------------------
SQL to generate initial example table:
SELECT year, name, number
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE number > 9000
ORDER BY year DESC
Into a table with the following structure:
year | John | Michael | Robert
---------------------------------
1946 | | 9,295 |
1947 | 9,634 | | 10,025
1948 | | 9,015 |
...
This then needs to be connected to downstream tools, without requiring maintenance when the data changes. I know that this is not always a great idea and that tidy form data is more universally useful, but there are still some scenarios where this behaviour is desirable.
I have seen a few solutions on here, but they all seem to involve string generation and then manually pasting the query... I can do this via the BigQuery API but am desperate to find a dynamic solution using nothing but SQL so I don't have to maintain an external function.
Thanks in advance for any pointers!
This question already has answers here:
Customized Auto-Number IDs for tables?
(2 answers)
Closed 3 years ago.
First of all, I am quite new to SQL and Microsoft Access.
I am setting a database in Access. My database collects information from four different departments. I store my data through forms. My main table (Business) stores information (department) using a Combo Box saving a number instead of text.
I want to have a column (similar to CODE ID already available in the table above) which shows the initial letter from a field (name Department) + a number.
Ie. In table "Business", I want to display a Code ID which contains the initials of column Department plus a number code (department order number ascending). I want to have this every time i add information.
+===============+=================+=========+==+
| DEPARTMENT | PARTNER | CODE ID | |
+===============+=================+=========+==+
| Data_Analysis | John Doe | D001 | |
+---------------+-----------------+---------+--+
| Marketing | Jane Doe | M001 | |
+---------------+-----------------+---------+--+
| Finance | Alex Mustermann | F001 | |
+---------------+-----------------+---------+--+
| Operations | Juan Perez | O001 | |
+---------------+-----------------+---------+--+
| Finance | Barack Trump | F002 | |
+---------------+-----------------+---------+--+
| Finance | Mark Merkel | F003 | |
+---------------+-----------------+---------+--+
| Marketing | Peggy Hilton | M002 | |
+---------------+-----------------+---------+--+
| Operations | Max Mustermann | O002 | |
+---------------+-----------------+---------+--+
| Operations | | OXXX | |
+---------------+-----------------+---------+--+
The values in column CODE ID are those I would like to have display every time I add a new row (new department order). I need this type of code for tracking my number of orders in each department and use it as a unique code for any inquires with partners. I dont want to have it as the primary key id.
Thanks in advance!
If you rethink the schema slightly it becomes trivial; instead of having the column with the ID and code combined, just keep a running count when inserting:
INSERT INTO business(department, name, code) SELECT Forms!Department, Forms!Name, COUNT(*)+1 FROM business WHERE name=Forms!Name
Then when you pull the information out:
SELECT department, name, LEFT(1, department) & code
For Example, I have a dataset like this:
| People | ID |
|-------------|-----|
| John Smith |A1234|
| John Doe |A1235|
| Jane Doe |A1236|
| John Smith |A1237|
And I also have another dataset like this:
| People | Company | City | Rank |
|-------------|---------|--------|-------|
| John Smith | XXX |New York| 1 |
| John Doe | YYY |London | 2 |
| Jane Doe | ZZZ |Seoul | 3 |
| John Smith | WWW |Tokyo | 4 |
I want to find the company of each people in the first table, using the information in another table. Note there're people with the same name (though few) in the second (and also the first) tables, so we need other columns for assistance.
Is it necessary to import two tables in one project? The reality is I have multiple tables providing possible name / company matchings, but they have little similarity (i.e. different dataset provides entirely different information) other then each dataset have name and company rows.
You need to create two separate OpenRefine projects and join them using the cell.cross function. You can also see this tutorial for joining two projects in OpenRefine
cell.cross performs the equivalent of a database join. You will need a unique identifier common to your two projects for the function to match the records, otherwise, OpenRefine will return the first match.
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.
I have 4 columns in my non-decomposed, non-normalized Job Application table which are all Nullable, for example my table is:
Name | SSN | Education | City | Job Applied | Post | Job Obtained | Post Obtained
John. | 123 | High School | LA | USPS | MailMan | USPS | MailMan
John. | 123 | High School | LA | Dept. of Agri | Assistant | *null* | *null*
Sam. | 123 | BS | NY | Intel | QA Analyst | Intel | QA Analyst
The first 4 Columns are non-nullable so I can easily determine Functional Dependencies between them.
The last 4 columns, can or cannot have values depending on if a person has got a job and also depending on if he/she has applied for a job.
My question is: Can I have Functional Dependencies on Nullable Columns either them being on the LHS or the RHS.
The answer should be yes, please see:
http://en.wikipedia.org/wiki/Functional_dependency