Django ORM Count group and subgroup - django

In my django project i had a table with a column named 'key_id'. Until today i had to group different values of this values, count it and display the largest result.
I did this:
maxpar = temp_test_keywords.objects.filter(main_id=test_id).values('key_id').annotate(total=Count('key_id')).order_by('-total').first()
maxMax = maxpar['total']
all done.
Today, we deceide to add another field to table, 'key_group'(could be 1,2,3,4) and now i have to use the same query for group and count max key_id field but related also for key_group.
For example before if in my table there was 5 record with key_id=187 and 3 with 112 my query had to return 5, now if for that 5 records 4 contain 'key_group=1' and one = 2 query have to return 4
Hope i was clear
Thanks in advance
Luke

Related

SSRS grouping all but one field

I have a query that all the fields are the same except for one. I want to group my SSRS report so that it has "blanks" for all the duplicated fields and only show the "different" one WHEN there is a "duplicate" record.
For instance:
Case Number PersonID Narrative
123 1 xxx
345 3
456 9 ABCD
KFL
So record 1 has a narrative and only one record. Record 2 has no narrative. Record 3 & 4 are the same case, same person, two different narratives.
I thought by grouping by all the other fields that I would achieve these results but that is not working, I still get the 456 and 9 on my 4th record when I have grouped by the other fields.
How can I get just the narrative to display when all the other fields in that record match the previous record?
Thanks,
Leslie
You can see my answer for:
How to get only one value in SSRS?
You have similar situation. You need to use expression for first 2 columns:
=IIF(Fields!CaseNumber.Value = Previous(Fields!CaseNumber.Value), "", Fields!CaseNumber.Value)
=IIF(Fields!PersonID.Value = Previous(Fields!PersonID.Value), "", Fields!PersonID.Value)
This will hide all repeating "Case Number" and "PersonID".
Don't forget to replace "CaseNumber" and "PersonID" with proper column names in DataSet.

How to fetch last inserted record for particular id?

Apologies, I am completely new to Django. My question is that I have 20 records in my database table and suppose 10 record is of same ID and I want to fetch last inserted record for that id I have date column in my table. How can I do that?
last_obj = YourModel.objects.last()
But generally, you can't create > 1 objects with same id, if you didn't specified your own id field to replace built-in. And even then, it's a bad idea.

Django inner join on nulls

I currently have Django models like this
MyFirstObject(models.Model):
some_field = models.BooleanField(default=False)
MySecondObject(models.Model):
first_object = models.ForeignKey(MyFirstObject, db_column='firstObjectId')
Because of various issues, our data integrity is corrupt. So, I need to find instances where MyFirstObject has been deleted, but MySecondObject still has a row w a foreign key to it.
The database would look similar to:
TABLE my_first_object
id someField
1 a
2 a
3 b
TABLE my_second_object
id firstObjectId
1 1
2 3
3 4
Notice row 3 of the TABLE my_second_object has an firstObjectID that does not have a corresponding record in the my_first_object table. I want to find all instances like that.
If I was doing raw SQL, I would do
SELECT my_second_object.id, my_second_object.firstObjectId
FROM my_second_object
LEFT JOIN ON ( my_second_object.firstObjectId = my_first_object.id )
WHERE my_first_object.id IS NULL
In Djago, I am trying
MySecondObject.objects.filter(my_first_object__id__isnull=true)
But when I look at the query that results, it is doing an inner join instead of left join. Does anyone have suggestions? Thanks!
Try like this:
first_object_ids = MyFirstObject.objects.values_list('id')
get_second_objects = MySecondObject.objects.exclude(my_first_object_id__in = first_object_ids)

APEX SELECT LIST unwanted value

I have a select list that insists on display an numeric extra value, based on an id. I didn't get this issue with another select lists.
The basic query is
select description,id
from situation
where ID = 2 and user_id = 1
UNION
select description,id
from situation
where ID = 3 OR ID = 4 and user_id = 5
The select list get an numeric value that represents the previous id from the query. I have tried with a simple query but doesn't work.
When i try it on sql command, works fine.
Anyone could help me?

Many-to-Many field find record

I want to query the database and get the data of against id which have highest occurence in the database table.
story table
id story_name
1 this is one
2 this is two
story_story_table
id story_id
1 1
2 1
3 1
4 2
5 3
now id 1 have highest occurence. first find highest occurence then get data against this id. using django
You can find the highest occurrence using django's ORM built in API.
unique_fields = ['story_id','story_name_id']
duplicates = (Model.objects.values(*unique_fields)
.annotate(max_id=models.Max('id'),
count_id=models.Count('id'))
.filter(count_id__gt=1)
.order_by())
This will gives repeated records as per given fields and to highest occurrence you can simply use len(duplicates)
Edited:
highest_occured = (Model.objects.values(*unique_fields)
.annotate(max_id=models.Max('id'),
count_id=models.Count('id'))
.filter(count_id=models.Max(count_id))
.order_by())
I hope this will be help full. :)