Forum Discussion

Azetoth's avatar
Azetoth
Copper Contributor
Oct 25, 2023

No result on * but result with explicit colums (probably related to window function)

I had a strange behavoir on one of my  Sql server (Azure) query:

 

select TOP 10 * from custom.View_Migration_LastState where [status] = N'Created' 

 

Return 0 row.

 

select top 10 [email], [date], [status] from custom.View_Migration_LastState where [status] = N'Created'

 

Returns some rows.

 

 

To understand the issue I extracted the SELECT from the view definition and applied the filter directly with still no result.

 

SELECT u.email, m.date, m.step, m.status, m.failureCount, m.content, m.info FROM
    (SELECT DISTINCT email FROM custom.View_Client WHERE UserEnabled = 1 and customerid IS NOT NULL) as u LEFT JOIN 
    (SELECT [email], [date], [step], [status], [failureCount], [content], [info], ROW_NUMBER() OVER (PARTITION BY email order by [date] desc) as rowNbr FROM custom.Migration) as m ON u.email = m.email AND rowNbr = 1
where m.[status] in ('Created','Found')

 

It lead me to the point where if the specific column [content] is in the query, no result was found. if a remove this column the query returns rows.

 

But If I change the query to:

 

SELECT u.email, m.date, m.step, m.status, m.failureCount, ci.content, m.info FROM
    (SELECT DISTINCT email FROM custom.View_Client WHERE UserEnabled = 1 and customerid IS NOT NULL) as u LEFT JOIN 
    (SELECT [email], [date], [step], [status], [failureCount], [info], ROW_NUMBER() OVER (PARTITION BY email order by [date] desc) as rowNbr FROM custom.Migration) as m ON u.email = m.email AND rowNbr = 1
    INNER JOIN custom.Migration as ci on ci.email = m.email and ci.[date] = m.[date]
where m.[status] in ('Created','Found')

 

It works as expected (return rows)

 

The table custom.Migration was created by

 

 

CREATE TABLE [custom].[Migration](
	[email] [nvarchar](254) NOT NULL,
	[date] [datetime] NOT NULL,
	[step] [varchar](25) NOT NULL,
	[status] [char](25) NOT NULL,
    [failureCount] [int] NULL,
	[content] [nvarchar](500)  NULL,
	[info] [nvarchar](500) NULL
) ON [PRIMARY]
SET ANSI_PADDING ON
GO
ALTER TABLE [custom].[Migration] ADD  CONSTRAINT [PK_Migration] PRIMARY KEY CLUSTERED 
(
	[email] ASC,
	[date] ASC,
	[step] ASC,
	[status] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]

 

 

 

So my question is:

Is there some kind of limitation that I am not aware on Sql Window fonction ?

Is this the intended  behavoir?

1 Reply

  • Arshad440's avatar
    Arshad440
    Brass Contributor
    Can you provide View_Migration_LastState ,View_Client views for better understandings

Resources