dbTalk Databases Forums  

stock control, procedures

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss stock control, procedures in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
estesiquevale@gmail.com
 
Posts: n/a

Default stock control, procedures - 05-07-2007 , 03:33 AM






hello,

I have a table like this:

thing, size, color, type_mov, vary
1, s, red, sell, 1
1, s, red, buy, 2
1, m, green, return, 10
....

and the question is how I can see the total number of products by size
and color having in mind that some type of movement are + and other
are -.

in other words, like stock control.

how I can control this in sql server?
code, procedures?

thanks!


Reply With Quote
  #2  
Old   
Ed Murphy
 
Posts: n/a

Default Re: stock control, procedures - 05-07-2007 , 04:01 AM






estesiquevale (AT) gmail (DOT) com wrote:

Quote:
I have a table like this:

thing, size, color, type_mov, vary
1, s, red, sell, 1
1, s, red, buy, 2
1, m, green, return, 10
....

and the question is how I can see the total number of products by size
and color having in mind that some type of movement are + and other
are -.
This should get you on the right track:

select thing, size, color, sum(
case when type_mov in ('buy', 'return') then vary else -vary end
) as total_change
from the_table
group by thing, size, color


Reply With Quote
  #3  
Old   
Ed Murphy
 
Posts: n/a

Default Re: stock control, procedures - 05-07-2007 , 12:31 PM



Ed Murphy wrote:

Quote:
estesiquevale (AT) gmail (DOT) com wrote:

I have a table like this:

thing, size, color, type_mov, vary
1, s, red, sell, 1
1, s, red, buy, 2
1, m, green, return, 10
....

and the question is how I can see the total number of products by size
and color having in mind that some type of movement are + and other
are -.

This should get you on the right track:

select thing, size, color, sum(
case when type_mov in ('buy', 'return') then vary else -vary end
) as total_change
from the_table
group by thing, size, color
Also, to avoid having to rewrite this every time you add a new
type of transaction, you may want to create a table like this:

type_mov | effect
---------+---------
buy | incoming
sell | outgoing
return | incoming

and then do:

select t1.thing, t1.size, t1.color, sum(
case when t2.effect = 'incoming' then +vary
when t2.effect = 'outgoing' then -vary end
) as total_change
from the_table t1
join the_new_table t2 on t1.type_mov = t2.type_mov
group by t1.thing, t1.size, t1.color


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.