Convert Script to CTE - sql-server

Please, can you help me with this query?
SELECT
[festno], 2
FROM
dbo.V_sadad_isar
WHERE
[festno] IN (SELECT DISTINCT [festno]
FROM
(SELECT
[festno], ROW_NUMBER() OVER (PARTITION BY [festno] ORDER BY fisar DESC) AS rn
FROM
dbo.V_sadad_isar) T
GROUP BY festno
HAVING MAX(rn) = 1)
AND [darsad_janbaz] <> 50

Try this:
;with cte as (
SELECT [festno],
ROW_NUMBER() OVER (PARTITION BY [festno] ORDER BY fisar DESC) AS rn
FROM dbo.V_sadad_isar
), cte2 as (
SELECT DISTINCT [festno]
FROM cte
GROUP BY festno
HAVING MAX(rn) = 1
)
SELECT fastno, 2
FROM dbo.V_sadad_isar
WHERE festno IN (SELECT festno FROM cte2)
But it can be simplified to this:
;with cte as (
SELECT DISTINCT [festno]
FROM cte
GROUP BY festno
HAVING COUNT(*) = 1
)
SELECT fastno, 2
FROM dbo.V_sadad_isar
WHERE festno IN (SELECT festno FROM cte)

Related

Most Recent Data

I have a schema that looks like this.
Using the recordings table , query the most recent data in the database for each entityid/accid/month combination (Main Table is the main table)
use row_number()
select * from
(
select *,row_number() over(partition by entid, accid, month order by timestamp desc) as rn
from recordings a inner join maintable b on a.logid=b.logid
)A where rn=1
use row_number()
select * from
(
select m.*, row_number() over(partition by acctid,entid,month order by timestamp desc) rn
from maintable m join racordings r on m.logid=r.logid
) a where a.rn=1
Here's your query.
select * from
(select m.*, r.*, e.*, a.*, row_number() over(partition by a.AcctID, e.EntID, Month order by r.TimeStamp desc) row_num
from maintable m
inner join racordings r on m.LogID = r.LogID
inner join Entities e on e.EntID = m.EntID
inner join AccInfo a on a.AcctID = m.AcctID
) t1
where t1.row_num = 1

How to get desired result by combining two CTE's with different tables?

I have two CTE's,firstly
;WITH CTE AS (SELECT A.*
, Row_NUMBER() Over (Partition by ID order by Date asc) RN
FROM TABLE A)
SELECT Sum(Weight) as IN_WT
FROM CTE
WHERE RN = 1 and name='dev1'
and then
;WITH CTE AS (SELECT B.*
, Row_NUMBER() Over (Partition by ID order by Date desc) RN1
FROM TABLE B)
SELECT Sum(Weight) AS out_wt
FROM CTE
WHERE RN1 = 1 and name='dev1'
Here we had two tablesTableA,TableB.We are getting In_wt from TableA and Out_wt from TableB.Now I had a requiremnt that the output should be combined and get in_wt,out_wt in single row from different tables and same name.I tried combining both the CTE's but didn't get the desired result.How can we do that?
Try this:
;WITH CTE1 AS (
SELECT name,
Row_NUMBER() Over (Partition by ID order by Date asc) RN
FROM TABLEA
), CTE2 AS (
SELECT name,
Row_NUMBER() Over (Partition by ID order by Date desc) RN
FROM TABLEB
)
SELECT (SELECT Sum(Weight) FROM CTE1 WHERE RN = 1 and name='dev1') AS IN_WT,
(SELECT Sum(Weight) FROM CTE2 WHERE RN = 1 and name='dev1') AS OUT_WT

How can I make every first row from my Id?

This is my image of database result:
using Row_number you can get first row of your ID
Select * from (
Select *,
Row_number()OVER(PARTITION BY ID ORDER BY (SELECT NULL))RN
From Your Table )T
Where T.RN = 1
Select Id,Label,Score,Flag,AutoGenId from (
Select *,
Row_number()OVER(PARTITION BY ID ORDER BY (SELECT NULL))RN
From [VisionApiResponseData] )T
Where T.RN = 1

Can cross join work with window function like - row_number() function

Window function can not be used in where clause. In the below query rn column can be used in where clause only in the outer query like -
select * from (
select *, row_number() over (partition by col1 order by col1) as rn
from tab1) as abc
where rn = 1
can we use rn column with cross join some thing like -
select *, row_number() over (partition by col1 order by col1) as rn
from tab1 as a
cross join(select rn from a)
Just curious to know as i am trying to learn SQL server
You cannot do it in this way. You either have to repeat ROW_NUMBER() inside the subquery:
SELECT *, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1) AS rn
FROM tab1 AS a
CROSS JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1) AS rn FROM tab1) t
or, alternatively, use a common table expression:
;WITH a AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col1) AS rn
FROM tab1
)
SELECT *
FROM a
CROSS JOIN (SELECT rn FROM a) t

Update a table using CTE and NEWID()

I want try update a table using random values from the same table, but the original table donĀ“t have a identity to follow or any other number column...
WITH cteTable1 AS (
SELECT
ROW_NUMBER() OVER (ORDER BY NEWID()) AS n,
ENDE_NO_Address
FROM TableX
)
UPDATE TableX SET ENDE_NO_Address = (
SELECT ENDE_NO_Address
FROM cteTable1
WHERE cteTable1.n = This is the problem...
tnks for the help
Guessing...
UPDATE TableX
SET ENDE_NO_Address = (
SELECT TOP 1 ENDE_NO_Address FROM TableX ORDER BY NEWID()
)
declare #T table(Col1 varchar(10))
insert into #T values (1),(2),(3),(4),(5)
;with cte as
(
select *,
row_number() over(order by newid()) as rn1,
row_number() over(order by Col1) as rn2
from #T
)
update C1 set
Col1 = C2.Col1
from cte as C1
inner join cte as C2
on C1.rn1 = C2.rn2
Edit:
WITH cte AS (
SELECT
ROW_NUMBER() OVER (ORDER BY NEWID()) AS n1,
ROW_NUMBER() OVER (ORDER BY (select 1)) AS n2,
ENDE_NO_Address
FROM TableX
)
update C1 set
ENDE_NO_Address = C2.ENDE_NO_Address
from cte as C1
inner join cte as C2
on C1.n1 = C2.n2

Resources