Click here to Skip to main content
15,906,081 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There is a table called 'NetworkSettings'

SQL
CREATE TABLE [dbo].[NetworkSettings](
    [MpnMappingId] [bigint] IDENTITY(1,1) NOT NULL,
    [PartNumber] [nvarchar](50) NULL,
    [MPN] [nvarchar](50) NOT NULL,
    [Network] [int] NOT NULL,
    [Description] [nvarchar](500) NULL,
    [System] [nvarchar](100) NULL,
    [Damaged] [nvarchar](50) NULL,
PRIMARY KEY CLUSTERED
(
    [MpnMappingId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


Now, if we execute below two quires:

SQL
SELECT Network FROM
  (
   SELECT
    ROW_NUMBER() OVER (PARTITION BY MPN ORDER BY MPN) AS RowNum,
    MpnMappingId, PartNumber, MPN, Network, [Description], [System], Damaged
   FROM
    NetworkSettings
  ) AS t
  WHERE
   t.RowNum = 1


and query below

VB
SELECT * FROM
  (
   SELECT
    ROW_NUMBER() OVER (PARTITION BY MPN ORDER BY MPN) AS RowNum,
    MpnMappingId, PartNumber, MPN, Network, [Description], [System], Damaged
   FROM
    NetworkSettings
  ) AS t
  WHERE
   t.RowNum = 1


Both shows different results in Network column. but no of count are same.

Can any one tell reason for it? Also, in both case how SQL query runs and shows differnt results?
Posted
Comments
Vinay Mistry 19-Aug-14 8:56am    
For me worked very well and got same output.
tanishtaman 19-Aug-14 9:32am    
Please, do multiple entries in table. with difference of just Network, as there is row_number over MPN. Editing the question, sending records to enter.
Vinay Mistry 19-Aug-14 9:43am    
I have used 11 records in table. try to select another column like PartNumber, [Description] or any other see it working or not?

1 solution

I believe that you are relying on the rows being presented in the same order for both queries, but you are only demanding order by MPN which is not unique.

In your inner query order by MpnMappingId. You can always amend the outer query to order by MPN.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900