how to get latest second row in django? - django

can anyone help me I'm new to django so I don't know how to make sql queries into django code.
my sql table is :
select * from mangazones.mangabank_comic_banks;
the table image:
sql table image
then for second row query :
WITH added_row_number AS (SELECT comic_chapter,comic_english_name_id, row_number() OVER(PARTITION BY comic_english_name_id ORDER BY comic_chapter DESC) AS row_num FROM mangazones.mangabank_comic_banks) SELECT * FROM added_row_number WHERE row_num = 2;
I get table:
seond row table
I want second row table query in django also.
Can anyone please help me?

You can get the latest or latest 2nd value in Django model:
If you want to filter using primary key:
latest_second = Model_Name.objects.filter().order_by('-pk')[1]
If you have created date and time you can do it such way:
latest_second = Model_Name.objects.filter().order_by('-created')[1]
here, filter() is for filtering certain items.
if you don't want to filter items then you can use:
latest_second = Model_Name.objects.all().order_by('-pk')[1]

Related

How to convert this SQL query to Django Queryset?

I have this query which selects values from two different tables and used array agg over matched IDs how can I get same results using the queryset. Thank you!
select
sf.id_s2_users ,
array_agg(sp.id)
from
s2_followers sf
left join s2_post sp on
sp.id_s2_users = sf.id_s2_users1
where
sp.id_s2_post_status = 1
and sf.id_s2_user_status = 1
group by
sf.id_s2_users
You can run raw SQL queries with Django's ORM if that's what you wanted. You don't have to change your query in that case, you can check documentation here.

Show all rows as default in calendar in Oracle Apex

I'm creating a report table type calendar where users can create back up by date select a filter that would filter out the table values depending on the user selected. (i.e. if they choose user1, then only back ups with user1 will show up)
I would like it to be when P106_BACK_UP_BY_USER = 0, the table shows all the values (aka getting rid of the "where" portion of the query.
Thank you for your help!
I'm having issues with trying to allow the user to see all the back ups of the table again (getting rid of the filtered value). My current query is this:
I would like it to be when P106_BACK_UP_BY_USER = 0, the table shows all the values (aka getting rid of the "where" portion of the query.
Thank you for your help!
You can use case when statements in your query's where condition as follows:
select *
from my_table
where my_table.created_by =
(select user_name from my_table2 where app_users_id =
case :P106_BACKUP_BY_USER when 0 then app_users_id
else :P106_BACKUP_BY_USER
end)
And for getting better help, please paste your code as text not as an image next time.
This should work too:
...
WHERE b.active_server = s.server_id
AND (:P106_BACK_UP_BY_USER = 0 OR
UPPER(b.created_by) =
(SELECT UPPER(user_name)
FROM eba_bt_app_users
WHERE app_users_id = :P106_BACK_UP_BY_USER
)
);

Power Query M formula language foreign key checking

I am currently using the Power BI Power Query Editor writing queries using the Power Query M formula language. I currently have two tables named employee and business. Each row of the employee table has a business_id that connects to a row of the business table. I want to run a Table.SelectRows function to include only the rows where employee.business_id = business.id.
This is what I have so far:
let
Source = MySQL.Database(<DATABASE>, "business_database", [ReturnSingleDatabase=true, CreateNavigationProperties=false]),
business_database_employee_all = Source{[Schema="business_database",Item="employee"]}[Data],
employee_included = . . . Return all rows from employee where employee.business_id = business.id . . .
in
employee_included
Any sort of help with this one would be appreciated! I'm pretty set on using Table.SelectRows but I'm down to utilize better functions if it's recommended!
It sounds like you want to filter the employee table so that it only shows rows where there is a match in the business_id field to the business table
the best way is to merge the business table into the employee table, matching the business_id field, with inner join
#"Merged Queries" = Table.NestedJoin(#"PriorStepNameinEmployeeTable",{"business_id"},business,{"business_id"},"business",JoinKind.Inner)
another way
#"Select" = Table.SelectRows(#"PriorStepNameinEmployeeTable", each Table.Contains(business_table,_,{"business_id"}))

Is there any way to convert this into django orm?

I needed to get all the rows in the table1 even if it is not existing in table2 and display it as zero. I got it using raw sql query but in django ORM i am getting the values existing only in table2. The only difference on my django orm is that iI am using inner join while in the raw sql query I am using left join. Is there any way to achieve this or should I use raw sql query? Thanks.
Django ORM:
total=ApplicantInfo.objects.select_related('source_type').values('source_type__source_type').annotate(total_count=Count('source_type'))
OUTPUT OF DJANGO ORM IN RAW SQL:
SELECT "applicant_sourcetype"."source_type", COUNT("applicant_applicantinfo"."source_type_id") AS "total_count" FROM "applicant_applicantinfo" INNER JOIN "applicant_sourcetype" ON ("applicant_applicantinfo"."source_type_id" = "applicant_sourcetype"."id") GROUP BY "applicant_sourcetype"."source_type"
RAW SQL:
SELECT source.source_type, count(info.source_type_id) as total_counts from applicant_sourcetype as source LEFT JOIN applicant_applicantinfo as info ON source.id = info.source_type_id GROUP BY source.id
You cannot query on ApplicantInfo if you want a left join with it. Query on SourceType instead:
qs = (SourceType.objects
.values('source_type')
.annotate(cnt=Count('applicantinfo'))
.values('source_type', 'cnt')
)
Since you haven't posted the models, you may need to adapt the field names to make it work.

Getting Max count based on Vehid group by Houseid Django

This is the image of the table and output required with details
So, I wrote a Django query
ModelClassName.objects.values('houseid','vehid').annotate(Count('vehid')).order_by('houseid')
This is giving me the count of each vehid but as I am not sure how to incorporate Max here, I am unable to get the right result.
Any help is appreciable.
Thanks.
Create a view to get the counts:
SELECT HOUSEID, VEHID, COUNT(*) FROM TABLE GROUP BY HOUSEID, VEHID
Then query the View
Select Houseid, VEHID, COUNT from MYVIEW A where A.COUNT = (Select Max(B.count) FROM MYVIEW B WHERE A.HOUSEID = B.HOUSEID)
Then you can use Django SQL (https://docs.djangoproject.com/en/2.0/topics/db/sql/) to help you.
(test and let me know if that worked)