Syntax error on selecting from sub-query - sql-server

Getting syntax error while executing the following query?
select *
from (select row_number() over (order by title) as RowNum from question)
where RowNum>5
Anyone knows how to get it fixed?

Give ALIAS to sub-query like this:
SELECT *
FROM (SELECT row_number() OVER (ORDER BY title) AS RowNum FROM question) AS t1
WHERE RowNum>5

It would help if you selected the COLUMNS from the TABLE in the subquery, no?
select *
from (
select *, row_number() over (order by title) as RowNum
from question) SQ
where RowNum > 5

;With CTE AS
(
select row_number() over (order by title) as RowNum from question
)
select *
From Cte
Where RowNum > 5
OR
Select * From
(select row_number() over (order by title) as RowNum from question)X
Where X.RowNum > 5

Related

Convert Script to CTE

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)

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 to achieve pagination in SQL Server 2008 with like statement

My query for pagination result is
SELECT
*
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY Id DESC) AS RowNum,
*
FROM
My_Table) AS RowConstrainedResult
WHERE
(RowNum >= 1 AND RowNum <= 10)
ORDER BY
RowNum
This works fine. But when I try this for search result with like statement, it didn't work.
Search query
SELECT
*
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY Id DESC) AS RowNum,
*
FROM
My_Table) AS RowConstrainedResult
WHERE
column1 LIKE '%search%'
AND (RowNum >= 1 AND RowNum <= 10)
ORDER BY
RowNum
If first 10 rows don't contains search value, its return 0 results instead looking for full table. I can't use offset and fetch because it doesn't support in SQL Server 2008.
Any better way to use for pagination which work fine for like and normal in SQL Server 2008
Thanks
You're really close.. I'd use a CTE to filter, then select your 10 rows from it.
;WITH X AS
(
SELECT ROW_NUMBER() OVER (ORDER BY Id DESC) AS RowNum, *
FROM My_Table
WHERE column1 LIKE '%search%'
)
SELECT *
FROM X
WHERE RowNum BETWEEN 1 AND 10
ORDER BY RowNum
Depending on your situation, you may be able to simply get away with:
SELECT TOP 10 *, ROW_NUMBER() OVER (ORDER BY Id DESC) AS RowNum
FROM My_Table
WHERE column1 LIKE '%search%'
ORDER BY RowNum

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

SQL Server 2000: How to get top 10 for each distinct field. A loop Maybe?

I have the following table
MyTable
ID
MessageType
MessageDate
MessageBody
The table is a few million rows but there are only 100 unique MessageType in it.
What I need is a sample of each MessageType (must include at least MessageType and MessageBody), but I can't do a DISTINCT as that only gets me the MessageType column.
I am thinking something like
SELECT TOP 5 *
FROM MyTable
WHERE MessageType IN (SELECT DISTINCT MessageType FROM MyTable)
I know this doesn't work as it just me the top 5, but I am not sure how to make SQL loop through this.
Thanks for any help
The Row_Number version
;WITH cte AS
(
SELECT ID,
MessageType,
MessageDate,
MessageBody,
ROW_NUMBER() OVER (PARTITION BY MessageType ORDER BY (SELECT 0)) AS RN
FROM MyTable
)
SELECT ID,
MessageType,
MessageDate,
MessageBody
FROM cte
WHERE RN <=5
The CROSS APPLY version
WITH m1 AS
(
SELECT DISTINCT MessageType
FROM MyTable
)
SELECT m2.*
FROM m1
CROSS APPLY
(
SELECT TOP 5 *
FROM MyTable m2
WHERE m2.MessageType = m1.MessageType
) m2
Martin, if I'm reading your answer correctly, I think what you will produce is 5 samples of each message. Marc_s just wants one sample from each message.
I think what you need is:
SELECT ID,
MessageType,
MessageDate
FROM (
SELECT ID,
MessageType,
MessageDate,
ROW_NUMBER() OVER (PARTITION BY MessageType, ORDER BY NEWID() ) AS RN
-- I am using NewID() because it will produce a nice random sampling,
-- but Mark's SELECT(0) will be faster.
FROM MyTable
) sampling
WHERE RN =1

Resources