OLEDB query to select first and last time for id - excel

I'm using ADODB connection in VBA excel file with Microsoft.Jet.OLEDB.4.0 provider.
The file with which I'm establishing connection is .csv file which looks like:
Date and time, Last name, First name
2011-08-29 05:48:50,lname1,fname1
2011-08-29 05:49:50,lname1,fname1
2011-08-29 05:55:50,lname2,fname2
2011-08-29 16:11:50,lname1,fname1
2011-08-29 17:55:50,lname2,fname2
2011-08-30 9:11:50,lname1,fname1
The point is that my data is sorted by Date and time which is in one field, and user names are not in any order.
What I really need to do is create a query to fill Recordset.
I need this query to select first, second and last hour for each day for each user name.
Is it even possible to split Date and time column just using a query?
I've got general idea how to select what I want, but the thing is too complicated for me, because of that Date and time in the same field.
Would you give me any suggestions?

Let us say your file is called Dates.csv, you can try:
SELECT
[Last name] & ", " & [First Name] AS FullName,
Format([Date And time],"yyyy/mm/dd") AS WorkingDay,
First(Format([Date And time],"hh:nn:ss")) AS FirstHour,
Last(Format([Date And time],"hh:nn:ss")) AS LastHour
FROM [Dates.csv]
GROUP BY [Last name] & ", " & [First Name], Format([Date And time],"yyyy/mm/dd")
I am a little suspicious of the double space between date and time. I would get rid of the spaces in the column names, if I were you. It will make things easier.

Related

KDB: How to match string of possible dates with rows of table?

I essentially just want to create a date column that represents the date of the filename.
My table filesInDir is just a single column and 4 rows called filepath:
":..\..\code\products\Q\ExtData\CIBC\availability\Global\EquityOnly\daily\bnyMellon_inventory\push_list_20190314_040253_Equity.csv"
":..\..\code\products\Q\ExtData\CIBC\availability\Global\EquityOnly\daily\bnyMellon_inventory\push_list_20190314_040306_Equity.csv"
":..\..\code\products\Q\ExtData\CIBC\availability\Global\EquityOnly\daily\bnyMellon_inventory\push_list_20190311_040321_Bond.csv"
":..\..\code\products\Q\ExtData\CIBC\availability\Global\EquityOnly\daily\bnyMellon_inventory\push_list_20190312_999999_Cash.csv"
I also have a list of possible dates, 2019.03.12 2019.03.11 2019.03.14. How can match the list of dates with the rows of the table above so that I can get a new column that specifies that date value that matched with the filepath string.
If all your file paths follow the same format as your example ones,you can create a date column pretty easily doing:
update date:"D"$8#'103_'filePaths from filesInDir
Then match with your dates using this column.
This is how you parse the filePath to get the date, Please note as "\" is the escape sign so you need to write as "\\" instead that or directly retrieve the string from system command.
Created table
filesInDir:([]filePaths:(":..\\..\\code\\products\\Q\\ExtData\\CIBC\\availability\\Global\\EquityOnly\\daily\\bnyMellon_inventory\\push_list_20190314_040253_Equity.csv";
":..\\..\\code\\products\\Q\\ExtData\\CIBC\\availability\\Global\\EquityOnly\\daily\\bnyMellon_inventory\\push_list_20190314_040306_Equity.csv";
":..\\..\\code\\products\\Q\\ExtData\\CIBC\\availability\\Global\\EquityOnly\\daily\\bnyMellon_inventory\\push_list_20190311_040321_Bond.csv";
":..\\..\\code\\products\\Q\\ExtData\\CIBC\\availability\\Global\\EquityOnly\\daily\\bnyMellon_inventory\\push_list_20190312_999999_Cash.csv"))
Create the date column
update date:{"D"$("_"vs last "\\" vs x)[2]} each filePaths from `filesInDir
I also have a list of possible dates, 2019.03.12 2019.03.11 2019.03.14. How can match the list of dates with the rows of the table above so that I can get a new column that specifies that date value that matched with the filepath string.
Can you give some example illustration on that? Not clear about what you intend and expect to see
If you just want let say a "Flag" column indicating whether the date in the records match with the dateRange, you can simply use a in to match that.
dateRange:2019.03.12 2019.03.11 2019.03.14
update match:date in dateRange from `filesInDir
This will be the output:
Another approach using 0:
update date:raze(" D";"_")0:filePaths from filesInDir
It is dependent on the occurrences of _ in your filepaths

"Data type mismatch in criteria expression" in VBA ACCESS SQL

I am trying to fetch data from my Access table into Excel sheet. The below mentioned SQL query is throwing an error "Data type mismatch in criteria expression" while the execution reaches Open query.
I googled for all possible fixes. All are saying the number might be passed in quotes. i checked for it still clueless, The query is pretty simple where i am trying select data from a userform text box in DD-MMM-YYYY format (03-OCT-2018) and comapring with date portion of timestamp field and order by my customer id field
SELECT * FROM Master_Intrpay_Cash where DateValue(LAST_UPDATE_TIMESTAMP)>=#" & Trim(startdate.value) & "# and DateValue(LAST_UPDATE_TIMESTAMP)<=#" & Trim(enddate.value) & "# ORDER BY CUSTOMER_ID
Below Shown is the msgbox showing the query being passed. if it helps.
Also the crazy part is the above query was copy pasted from an existing working query, just changed the table name and timestamp field name. Rest everything is exactly the same.
Try without DateValue:
SELECT *
FROM Master_Intrpay_Cash
WHERE LAST_UPDATE_TIMESTAMP >= # " & Trim(startdate.value) & " #
AND LAST_UPDATE_TIMESTAMP <= # " & Trim(enddate.value) & " #
ORDER BY CUSTOMER_ID
DateValue expects a String as an argument, your column as Date/Time
Also, a preferable format for the date is #mm/dd/yyyy# (US date format), otherwise you may have problems with different locales.

updating multiple MS ACCESS tables with 1 spreadsheet

I have a MS Access database with about 1000 tables. The Tables are independent of each other, that is they are not linked to each other. Basically each table contains records of stock of a particular good. The Names of each table is the name of the item, for example 125 gm Soap, 200 ml Oil, etc. and contains the following columns : -
Date
Daily Opening Stock
Daily Sales
Daily Purchases
Daily Closing Stock
Each day, sales/purchases of all the goods are recorded in accounting software. The End of Day report summary is generated in Excel Format. This report is a consolidated report of daily Sales/Purchases of all the goods, row-wise. The Columns in Excel are :
Date
Item Name
Daily Opening Stock
Daily Sales
Daily Purchases
Daily Closing Stock
Presently I have to manually open each table in Access & copy-paste the respective daily sales/purchases data from the excel report. I want to automate this process so that the record is automatically appended to each of the respective table, basis the Item Name.
In summary, I want to update multiple tables in Ms Access database file, from data contained in 1 Ms Excel spreadsheet.
Is that possible ? Your help would be highly appreciated.
Thanks,
Thiru V. Klack
My first question to you is why can't the Access database match the data structure of the Excel spreadsheet? Updating 1 Access table from 1 spreadsheet is a lot simpler than what you're trying to do. Having 1,000 tables with the same data structure, where each table is named after a product, seems pretty fishy to me.
That being said, if you need to keep your 1,000 tables (I'm honestly surprised you're not hitting the DB maximum threshold for Access), there are ways you can make it work. I would suggest having a lookup table that matches the product (as it appears on the spreadsheet) to the table in Access it goes to. Then, you'd import the Excel data into a staging table, and run some VBA code to loop through the lookup table, running a query to append all the data from Excel into the appropriate Access table. Something like this:
private const STAGING_TABLE_NAME as string = "tDataFromExcel"
private Const LOOKUP_TABLE_NAME as String = "tGoodsToTableNames"
public sub AddData()
Dim db as DAO.Database
Dim rsItemLookup as DAO.Database
Dim strSQL as string
Dim GoodName as string, TableName as string
set db = CurrentDB
set rsItemLookup = db.OpenRecordset("select good_name, table_name from " & LOOKUP_TABLE_NAME)
do while not rsItemLookup.EOF
GoodName = rsItemLookup.fields("good_name").Value
TableName = rsItemLookup.fields("table_name").value
strSQL = _
"insert into " & TableName & " (" & _
"[date], " & _
"[Daily Opening Stock], " & _
"[Daily Sales], " & _
"[Daily Purchases], " & _
"[Daily Closing Stock]) "
"select " & _
"[date], " & _
"[Daily Opening Stock], " & _
"[Daily Sales], " & _
"[Daily Purchases], " & _
"[Daily Closing Stock]) " & _
"from " & STAGING_TABLE_NAME & " " & _
"where [Item Name] = """ & GoodName & """"
db.Execute strSQL
rsItemLookup.MoveNext
loop
end sub
Again, I'm certainly not advocating you take this approach: If you can, I would strongly implore you to refactor your database to not have 1,000 virtually identical tables.
Zack's answer provides a way to do what you're asking, but recommends that you do something else. I'm going to try to demonstrate that what he recommends would work for your situation, and show you how to achieve it.
You want to transfer data in an excel spreadsheet into a relational database that holds the same data - usually to give better access/manipulation. The data is in a big table of one day, with a separate line for each product, and you want to be able to view the entire history of a single product, so you have separated out the tables.
This is going to significantly slow down the automatic transfer process - that long loop that re-writes and processes the SQL over and over. You can look at the data in that way without this setup though. Lets call this tblProductDetails
One table has your product information - things like the name of the product, how much it costs... The other has the in/out records of what was bought and sold, keeping track of stock on hand at any point. This second table is going to have a LOT of records, as it's a new record for each product that was bought and/or sold each and every day. We might call this tblInOut.
With a Select query you can save in Access, you can look at the history of any one product at any time - you can even set this as a variable so that when you open the only query you save it prompts you for the name of the product you want to look up.
I've mocked up a 4 product sample schema for you with a query to show all the data, and another to show just the one product's history. You can use the second query exactly as is in Access' query builder (SQL view) where '125g Soap' (on the second last line) is mentioned in the query name so you know which query is which.
If you want to use just one query, do the same thing with one exception: on the second last line, where it says '125g Soap' replace the whole thing with Product_Name (without the inverted commas) and when you open the query, Access will give you a message box asking you to type in a product name.
This feature will only work if you type the name exactly right though, so you might prefer just to build a form so you can give the user a combo box with which to select the exact product name.

Excel Power Query - from web with dynamic worksheet cell value

We have a spreadsheet that gets updated monthly, which queries some data from our server.
The query url looks like this:
http://example.com/?2016-01-31
The returned data is in a json format, like below:
{"CID":"1160","date":"2016-01-31","rate":{"USD":1.22}}
We only need the value of 1.22 from the above and I can get that inserted into the worksheet with no problem.
My questions:
1. How to use a cell value [contain the date] to pass the date parameter [2016-01-31] in the query and displays the result in the cell next to it.
2. There's a long list of dates in a column, can this query be filled down automatically per each date?
3. When I load the query result to the worksheet, it always load in pairs. [taking up two cells, one says "Value", the other contains the value which is "1.22" in my case]. Ideally I would only need "1.22", not the title, can this be removed? [Del won't work, will give you a "Column 1" instead, or you have to hide the entire row which will mess up with the layout].
I know this is a lot to ask but I've tried a lot of search and reading in the last few days and I have to say the M language beats me.
Thanks in advance.
Convert your Web.Contents() request into a function:
let
myFunct = ( param as date ) => let
x = Web.Contents(.... & Date.ToText(date) & ....)
in
x
in
myFunct
Reference your data request function from a new query, include any transformations you need (in this case JSON.Document, table expansions, remove extraneous data. Feel free to delete all the extra data here, including columns that just contain the label 'value'.
(assuming your table of domain values already exists) add a custom column like
=Expand(myFunct( [someparameter] ))
edit: got home and got into my bookmarks. Here is a more detailed reference for what you are looking to do: http://datachix.com/2014/05/22/power-query-functions-some-scenarios/
For a table - Add column where you get data and parse JSON
let
tt=#table(
{"date"},{
{"2017-01-01"},
{"2017-01-02"},
{"2017-01-03"}
}),
add_col = Table.AddColumn(tt, "USD", each Json.Document(Web.Contents("http://example.com/?date="&[date]))[rate][USD])
in
add_col
If you need only one value
Json.Document(Web.Contents("http://example.com/?date="&YOUR_DATE_STRING))[rate][USD]

Excel 2010: Merge and Concatenate Rows

I need help.
It seems all macros I stumble upon on this website require me to write out all the rows I'm concatenating and merging. I was wondering if I could do this with a while or if statement. In the meantime,
I need to merge a table of over 21000 names (more than half of which are duplicates) but each name duplicate either has data that the original is missing, or vice versa or sometimes has different data under each column and I need to merge them. There are also like 34 (up to AF) columns.
Thanks,
Eddie
P.S. Apparently I need at least 10 reputation to post images, so message me via my account name if you want a screenshot of what I'm looking for.
P.S.S.
So after consulting with someone who helped through a comment I wrote this java-based pseudocode. Could someone help me translate it to VBA while I start to learn VBA myself? Also could they verify that it theoretically works? I'd like to have this done by the end of the day, which is why I'm asking for translation help, but I'm planning to be able to do this on my own in the near future.
//Create primary keys for comparison, using the last cell as PK for easy line finishing
//Create concatenation comparison keys (conKey) to compare cells and merge
//import new sheet and create cell location to write to new sheet.
Create PK1 = (cell) AF1
Create PK2 = (cell) AF2
Create conKey1 = (cell) A1
Create conKey2 = (cell) A2
Create newSheet = [a new sheet]
Create writeLine = A1
//Initialize while loop. This list lasts until it reaches the last person's name
While(conKey2 <= maxClient) {
//Initialize the if statement. It finds out whether merge is necessary
If(PK1.equals(PK2)) {
//Initialize while loop. This lasts until PK1 no longer equals PK2
while(PK1.equals(PK2)) {
//Initialize if loop. It checks to see if the values are not equal. if so, it concatenates into conKey1
if(!conKey1.equals(conKey2)) {
conKey1 = concatenate(conKey1,", ",conKey2)
}
//Export cell to writeLine of newSheet. Shift everything to the right. verify writeLine equals conKey1
//Clear the doubled cell for safe keeping and to assist us in closing the while loop.
exportTo(newSheet.writeLine, conKey1)
conKey1.shiftsRight
writeLine.coordinates(equals(conKey1))
conKey2 = ""
conKey2.shiftsRight
}
//After this while loop is finished, delete the blank row.
//coordinates of PK1 and PK2 should remain the same at this point
deleteRow(PK2)
//If the merge was not necessary it will skip all of that above and shift each variable down a row.
} else {
PK1.nextRow
PK2.nextRow
conKey1.nextRow
conKey2.nextRow
writeLine.nextRow
}
If SQL is a solution and MAX value of all values is an accepted value... then using an ODBC, a defined table and a self reference may work..
Select Formulas then define name
Define the range of existing data
Select where you want the combined results to display (for example Sheet 2 (A1) instead of sheet 1)
Save the workbook
Select data then from other sources.
select data connection wizard
Select ODBC DSN
Select Excel Files
Find the file saved
Now select the new table (the one defined in step 1)
and complete the wizard.
Go to data menu
Properties
click connection properties button
select definition tab.
modify command text to fit needs
It takes data from sheet 1 such as this:
And provided the wizard is completed (steps 5-11) and then the SQL updated in step 16 you'll get something like
.
This is the command text I used. since it's SQL it can be altered to fit your needs max, concat, whatever
Select firstName, MiddleName, LastName, max(attrib1), max(attrib2), max(attrib3), max(attrib4)
From `yourPath&FileName.xlsx`.`YourDefinedName`
GROUP BY firstName, MiddleName, LastName

Resources