How can I capture and store data from a repeating HL7 segement? - hl7

We currently capture data from HL7 messages like below and then insert the same in database. This is easy as it is value from a single segment
var vACC_NO =checkSize("ACC",msg['PID']['PID.3']['PID.3.1'].toString(),20);
INSERT INTO adt_tab ( SITEID,ACC_NO) VALUES (vSITEID,vACC_NO);
Now I need to capture DG1 segment data, where we have multiple DG1 segments in HL7 message. And also need to store in Database
| DG1 | 1 | ICD10 | I22.8^MYOCARDIAL INFARCT^ICD10 | MYOCARDIAL | | | | | | | | | | | |
| INFARCTION | 201702010437 | B | | | | | | | | | 7 | | | | |
| DG1 | 2 | ICD10 | A44.9^ORGANISM^ICD10 | ORGANISM | 20170201 0437 | B | | | | | | | | | 7 |
So in my database table I have now more columns - SITEID, ACC_NO, CODE1, CODE2...
From the above message I need to insert I22.8 into CODE 1, A44.9 into CODE2 and so on ..
How I should first capture these codes in loop from multiple DG1 segments in the message ?
And then how I should store it in the database ?
Thanks

You can iterate over the segments like this
for each (dg1 in msg['DG1']){
variable1 = dg1['DG1.3']['DG1.3.1'];
variable2 = dg1['DG1.3']['DG1.3.2'];
// database call with the previus
databaseCall(variable1,variable2, ...
}
For each segment you are going to do an insert.
Apart from this, I do not think is a good idea to make more columns in the same row by adding variable1, variable2, variable3 ... as it is not normalized and it not a good database design practice.

Related

Formula to randomly assign names in a given group with Google Sheets

I would like to create a formula in the below column Assigned so that a random name matching the same group as the current name is automatically inserted.
Names in the Assigned column must match the group of the current name and must not repeat throughout the list. I can't assign the current name with it's own name either. Any suggestions on how to do this?
I guess it is worth posting this. Although it might not be practical for large groups, it is a formula solution that works in Excel and Google sheets:
=INDEX($A$2:$A$8,SMALL(IF(($B$2:$B$8=B2)*($A$2:$A$8<>A2)*(COUNTIF(C$1:C1,$A$2:$A$8)=0),ROW($A$2:$A$8)-1),
RANDBETWEEN(1,SUM(($B$2:$B$8=B2)*($A$2:$A$8<>A2)*(COUNTIF(C$1:C1,$A$2:$A$8)=0)))))
entered as an array formula using CtrlShiftEnter
Here is an example of a successful match:
and an unsuccessful one:
As you can see, Mike, Jack and Fred have paired up together leaving Dave on his own, likewise with Lucy and Harry.
In Excel you may have to press F9 a few times to get a successful result - in Google sheets you just have to keep changing something, or set it to update every minute while you make a cup of tea.
It was my first time to work on something related to unique values, it took time but I learned lot from this question.
Answer of #Tom-Sharpe gave me an inspiration that it can be done, thanks to Tom for that. I tried and here it is.
I have checked it on some random data
+------------+-------+-------------+------------+
| Name | Group | RAND | Assigned |
| Malynda | 1 | 0.644382728 | Boonie |
| Boonie | 1 | 0.167369621 | Venus |
| Venus | 1 | 0.547165865 | Malynda |
| Jamal | 2 | 0.193081046 | Cora |
| Cora | 2 | 0.399459181 | Jamal |
| Alaster | 2 | 0.910498559 | Enrika |
| Enrika | 2 | 0.45819549 | Melisandra |
| Melisandra | 2 | 0.612592303 | Alaster |
| Petunia | 3 | 0.957104883 | Lawton |
| Mariam | 3 | 0.602293619 | Grenville |
| Caterina | 3 | 0.695342797 | Mariam |
| Stace | 3 | 0.942926886 | Caterina |
| Perle1 | 3 | 0.787227158 | Stace |
| Lawton | 3 | 0.315403693 | Perle1 |
| Grenville | 3 | 0.515276361 | Petunia |
| Elia | 4 | 0.655404975 | Catarina |
| Agosto | 4 | 0.322045058 | Fidela |
| Fidela | 4 | 0.490635045 | Agosto |
| Catarina | 4 | 0.121053081 | Elia |
| Elliot | 5 | 0.994137138 | Eddie |
| Mae | 5 | 0.349103119 | Wadsworth |
| Farleigh | 5 | 0.645375865 | Mae |
| Trudey | 5 | 0.473849475 | Farleigh |
| Gwenneth | 5 | 0.678186154 | Trudey |
| Wadsworth | 5 | 0.254168853 | Gwenneth |
| Eddie | 5 | 0.02103249 | Elliot |
| Denyse | 6 | 0.294801945 | Fayina |
| Tracie | 6 | 0.113670327 | Denyse |
| Aili | 6 | 0.901562575 | Tracie |
| Fayina | 6 | 0.029515522 | Alain |
| Mort | 6 | 0.938536467 | Perle |
| Alain | 6 | 0.389741125 | Aili |
| Perle | 6 | 0.513800791 | Mort |
| Mathew | 6 | 0.972656521 | #N/A |
| Benton | 7 | 0.423710316 | Bret |
| Bret | 7 | 0.127478128 | Benton |
| Mayne | 7 | 0.701027869 | Kirbee |
| Derry | 7 | 0.564710572 | Marje |
| Kirbee | 7 | 0.510258205 | Derry |
| Marje | 7 | 0.600908601 | Mayne |
| Devin | 7 | 0.718740939 | #N/A |
| Wilbert | 8 | 0.763761013 | Griswold |
| Brandice | 8 | 0.482092682 | Marty |
| Griswold | 8 | 0.111418464 | Brandice |
| Brandais | 8 | 0.594020577 | Fair |
| Kim | 8 | 0.727863883 | Brandais |
| Cam | 8 | 0.858246187 | Kim |
| Fair | 8 | 0.640979168 | Wilbert |
| Ardath | 8 | 0.883008322 | Cam |
| Marty | 8 | 0.339506717 | Ardath |
+------------+-------+-------------+------------+
D2 contains the below formula
=INDEX(
$A$2:$A$51,
MATCH(
MIN(IF(
(COUNTIF($D$1:D1,$A$2:$A$51)=0)*
($B$2:$B$51=B2)*
($A$2:$A$51<>A2),
$C$2:$C$51)
),
$C$2:$C$51,0)
)
C2 contains just the RAND() function but it is pasted as values so that it doesn't update on every calculation, but if you want you can keep it as a function
There are some #N/A values returned by the formula, it arrives when there is no other person left that can be assigned.
Check it on your data and let me know if it working correctly.
Note that the formula is assuming that names are unique and are sorted based on the groups.
If you're fluent with javascript you could attempt to use Google Apps Script to handle this by going to the toolbar Tools > Script Editor. Another way would be to record a macro via Tools > Macros > Record Macro. The script I generated via this method is below, however I couldn't figure out how to prevent assigning the current name with it's own name. My workaround for that is to select the range of assigned names in the same group and applying Data > Randomize Range until the assignment is satisfactory.
function AppendRandomNameByGroup() {
//Sort based on integer in group column
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
spreadsheet.getActiveRange().offset(1, 0, spreadsheet.getActiveRange().getNumRows() - 1).sort({column: spreadsheet.getActiveRange().getColumn() + 1, ascending: true});
spreadsheet.getRange('C2').activate();
//Duplicate names to assigned names column
spreadsheet.getRange('A2:A1000').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
var sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
//Filter for each group and randomize items in 'assigned' column
sheet = spreadsheet.getActiveSheet();
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).createFilter();
spreadsheet.getRange('B1').activate();
var criteria = SpreadsheetApp.newFilterCriteria()
.setHiddenValues(['', '2', '3'])
.build();
spreadsheet.getActiveSheet().getFilter().setColumnFilterCriteria(2, criteria);
spreadsheet.getRange('C2:C1000').activate();
spreadsheet.setCurrentCell(spreadsheet.getRange('C4'));
spreadsheet.getActiveRange().randomize();
spreadsheet.getRange('B1').activate();
criteria = SpreadsheetApp.newFilterCriteria()
.setHiddenValues(['1', '', '3'])
.build();
spreadsheet.getActiveSheet().getFilter().setColumnFilterCriteria(2, criteria);
spreadsheet.getRange('C2:C1000').activate()
.randomize();
spreadsheet.getRange('B1').activate();
criteria = SpreadsheetApp.newFilterCriteria()
.setHiddenValues(['2', '', '1'])
.build();
spreadsheet.getActiveSheet().getFilter().setColumnFilterCriteria(2, criteria);
spreadsheet.getRange('C2:C1000').activate();
spreadsheet.setCurrentCell(spreadsheet.getRange('C8'));
spreadsheet.getActiveRange().randomize();
spreadsheet.getActiveSheet().getFilter().remove();
};
If you're trying to set-up a secret santa of some kind, another possible alternative to using scripts would be this Google Sheets Add-On that does something similar including the group functionality that you desire. However instead of assigning a recipient name in-sheet, it requires you to input corresponding e-mail addresses for each name and proceeds to email each participant with their assigned name.
Secret Santa
Sorry that this doesn't fit all your criteria.

Try to match string in column and print matching column name

I am trying to build an expense dashboard in google sheets for my personal use.
I have data that I will pull from my receipts like so:
First sheet: "Expenses Feb 18"
+------------+--------+--------+
| Item | Amount | Type |
+------------+--------+--------+
| Tomatoes | 2.39 | veggie |
| Joghurt | 1.45 | dairy |
| mozzarella | 1.99 | dairy |
| macadamia | 4.59 | nuts |
+------------+--------+--------+
Second table: "Categories"
+------------+----------+-----------+---------------+
| dairy | veggie | nuts | uncategorised |
+------------+----------+-----------+---------------+
| joghurt | tomatoes | macadamia | a |
| mozzarella | cucumber | pecan | b |
| feta | | | c |
| | | | d-z |
| | | | 0-9 |
| | | | - |
| | | | _ |
+------------+----------+-----------+---------------+
I want to automatically fill out the type column based on the item name.
So far I have a regex that is able to match an item. It will print the matched string. But what I need is the column name (header). And it has to be able to loop through the columns. This only works for a single column.
=REGEXEXTRACT(C11, JOIN("|", INDIRECT("Categories!A1:A"&COUNTA(Categories!A:A))))
The second table is not a desirable way to enter data. Data should be entered preferably with more rows than columns ( not in a pivoted manner).
=ARRAYFORMULA(CONCATENATE(IF(A16=$C$24:$E$25,C$23:E$23,)))
A16 : 🍅
C24:E25: Category table
C23:E23: Category header.

SUMPRODUCT with wildcard

I have the SUMPRODUCT working with hardcoded values however I want to use a wild card for the B clomun in my example.
Here is my data
+----------+----------+-----------+
| A COLUMN | B COLUMN | C COLUMN |
+----------+----------+-----------+
| Status | Fruit | Quanitity |
| | | |
| Fresh | Apple | 6 |
| | | |
| Fresh | Apricot | 7 |
| | | |
| Stale | Apple | 4 |
+----------+----------+-----------+
I would like to match Fresh, AP* and then sum the matches form Column C.
I have the following
=SUMPRODUCT(--($B$2:$B$840="AP*"),--($A$2:$A$840="Fresh"),$C$2:$C$840)
Working code with the Wildcard but the count is off
=SUMPRODUCT(ISNUMBER(SEARCH"AP",$B$2:$B$840,1))*($A$2:$A$840="Fresh")*($C$2:$C$840))
The SUMPRODUCT() function does not support wildcards within an array-type expression. The same result can be achieved with:
=SUMPRODUCT((A2:A1000="Fresh")*(LEFT(B2:B1000,2)="Ap")*(C2:C1000))

Rails Many to many relationships with connecting or cloning two table with references?

I'm a new with Rails and I'm having trouble with some types of associations that seem a bit more complex than the ones I've been exposed to so far.
Zombie_users Body_parts_status Body_parts
| id | name | | id | user_id | body_part_id | recovery | | id | name |
|-----------| --> |----------------------------------------| --> |---------------|
| 1 | Joe | | 1 | 1 | 2 | 10% | | 1 | left leg |
| 2 | Max | | 2 | 1 | 3 | 43% | | 2 | brain |
| 3 | hair |
| 4 | blue eye |
Zobmie_users Recovery_tools Body_parts_impacts
| id | name | | id |user_id| name | | id|recovery_tool_id| body_part_id | impact |
|-----------|-->|-------------------|-->|--------------------------------------------|
| 1 | Joe | | 1 | 1 |hammer| | 1 | 1 | 2 | 10% |
| 2 | Max | | 2 | 1 |magic | | 2 | 2 | 3 | 43% |
graphic illustration of the needed functionality
We have users and a list of body parts.
I need that the users will be able to create recovery tools with which they can through Body Parts impact recover their body parts status :)
and be able to check what part of the body still need to be fixed(compared to the list) and what body parts they have already corrected.
My problem is that I do not know how to implement such connections.
because I need to have some kind of clone of the body parts to body parts status for each user.
But how I reference it so it also works with Body Parts impacts
I do not have even a concept of where to start :)
body parts table is just a long listing of all the parts of the human body
and each user should have their own "copy" of all these parts.

Neo4j - count very slow

I am running this query (bisac_code is uniquely indexed).
Execution time is more than 2.5 minutes.
52 main codes are selected from almost 4000 in total.
The total number of wokas is very large, 19 million nodes.
Are there any possibilities to make it run faster?
neo4j-sh (?)$ MATCH (b:Bisac)-[r:INCLUDED_IN]-(w:Woka)
> WHERE (b.bisac_code =~ '.*000000')
> RETURN b.bisac_code as bisac_code, count(w) as wokas_count
> ORDER BY b.bisac_code
> ;
+---------------------------+
| bisac_code | wokas_count |
+---------------------------+
| "ANT000000" | 13865 |
| "ARC000000" | 32905 |
| "ART000000" | 79600 |
| "BIB000000" | 2043 |
| "BIO000000" | 256082 |
| "BUS000000" | 226173 |
| "CGN000000" | 16424 |
| "CKB000000" | 26410 |
| "COM000000" | 44922 |
| "CRA000000" | 18720 |
| "DES000000" | 2713 |
| "DRA000000" | 62610 |
| "EDU000000" | 228182 |
| "FAM000000" | 42951 |
| "FIC000000" | 474004 |
| "FOR000000" | 41999 |
| "GAM000000" | 8803 |
| "GAR000000" | 37844 |
| "HEA000000" | 36939 |
| "HIS000000" | 3908869 |
| "HOM000000" | 5123 |
| "HUM000000" | 29270 |
| "JNF000000" | 40396 |
| "JUV000000" | 200144 |
| "LAN000000" | 89059 |
| "LAW000000" | 153138 |
| "LCO000000" | 1528237 |
| "LIT000000" | 89611 |
| "MAT000000" | 58134 |
| "MED000000" | 80268 |
| "MUS000000" | 75997 |
| "NAT000000" | 35991 |
| "NON000000" | 107513 |
| "OCC000000" | 42134 |
| "PER000000" | 26989 |
| "PET000000" | 4980 |
| "PHI000000" | 72069 |
| "PHO000000" | 8546 |
| "POE000000" | 104609 |
| "POL000000" | 309153 |
| "PSY000000" | 55710 |
| "REF000000" | 96477 |
| "REL000000" | 133619 |
| "SCI000000" | 86017 |
| "SEL000000" | 40901 |
| "SOC000000" | 292713 |
| "SPO000000" | 172284 |
| "STU000000" | 10508 |
| "TEC000000" | 77459 |
| "TRA000000" | 9093 |
| "TRU000000" | 12041 |
| "TRV000000" | 27706 |
+---------------------------+
52 rows
198310 ms
And the response time is not consistent.
After a while drops to less than half of a minute.
52 rows
31207 ms
In Neo4j 2.3 there will be index support for prefix LIKE searches but probably not for postfix ones.
There are two ways of making #user2194039's solution faster:
Use path expression to count the Woka per Bisac:
MATCH (b:Bisac) WHERE (b.bisac_code =~ '.*000000')
WITH b, size((b)-[:INCLUDED_IN]->()) as wokas_count
RETURN b.bisac_code as bisac_code, wokas_count
ORDER BY b.bisac_code
Mark the Bisac's with that pattern with a label
MATCH (b:Bisac) WHERE (b.bisac_code =~ '.*000000') SET b:Main;
MATCH (b:Main:Bisac)
WITH b, size((b)-[:INCLUDED_IN]->()) as wokas_count
RETURN b.bisac_code as bisac_code, wokas_count
ORDER BY b.bisac_code;
The slow speed is caused by your regular expression pattern matching (=~ ). Although your bisac_code is indexed, the regex match causes the index to be ineffective. The index only works when you are matching full bisac_code values.
Cypher does include some string manipulation facilities that might let you get by without using a regex =~, but I doubt it would make any difference, because the index will still be useless.
I might suggest considering if you can further categorize your bisac_codes so that you do not need to do a pattern match. Maybe an extra indexed property that somehow denotes those codes that end in 000000?
If you do not want to add properties, you may try matching only the Bisacs first, and then including the Wokas. Something like this:
MATCH (b:Bisac) WHERE (b.bisac_code =~ '.*000000')
WITH b
MATCH (b)-[r:INCLUDED_IN]-(w:Woka)
RETURN b.bisac_code as bisac_code, count(w) as wokas_count
ORDER BY b.bisac_code
This may help Cypher stick to the 4000 Bisac nodes while doing the pattern match, before getting involved with all 19 million Woka nodes, but I am not sure if this will make a material difference. Even slogging through 4000 nodes (effectively without an index) is a slow process.
Hash Tables in Database Indexing
The reason that your index is ineffective for regex pattern matching is that Neo4j likely uses a hash table for indexing properties. This is common of many databases. Wikipedia has an article here.
The basics though are that the index is not storing all of the properties that you want to search through. It is storing values that represent the properties you want to search through, and the representation is only valid for the whole property. If you are searching for only a part of the property value, the hashes stored in the index are useless, and the database must search through the properties the old-fashioned way -- one by one.
Edit re: your edit
The improvement in response time after running this query multiple times is certainly due to caching. Neo4j is remembering that you access the Bisac nodes and bisac_code properties frequently, and is keeping them in memory. This makes future queries faster because the values do not need to be read off disk.
However, eventually, those nodes a properties will likely be dropped from the cache, as Neo4j finds you manipulating different nodes, which it will cache instead. There are only so many nodes Neo4j can cache before running out of memory, so it picks the most recent and/or frequently used data.

Resources