I need to search in database, below is my table and what I need to search for and How I will expect my output
Category table
Id
Name
1
DryFruit
2
Flower
category_attachment table
Id
attachmentName
catid
1
Almond
1
2
Cookie-flower
1
3
Cashew
1
4
Rose
2
Now when I search flower, below is my expected output,
category.Id
category.Name
category.attachment.attachmentName
2
Flower
null
null
null
Cookie-flower
So, can someone help me how to write a query for this?
Related
I have a table in qlik Sense loaded from the database.
Example:
ID
FRUIT
VEG
COUNT
1
Apple
5
2
Figs
10
3
Carrots
20
4
Oranges
12
5
Corn
10
From this I need to make a filter that will display all the Fruit/Veg records along with records from other joined tables, when selected.
The filter needs to be something like this :
|FRUIT_XXX|
|VEG_XXX |
Any help will be appreciated.
I do not know how to do it in qlicksense, but in SQL it's like this:
SELECT
ID
CASE WHEN FRUIT IS NULL THEN VEG ELSE FRUIT END as FruitOrVeg,
COUNT
FROM tablename
Not sure if its possible to be dynamic. Usually I solve these by creating a new field that combines the values from both fields into one field
RawData:
Load * Inline [
ID , FRUIT ,VEG , COUNT
1 , Apple , , 5
2 , Figs , , 10
3 , ,Carrots , 20
4 , Oranges , , 12
5 , ,Corn , 10
];
Combined:
Load
ID,
'FRUIT_' & FRUIT as Combined
Resident
RawData
Where
FRUIT <> ''
;
Concatenate
Load
ID,
'VEG_' & VEG as Combined
Resident
RawData
Where
VEG <> ''
;
This will create new table (Combined) which will be linked to the main table by ID field:
The new Combined field will have the values like this:
And the UI:
P.S. If further processing is needed you can join the Combined table to the RawData table. This way the Combined field will become part of the RawData table. To achieve this just extend the script a bit:
join (RawData)
Load * Resident Combined;
Drop Table Combined;
Let's say I have the following table in Postgres:
fruit
fruit_id owner_id fruit_price notes
-------------------------------------------
1 5 15
2 5 30
3 5 20
4 8 10
5 8 80
I am looking for a way to update the cheapest fruit owned by someone.
That is, I am looking for an operation that would allow me to set the notes column for the cheapest fruit owned by an individual. So this should only ever update one row (updating multiple rows is fine if there are several ties for the smallest value).
For example (psuedocode):
UPDATE fruit SET notes = 'wow cheap' WHERE owner_id = 5 AND fruit_price IS cheapest;
And this would update the first row in the above example data, because fruit_id of 1 is the cheapest fruit owned by user 5.
One possible way is simply to use a correlated subquery:
update fruit set
notes = 'some notes'
where owner_id = 5
and fruit_price = (
select min(fruit_price) from fruit f2
where f2.owner_id = fruit.owner_id
);
For background purposes: I'm using PostgreSQL with SQLAlchemy (Python).
Given a table of unique references as such:
references_table
-----------------------
id | reference_code
-----------------------
1 | CODEABCD1
2 | CODEABCD2
3 | CODEWXYZ9
4 | CODEPOIU0
...
In a typical scenario, I would have a separate items table:
items_table
-----------------------
id | item_descr
-----------------------
1 | `Some item A`
2 | `Some item B`
3 | `Some item C`
4 | `Some item D`
...
In such typical scenario, the many-to-many relationship between references and items is set in a junction table:
references_to_items
-----------------------
ref_id (FK) | item_id (FK)
-----------------------
1 | 4
2 | 1
3 | 2
4 | 1
...
In that scenario, it is easy to model and obtain all references that are associated to the same item, for instance item 1 has references 2 and 4 as per table above.
However, in my scenario, there is no items_table. But I would still want to model the fact that some references refer to the same (non-represented) item.
I see a possibility to model that via a many-to-many junction table as such (associating FKs of the references table):
reference_similarities
-----------------------
ref_id (FK) | ref_id_similar (FK)
-----------------------
2 | 4
2 | 8
2 | 9
...
Where references with ID 2, 4, 8 and 9 would be considered 'similar' for the purposes of my data model.
However, the inconvenience here is that such model requires to choose one reference (above id=2) as a 'pivot', to which multiple others can be declared 'similar' in the reference_similarities table. Ref 2 is similar to 4 and ref 2 is similar to 8 ==> thus 4 is similar to 8.
So the question is: is there a better design that doesn't involve having a 'pivot' FK as above?
Ideally, I would store the 'similarity' as an Array of FKs as such:
reference_similarities
------------------------
id | ref_ids (Array of FKs)
------------------------
1 | [2, 4, 8, 9]
2 | [1, 3, 5]
..but I understand from https://dba.stackexchange.com/questions/60132/foreign-key-constraint-on-array-member that it is currently not possible to have foreign keys in PostgreSQL arrays. So I'm trying to figure out a better design for this model.
I can understand that you want to group items in a set, and able to query the set from any of item in it.
You can use a hash function to hash a set, then use the hash as pivot value.
For example you have a set of values (2,4,8,9), it will be hashed like this:
hash = ((((31*1 + 2)*31 + 4)*31 + 8)*31 + 9
you can refer to Arrays.hashCode in Java to know how to hash a list of values.
int result = 1;
for (Object element : a)
result = 31 * result + (element == null ? 0 : element.hashCode());
Table reference_similarities:
reference_similarities
-----------------------
ref_id (FK) | hash_value
-----------------------
2 | hash(2, 4, 8, 9) = 987204
4 | 987204
8 | 987204
9 | 987204
To query the set, you can first query hash_value from ref_id first, then, get all ref_id from hash_value.
The draw back of this solution is every time you add a new value to a set, you have to rehash the set.
Another solution is you can just write a function in Python to produce a unique hash_value when creating a new set.
I have a dataframe like this :
user_id items
1 item1
1 item2
1 item3
2 item1
2 item5
3 item4
3 item2
If I put user_id as row and items as columns, I get this :
user_id number_of_items
1 3
2 2
3 2
Now I would like to group this result again, like this :
number_of_user_id number_of_items
1 3
2 2
How can I do this, as a calculated field or in a graph(maybe an histogramm?)
First create the following calculated field, called users_per_item
{ fixed items : countd(user_id) }
Then highlight the new measure you just created in the data pane, users_per_item and right click to create Bins. Set the bin size to 1 or whatever value you like. That will create a dimension called users_per_item (bin)
Finally, you can now use the bin field to create the view you want, say place users_per_item (bin) on the columns shelf and CNTD(items) on the rows shelf.
A natural use for LOD calculations for a 2 stage analysis.
I have a table with one to many to many relation ie
ManyToMany table:
MenuGroup
menuid groupid
1 4
1 5
Menu
menuid name
1 One
2 Two
Group
groupid name
4 group4
5 group5
Groupuser
groupid userid
4 101
4 103
5 102
i would like to get all menus of the user 101
ie
Menuid groupid name
1 4 group4
But i am getting the wrong out put eventhough writing the correct join queries. Can anyone help what am i doing wrong here?
Menuid groupid name
1 4 group4
1 5 group5
(from m in context.Menus
join mg in context.MenuGroup on m.MenuId equals mg.MenuId
join gu in context.Groupuser on mg.GroupId equals gu.GroupId
where gu.UserId == 101
select m);
i would like to include only this particular user's group details though this menu is in other group as well.
my expected output in json would be
{
"menuid": 1,
"name": "One",
"groups":[
{
"groupid":4,
"name":"group4"
}
]
}
Your linq query looks good, I suspect data issue, but would you like to try the following query to see what you get back. This following query requires navigation properties declared.
var userMenus = context.GroupUser.Where(u=>u.UserId = 101).SelectMany(g=>g.Group.Menus
.Select(m=> new {Menu=m.MenuId, GroupId=g.GroupId,GroupName=g.Group.name))
.ToList();
In case you want the complete Menu object
var userMenus = context.GroupUser.Where(u=>u.UserId = 101).SelectMany(g=>g.Group.Menus
.Select(m=> new {Menu=m.Menu, GroupId=g.GroupId,GroupName=g.Group.name))
.ToList();
In case you don't care about Group columns and just want Menu then
var userMenus = context.GroupUsers.Where(u => u.UserId == 101)
.SelectMany(g => g.Group.Menus.Select(m=>m.Menu));