sql server - Restricting WHERE clause to specific SELECT statement in sql -


i have following sql code. use clause date interval. problem want date interval used on specific part of select statement.

i want restrict clause date select on qtyinperiod , profitinperiod , not other columns selected (lines.item, inventory.itemalternative, inventory.onhandphys, inventory.allocated, inventorry.costprice, brand & stockstatus).

do need fetch data in 2 seperate sql querys or can limit date clause part of select statement?

select lines.item,        inventory.itemalternative,        inventory.onhandphys,        inventory.allocated,        inventory.costprice,        sum(lines.invoiced)*-1 qtysoldinperiod,        (sum(lines.amountbase-lines.costamount))*-1 profitinperiod,        replace(vinventoryoptional2values.value, '/', '') brand,        inventory.optional3 stockstatus lines inner join inventory on lines.item = inventory.item inner join vinventoryoptional2values on inventory.optional2 = vinventoryoptional2values.recordid inventory.item = 'item#'   , lines.date >= 'date'   , lines.date <= 'date'   , lines.invoiced < 0   , inventory.status = 0   , inventory.optional3 <> 3 group lines.item,          inventory.optional3,          inventory.itemalternative,          inventory.onhandphys,          inventory.costprice,          vinventoryoptional2values.value,          inventory.allocated having sum(lines.amountbase-lines.costamount) < 0 

edit: have tried sub-query, can't work:

select lines.item,        inventory.itemalternative,        inventory.onhandphys,        inventory.allocated,        inventory.costprice,        replace(vinventoryoptional2values.value, '/', '') brand,        inventory.optional3 stockstatus   (select sum(lines.invoiced)*-1 qtysoldinperiod    (lines.item          lines.date >= '2014-01-01'            , lines.date <= '2014-01-31' ) ) lines inner join inventory on lines.item = inventory.item inner join vinventoryoptional2values on inventory.optional2 = vinventoryoptional2values.recordid inventory.item = 'a158wa'   , lines.invoiced < 0   , inventory.status = 0   , inventory.optional3 <> 3 group lines.item,          inventory.optional3,          inventory.itemalternative,          inventory.onhandphys,          inventory.costprice,          vinventoryoptional2values.value,          inventory.allocated 

you should use subquery , in updated question syntax wrong, try using,

select lines.item,        inventory.itemalternative,        inventory.onhandphys,        inventory.allocated,        inventory.costprice,        replace(vinventoryoptional2values.value, '/', '') brand,        inventory.optional3 stockstatus,        (select sum(l.invoiced) * -1          lines l         l.date >= '2014-01-01'            , l.date <= '2014-01-31'             , l.item = lines.item) qtysoldinperiod lines inner join inventory on lines.item = inventory.item inner join vinventoryoptional2values on inventory.optional2 = vinventoryoptional2values.recordid inventory.item = 'a158wa'   , lines.invoiced < 0   , inventory.status = 0   , inventory.optional3 <> 3 group lines.item,          inventory.optional3,          inventory.itemalternative,          inventory.onhandphys,          inventory.costprice,          vinventoryoptional2values.value,          inventory.allocated 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -