Find and combine results from two columns based on same search criteria - arrays

I am setting up a staff database and some of the staff have split roles across two teams in the same department. I am using the QUERY function on the Dashboard tab to bring up details for various teams, searching via manager or team. The problem I have is when trying to factor in the split roles into the searches.
For example, an employee who works part-time in one team and part-time in another will be listed under two managers and two teams in the main database. See image below:
What I want it to come up with is as follows when you search for either Manager or Team:
Staff Member - Hours - Role
Person 1 - - - - 37 - - A
Person 2 - - - - 37 - - A
Person 4 - - - - 10 - - B
So all the people who work for a certain manager, whether the role is 1 or role 2 would be shown.
I have tried the following to combine the two column results:
={query(StaffData2,"select B,AT where K = '"&Dashboard!O8 &"' and AT is not null",1);query(StaffData2,"select B,AU where AO = '"&Dashboard!O8&"' and AU is not null",0)}
It seems to half work when the employee in a team does have a split role but it adds them as another entry on to the bottom of the results eg:
Person 1
Person 2
Person 1
Alternatively, if there are no split roles then the result comes back with an ARRAY LATERAL error.
Any help would be great.

the proper way would be:
=QUERY({B3:E13; B3:B13, F3:H13},
"select Col1,Col2,Col4 where Col3 = '"&C16&"'", 1)

Related

Simple database design - some columns have multiple values

Caveat: very new to database design/modeling, so bear with me :)
I'm trying to design a simple database that stores information about images in an archive. Along with file_name (which is one distinct string), I have fields like genre and starring where each field might contains multiple strings (if an image is associated with multiple genres, and/or if an image has multiple actors in it).
Right now the database is just a single table keyed on file_name, and the fields like starring and genre just have multiple comma-separated values stored. I can query it fine by using wildcards and like and in operators, but I'm wondering if there's a more elegant way to break out the data such that it is easier to use/query. For instance, I'd like to be able to find how many unique actors are represented in the archive, but I don't think that's possible with the current model.
I realize this is a pretty elementary question about data modeling, but any guidance anyone can provide or reading you can direct me to would be greatly appreciated!
Thanks!
You need to create extra tables in order to stick with the normalization. In your situation you need 4 extra tables to represent these n->m relations(2 extra would be enough if the relations were 1->n).
Tables:
image(id, file_name)
genre(id, name)
image_genres(image_id, genre_id)
stars(id, name, ...)
image_stars(image_id, star_id)
And some data in tables:
image table
id
file_name
1
/users/home/song/empire.png
2
/users/home/song/promiscuous.png
genre table
id
name
1
pop
2
blues
3
rock
image_genres table
image_id
genre_id
1
2
1
3
2
1
stars table
id
name
1
Jay-Z
2
Alicia Keys
3
Nelly Furtado
4
Timbaland
image_stars table
image_id
star_id
1
1
1
2
2
3
2
4
For unique actor count in database you can simply run the sql query below
SELECT COUNT(name) FROM stars

Power BI - TopN + Others on data from two tables

I am a bit stuck with a specific case in Power BI. Let's say that we have two tables. The first one contains the product ID and the product name, and the second one contains the product ID and a specific budget.
I want to create a piechart showing the topN + group others. I have made a dax formula which works for data in a single table, but not on two.
Here is the formula :
ProductTop =
VAR rankSiteImpressions = RANKX(ALL(Piechart); [Impressions ];;DESC)
return
IF(rankSiteImpressions<=3;Piechart[Site];"Others")
How can I apply this on data from two tables to get the top products by budget?
Many thanks,
RĂ©mi

Find duplication in multi tables

We have 4 tables
Student, School, Location and StudentSchool
Students can can have same name but they are different persons
each student can be in one school only and each school located in one location
We found out that same student is somehow located in 2 different school
In this example "Adam Mike" with the Id '1' is located in 2 different schools in different locations.
How can i find a list of all students who are located in 2 different school?
You only need to search one table
select student_id
from school_student
group by student_id
having count(*) > 1
In the result, group students by name (group by in SQL), producing their count in the group (count(*) in SQL), then filter only those who have count > 1 (having count(*) > 1 in SQL).
I don't know how to express it in the query building tool, but it must support it.
Note that such grouping will lose IDs and school names; you will have to query for them again using the names.

Is this use case a candidate for Graph Database application?

Consider I have some users U1, U2, U3 each with property 'age' such that;
U1.age = 10
U2.age = 30
U3.age = 70
I also have some lists which are dynamic collections of users based on some criteria, say L1, L2, L3, such that;
L1: where age < 60
L2: where age < 30
L3: where age > 20
Since the lists are dynamic, the relationship between lists and users is established only through the user properties and list criteria. There is no hard mapping to indicate which users belong to which list. When the age of any user changes or when the criteria of any list changes, the users associated with a list may also change.
In this scenario, at any point of time it is very easy to get the users associated with a list by querying users matching the list criteria.
But to get the lists associated with a user, is an expensive operation which involves first determining users associated with each list and then picking those lists where the result has the user in question.
Could this be a candidate for using Graph Database? And why? (I'm considering Neo4j) If yes, how to model the nodes and the relationships so that I can easily get the lists given a user.
Since 2.3 Neo4j does allow index range queries. Assume you have an index:
CREATE INDEX on :User(age)
Then this query gives you the list of people younger 60 years and is performed via the index
MATCH (u:User) WHERE u.age < 60 RETURN u
However I would not store the age, instead I'd store the date of birth as a long property. Otherwise you have can the age over and over again.
Update based on comment below
Assume you have a node for each list:
CREATE (:List{name:'l1', min:20, max:999})
CREATE (:List{name:'l2', min:0, max:30})
CREATE (:List{name:'l3', min:0, max:60})
Let's find all the lists a user U1 belongs to:
MATCH (me:User{name:'U1'})
WITH me.age as age
MATCH (l:List) WHERE age >= l.min AND age <= l.max // find lists
WITH l
MATCH (u:User) WHERE u.age >= l.min AND age <= l.max
RETURN l.name, collect(u)
Update 2
A complete different idea would be to use a timetree. Both, all users and your list definitions are connected to the timetree

Database design for voting

I am implementing a voting feature to allow users to vote for their favourite images. They are able to vote for only 3 images. Nothing more or less. Therefore, I am using checkboxes to do validation for it. I need to store these votes in my database.
Here is what i have so far :
|voteID | name| emailAddress| ICNo |imageID
(where imageID is a foreign key to the Images table)
I'm still learning about database systems and I feel like this isn't a good database design considering some of the fields like email address and IC Number have to be repeated.
For example,
|voteID | name| emailAddress | ICNo | imageID
1 BG email#example.com G822A28A 10
2 BG email#example.com G822A28A 11
3 BG email#example.com G822A28A 12
4 MO email2#example.com G111283Z 10
You have three "things" in your system - images, people, and votes.
An image can have multiple votes (from different people), and a person can have multiple votes (for different images).
One way to represent this in a diagram is as follows:
So you store information about a person in one place (the Person table), about Images in one place (the Images table), and Votes in one place. The "chicken feet" relationships between them show that one person can have many votes, and one image can have many votes. ("Many" meaning "more than one").

Resources