Solved

Show stock figures as written text in TSQL

Posted on 2011-02-23
17
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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