Put Array in to Table Column - pytables

I'm trying to store information in a pytables subclass. I have my class Record and subclass Data. Data will have many rows for every row of Record. I don't want to use a loop with row.append() because it seems like it would be horribly slow. Can I just create an array and drop it in Data.v column? How?
import tables as tbs
import numpy as np
class Record(tbs.IsDescription):
filename = tbs.StringCol(255)
timestamp = tbs.Time32Col()
class Data(tbs.IsDescription):
v = tbs.Int32Col(dflt=None)
...
row = table.row
for each in importdata:
row['filename'] = each['filename']
row['timestamp'] = each['timestamp']
# ???? I want to do something like this
row.Data = tbs.Array('v', each['v'], shape=np.shape(each['v']))
row.append()

OK, When I read about nested tables I was think about relational data in a one-to-many situation. This isn't possible with nested tables. Instead I just created a separate table and stored row references using
tables.nrows
to get the current row of my Data table. This works for me because for every entry in Record I can calculate the number of rows that will be stored in Data. I just need to know the starting row. I'm not going to modify/insert/remove any rows in the future so my reference doesn't change. Anyone considering this technique should understand the significant limitations it brings.

Nested columns use the '/' separator in the column key. So I think that you simply need to change the line:
row.Data = tbs.Array('v', each['v'], shape=np.shape(each['v']))
to the following:
row['Data/v'] = each['v']

Related

Convert 2 columns of data in excel power query to one header row with multiple columns

I am trying to figure out how to change 2 columns of data into one header row with multiple columns in Excel Power Query. It's my understanding that Query keeps the Excel file size small, and is less on the processes, as opposed to using tons of vlookups or pivot tables. I'm open to VBA if that's a better option.
For example, I have Column A with a list of names. Then, column B has another list of names with multiple instances of the same name(s). The names in column A are individuals assigned to report to individuals in B.
I'm trying to create a query (or VBA if better) where the names in B become the row headers, and the names in A fall under the corresponding person in each header.
I hope that makes sense. Thank you in advance for your help!
Here's a screenshot, demonstrating what I'm working with, and the end result I'm trying to get:
You can use Power Query:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ListEmployees = Table.Group(Source, {"Supervisor Name"}, {{"Employees", each Text.Combine([Employee Name],","), type text}}),
CountEmployees = Table.AddColumn(ListEmployees, "Count", each List.Count(Text.Split([Employees],","))),
SplitEmployees = Table.SplitColumn(ListEmployees, "Employees", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv),List.Max(CountEmployees[Count])),
Transpose = Table.Transpose(SplitEmployees),
PromoteHeaders = Table.PromoteHeaders(Transpose, [PromoteAllScalars=true])
in
PromoteHeaders
Make sure your source data is structured as a Table (listobject).

SQL - LEFT JOIN and WHERE statement to show just first row

I read many threads but didn't get the right solution to my problem. It's comparable to this Thread
I have a query, which gathers data and writes it per shell script into a csv file:
SELECT
'"Dose History ID"' = d.dhs_id,
'"TxFieldPoint ID"' = tp.tfp_id,
'"TxFieldPointHistory ID"' = tph.tph_id,
...
FROM txfield t
LEFT JOIN txfielpoint tp ON t.fld_id = tp.fld_id
LEFT JOIN txfieldpoint_hst tph ON fh.fhs_id = tph.fhs_id
...
WHERE d.dhs_id NOT IN ('1000', '10000')
AND ...
ORDER BY d.datetime,...;
This is based on an very big database with lots of tables and machine values. I picked my columns of interest and linked them by their built-in table IDs. Now I have to reduce my result where I get many rows with same values and just the IDs are changed. I just need one(first) row of "tph.tph_id" with the mechanics like
WHERE "Rownumber" is 1
or something like this. So far i couldn't implement a proper subquery or use the ROW_NUMBER() SQL function. Your help would be very appreciated. The Result looks like this and, based on the last ID, I just need one row for every og this numbers (all IDs are not strictly consecutive).
A01";261511;2843119;714255;3634457;
A01";261511;2843113;714256;3634457;
A01";261511;2843113;714257;3634457;
A02";261512;2843120;714258;3634464;
A02";261512;2843114;714259;3634464;
....
I think "GROUP BY" may suit your needs.
You can group rows with the same values for a set of columns into a single row

How to do some aggregate calculation through several columns one by one?

I am new to SQL and needed some help. I have a table that has some numeric values and I need to populate a column (all values null) with calculations from other columns in the table.
For example. I have some values and a total value. I need another column to calculate the percentage between those two.
I have many columns that need populating from calculation based on different columns. For eg. A column name "risk1" will help me calculate and populate another column called "1per". My code looks something like this:
UPDATE DPRA2_Export
SET "1Per" = ((CAST(Risk1 AS DECIMAL (38,2))/CAST(GrandTotal AS DECIMAL(38,2))) * 100);
UPDATE DPRA2_Export
SET "2Per" = ((CAST(Risk2 AS DECIMAL (38,2))/CAST(GrandTotal AS DECIMAL(38,2))) * 100);
UPDATE DPRA2_Export
SET "3Per" = ((CAST(Risk3 AS DECIMAL (38,2))/CAST(GrandTotal AS DECIMAL(38,2))) * 100);
.................
It goes on like this.
Is there a way I can for-loop this thing instead of writing over and over again. The only thing that changes in the code is the column name "Risk%" and the SET column name "%Per"
Any ideas?
First, having columns with sequential names -- , , . . . -- is suspicious. In general, this format is not a good fit for relational databases. Instead, the data should be stored with one row per risk and an identifier for the risk.
Such tables are often useful for output purposes, but not for storing data.
Second, the data types for the division are probably entirely unnecessary. If you are using a database that does integer division and your values are integers, you can convert using a simpler method such as '*1.0'.
Finally, you can issue a single update:
UPDATE DPRA2_Export
SET "1Per" = Risk1*1.0 / GrandTotal,
"2Per" = Risk2*1.0 / GrandTotal,
"3Per" = Risk3*1.0 / GrandTotal ;
You can construct the logic by querying for the columns, using the information_schema tables.

Access Check If DataSet contains string via vba Macro

Hello Stackoverflow community!
I am currently confronted with writing somthing, that automatically cleans up a database after certain requirements. Here is what I have to do:
If a cell in one table contains a certain substr (lets say "Mrs."), in another table a Togglebox is supposed to be checked (Yes if "Mrs." is contained and No if not)
I already worked out something that sets The Toggleboxes to -1 in that column, but I have to include a check, if it even has to be changed and in which row. For that I wanted to bring up an If-construction and a variable that would contain the ID of the first table's row to determine, which of the Rows in the second table have to be changed.
So the thought is like this:
If 1st_Table contains "Mrs." Then
CurrentDb.Execute "UPDATE 2nd_Table SET gender = -1 WHERE foo = 1st_Table.ID_Var"
1st_Table.ID_Var would contain the ID of the row of the first table
Now the more or less obvious question: How do I accomplish that?
This is bascally the first time working with VBA/Access AND SQL so I have no closer thoughts on how to do that.
Thanks in advance
Greetings
geisterfurz007
P.S. The variable names will be changed in the final version; they are just to visualize. Just saw that they were colored by SO.
Try this (SQL update query):
UPDATE 2nd_Table INNER JOIN 1st_Table ON 2nd_Table.ID = 1st_Table.ID
SET 2nd_Table.gender = -1
WHERE 1st_Table.PersonName LIKE '*Mrs.*'
ID -> reference between both tables
PersonName -> column in 1st table containing Mrs.

Joining a dimension ID to a fact table

I am currently populating a fact table. I've already populated the dimension tables. I've populated two of the columns in the fact table (time, abandoned) but I am struggling to populate the third dimension key across, which is the call type key. The reason for this is there are many attributes to be calculated to work out the key.
The dimension table is for call work type. It has three columns, these being:
CallWorkTypeID - CallWorkTypeCode - CallWorkTypeName.
Example values are
1 - OutAttemp - Outbound Attempt
The problem i have is working out the call type name, here is a example code for working out the name for example this selects the outbound attempt calls.
SELECT * from outgoing_measure om
WHERE om.CTICallType_ID = 3
AND om.CTIAgentCallType_ID = 1
AND om.Abandoned = 1
AND om.AnsTime IS NULL
AND om.CallState IS NULL
There are 19 different scenarios like this with the same columns but the values changing for each call type.
Anyone got any ideas? I've thought maybe CASE statements but not sure how the code would work on the join.
CASE statements work great for this if you don't already have a mapping table or a rules table to help you. You don't need to make any join if your dimension members are already put in the dimension table. You can just put them in the case.
UPDATE f
set thirddimensionforeignkey = CASE
WHEN (cticalltype = 3 and all that other stuff is true) THEN 1
WHEN (some other stuff is true) THEN 2
WHEN (rest of the stuff is true) THEN 19
--ELSE unknownmember
END
FROM yourfacttable as f