I’m working with SQL Server and Datatables in a PHP environment; I’ve researched a bit, and found that by using row_number I can achieve paged results, but I am getting:
The multi-part identifier “Flags.FlagValue” could not be bound.
SELECT dbo.PlayingCharacters.PlayerName,dbo.PlayingCharacters.CurrentLevel,dbo.PlayingCharacters.XP,(SELECT CASE WHEN dbo.PlayingCharacters.Karma >= 1 THEN 2 WHEN dbo.PlayingCharacters.Karma <= -1 THEN 1 ELSE 0 END) as Karma,ISNULL(Flags.FlagValue,0) AS Remort FROM ( SELECT row_number() OVER (ORDER BY dbo.PlayingCharacters.XP desc) AS CI_offset_row_number, dbo.PlayingCharacters.PlayerName, dbo.PlayingCharacters.CurrentLevel, dbo.PlayingCharacters.XP, (SELECT CASE WHEN dbo.PlayingCharacters.Karma >= 1 THEN 2 WHEN dbo.PlayingCharacters.Karma <= -1 THEN 1 ELSE 0 END) as Karma, ISNULL(Flags.FlagValue, 0) AS Remort FROM dbo.PlayingCharacters LEFT JOIN dbo.Flags ON dbo.Flags.OwnerID = dbo.PlayingCharacters.UserID AND Flags.FlagID = 30419 WHERE dbo.PlayingCharacters.AccountName NOT IN (SELECT DISTINCT AccountName FROM UserFlags WHERE FlagBitPosition BETWEEN 0 AND 40) AND PlayingCharacters.AccountName NOT LIKE 'DeletedFrom:%' ) AS A WHERE A.CI_offset_row_number BETWEEN (141) AND (150)
Upon examining the query, and a bit of further research I found that “AS A” near the end of the query may be the culprit.. But I am unsure. I am familiar with SQL to an extent, but with this it seems I am a fish out of water.. I cannot seem to figure out how to fix this query.
I apologize if this may have been asked prior, I found a few results regarding my error but couldn’t put together any combination of answers with any success.
As a side note, this is my query before trying to add a limit/offset with row_number()
SELECT TOP 30 dbo.PlayingCharacters.PlayerName, dbo.PlayingCharacters.CurrentLevel, dbo.PlayingCharacters.XP, (SELECT CASE WHEN dbo.PlayingCharacters.Karma >= 1 THEN 2 WHEN dbo.PlayingCharacters.Karma <= -1 THEN 1 ELSE 0 END) as Karma, ISNULL(Flags.FlagValue, 0) AS Remort
FROM dbo.PlayingCharacters
LEFT JOIN dbo.Flags ON dbo.Flags.OwnerID = dbo.PlayingCharacters.UserID AND Flags.FlagID = 30419
WHERE dbo.PlayingCharacters.AccountName NOT IN (SELECT DISTINCT AccountName FROM UserFlags WHERE FlagBitPosition BETWEEN 0 AND 40) AND PlayingCharacters.AccountName NOT LIKE 'DeletedFrom:%'
ORDER BY dbo.PlayingCharacters.XP desc
Which works, but obviously doesn’t perform the desired limit/offset. The code I am using to generate the query in question was grabbed from: http://codeigniter.com/forums/viewthread/160626/P10/#985759
try this one