SPARQL using union with a dataset - union

The challenge set is to use the UNION keyword with the following datasets and return a list of grandads.
RELDB1 holds 2 useful tables:
The fatherOf table has this data:
Table fatherof:
('Chuck', 'Paul'),
('Chuck', 'Bonnie'),
('Jerry', 'George'),
('Jerry', 'Mary'),
('Chuck', 'Olivia'),
('Paul', 'Joan'),
('George', 'John'),
('George', 'Carole'),
('Ringo', 'Yoko'),
('Ringo', 'Peter')
and the table Person has:
Table person:
('Sara', 64, 'f'),
('Nancy', 68, 'f'),
('Mary', 27, 'f'),
('Olivia', 37, 'f'),
('Bonnie', 35, 'f'),
('Carole', 7, 'f'),
('Yoko', 6, 'f'),
('Joan', 9, 'f'),
('Chuck', 67, 'm'),
('Jerry', 65, 'm'),
('Paul', 30, 'm'),
('George', 25, 'm'),
('Ringo', 39, 'm'),
('John', 8, 'm'),
('Peter', 11, 'm')
I tried this query:
>SELECT ?x ?y ?z
>WHERE {{?x rdf:type ex:Person}
>UNION
>{?x ex:fatherOf ?y . ?y ex:fatherOf ?z}}
>ORDER BY (?x)
The output was everyone in the Person table and then also the son and grandchild for Chuck (Paul, Joan) and Jerry (George, John and George and Carole). I tried removing ?y ?z. The query returned a list of the person table!
How can I modify the query so that the UNION remains but I get only grandparents. Any guidance would be appreciated.

You are trying to return every person who is a grandfather. In your question you show that you are only returning paternal grandparents...
A grandfather has children who has children who has children.
Grandfather
Son Daughter
Son Daughter Son Daughter
The union part come into play because you are going to return the following:
?z father of ?a
?a mother of ?b
?a father of ?b
?z
?a ?a
?b ?b ?b ?b
You need a union on the mother and father of ?b as you are combining the results of multiple triple patterns. I won't provide you with the query for this as this is classwork, however, this should help you arrive at the answer using what you have.

Related

can we delete element in list based on certain condition

I have a Python list:
test_list = ['LeBron James', 'and', 'Nina Mata', 'Dan Brown', 'Derrick Barnes', 'and',
'Gordon C. James', 'J. Kenji López-Alt', 'and', 'Gianna Ruggiero', ]
I want output like this:
final_list = ['LeBron James and Nina Mata', 'Dan Brown', 'Derrick Barnes and
Gordon C. James', 'J. Kenji López-Alt and Gianna Ruggiero']
In short, I want one item before 'and' one item after 'and' to be combined. On the other hand, names coming without 'and' should not be combined and left as it is. How can we do this in Python?
Here's a solution perhaps not too elegant but simple and functional: join all words with a glue symbol that does not happen in any of them (e.g., "~"), replace the resulting "~and~"s with " and "s, and split by the glue symbol again:
"~".join(test_list).replace("~and~", " and ").split("~")
#['LeBron James and Nina Mata', 'Dan Brown',
# 'Derrick Barnes and Gordon C. James',
# 'J. Kenji López-Alt and Gianna Ruggiero']
This should work for groups with more than one "and," too.

Postgres how to find numeric element in json array (revision)

table name: muscle_groups
fields: id, name, segment_ids
data:
{"f": [], "m": [31, 32, 33, 34, 35, 36, 38, 39]}
tried many variations like:
select id, name, segment_ids->>"m"
where 5 = any(json_array_element(segment_ids->>"m")
You are looking for the contains operator #>
select *
from muscle_groups
where segment_ids #> '{"m": [5]}'

Turn nested lists into a table Python

sorry for my annoying questions again.
Having a bit of trouble with some code. The task is to write a function that takes a nested list of currency conversions and turn it into a table (I've attached some pictures for clarification)
(could only attach one image, this is the nested list it converts)
[[10, 9.6, 7.5, 6.7, 4.96], [20, 19.2, 15.0, 13.4, 9.92], [30, 28.799999999999997, 22.5, 20.1, 14.879999999999999], [40, 38.4, 30.0, 26.8, 19.84], [50, 48.0, 37.5, 33.5, 24.8], [60, 57.599999999999994, 45.0, 40.2, 29.759999999999998], [70, 67.2, 52.5, 46.900000000000006, 34.72], [80, 76.8, 60.0, 53.6, 39.68], [90, 86.39999999999999, 67.5, 60.300000000000004, 44.64], [100, 96.0, 75.0, 67.0, 49.6]]
I've got the Header column for the table to work fine.
I'm having issues when I'm trying to iterate over each sublist in the nested list, convert it to a string (and two decimal places) and with a tab between each entry.
the code I've got so far is:
def printTable(cur):
list2 = makeTable(cur)
lst1 = Extract(cur)
lst1.insert(0, "$NZD")
lne1 = "\t".join(lst1)
print(lne1)
list=map(str,list2)
print(list2)
for list in list2:
for elem in list:
linelem = "\t".join(elem)
print(linelem)
printTable(cur)
(Note: The first function I call and assign to list2 is what generates the data/nested list)
I've tried playing around a bit but I keep coming up with different error messages trying to convert each sublist to a string.
Thank you all for your help!![enter image description here][1]
Try using this method;
import pandas as pd
import csv
with open("out.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(a)
pd.read_csv("out.csv", header = [col1, col2, col3, col4, col5])

SQL Server substring(substring())

Using two times the substing() is giving me an error.
Is there a way to get the same result ?
select [Identifiant] as [ID payment],
[Etat de la dépense] as [Status payment],
[Code bénéficiaire] as [Recipient code of payment],
substring(substring([Information projet], 83, 100) as [sting], 1, PATINDEX('%Code Branche%', [string])-1) as [Recipient of payment]
into [DB].[dbo].[Check_Result]
from [DB].[dbo].Expenses_SAP$
You can't alias the inner substring. It's unclear what you would try to achieve by doing this, so I can't really suggest the correct way to do whatever it is.
Take out as [sting] and at least you should have valid syntax:
substring(substring([Information projet], 83, 100), 1, PATINDEX('%Code Branche%', [string])-1) as [Recipient of payment]
I have a feeling that you're trying to do something like the following...
SELECT
es.Identifiant as [ID payment],
es.[Etat de la dépense] as [Status payment],
es.[Code bénéficiaire] as [Recipient code of payment],
substring(ss.sub_string, 1, PATINDEX('%Code Branche%', ss.sub_string)-1) as [Recipient of payment]
into [DB].[dbo].[Check_Result]
FROM
DB.dbo.Expenses_SAP es
CROSS APPLY ( VALUES (substring([Information projet], 83, 100)) ) ss (sub_string);

Mongodb Aggregate lookup all products from one specific client

I have a Database from a .data and these collections: clientes, mercancias and vagones. I want to find all the data of mercancias from the same client. So in this case, "Electronica Chispas" has 2 "mercancia" entities in the database:
[{"cliente": {"nombre": "Cafes el amanencer"},
"mercancia": {"envio": "Normal", "tipo": "Gaseoso", "fecha": "24/12/2003", "peso": 21, "volumen": 43, "origen": "Cadiz", "destino": "Castellon"},
"vagon": {"id": 1330, "volumen": 202, "peso": 433 }},{"cliente": {"nombre": "Electronica Chispas"}, "mercancia": {"envio": "Normal", "tipo": "Liquido", "fecha": "08/02/2005", "peso": 17, "volumen": 24, "origen": "San Sebastian", "destino": "Orense"}, "vagon": {"id": 1290, "volumen": 111, "peso": 464 }},{"cliente": {"nombre": "Electronica Chispas"}, "mercancia": {"envio": "Urgente intradia", "tipo": "Contaminante", "fecha": "15/09/2002", "peso": 11, "volumen": 83, "origen": "Valladolid", "destino": "Ciudad Real"}, "vagon": {"id": 1315, "volumen": 115, "peso": 481 }}]
I am trying this query:
db.mercancias.aggregate([{$lookup:{'from': 'clientes', 'localField':'destino', 'foreignField': 'nombre', 'as': 'clientes'}}])
But it is not working how I want to. Of course, I am missing to write the "Electronica Chispas" name somewhere in that query, but I am not sure where to write it.
In MongoDB, having duplicate data is not important nor an issue!
Making how to join was impossible. I decided to add in mercancias an id of vagonand nombreof clientes, so the relation could be named. That way, there was no need to do LookUpin mongo, and a "simple" aggregate query could be done!

Resources