Group by query and filtered by Max date - entity-framework

I am trying to get the entities of my table grouped by an ID and filtered by MAX date, but I am not capable to do it with Entity Framework.
SELECT * FROM My_table UI
INNER JOIN
(
SELECT idgroup, MAX(filter_date) AS DateFilter FROM My_table
WHERE license = 1
GROUP BY idgroup
) T
ON T.idgroup= UI.idgroup AND T.DateFilter = UI.filter_date
WHERE license = 1
My solution for that right now is a StoredProcedure, but I think that this could be possible with EF, but I do not know how to do it.
Anyone know how to do that with EF statement?
My_table is already mapped as a EF entity in my project.

Related

Use postgresql query results to form another query

I am trying to select from one table using the select result from another table. I can run this in two queries but would like to optimize it into just one.
First query.. Select ids where matching other id
select id from lookuptable where paid = '547'
This results in something like this
6316352
6316353
6318409
6318410
6320468
6320469
6320470
6322526
6322527
6324586
6324587
6326648
I would like to then use this result to make another selection. I can do it manually like below. Note, there could be many rows with these values so I've been using a IN statement
select * from "othertable" where id in (6316352,6316353,6318409,6318410,6320468,6320469,6320470,6322526,6322527,6324586,6324587,6326648);
select
ot.*
from
"othertable" as ot
join
lookuptable as lt
on
ot.id = lt.id
where
lt.paid = '547'
The IN operator supports not just value lists but also subqueries, so you can literally write
select * from "othertable" where id in (select id from lookuptable where paid = '547');

How to set a Where clause on the top (parent) level in PostgreSQL?

I'm relatively new to PostgreSQL.
My issue is that I have apartments with their reservations and I need select those are available (don't have reservations) for a given range.
Reservations table has fields apartmentId (which references Apartments table), userId and datesReserved which is tstzrange.
For these purposes I'm doing this query:
SELECT * FROM apartments
JOIN appointments ON appointments.aparmentId = apartments.id
WHERE NOT(datesreserved && '[2020-12-15T15:00:00.000Z, 2020-12-17T16:00:00.000Z)');
But instead I'm getting filtered appointments - those that do not contain the value [2020-12-15T15:00:00.000Z, 2020-12-17T16:00:00.000Z).
As far as I researched there are top-level where and inner-level, and as far as I understand this query should give me top-level results - those apartments that do not contain the provided range.
Am I understanding correctly in this case?
I think you need NOT EXISTS here. As it is, you don't have a "top level", the tables are peers.
SELECT * FROM apartments WHERE NOT EXISTS (
SELECT 1 from appointments WHERE appointments.aparmentId = apartments.id and datesreserved && '[2020-12-15T15:00:00.000Z, 2020-12-17T16:00:00.000Z)'
);

Copy SQL result into another table

I need some help. I'm trying to copy a tsql query result into another table. I was able to do it with the below tsql but I need to put some sort of check method to not copy a record if it already exist in the "PageControls" table.
INSERT INTO PageControls (UserId, PageId)
SELECT t1.UserId, t2.PageId FROM
aspnet_users t1, Pages t2
How can I accomplish this?
Thank you.
It looks like you're trying to populate the pagecontrols table with a cartesian product of users and pages. Assuming that's your goal, then you can add not exists to your query to exclude those already in the pagecontrols table:
INSERT INTO PageControls (UserId, PageId)
SELECT t1.UserId, t2.PageId
FROM aspnet_users t1, Pages t2
WHERE NOT EXISTS (
SELECT *
FROM PageControls p
WHERE p.userid = t1.userid and p.pageid = t2.pageid
)
SQL Fiddle Demo

t sql query returns duplicate values

I know this is an often asked question, but I've tried to resolve this myself and could not.
I've got 2 tables to join and now it's returning a duplicate value from the right table.
select am.Journal
,am.EntryNumber
,am.PayInvoice
,am.PayDiscAllowed
,am.PayTaxAmtDisc
,am.PayGrossPayment
,tm.*
from CshJnlPay am right join
(select
Invoice
,SUM(NetSalesValue) as NetSalesValue
,SUM(DiscValue) as DiscValue
,SUM(TaxValue) as TaxValue
,SUM(QtyInvoiced) as QtyInvoiced
from Salesdetail
group by Invoice) tm
on am.PayInvoice = tm.Invoice
where Invoice = 'C90831'
If the query returns 2 rows with the same data from the right table then you have 2 rows in the left table with the same invoice number...
You should check the left table with this query
Select * from CshJnlPay where PayInvoice = 'C90831'
You should get two rows.

Fairly complex LINQ to Entities query

I have two entities, assume they are called Container and Record. They have a master-child relationship: a 'container' can hold many records.
The Records table in the database has the following columns:
Id
Date
Container_Id
RecordType_Id
The Record entity does not have any navigation properties that back reference the Container.
I am writing a LINQ query for my repository that will retrieve ONLY the records for a container that have the most recent date for each RecordType_Id. All older records should be ignored.
So if a container has say 5 records, one for each RecordType_Id, with the date 24/May/2011. But also has another 5 records for each RecordType_Id but with the date 20/May/2011. Then only the first 5 with the 24/May date will be retrieved and added to the collection in the container.
I came up with an SQL query that does what I need (but maybe there is some more efficient way?):
select t.*
from Records t
inner join (
select Container_Id, RecordType_Id, max(Date) AS MaxDate
from Records
group by Container_Id, RecordType_Id ) g
on t.Date = g.MaxDate
and t.Container_Id = g.Container_Id
and t.RecordType_Id = g.RecordType_Id
order by t.Container_Id
, t.RecordType_Id
, t.Date
However I am struggling to translate this into a proper LINQ query. EF is already generating a fairly large query all by itself just to load the entities, which makes me unsure of how much of this SQL query is actually relevant to the LINQ query.
Off the top of my head:
var q = from c in Container
from r in c.Records
group r by r.RecordType.RecordType_Id into g
select new
{
Container = c,
RecordType_Id = g.Key,
Records = from gr in g
let maxDate = g.Max(d => d.Date)
where gr.Date == maxDate
select gr
};
Try using LinqPad, it helps you test linq queries easily. Even against an existing EF model (which is in your project). Visit http://www.linqpad.net/