• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

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

0
dexterhome
Asked:
dexterhome
  • 9
  • 8
1 Solution
 
dexterhomeAuthor Commented:
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.
0
 
pivarCommented:
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
0
 
pivarCommented:
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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
dexterhomeAuthor Commented:
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

0
 
pivarCommented:
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
0
 
pivarCommented:
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
0
 
dexterhomeAuthor Commented:
That last one appears to have done it as required.
I will test more tomorrow and advise.
0
 
dexterhomeAuthor Commented:
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

0
 
dexterhomeAuthor Commented:
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

0
 
pivarCommented:
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
0
 
pivarCommented:
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?
0
 
dexterhomeAuthor Commented:
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.
0
 
dexterhomeAuthor Commented:
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.
0
 
pivarCommented:
By that description it seems to me that you should use something like this rather than your latest solution.
Since you have to sum some rows in stk_location per stock. This would sum stockquantities for locations not in 'Q%','M%','HIST%' and check this sum for > 3 or not. Your solution would rather check the total sum if there was a location for the stock in 'Q%','M%','HIST%'

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_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
0
 
dexterhomeAuthor Commented:
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

0
 
pivarCommented:
If there's a loc_reserve_out for every loc_physical, it should be ok.
0
 
dexterhomeAuthor Commented:
Thanks very very much.  does exactly what I need.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now