I am trying to create an SOQL query that looks to an employees report to position id and create a string that will concat the employees position id and the position id of his manager and concat the managers manager and continue this up the organization chart.
Example Data in Position__c:
Employee_Name Position_ID Reports_to_Position_ID
John Doe 123 456
Billy Bob 456 789
Jane Doe 789 321
Harvey Sample 321 654
John Doe's position id is 123, he reports to position 456. position 456 reports to 789 and so on.
Expected Result for John Doe reports_to_Hierarchy --> 123,456,789,321,654
SOQL looks bit object-oriented, you use dots to go "up" the relationship. https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships.htm
The basic idea is
SELECT Name, Manager.Name, Manager.Manager.Name, Manager.Manager.Manager.Name
FROM User
Actual result depends if you have standard or custom object and what are the field names. You can go up to 5 levels (dots) away from your starting point.
Is the list flat just like that? You might need a helper lookup to "self" field on Position__c, call it "Parent__c", "Manager__c" or something. And you'd need to populate it during load (read about upsert and external ids?)
And then it'd be something like
SELECT PositionId__c,
Parent__r.PositionId__c,
Parent__r.Parent__r.PositionId__c,
Parent__r.Parent__r.Parent__r.PositionId__c
FROM Position__c
Related
Goal: Implement a SQL query in a Snowflake database that, given an address-like string string (user input), does a fuzzy/approximate search against a single field, returning results with a similarity score, ordered by that score.
I see that Snowflake offers a few tools that seem related to this problem, such as APPROXIMATE_SIMILARITY and MINHASH, but it's not clear to me which of these tools I need or how to put them together. The documentation is good, but lacking a straightforward example, and seems to focus on the similarity of two tables, rather than comparing an arbitrary string to values in a column.
Given user_input and field locations.FullAddress, I'm looking for something like this pseudo query:
SELECT "score", field1, field2 from locations
WHERE FullAddress LIKE user_input;
I know there's more to it than that but just can't quite see how to integrate the functions provided by Snowflake to make it work.
Here is a sample of the locations table - note that the complete address is in a single field, and can be rendered in inconsistent ways.
| Somefield | FullAddress | OtherField |
|-----------|--------------------------------------------------|------------|
| foo | 123 SW Marble Street, Brainerd MA 55555 | yellow |
| bar | 98 Main, San Diego CA 99999 | green |
| beep | 123 SW Marble St, Brainerd 55555-2222 | orange |
| baz | 456 Somewhere Blvd, Apt 23, Boise ID, 44444-1234 | blue |
A user might search for 123 SW Marble Street, Brainerd MA 55555 (a perfect match). I would hope to return rows 1 and 3, with row 1 getting the highest score. Or user might search for 123 Marble Street (imperfect) and I would still want to return rows 1 and 3, ranked by the similarity algorithm.
There are three built-in fuzzy matching functions in Snowflake, JAROWINKLER_SIMILARITY (mentioned by NickW), EDITDISTANCE and SOUNDEX. It's a simple matter to extend this library using Java, Python, or JavaScript code in a UDF.
Here is an example of the three built-in functions compared to a given address:
set comparison = '123 SW Marble Street, Brainerd MA 55555';
select *
,JAROWINKLER_SIMILARITY($comparison, full_address)
,EDITDISTANCE($comparison, full_address)
,SOUNDEX($comparison) = SOUNDEX(full_address)
from T1
(Basic question that probably is a duplicate, but I don't know what to search for, so feel free to edit this question with the proper database terms)
How would one retrieve groups of rows (family members) from a database table based on columns (last and first names) efficiently? E.g. from this
last first ...
doe john ...
doe jane ...
smith jimmy ...
smith ted ...
smith anna ...
to something like this (additional data omitted)
doe : [{first:john, ...}, {jane}],
smith: [{jimmy}, {ted}, {anna}]
Does this require retrieving the common data (last name) with distinctor group by first and then iterating with additional queries (where last="smith") for each name?
I'd think that that naive approach likely is inefficient and there are better solutions.
You need GROUP_CONCAT() aggregate function:
SELECT last, GROUP_CONCAT(first) first
FROM tablename
GROUP BY last
or JSON_GROUP_ARRAY():
SELECT last, JSON_GROUP_ARRAY(first) first
FROM tablename
GROUP BY last
See the demo.
I want to show the results of a MySQL query on my website using angularjs. For now, I'm showing them using a simple table with ng-repeat and it works with no problem. But because the data is a lot, I wanted to ask if it is possible to create multiple panels or tables per specific field.
To be more specific, I have 4 fields returned from the query: name, address, occupation, department. Right now I have a table such as:
George Smith Nikis 10 Project Manager Finance
Maria Bexley Lincoln 20 Project Manager Research
Chris Liggs Forks 123 Programmer Computer Science
etc. I want to know if I can create as many panels or tables as the unique values of the "occupation" field are and then show the results per that unique value inside each panel/table. So instead of the above table I would have something like:
Project Manager
George Smith Nikis 10 Finance
Maria Bexley Lincoln 20 Research
Programmer
Chris Liggs Forks 123 Computer Science
I think you need to use groupby filter
Check this fiddle by Darryl Rubarth, it contains the answer you need
http://jsfiddle.net/drubarth/R8YZh/
<div ng-repeat="item in MyList | orderBy:'groupfield' | groupBy:'groupfield'" >
You can use group by filter in ng-repeat with which you need to group
So I've been developing a sort of data entry platform within accessing using forms and subforms.
I have a form titled PHYSICIAN. Each physician will have basic data like first/last name, DOB, title, contract dates, etc. The aspect I'm wanting to cover is addresses as they may have multiple, since they may work/practice at 2 or 3 or even 10 different locations.
Instead of having our data entry team key in a full record each time they need to add an address, I'd like a way for the form to retain ALL information not related to the address.
So if Ken Bone works at 7 places, I want to allow them to key all of those addresses a bit more efficiently than creating a new record.
There's one main issue I'm running into --- A subform or autopopulate option doesn't necessarily increment the autonumber ID (primary key) for the record. All of the information is being stored in 1 master table.
Is there a way around this or a more logical approach that you folks might suggest?
I recommend that you have a couple of tables perhaps even three.
tblDoctorInfo
- Dr_ID
- Name
- DOB
- Title
tblAddresses
- AddressID
- Address1
- Address2
- City
- State
- Zip
- Country
tblDr_Sites
- DrSites_ID
- Dr_ID
- AddressID
The tables might have data like this.
tblDoctorInfo
1, Bob Smith, 12/3/1989, Owner
2, Carl Jones, 1/2/1977, CEO
3, Carla Smith, 5/3/1980, ER Surgeon
tblAddresses
1, 123 Elm St, Fridley, MN 55038
2, 234 7th St, Brookdale, MN 55412
3, 345 Parl Ave, Clinton, MN 55132
tblDr_Sites
Then you could associate the tables with the third table. (Note each of the three tables have an ID field that increments).
1,1,1 This record means Dr. Bob works in Fridley
2,1,2 This record means Dr. Bob works in Brookdale
3,3,1 This record means Dr. Carla works in Fridley
4,2,3 This record means Dr. Carl works in Clinton
5,2,2 This record means Dr. Carl works in Brookdale
6,2,1 This record means Dr. Carl works in Fridley
I am designing a database with the code igniter datamapper.
Imagine that I have a three tables one with users and one with friends and one with foods friend like. I am trying to access the food friend like. I have populated all the tables with data mapper.
Here is how I do it:
user table has id, user_name, gender, user_id fields.
Every user has many friends and friend table is:
friend table has id, friend_id, friend_name, user_id
Every friend has many foods and here is table for it:
friend id food
I was able to populate the tables by filing out the values. Lets say for example:
user:
1 andy male 394
2 jen female 439
..
friend:
1 243 mark 394
2 493 silvia 394
3 459 gloria 439
...
So when I try to access friend of a user from the database I do this:
$u->get_by_user_id($data['user']['id']);
if ($u->exists())
{
$data['user']['first_name'] = $u->first_name;
$data['user']['last_name'] = $u->last_name;
$data['user']['gender'] = $u->gender;
$data['user_friends']=$u->friend->get();
and it works...(i don't know why it knows what is the id of friend to relate to) but it works...
now I am trying to go one level deeper and access or get() every users' friends foods...but I do't know how?
Ok I found the answer:
if ($u->exists())
{
$data['user_profile']['first_name'] = $u->first_name;
$data['user_profile']['last_name'] = $u->last_name;
$data['user_friends']=$u->friend->get();
foreach($u->friend->get()->all as $u){
$fm=new Food();
$fm->get_by_friend_id($u->friend_id)->get();
foreach($fm->all as $fm){
//stuff
}
}