Need help in writing Linq Query - entity-framework

Need help to convert the below SQL Query to Linq Query
WITH cteStatus AS(
SELECT convert(varchar(10), UpdatedDate, 120) as UpdatedDate, Status, ROW_NUMBER() OVER (PARTITION BY convert(varchar(10), UpdatedDate, 120), Status ORDER BY UpdatedDate, Status) rn
FROM Pipeline
)
SELECT Top 1 c.UpdatedDate
FROM cteStatus c
WHERE Status = N'SUCCESS' AND rn = 3
ORDER BY c.UpdatedDate DESC

You cannot run Common Table Expressions (CTEs) with LINQ. This is a provider-specific feature (although supported by SQL Server and Oracle) and therefore it is not supported.

Related

ROW_NUMBER() over partition by [DateCol vs 1] order by DateCol

Im pretty new at T-SQL.
I saw this T-SQL script:
SELECT [Date], ClosePrice, ROW_NUMBER() over (partition by 1 order by [Date])rn
FROM NIFTY_SMALLCAP_250_STOCKS
source: https://youtu.be/vE8UcS8U_xE?t=2882 (with my own small change)
Works as expected:
Expected result (and sample data)
Then I changed the script to : [Date] instead of 1
SELECT [Date], ClosePrice, ROW_NUMBER() over (partition by [Date] order by [Date])rn
FROM NIFTY_SMALLCAP_250_STOCKS
The Result - all "dynamic" rn column values equals 1
The Result
My question:
Why partitioning by [Date] (that is obviously uniqe) doesnt work here?
What am i missing here about the "ROW_NUMBER()" and "partition by" combination?

How to use new created column in where column in sql?

Hi I have a query which looks like the following :
SELECT device_id, tag_id, at, _deleted, data,
row_number() OVER (PARTITION BY device_id ORDER BY at DESC) AS row_num
FROM mdb_history.devices_tags_mapping_history
WHERE at <= '2019-04-01'
AND _deleted = False
AND (tag_id = '275674' or tag_id = '275673')
AND row_num = 1
However when I run the following query, I get the following error :
ERROR: column "row_num" does not exist
Is there any way to go about this. One way I tried was to use it in the following way:
SELECT * from (SELECT device_id, tag_id, at, _deleted, data,
row_number() OVER (PARTITION BY device_id ORDER BY at DESC) AS row_num
FROM mdb_history.devices_tags_mapping_history
WHERE at <= '2019-04-01'
AND _deleted = False
AND (tag_id = '275674' or tag_id = '275673')) tag_deleted
WHERE tag_deleted.row_num = 1
But this becomes way too complicated as I do it with other queries as I have number of join and I have to select the column as stated from so it causes alot of select statement. Any smart way of doing that in a more simpler way. Thanks
You can't refer to the row_num alias which you defined in the same level of the select in your query. So, your main option here would be to subquery, where row_num would be available. But, Postgres actually has an option to get what you want in another way. You could use DISTINCT ON here:
SELECT DISTINCT ON (device_id), device_id, tag_id, at, _deleted, data
FROM mdb_history.devices_tags_mapping_history
WHERE
at <= '2019-04-01' AND
_deleted = false AND
tag_id IN ('275674', '275673')
ORDER BY
device_id,
at DESC;
Too long/ formatted for a comment. There is a reason behind #TimBiegeleisen statement "alias which you defined in the same level of the select". That reason is that all SQL statement follow the same sequence for evaluation. Unfortunately that sequence does NOT follow the sequence of clauses within the statement presentation. that sequence is in order:
from
where
group by
having
select
limits
You will notice that what actually gets selected fall well after evaluation of the where clause. Since your alias is defined within the select phase it does not exist during the where phase.

How to translate SQL to DAX, Need to add FILTER

I want to create calculated table that will summarize In_Force Premium from existing table fact_Premium.
How can I filter the result by saying:
TODAY() has to be between `fact_Premium[EffectiveDate]` and (SELECT TOP 1 fact_Premium[ExpirationDate] ORDE BY QuoteID DESC)
In SQL I'd do that like this:
`WHERE CONVERT(date, getdate()) between CONVERT(date, tblQuotes.EffectiveDate)
and (
select top 1 q2.ExpirationDate
from Table2 Q2
where q2.ControlNo = Table1.controlno
order by quoteid` desc
)
Here is my DAX statement so far:
In_Force Premium =
FILTER(
ADDCOLUMNS(
SUMMARIZE(
//Grouping necessary columns
fact_Premium,
fact_Premium[QuoteID],
fact_Premium[Division],
fact_Premium[Office],
dim_Company[CompanyGUID],
fact_Premium[LineGUID],
fact_Premium[ProducerGUID],
fact_Premium[StateID],
fact_Premium[ExpirationDate]
),
"Premium", CALCULATE(
SUM(fact_Premium[Premium])
),
"ControlNo", CALCULATE(
DISTINCTCOUNT(fact_Premium[ControlNo])
)
), // Here I need to make sure TODAY() falls between fact_Premium[EffectiveDate] and (SELECT TOP 1 fact_Premium[ExpirationDate] ORDE BY QuoteID DESC)
)
Also, what would be more efficient way, to create calculated table from fact_Premium or create same table using sql statement (--> Get Data--> SQL Server) ?
There are 2 potential ways in T-SQL to get the next effective date. One is to use LEAD() and another is to use an APPLY operator. As there are few facts to work with here are samples:
select *
from (
select *
, lead(EffectiveDate) over(partition by CompanyGUID order by quoteid desc) as NextEffectiveDate
from Table1
join Table2 on ...
) d
or
select table1.*, oa.NextEffectiveDate
from Table1
outer apply (
select top(1) q2.ExpirationDate AS NextEffectiveDate
from Table2 Q2
where q2.ControlNo = Table1.controlno
order by quoteid desc
) oa
nb. an outer apply is a little similar to a left join in that it will allow rows with a NULL to be returned by the query, if that is not needed than use cross apply instead.
In both these approaches you may refer to NextEffectiveDate in a final where clause, but I would prefer to avoid using the convert function if that is feasible (this depends on the data).

Using top in a subquery

I have the following data structure and I want to write a query that returns for a given order number, all the orderlineid's with the most recent statusId for that orderline.
If I was just interested in a particular order line I could use
select top 1 StatusId from task where OrderLineId = #OrderLineId order by TaskId desc
but I can't figure out how to get all the results for a given OrderId in one SQL Statement.
If I'm understanding your question correctly, you could use row_number in a subquery:
select orderid, orderlineid, statusid
from (
select o.orderid,
ol.orderlineid,
t.statusid,
row_number() over (partition by o.orderid order by t.taskid desc) rn
from order o
join orderline ol on o.orderid = ol.orderid
join task t on ol.orderlineid = t.orderlineid
) t
where orderid = ? and rn = 1
Please note, order is a reserved word in sql server so if that's your real table name, you'll need to use brackets around it. But I'd recommend renaming it to make your life easier.

Are there any way to create query from sql query to entity object query in jpa

I have a query like the one shown below:
SELECT
SUM (LIMIT_AMOUNT)
FROM
(SELECT
CTR_NO
, REPORT_DATE
, LIMIT_AMOUNT
, ROW_NUMBER()
OVER (PARTITION BY CTR_NO, REPORT_DATE ORDER BY REPORT_DATE) rn
FROM LOD_CONTRACT
WHERE
br_cst_code='3432434'
AND REPORT_DATE BETWEEN '20-FEB-15' AND '28-FEB-15') b
WHERE
b.rn=1;
How can I build a SQL query for a JPA-managed entity like:
SELECT
SUM (o.limit_amount)
FROM
(SELECT
o.ctr_no
, o.rpt_dt
, o.limit_amount
, ROW_NUMBER()
OVER (PARTITION BY o.ctr_no, o.rpt_dt ORDER BY o.rpt_dt) rn
FROM LOD_CONTRACT o
WHERE
o.br_cst_code='3432434'
AND o.rpt_dt BETWEEN '20-FEB-15' AND '28-FEB-15') b
WHERE
b.rn=1;
JPQL doesn't currently support Oracle partition tables. To use this feature you will have to stick to Native queries.