Django Advanced DB Query - django

I'm running a very complicated query against oracle to return some data. I'm running it manually because django can't natively do what I am doing from what I have read.
I run the query but django doesn't get any values in return. I recently switched from my dev database to my production to run the query because it has live data and the required
data doesn't exist in dev yet because its a new feature.
The query executes but doesn't return anything. However there is another very similarly query that I am running that is returning data. Also if I output the exact statement
django is using to make the query against oracle for the data that is not returning and then if i goto terminal and execute the query against the sql server manually it will return the data.
I also output the fetchall to the page as a debuging tool and the working query has all the info but the none working one outputs blank list.
At first I thought it was the query but i have proven the query django is using works but django gets no data. The only logical answer is django is trying to use the dev database or the models table something is off.
However I ran syncdb and sql and no errors came up... If I run the query against dev it doesnt return data, but does against production.
Here is the query I am running.
if 'customer_licenses_all' in request.POST:
return_query_type = 'customer_licenses_all'
cursor.execute("alter session set time_zone='UTC'")
selected_customer_id = int(selected_customer['selected_customer'])
rundate = selected_customer['compute_date']
date2 = datetime.strptime(rundate, "%d-%b-%Y")
date_list = ()
date_list = (date2.day, date2.month, date2.year)
last_date = get_last_day(date_list)
last_date = last_date.strftime("%d-%b-%Y")
sql = (('''select customer_name, count(*) license_count, sum(cpu_ghz_hours) CPU_Core_Hours, sum(ram_gb_hours) RAM_GB_Hours, licensing_authority, product
from customers a, vm_groups b, vms c, vm_license_histories d, licenses e, vm_compute_usage_histories f, license_authorities g
where a.customer_id = b.customer_id
and b.vm_group_id = c.vm_group_id
and c.vm_id = d.vm_id
and d.license_id = e.license_id
and f.vm_id = c.vm_id
and e.license_authority_id = g.license_authority_id
and trunc(f.datetime) = to_date(%s,'DD-MON-YYYY')
and inactive = 'N'
and (deassignment_date is null or trunc(deassignment_date) between to_date(%s,'DD-MON-YYYY') and last_day(to_date(%s,'DD-MON-YYYY')))
and cpu_ghz_hours > 0
and g.license_authority_id not in (28,27,31)
group by customer_name, licensing_authority, product order by 1,5,6''')% ("'"+rundate+"'","'"+ rundate+"'" ,"'"+last_date+"'"))
cursor.execute(sql)
return_query = cursor.fetchall()
context = Context({'customers': customers,
'return_query': return_query,
'return_query_type':return_query_type,
'rundate':rundate,
'last_date':last_date,
'sql':sql,
})
the django output
select customer_name, count(*) license_count, sum(cpu_ghz_hours)
CPU_Core_Hours, sum(ram_gb_hours) RAM_GB_Hours, licensing_authority,
product from customers a, vm_groups b, vms c, vm_license_histories d,
licenses e, vm_compute_usage_histories f, license_authorities g where
a.customer_id = b.customer_id and b.vm_group_id = c.vm_group_id and
c.vm_id = d.vm_id and d.license_id = e.license_id and f.vm_id =
c.vm_id and e.license_authority_id = g.license_authority_id and
trunc(f.datetime) = to_date('01-Jun-2014','DD-MON-YYYY') and inactive
= 'N' and (deassignment_date is null or trunc(deassignment_date) between to_date('01-Jun-2014','DD-MON-YYYY') and
last_day(to_date('30-Jun-2014','DD-MON-YYYY'))) and cpu_ghz_hours > 0
and g.license_authority_id not in (28,27,31) group by customer_name,
licensing_authority, product order by 1,5,6
[]

Related

PowerBI Query contains transformations that can't be used for DirectQuery

I am using PowerBI Desktop (2.96.1061.0) to connect to a local MS SQL server so I can prepare some visualizations. It is important to mention that all data connections (Tables, SQL queries) are using the DirectQuery option.
It's been quite a smooth experience so far. No issues at all. Now I am trying to get some new data, again, through a direct SQL query:
SELECT BillId, string_agg(PGroupName, ', ')
FROM
(SELECT bm.ImportedBillsId as BillId, pg.Name as PGroupName
FROM [BillMp] bm
JOIN [Mps] m on bm.ImportersId = m.Id
JOIN [PGroups] pg on m.PoliticalGroupId = pg.Id
GROUP BY bm.ImportedBillsId, pg.Name) t
GROUP BY BillId
but for some reason, it is not letting me re-create the model and apply the new changes. No matter that the import wizard is able to visualize the actual data prior to the update. This is the error that I am getting:
I have also tried to import only the data from the internal/nested query
SELECT bm.ImportedBillsId as BillId, pg.Name as PGroupName
FROM [BillMp] bm
JOIN [Mps] m on bm.ImportersId = m.Id
JOIN [PGroups] pg on m.PoliticalGroupId = pg.Id
GROUP BY bm.ImportedBillsId, pg.Name
and process (according to this article) the other/outer query through PowerBI but I am still getting the same error.

Oracle Apex IG force user to have filter on column

I need to force user to have filter on column with date. Dataset is rly big and it must-have. I know how to force user to have any filter. Just add to "Where":
:apex$f1 is not null
But I need to find how to force user to have filter on specific column
Like i wrote in comment this is solution (I couldn't find better one)
In 'Where Clause' I added this
EXISTS (SELECT /*+ NO_UNNEST */ F.*, R.*, C.* FROM apex_appl_page_ig_rpt_filters F, APEX_APPL_PAGE_IG_RPTS R, APEX_APPL_PAGE_IG_COLUMNS C
WHERE 1=1
AND F.report_id = R.report_id AND C.column_id = F.column_id
AND F.APPLICATION_ID = 100 AND F.PAGE_ID = 100
AND C.NAME = 'MY_COLUMN_NAME'
AND C.REGION_NAME = 'MY_REGION_NAME'
AND R.SESSION_ID = :APP_SESSION)
This checks setting of IG in apex objects. In addition you just need to wrote in Messages "When No Data Found" something about that column that is required, just to inform users.

Django How to select differrnt table based on input?

I have searched for the solution to this problem for a long time, but I haven't got the appropriate method.
Basically All I have is tons of tables, and I want to query value from different tables using raw SQL.
In Django, we need a class representing a table to perform the query, for example:
Routes.objects.raw("SELECT * FROM routes")
In this way, I can only query a table, but what if I want to query different tables based on the user's input?
I'm new to Django, back in ASP.NET we can simply do the following query:
string query = "SELECT * FROM " + county + " ;";
var bus = _context.Database.SqlQuery<keelung>(query).ToList();
Is this case, I can do the query directly on the database instead of the model class, and I can select the table based on the user's selection.
Is there any method to achieve this with Django?
You can run raw queries in Django like this -
From django.db import connection
cursor = connection.cursor()
table = my_table;
cursor.execute("Select * from " + table)
data = cursor.fetchall()

sqlite3 & python: get list of primary and foreign keys

I am very new to sql and intermediate at python. Using sqlite3, how can I get a print() list of of primary and foreign keys (per table) in my database?
Using Python2.7, SQLite3, PyCharm.
sqlite3.version = 2.6.0
sqlite3.sqlite_version = 3.8.11
Also note: when I set up the database, I enabled FKs as such:
conn = sqlite3.connect(db_file)
conn.execute('pragma foreign_keys=ON')
I tried the following:
conn=sqlite3.connect(db_path)
print(conn.execute("PRAGMA table_info"))
print(conn.execute("PRAGMA foreign_key_list"))
Which returned:
<sqlite3.Cursor object at 0x0000000002FCBDC0>
<sqlite3.Cursor object at 0x0000000002FCBDC0>
I also tried the following, which prints nothing (but I think this may be because it's a dummy database with tables and fields but no records):
conn=sqlite3.connect(db_path)
rows = conn.execute('PRAGMA table_info')
for r in rows:
print r
rows2 = conn.execute('PRAGMA foreign_key_list')
for r2 in rows2:
print r2
Unknown or malformed PRAGMA statements are ignored.
The problem with your PRAGMAs is that the table name is missing. You have to get a list of all tables, and then execute those PRAGMAs for each one:
rows = db.execute("SELECT name FROM sqlite_master WHERE type = 'table'")
tables = [row[0] for row in rows]
def sql_identifier(s):
return '"' + s.replace('"', '""') + '"'
for table in tables:
print("table: " + table)
rows = db.execute("PRAGMA table_info({})".format(sql_identifier(table)))
print(rows.fetchall())
rows = db.execute("PRAGMA foreign_key_list({})".format(sql_identifier(table)))
print(rows.fetchall())
SELECT
name
FROM
sqlite_master
WHERE
type ='table' AND
name NOT LIKE 'sqlite_%';
this sql will show all table in database, for eache table run sql PRAGMA table_info(your_table_name);, you can get the primary key of the table.
Those pictures show what sql result like in my database:
first sql result
second sql result

SqlAlchemy core union_all not adding parentheses

I have the following sample code:
queries = []
q1 = select([columns]).where(table.c.id == #).limit(#)
queries.append(q1)
q2 = select([columns]).where(table.c.id == #).limit(#)
queries.append(q2)
final_query = union_all(*queries)
The generated SQL should be this:
(select columns from table where id = # limit #)
UNION ALL
(select columns from table where id = # limit #)
But, I'm getting
select columns from table where id = # limit #
UNION ALL
select columns from table where id = # limit #
I tried using subquery, as follows for my queries:
q1 = subquery(select([columns]).where(table.c.id == #).limit(#))
The generated query then looks like this:
SELECT UNION ALL SELECT UNION ALL
I also tried doing
q1 = select([columns]).where(table.c.id == #).limit(#)).subquery()
But, I get the error:
'Select' object has no attribute 'subquery'
Any help to get the desired output with my subqueries wrapped in parentheses?
Note: this is not a duplicate of this question, because I'm not using Session.
EDIT
Okay, this works, but I don't believe it is very efficient, and it's adding an extra select * from (my sub query), but it works.
q1 = select('*').select_from((select(columns).where(table.c.id == #).limit(#)).alias('q1'))
So, if anyone has any ideas to optimize, or let me know if this is as good as it gets. I would appreciate it.
The author of SQLAlchemy seems to be aware of this and mentions a workaround for it on the SQLAlchemy 1.1 changelog page. The general idea is to do .alias().select() on each select.
stmt1 = select([table1.c.x]).order_by(table1.c.y).limit(1).alias().select()
stmt2 = select([table2.c.x]).order_by(table2.c.y).limit(2).alias().select()
stmt = union(stmt1, stmt2)