Why do sys.schemas and INFORMATION_SCHEMA.SCHEMATA return different results? - azure-sqldw

Why do sys.schemas INFORMATION_SCHEMA.SCHEMATA return different results and which one I should trust?
On a default azure data warehouse database,
SELECT *
FROM sys.schemas
returns 4 rows: dbo, INFORMATION_SCHEMA, sys and sysdiag.
SELECT *
FROM INFORMATION_SCHEMA.SCHEMATA
returns 17 rows, with additional guest, pdw, QTables and db_*.
I'm working an Azure SQLDW editor.

It's a bug. The two queries should return the same results. I've filed this internally and it should get fixed quickly. Thanks for reporting it.

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.

The query of query ( QoQ ) with order by return duplicate column after update12 Coldfusion 2018

I've updated my coldfusion 2018 server with most recent update12 by Adobe. As soon as I've updated the server I've wrote some QoQ for my application with ORDER BY in my QoQ. Whenever I use order by in QoQ then the result data have some duplicate columns.
For my simple sample query as example,
<cfquery name="testRead" datasource="testmssql">
SELECT * FROM loginDetails
</cfquery>
<cfdump var="#testRead#" label="Main Query">
<cfquery name='readSub' dbtype="query">
SELECT userID, Username FROM testRead
ORDER BY userid DESC
</cfquery>
<cfdump var="#readSub#" label="QoQ Result" abort="true">
**Output:** Refer my image please.
Here you can see the second QoQ dump have two userID column. I'm not sure why we are having it here & where it's come from. ? If I add one more column in ORDER BY list then that column also get duplicated in result query. For example, If add ORDER BY userid DESC, userName then the query dump query having userID,userid,userName,username.
Note : It's not happening before my update12. And it's not happening for main query.
Any thoughts ? Please share. Thank you advance !.
This is a known issue with the update and a bug has been filed with Adobe. I would recommend you add a comment and vote for the bug.
Adobe Bug Tracker - CF-4212383
Duplicate columns with the same name in a Query of Queries which contains an ORDER BY clause.
Description from that bug:
Problem Description:
After applying CF 2021 Update 2, when using an ORDER BY clause in a QoQ, the fields in the ORDER BY clause have become case sensitive, and if they don't match exactly the case of the fields in the SELECT list, then a duplicate column is added to the resultant query, resulting in a query that has two (or more) columns with the same name.
Further, if no fields are added to the SELECT list and * is used instead, the fields in the ORDER BY clause must be upper case, otherwise duplicate columns with the same name (but different case) again appear in the resultant query.
This showstopping behaviour has been introduced in CF 2021 Update 2. CF2021 Update 1 behaves as expected. (CF2016 also behaves as expected).
Even though the bug mentions CF 2021 Update 2, it also affects CF 2018 Update 12. As verified by bug CF-4212430 submitted for CF 2018 Update 12 which was closed as a duplicate of the CF 2021 bug.

Identify Rows with Same ID but different Values in Power BI

I have a table with user License Details. And there are cases where I have multiple rows for a same user but with different License type. I want to Identify the users who have Admin License via DAX Calculated Column.
Eg: My table will be like this where User A has multiple rows with 1 Admin License
And I am expecting a result like this where User A is marked as True for all his Row enteries
So in short I want to Flag the user as True on all his rows if he has at least one Admin License.
I hope my explanation was clear.
Have a good day !!!
Best Regards,
Pratik
To display all Trues when the user has at least one admin right, first you need to create a index column using the following formula:
index = IF(Sheet1[License]="Admin",1,0)
Next, based on the result of the index column then you will be able to display the correct output with following formula:
status =
var index1 = CALCULATE(SUM(Sheet1[index]),
FILTER(Sheet1,Sheet1[User]=EARLIER(Sheet1[User])))
return
IF(index1 > 0,"True","False")
Table result:

Executemany on pyodbc only return result from last parameter

I have a problem when I try to use pyodbc executemany function.
I have an Oracle database and I want to extract data for multiple days.
I cannot use between in my request, because the database is not indexed on the date field and its taking forever.
I want to manually ask all day and process answers.
I cannot thread this part, so I wanted to use executemany to get rows more quickly.
The problem is when I use executemany I only got the result of the last argument asked.
Here is my code:
import pyodbc
conn = pyodbc.connect('DRIVER={Oracle in instantclient_11_2};DBQ=dbname;UID=uid;PWD=pwd')
cursor = conn.cursor()
query = "SELECT date FROM table WHERE date = TO_DATE(?, 'DD/MM/YYYY')"
query_args = (
('29/04/2016',),
('28/04/2016',),
)
cursor.executemany(query, query_args)
rows = cursor.fetchall()
In rows, I can only find rows with (datetime.datetime(2016, 4, 28, 0, 0), ).
Always the last argument.
I am using python 2.7.9 from WinPython on a Oracle database with a client on 11.0.2.
Except this query, every other query is perfectly fine.
I cannot use IN () synthax for 2 reasons:
I want to limit operations on database side, and do most of thing on script side (I've tried but it's way too long)
I might have more than 1000 different dates in the request.
(Right now I'm using IN() OR IN() OR IN()... but if anyone find something better that would be wonderful !)
Am I doing something wrong ?
Thanks for helping.
Your query runs once with one argument. If you want to run for multiple dates either use "IN" clause, this will require to modify query_args a bit.
"SELECT date FROM table WHERE date in (TO_DATE(?, 'DD/MM/YYYY'), TO_DATE(?, 'DD/MM/YYYY'))"
query_args = (
('29/04/2016','28/04/2016'),
)
or cursor through each date argument:
while query_arg in query_args:
cursor.executemany(query, query_arg )
rows = cursor.fetchall()

Django: Distinct foreign keys

class Log:
project = ForeignKey(Project)
msg = CharField(...)
date = DateField(...)
I want to select the four most recent Log entries where each Log entry must have a unique project foreign key. I've tries the solutions on google search but none of them works and the django documentation isn't that very good for lookup..
I tried stuff like:
Log.objects.all().distinct('project')[:4]
Log.objects.values('project').distinct()[:4]
Log.objects.values_list('project').distinct('project')[:4]
But this either return nothing or Log entries of the same project..
Any help would be appreciated!
Queries don't work like that - either in Django's ORM or in the underlying SQL. If you want to get unique IDs, you can only query for the ID. So you'll need to do two queries to get the actual Log entries. Something like:
id_list = Log.objects.order_by('-date').values_list('project_id').distinct()[:4]
entries = Log.objects.filter(id__in=id_list)
Actually, you can get the project_ids in SQL. Assuming that you want the unique project ids for the four projects with the latest log entries, the SQL would look like this:
SELECT project_id, max(log.date) as max_date
FROM logs
GROUP BY project_id
ORDER BY max_date DESC LIMIT 4;
Now, you actually want all of the log information. In PostgreSQL 8.4 and later you can use windowing functions, but that doesn't work on other versions/databases, so I'll do it the more complex way:
SELECT logs.*
FROM logs JOIN (
SELECT project_id, max(log.date) as max_date
FROM logs
GROUP BY project_id
ORDER BY max_date DESC LIMIT 4 ) as latest
ON logs.project_id = latest.project_id
AND logs.date = latest.max_date;
Now, if you have access to windowing functions, it's a bit neater (I think anyway), and certainly faster to execute:
SELECT * FROM (
SELECT logs.field1, logs.field2, logs.field3, logs.date
rank() over ( partition by project_id
order by "date" DESC ) as dateorder
FROM logs ) as logsort
WHERE dateorder = 1
ORDER BY logs.date DESC LIMIT 1;
OK, maybe it's not easier to understand, but take my word for it, it runs worlds faster on a large database.
I'm not entirely sure how that translates to object syntax, though, or even if it does. Also, if you wanted to get other project data, you'd need to join against the projects table.
I know this is an old post, but in Django 2.0, I think you could just use:
Log.objects.values('project').distinct().order_by('project')[:4]
You need two querysets. The good thing is it still results in a single trip to the database (though there is a subquery involved).
latest_ids_per_project = Log.objects.values_list(
'project').annotate(latest=Max('date')).order_by(
'-latest').values_list('project')
log_objects = Log.objects.filter(
id__in=latest_ids_per_project[:4]).order_by('-date')
This looks a bit convoluted, but it actually results in a surprisingly compact query:
SELECT "log"."id",
"log"."project_id",
"log"."msg"
"log"."date"
FROM "log"
WHERE "log"."id" IN
(SELECT U0."id"
FROM "log" U0
GROUP BY U0."project_id"
ORDER BY MAX(U0."date") DESC
LIMIT 4)
ORDER BY "log"."date" DESC