Solved

Show stock figures as written text in TSQL

Posted on 2011-02-23
17
364 Views
Last Modified: 2012-05-11
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
Comment
Question by:dexterhome
  • 9
  • 8
17 Comments
 
LVL 5

Author Comment

by:dexterhome
ID: 34961985
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
 
LVL 22

Expert Comment

by:pivar
ID: 34962020
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
 
LVL 22

Expert Comment

by:pivar
ID: 34962058
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 5

Author Comment

by:dexterhome
ID: 34962194
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
 
LVL 22

Expert Comment

by:pivar
ID: 34962218
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
 
LVL 22

Expert Comment

by:pivar
ID: 34962230
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
 
LVL 5

Author Comment

by:dexterhome
ID: 34963445
That last one appears to have done it as required.
I will test more tomorrow and advise.
0
 
LVL 5

Author Comment

by:dexterhome
ID: 34968043
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
 
LVL 5

Author Comment

by:dexterhome
ID: 34968196
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
 
LVL 22

Expert Comment

by:pivar
ID: 34968379
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
 
LVL 22

Expert Comment

by:pivar
ID: 34968425
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
 
LVL 5

Author Comment

by:dexterhome
ID: 34968431
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
 
LVL 5

Author Comment

by:dexterhome
ID: 34968506
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
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 34968516
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
 
LVL 5

Author Comment

by:dexterhome
ID: 34968603
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
 
LVL 22

Expert Comment

by:pivar
ID: 34968618
If there's a loc_reserve_out for every loc_physical, it should be ok.
0
 
LVL 5

Author Closing Comment

by:dexterhome
ID: 34968842
Thanks very very much.  does exactly what I need.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question