EntityFramework how to: select Max(Column), Count(*) from Table [duplicate] - entity-framework

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get sum of two columns in one LINQ query without grouping
how to select Max and Count in one call using EF?
Select Max([Column]), Count(*) from [Table]

I think you can fake it grouping by 0. SQL Server will see through that constant group-by and eliminate it. It has no runtime cost.
The question is: Will EF translate this to SQL properly? Given the track record (in contrast to good old trusty LINQ to SQL!) this is in doubt.

Related

Converting SQL query with FORMAT command to use in entity framework core

I have an SQL query:
SELECT
FORMAT(datetime_scrapped, 'MMMM-yy') [date],
count(FORMAT(datetime_scrapped, 'MMMM-yy')) as quantity
FROM scrap_log
GROUP BY FORMAT(datetime_scrapped, 'MMMM-yy')
It basically summarises all the entries in the scrap_log table by month/year and counts how many entries are in each month/year. Returns two columns (date and quantity). But I need to execute this in an ASP.NET core API using Entity Framework core. I tried using .fromSqlRaw(), but this expects all columns to be returned and so doesn't work.
I can find plenty of info on EF to implement group by and count etc... But I cannot find anything for the FORMAT(datetime, "MMMM-yy") part. Please could somebody explain to me how to do this?
EDIT: Seems already I appear to be going about this the wrong way in terms of efficiency. I will look into alternative solutions based on comments already made. Thanks for the fast response.

Need a better approach in how to design this relation [duplicate]

This question already has answers here:
How can you represent inheritance in a database?
(7 answers)
Closed 2 years ago.
I have a table fluxo, and fluxo have many fluxo_acao, but a entry in fluxo_acao can have three types, and each type have they particularities. to acomplish this, i created more three tables(fluxo_acao_mensagem, fluxo_acao_pergunta and fluxo_acao_escolher_fila), connected to fluxo_acao.
The problem is i can't select any of the three tables, from fluxo, because first i need to check wich type is and them i do the query.
I need a better approat in how to acomplish this relation. Even why i need create another two more. and this is the perfect time to change.
You could add the columns for all three types to fluxo_acao and use a check constraint to ascertain that the correct columns are NULL or NOT NULL for each type. Then the join would be trivial.
For convenience, you could define views on fluxo_acao for each type that show only the pertinent columns.
It's hard to understand what exactly these types are supposed to do, but it sounds like inheritance could help you: https://www.postgresql.org/docs/13/tutorial-inheritance.html

Why after the UPDATE operation in PostgreSQL the row goes to the last place? [duplicate]

This question already has answers here:
Why postgres returns unordered data in select query, after updation of row?
(2 answers)
Postgresql: row number changes on update
(1 answer)
What is the default select order in PostgreSQL or MySQL?
(4 answers)
Different Default ordering between ORACLE and PostgreSQL
(1 answer)
Closed 2 years ago.
I had this question on the job interview in some smell company. Now I understand that it's a wrong question itself.
My suggestion was that the PostgreSQL is copying the row and then deleting the previous one, i.e. using transaction, and then sorting rows by system hidden index.
They said that it's not a right answer, but they didn't say the right answer anyway because the interview was like a ping-pong with a high speed in one direction.
I've asked guys from core-team who make PostgreSQL in IRC. They said that the result order could be unpredictable and gave me documentation of PostgreSQL in very low-level in C so I didn't understand anything.
Now I found this statement https://www.postgresql.org/docs/current/sql-select.html :
If the ORDER BY clause is specified, the returned rows are sorted in
the specified order. If ORDER BY is not given, the rows are returned
in whatever order the system finds fastest to produce. (See ORDER BY
Clause below.)
Okay, but what if we use very simple table without any relations in the scheme, like
USER (id, name). What the point is there? Why the updated row would be on the last place? What should I answer on the interview?
First of all, not only PostgreSQL, but all other databases, the order of tuples isn't guaranteed. When you do insertion, each tuple is been written to page in PostgreSQL. Then, the page will be written into the disk and following the corresponding file system mechanism. The newly updated row isn't necessary to be the last place, unless the ORDER BY is specified. The reason the newly updated row appear at the last place, that might be the fact of LRU replacement policy is been adopted in that file system.

How to avoid cached plan must not change result type error? [duplicate]

This question already has answers here:
"ERROR: cached plan must not change result type" when mixing DDL with SELECT via JDBC
(2 answers)
Closed 5 years ago.
Let's say I want to add a column in my table. If I add column in DB without restarting the application, my application starts failing with "cached plan must not change result type" because return type changes for queries doing wildcard select after this column addition.
jdbc postgres driver automatically creates prepared statement after a certain threshold, default threshold value is 5.
Either I can disable it by setting prepareThreshold to 0 which is bad since I'm losing on the benefits of prepared statement optimization driver was doing for me.
Or I'll have to change all my SQL statements to specify the exact list of columns it will operate upon. So, writing statements like "SELECT * FROM TABLE" is not feasible.
Is there any other approach to solve this problem?
It is a bad habit to use * in SQL queries (except in certain cases like count(*)) for the reason that queries can suddenly fail or behave differently when a column is added.
If you really want that, you can catch the exception, close the prepared statement and recreate a new one with the same query string.

how to change character length in execute file for postgresql [duplicate]

This question already has answers here:
pgAdmin: How to see complete value in a cell in output
(1 answer)
pgAdmin III faulty behavior?
(2 answers)
Closed 5 years ago.
I have a view in my database (PostgreSQL) and I would like to see it's code.
I wrote this query:
select definition from pg_views where viewname='x'
this works most of the time, However in some of the views when the select code is long I get at some point (...)
for example this is one of the results of query where it shows (...):
" SELECT f.selectid,
a.clientid,
a.orderid,
a.clientname,
c.part,
c.product,
c.okey,
e.contry,
d.city,
(
CASE
WHEN (b.dateofissue IS NULL) THEN
CASE
(...)"
This is only part of the code... Why it doesn't show me the whole code?
In pgAdmin III, under Query tool options:
pgAdmin Query tool Options
You want pg_get_viewdef, but I suspect you'll have the same issue there. The problem is probably that the client application is truncating the returned query.
If you're using PgAdmin-III this is in the FAQ.
If you're using psql this shouldn't happen.