Stack Exchange
Not equal to is not working in sql query
Hi i have tried to get the data from tables using below query but its not working..
    SELECT
    ProductMaster.ProductName + '(' + CONVERT(nvarchar(10), 0) + ')' AS orderedproducts,
    BranchName + '(' + CONVERT(nvarchar(10), SUM(StockInward.Qty)) + ')' AS currentstock,
    Setup.Id AS BranchId
FROM ProductMaster
INNER JOIN StockOutward
    ON ProductMaster.Productid = StockOutward.productid
INNER JOIN stockinward
    ON ProductMaster.Productid = stockinward.productid
    AND stockinward.StockLocation <> StockOutward.Location
INNER JOIN Setup
    ON stockinward.stocklocation = Setup.id
WHERE StockOutward.ProductId = '7'
GROUP BY    ProductMaster.ProductName,
            StockOutward.Qty,
            BranchName,
            Setup.Id

It is displaying all the records from the table StockOutward.

Some example data might help !

Please either simplify this to take out any joins that are unrelated to the problem, or state that, for example, the inner join with Setup on stockinward.stocklocation = Setup.id is necessary to reproduce the problem!

Can ProductMaster.Productid be NULL?

If yes, check this http://stackoverflow.com/questions/2202172/sql-inner-join-on-null-values

Should the 7 be in apostrophes?

Try In your Inner join move your  <> in a where clause.
And in your sample, have you try more simple query (select 1 simple element and add one by one innerjoin.

An INNER JOIN clause and the WHERE clause are the same, so there's no use of moving the condition.

yes but : 'stockinward.StockLocation <> StockOutward.Location' better to put a where clause with a NOT IN instruction. No? like ' where stockinward.StockLocation not in (select StockOutward.Location...) with more optimization
