Link to home
Start Free TrialLog in
Avatar of dexterhome
dexterhomeFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Show stock figures as written text in TSQL

Hi Guys

I wish to run reports for customers that will show stock levels as a written text for website postings.
I wish to show stock levels over 3 as 'YES' and below 3 as 'NO'

I have done this in an SSMS query with a declare statement and union to join the results but this does not work in the alerts program Orbis.
My investigations reveal it is the declare statement.

Please can someone advise the best way to correct this.

Note: my sql is getting better but is still very basic.
I have attached the top half of the code


DECLARE @av_yes varchar(5)
SET		@av_yes = 'YES'
select stkcode,stkname,stk_usrchar1,stk_sort_key,stk_sort_key1,stk_sort_key2,stk_sort_key3,@av_yes as available
from stk_stock left outer join stk_stock3 on stkcode = stkcode3
where stk_physical > 3

Open in new window

Avatar of dexterhome
dexterhome
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Orbis alerts advises of fetch errors.
If I try to create a view with this syntax it advises of issues but is not being specific.
Hi,

Why not

select stkcode,stkname,stk_usrchar1,stk_sort_key,stk_sort_key1,stk_sort_key2,stk_sort_key3,'YES' as available
from stk_stock left outer join stk_stock3 on stkcode = stkcode3
where stk_physical > 3

/peter
If you want to combine the querys try


select stkcode,stkname,stk_usrchar1,stk_sort_key,stk_sort_key1,stk_sort_key2,stk_sort_key3,
case when stk_physical > 3 then 'YES' when stk_physical < 3 'NO' else '-' end as available
from stk_stock left outer join stk_stock3 on stkcode = stkcode3
Thanks for the response Pivar.

The first statement worked for me when I added the UNION command.(see attached)

I now see your second statement will do this cleaner except it does not appear to work.


SELECT stkcode,stkname,stk_usrchar1,stk_sort_key,stk_sort_key1,stk_sort_key2,stk_sort_key3,'YES' as available
FROM stk_stock LEFT OUTER JOIN stk_stock3 ON stkcode = stkcode3
WHERE stk_physical > 3
AND stk_usrchar1 IN ('CATALOGUE','NEW PRODUCT')

UNION

SELECT stkcode,stkname,stk_usrchar1,stk_sort_key,stk_sort_key1,stk_sort_key2,stk_sort_key3,'NO' as available
FROM stk_stock LEFT OUTER JOIN stk_stock3 ON stkcode = stkcode3
WHERE stk_physical <= 3
AND stk_usrchar1 IN ('CATALOGUE','NEW PRODUCT')

Open in new window

Sorry, typo, should be

select stkcode,stkname,stk_usrchar1,stk_sort_key,stk_sort_key1,stk_sort_key2,stk_sort_key3,
case when stk_physical > 3 then 'YES' when stk_physical < 3 then 'NO' else '-' end as available
from stk_stock left outer join stk_stock3 on stkcode = stkcode3
Missed the "stk_physical <= 3", so instead try

select stkcode,stkname,stk_usrchar1,stk_sort_key,stk_sort_key1,stk_sort_key2,stk_sort_key3,
case when stk_physical > 3 then 'YES' else 'NO' end as available
from stk_stock left outer join stk_stock3 on stkcode = stkcode3
That last one appears to have done it as required.
I will test more tomorrow and advise.
That appears to have worked well except for the fact that I have just realised that this will not be a true stock figure as it will be showing quarantined stock.

Please can you advise how I get your statement to actually check the bin locations for the physical figure.

I have attached some extra code for you to see what I mean,

select loc_stock_code,SUM(loc_physical)as physical
from stk_location
--where loc_code like 'Q%'
where loc_stock_code = '68240'
and loc_code not like ('Q%')
and loc_code not like ('M%')
and loc_code not like ('HIST%')
group by loc_stock_code

Open in new window

I have now added a where clause to your statement to give me what I need.
Please can you advise if this will be ok or is there a better way of doing it.


select stkcode,stkname,stk_usrchar1,stk_sort_key,stk_sort_key1,stk_sort_key2,stk_sort_key3,
case when stk_physical > 3 then 'YES' else 'NO' end as available
from stk_stock left outer join stk_stock3 on stkcode = stkcode3
where stk_usrchar1 in ('CATALOGUE','NEW PRODUCT')
and stkcode IN 
				(select loc_stock_code
					from stk_location
						where loc_code not like ('Q%')
							and loc_code not like ('M%')
								and loc_code not like ('HIST%')
									and loc_physical > 0
										group by loc_stock_code)

Open in new window

I'm not sure I'm following. But perhaps something like this?

select stkcode,stkname,stk_usrchar1,stk_sort_key,stk_sort_key1,stk_sort_key2,stk_sort_key3,
case when loc_physical > 3 then 'YES' else 'NO' end as available
from stk_stock
join (select loc_stock_code,SUM(loc_physical) as loc_physical
from stk_location
where loc_stock_code = '68240'
and loc_code not like ('Q%')
and loc_code not like ('M%')
and loc_code not like ('HIST%')
group by loc_stock_code ) loc on loc_stock_code=stkcode
left outer join stk_stock3 on stkcode = stkcode3
Didn't see your latest post. But I think that solution indicates some of my assumptions was wrong regarding the stk_location table.

Can you show the fields of stk_stock, stk_stock3 and stk_location and note if the relation is one-to-one or one-to-many?
What's the difference between stk_physical and loc_physical?
sorry.. the loc_stock code in the extra bit should not have been there.
See attached.

What I am trying to do is move the focus of the physical quantity in your original query from the stock record to the stock locations table so that we can we do not include the physical stock that is in the Q%, M% and His% locations as this should not be included but will be in the original work on the stock record.

Hope this makes sense.
Thanks for your patience with me.

I also need to check for the allocated and remove that as well.
Here goes:

The stk_stock.stk_physical is a figure made up from the stk_location table (it calculates as row with the matching stock code to produce the figure) - my problem is that this calculation will include the quarantine locations that must not be included in the yes/no section.

The stock and stock 3 tables are presumed one to one with the stk_locations being a to many as multiple rows per stock record.

Does this advise as required.
ASKER CERTIFIED SOLUTION
Avatar of pivar
pivar
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I forgot to advise regarding the allocated (loc_reserved_out) that also needs removing from the query.

I have just added this to your last statement in the sum section.

will that be ok.

The query appears to work correctly.
select stkcode,stkname,stk_usrchar1,stk_sort_key,stk_sort_key1,stk_sort_key2,stk_sort_key3,
case when loc_physical > 3 then 'YES' else 'NO' end as available
from stk_stock
join (select loc_stock_code,SUM(loc_physical-loc_reserve_out) as loc_physical
		from stk_location
			where loc_code not like ('Q%')
				and loc_code not like ('M%')
					and loc_code not like ('HIST%')
						group by loc_stock_code ) 
loc on loc_stock_code=stkcode left outer join stk_stock3 on stkcode = stkcode3
--where stk_usrchar1 in ('CATALOGUE','NEW PRODUCT')

Open in new window

If there's a loc_reserve_out for every loc_physical, it should be ok.
Thanks very very much.  does exactly what I need.