Many-to-Many field find record - django

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. :)

Related

COGNOS report poor performance when using repeater

I have a table with 3 columns: CLIENT_ID, STORE_ID and MADE_PURCHASE. Basically I'm trying to get a list of CLIENT_ID and an array of STORE_ID where a customer made a purchase. For the following data, here is the expected result:
DATA:
CLIENT_ID
STORE_ID
MADE_PURCHASE
1
a
YES
1
b
YES
1
c
YES
2
a
YES
2
b
NO
2
c
YES
3
a
NO
3
b
NO
3
c
NO
Expected result:
CLIENT_ID
STORE_ID
1
a,b,c
2
a,c
I was able to achieve the desired result by creating a query to filter out lines where MADE_PURCHASE = 'NO'. Then I created a list in the report. The first column is CLIENT_ID then I insert a repeater in the second column that contains STORE_ID.
The problem is that the repeater slows my report by a factor about equal to the number of CLIENT_ID retrieved. For example if I run the query without a repeater and it returns 10 unique CLIENT_ID in 10 seconds, then adding the repeater slows the report to 100 seconds. As soon as I enter more than a few hundred CLIENT_ID in the prompt the report takes multiple hours to run.
I tried editing the master-detail relatioship between the list and the repeater without much change. Anyone has any idea how I could make it run faster?
P.S. I know the desired output format is not ideal but the goal is to mimic a legacy report that was built on excel using concatenate on STORE_ID, as such, the client wants to keep the original format.
You can try to edit the FM - Governors with the parameter (DQM) Master-Detail Optimization with "Cache Relaional Detail Query".

Django ORM Count group and subgroup

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

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.

I need serious help in changing the vaue of a column within the same column by going 2 rows up and changing the value

I am a DBA of 7 months so please bear with me. I am needing to write a code that will find a particular ProductIdentifier. When this particular ProductIdentifier is found, 1. I need to grab this ProductIdentifier. 2. I need to go 2 rows up and place that ProductIdentifier in the field that is 2 rows above it.
Here is my code(everything is sorted properly already in this table)
SELECT
SipID,
SaleInvoiceID,
AssociationNumber,
Priority,
TotalPrice,
TotalCost,
SerialNumber,
ContractNumber,
ActivatedThroughPAW,
DateCreatedatMidnight,
ReceivedDate,
InvoiceIDByStore,
Location,
ProductIdentifier,
Description,
ShortDescription,
CategoryName,
RevenueStreamID,
RevenueType
FROM REVISEDTABLE.
I will better show you what needs to be done ![enter image description here][1]
ProductIdentifier
AWUPG2001RGP -- replace this product identifier with the 'AWRPNS000%'
POSC0021PRW
AWRPNS000343 --take this product identifier
What I need for this code to do is this: whenever I find any ProductIdentifier like 'AWRPNS000%', I need for the query to take this and go 2 rows up and replace whatever ProductIdentifier is in this with 'AWRPNS000%'. I then need to insert the results into a table. I believe the best thing to do is to select the ProductIdentifier row again and give it an alias. This will be the row that I need to transform. I can then do a comparison to see if things worked out. I do not know how to write the code to do the actual grabbing of the ProductIdentifier and going up 2 rows and replacing it, so any help or input would be greatly appreciated.
So what does two rows up mean. Why is it two rows up.
e.g.
ID Class Type Date
1 1 2 20/12/2012
2 1 2 21/12/2012
3 1 2 22/12/2012 *
ie yes ID is two rows up but that's because The records are in ID and date order and there are at least three of them.
If you can come up with that rule e.g.
Select * From SomeTable Where Class = 1 and Type = 2 And Date = 20/12/2012
Then all your problems go away...