Getting Max count based on Vehid group by Houseid Django - 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)

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.

Query for listing Datasets and Number of tables in Bigquery

So I'd like make a query that shows all the datasets from a project, and the number of tables in each one. My problem is with the number of tables.
Here is what I'm stuck with :
SELECT
smt.catalog_name as `Project`,
smt.schema_name as `DataSet`,
( SELECT
COUNT(*)
FROM ***DataSet***.INFORMATION_SCHEMA.TABLES
) as `nbTable`,
smt.creation_time,
smt.location
FROM
INFORMATION_SCHEMA.SCHEMATA smt
ORDER BY DataSet
The view INFORMATION_SCHEMA.SCHEMATA lists all the datasets from the project the query is executed, and the view INFORMATION_SCHEMA.TABLES lists all the tables from a given dataset.
The thing is that the view INFORMATION_SCHEMA.TABLES needs to have the dataset specified like this give the tables informations : dataset.INFORMATION_SCHEMA.TABLES
So what I need is to replace the *** DataSet*** by the one I got from the query itself (smt.schema_name).
I am not sure if I can do it with a sub query, but I don't really know how to manage to do it.
I hope I'm clear enough, thanks in advance if you can help.
You can do this using some procedural language as follows:
CREATE TEMP TABLE table_counts (dataset_id STRING, table_count INT64);
FOR record IN
(
SELECT
catalog_name as project_id,
schema_name as dataset_id
FROM `elzagales.INFORMATION_SCHEMA.SCHEMATA`
)
DO
EXECUTE IMMEDIATE
CONCAT("INSERT table_counts (dataset_id, table_count) SELECT table_schema as dataset_id, count(table_name) from ", record.dataset_id,".INFORMATION_SCHEMA.TABLES GROUP BY dataset_id");
END FOR;
SELECT * FROM table_counts;
This will return something like:

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
)
);

Return all rows for ID where a condition is met on one row

Im really new to sql and have written a query like this in Athena:
CREATE OR REPLACE VIEW vw_em AS
SELECT *
FROM
s_prod_m.metm_inm
WHERE ((channel IN (SELECT msn
FROM
smartco_prod_meterdata.meter_interval
WHERE (channel = '1825')
)) AND (interval_date BETWEEN (current_date - INTERVAL '1' MONTH) AND current_date))
Basically what Im trying to do is return all rows for an ID (msn) if any of the rows have 1825 in the channel column.
The query works in that it creates a view for me but when I try to preview the view with a limit of 10 it takes ages to run (I stopped it after 10 mins) so I dont know if this is even the correct way of doing it, if it isnt can someone help with a solution if it is, is there a better/faster way to achieve this result?
Thanks

how to get latest second row in 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]