I am working on a small project at my office to do some clean up on our customers table stored in SQL Server and a similar one stored in PostgreSQL. Basically we found many customers have one or several columns that have 0 or are blank and need to be filled in. Our plan is to first gather all of the data for the 0 or blank columns and fill in the csv, and then somehow script it to fill in the blank columns for each customer.
Below is an example of what the current table looks like with 0s and blank columns. It's random as to which column or columns need to be filled in for a given customer (the one thing we always have is the customer's username).
username emp_num title email phone
-------------------------------------------------------------------
jsmith 0 Manager jsmith#somewhere.com
kjones 112222 Clerk 111-222-3333
wgarcia 0 wgarcis#domain.com 444-555-6666
We would be filling in anything with "0" with a 6 digit number and any blanks would have values filled in on the csv.
Question is, after I have a completed csv with all customer info, how might I go about:
loading the csv into SQL; and
scripting this to compare the csv file against the existing customer table and fill in only what's missing? (i.e. 0s and blanks).
Can anyone share example SQL queries to get me started? (How might it need to be approached differently for PostgreSQL?)
TIA,
Chris
Update off a join to the correct customer table like this:
update a
set a.emp_num=b.emp_num
from currenttable a join customer b on a.username=b.username
where a.emp_num in (0,'')
Presumably this is a one time task. You can do this very easily with some simple string building using Excel or another spreadsheet application. It won't be reusable, but it's often the quickest solution.
That said it isn't going to work if you have a very large number of customers, but it'll do the trick for several thousand.
Excel Formula
="UPDATE YourTable SET emp_num = " & B2 & ", title = '" & C2 & "', email = '" & D2 & "', phone = '" & E2 & "' WHERE username = '" & A2 & "'"
Simply copy the formula to all the rows in your csv, and execute the generated queries.
Related
I'm working in visual studio with vb.net and crystal report where I have few tables that store the order information of any restaurant. I want to print the bill of any order for the guest and also in case if guest has placed the order multiple times in a restaurant.
I'm attaching a image that will give you some sort of details about the scenario where 3 tables are designed with bill format as a output.
I want to use a record selection formula for any particular table to print the items details in Bill for all the order placed. Table 2 & 3 are linked to each other with 'Order No'. When I give the command to print the bill for table 'T1' it should give me the details as shown in the bill format which include all the order items from table 'TableOrderedItems' for the list of order numbers placed for that particular table (i.e. T1).
Firstly, I create a list of all orders that pertain to table # 'T1' like ("K1", "K2"). Then I will look into table 'TableOrderedItems' for each order no. in list and get printed all the items in bill (see image).`
Dim cryRptBill As New ReportDocument
Dim RepLocationBill As String
RepLocationBill = Application.StartupPath & "\CryRptBillTable.rpt"
cryRptBill.Load(RepLocationBill)
cryRptBill.RecordSelectionFormula = "{TableOrderedItems.OrderNumber}=" & " in [" + kotList + "]"
Image For Tables & Bill Format
I think you want this:
CryRpt.RecordSelectionFotmula= "{tblUsers.ID} in [" + List + "]"
This assumes your list is a string with number and commas. For example "1, 3, 4"
I am trying to create a form for MS Access where you at first search for companies in a table based on criteria. The returned data is simply the names of the matching companies. I then want to take these names and add them to a different table.
So far there are 3 tables: one stores the User Names (tblStartup), one Stores the Company Names (tblVC) and one shall be used to save the matches (tblContact).
The problem I have is that I want to add a constant user name alongside the data from the query.
So e.g. "MAX" searches for companies that are from Automotive. He gets a list of matching companies e.g BMW, DAIMLER and AUDI.
So the data which should be added to the table tblContact would be:
MAX BMW
MAX DAIMLER
MAX AUDI
INSERT INTO...SELECT... doesn´t work because I not only need to add the Info from the query but also a constant which the User selects from a Combobox(e.g. the user selects "MAX" and then uses the button "query" to find matching companies for MAX, then the button "add to Contacts".
This is the code I have so far:
Dim sql As String
sql = "INSERT INTO tblContact(txtNameStart, txtNameVC) " & _
"SELECT txtName FROM tblVC WHERE Branche ='" & Me.cboBranchen & "';"
So how do I put my constant User Name inside the query). Would it be something like SELECT... AND VALUES...?
Regards
Max
You can just use an INSERT INTO .... SELECT .... statement, with one column and one constant value.
Example:
sql = "INSERT INTO tblContact(txtNameStart, txtNameVC) " & _
"SELECT """ & Me.cboName & """, txtName FROM tblVC WHERE Branche ='" & Me.cboBranchen & "';"
I have a query against an Item table. This is all the items our library has, e.g., books, DVDs,CDs, etc.
I have to send a tab-delimited file that contains data on all these items.
There are over 100,000 items.
I'm stuck using MS Access.
Access can pull all the data, but it cannot send the result set via email because it is too big (over 65,535 rows; I am aware that later versions of Excel past 2007 can hold more rows but that does not help me).
All the columns are text data. So normal relational operators won't work (I tried).
I need to split the result set into two or three result sets in order to get it from Access to Excel. The need for Excel is that this is how the vendor expects it, and it needs to be tweaked some before shipping.
How can I divide it?
I have thought of at least two ways
If I can count rows, I can tell Access to use the first 60,0000 rows it gets. How do I tell it do that and then fetch only the second set of all the rows past 60,000. I have not figured out how to do this.
Divide based upon a field. The only field that is unique is the barcode, e.g., "30001001672906" Usually, the barcode is 14 numbers in length. I have experimented with using StrComp in a where clause, but I have a problem:
The barcodes are not in sorted order before they are fetched. "Order by" works on the result set, not how the data is processed before it is selected.
I am at a loss as to how to accomplish my big goal. That's the one that matters, not the particular way to fix my SQL to get it. I've looked at some pages, such as those below but not found a solution.
https://support.office.com/en-us/article/Table-of-operators-e1bc04d5-8b76-429f-a252-e9223117d6bd#__toc272228349
MS ACCESS count/sum number of rows, with no duplicates
http://www.techonthenet.com/access/functions/string/strcomp.php
I don't understand the problem with 2.
SELECT barcode FROM items ORDER BY barcode
Open a Recordset on that, move to record 60000, get the barcode
rst.Move 60000
strBarcode = rst!barcode
See https://msdn.microsoft.com/en-us/library/bb243789%28v=office.12%29.aspx
Then build your queries dynamically.
myQuerydef.SQL = "SELECT * FROM items WHERE barcode <= '" & strBarcode & "'"
Export the query e.g. with DoCmd.TransferSpreadsheet
myQuerydef.SQL = "SELECT * FROM items WHERE barcode > '" & strBarcode & "'"
Export to second file.
If you need more than two files, use an array instead of strBarcode and do
myQuerydef.SQL = "SELECT * FROM items WHERE barcode > '" & Barcode(i) & _
"' AND barcode <= '" & Barcode(i+1) & "'"
Being an amateur in Access and VBA, I've hit a wall with the following...
In MS Access 2007, I have the following query:
SELECT .... WHERE format(NZ(l_f_date),'yyyy-mm-dd')<=**'2012-04-03'**);
I have shortened it a bit of course.
The database has approx 20 queries that need to be run on a daily basis. I have created 4 macros to run groups of queries in the manner that I need them to be run. The problem is that in every one of those queries I first have to change the date (like in the upper query). I am looking for the way to automate it a bit more.
I have an idea to create a Form, place a button for every macro on it and 2 textbox-es to enter the 2 dates I need. Now, I need those dates to appear in the bolded part. As I think about it, I have 2 options:
Create a temporary table in the database to store those two dates and pull those 2 fields in my queries.
Insert the value of the textbox somehow directly into the bolded part of the query.
I think I can manage the first solution, but the second one is making my head hurt.
Can you help?
SQL from comment
select proc_desc,count(s) as broj
into upit3
from ( select distinct a.case_id as s,
a.org_case_id,a.act_date as day,d.prod_id,d.prod_desc,
c.fname,c.dpd,c.due_amount,proc_id,proc_desc
from actions_b as a, cases_old_b as c,processes_b as p,
product_dict_b as d
where a.org_case_id=c.[org_ case_id]
and a.act_date=Forms!Form!Text10 and d.prod_id=c.product
and p.proc_id=c.process and not_lead=1 )
group by proc_desc order by proc_desc;
OK, sample data....
In x.dates, value is exactly like this: 03.04.2012
In a.act_date value is like this: 01.07.2011 13:53:56
so if its not possible with this values as they are, is it possible to use a LIKE statement in the query? Pseudo: WHERE a.act_date LIKE x.date%
If its possible, how to use it? i am a novice in sql and access queries... I google but sometimes, like this time, i get stuck.
Thanks
This structure:
SELECT .... WHERE format(NZ(l_f_date),'yyyy-mm-dd')<='2012-04-03');
Is not a good idea. The general rule is to try and stick to field (column) names on the left side of the equals. So
SELECT .... WHERE l_f_date <=#2012-04-03#
Nulls will not be included and I hope your dates are dates and not strings.
Next add the form:
SELECT .... WHERE l_f_date <= Forms!TheNameOfTheForm!TheNameOfTheTextbox
EDIT re comments
You are using the query design window, yes? Please try this test query:
SELECT a.case_id, a.act_date
FROM actions_b AS a
WHERE a.act_date=Forms!Form!Text10
OK, so i decided to create a simple 2-column table in my database that will be used just for storing yesterdays and todays date... i added 2 text-boxes to a form, 1 button and added the next procedure to the button:
Private Sub Command25_Click()
CurrentDb.Execute "DELETE * FROM Datumi"
Dim tbl As Recordset
Set tbl = CurrentDb.OpenRecordset("Datumi")
tbl.AddNew
tbl!brojka = "1"
tbl!datum = Text8.Value
tbl.Update
tbl.AddNew
tbl!brojka = "2"
tbl!datum = Text10.Value
tbl.Update
End Sub
As you can see, the click on the button will clear the table and store new values into 2 rows... row 1 is yesterday, row 2 is today...
And after that im pulling the values from that table like this:
... where x.brojka=1 and format(a.act_date,'yyyy-mm-dd')=format(x.datum,'yyyy-mm-dd') ...
I'm sure it can be done better but this one works for me...
I Solved this problem with :
[Software: MS Access 2013]
CurrentDb.Execute "INSERT INTO [Inventory Transactions] ([Product ID], [Order ID])" _
& "VALUES ( " & Me.Product_ID & ", " & Me.Order_ID & ")"
I have found the way to copy the record that I would like, but now I am having a violation of the Primary Key constraint. Here is what I am trying to do:
We make a product that comes out of our maching into 2000 lbs bags and it is giving a number, e.g. 26273.
We store and sell it in those bags, but we also can sell it in smaller 50 lbs and 25 lbs bags.
When we convert the bag from 2000 lbs to 25 lbs the product takes up 80 bags.
Only 40 bags can be put onto a pallet, making the product number 26273 take up two pallets.
The problem we have is when we store the pallet we scan the barcode of the product and then scan the barcode of the warehouse location, ONE pallet per location, and only ONE location per pallet. If we have two pallets with the same number than we cannot store them in the warehouse.
To solve this problem my bosses what the first pallet to be number 26273B1 and the second pallet to be 26273B2 so that the pallets still contain the original number but is slighlty different in order to store them.
When the product receives a number it also goes through several tests and that data is part of the record so both of the records still nedd to contain those test results.
When I try to copy the record and place the B2 onto the number I get a Primary Key Constraint ODBC Failure. I know why I am getting the error, and I don't what to dissable the constraint to allow duplicate records, but I still need to have the ability to create this new record when we convert to 25lbs bags.
So my question: Is there any way to copy a record, slighty change the Primary Key while copying it, and still be able to save it without the Primary Key Constraint error occuring?
NOTE: the database is in SQL with the interface front-end is in Access 2007.
Why not store the original 2000 bag with the PK "26273-00-0000". That "00-0000" suffix indicates the original Bag.
For each subdivision into a smaller bag, "one-up" or increment the sequence that is suffixed at the end. You could use "00" for palletts, and "0000" for the bag-sequence number.
Hence "26273-B1-0001" - thru "26273-B1-0040" indicates the Product id that went into Pallett - one - the first 40 bags.
I am not sure how you are wanting to copy a record i.e. in a form or programatically.
Note you can have a Primary Key based on more than 1 column so provided the 2 columns together are unique then you will not violate the constraint.
Simply build what is called an append query. You can pull all of the old previous values and substitute a value for any of the columns to be something of your choice.
So, the code + sql would look like:
Dim strSql As String
Dim oldPK As String
Dim newPK As String
oldPK = "26273B1"
newPK = "26273B2"
strSql = "insert into tblPallets (PalletNumber, Description, StockNumber)" & _
"select '" & newPK & "', Description, StockNumber" & _
"from tblPallets where PallentNumber = '" & oldPK & "'"
CurrentDb.Execute strSql, dbFailOnError