-1

This is my query:

SELECT * 
FROM [dbo].[datamerge] 
WHERE Timesheet_Id = 'xxx' 
  AND AdminType_Id NOT IN (SELECT [ConfigValue] 
                           FROM [dbo].[adf_Config] 
                           WHERE [ConfigKey] = 'NonBillableEntries')

The above query returns 9 rows.

However, if I put in the value of the sub query I get 8 rows as expected:

SELECT * 
FROM [dbo].[datamerge] 
WHERE Timesheet_Id = 'xxx' 
  AND AdminType_Id NOT IN (106, 4, 3, 7, 100, 5, 105)

Any idea why this is happening?

EDIT

timesheet_Id    AdminType_Id
-----------------------------
16530056441     2033563818
16530056441     2033563818
16530056441     2033563818
16530056441     2033563818
16530056441     101
16530056441     101
16530056441     101
16530056441     101
16530056441     4

This is the table structure and sample data. I am not expecting the last row to be returned by the query.

And the result of the query

SELECT [ConfigValue] 
                           FROM [dbo].[adf_Config] 
                           WHERE [ConfigKey] = 'NonBillableEntries'

is

ConfigValue
106,4,3,7,100,5,105
MAK
  • 1,250
  • 21
  • 50
  • 1
    Show us some sample table data, the current result(s), and the wanted result - all as formatted text (no images.) Simplify! [mcve]. – jarlh Mar 31 '21 at 19:23
  • What is the AdminType_Id of the row you don't expect? – Brian Stork Mar 31 '21 at 19:28
  • @jarlh I updated the question to include the table data – MAK Mar 31 '21 at 19:32
  • And what do you get with `SELECT [ConfigValue] FROM [dbo].[adf_Config] WHERE [ConfigKey] = 'NonBillableEntries'`? – Brian Stork Mar 31 '21 at 19:35
  • @BrianStork I updated the question – MAK Mar 31 '21 at 19:36
  • Do you get a comma-separated string of numbers as a single row for ConfigValue? Or do you get seven, separate rows? – Brian Stork Mar 31 '21 at 19:46
  • @BrianStork A single row for configValue separated by a string of numbers. – MAK Mar 31 '21 at 19:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230610/discussion-between-brian-stork-and-mak). – Brian Stork Mar 31 '21 at 19:50
  • Does this answer your question? [NOT IN vs NOT EXISTS](https://stackoverflow.com/questions/173041/not-in-vs-not-exists) You have a nullable column, `NOT IN` doesn't work properly for that – Charlieface Mar 31 '21 at 20:01

2 Answers2

1

It's better to use not exists. But if you want to use not in then please make sure there is no null value in the list. Please try this.

Select * from [dbo].[datamerge] WHERE Timesheet_Id = 'xxx' AND AdminType_Id NOT IN 
(SELECT [ConfigValue] FROM [dbo].[adf_Config] WHERE [ConfigKey] = 'NonBillableEntries'and ConfigValue is not null)

With not exists (strongly recommended)

Select * from [dbo].[datamerge] d WHERE Timesheet_Id = 'xxx' AND not exists
    (SELECT 1 FROM [dbo].[adf_Config] a WHERE a.[ConfigKey] = 'NonBillableEntries'and a.ConfigValue =d.AdminType_Id)
0

Not sure, but try this:

SELECT  * 
FROM        [dbo].[datamerge]   dm
LEFT JOIN   [dbo].[adf_Config]  ac  ON  ac.[ConfigValue]    =   dm.AdminType_Id
                                    AND ac.[ConfigKey]      =   'NonBillableEntries'
WHERE 
        dm.Timesheet_Id     =   'xxx' 
    AND ac.[ConfigValue]    IS  NULL
Andy3B
  • 444
  • 2
  • 6