Solved

Show stock figures as written text in TSQL

Posted on 2011-02-23
17
362 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Author Comment

by:dexterhome
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That last one appears to have done it as required.
I will test more tomorrow and advise.
0
 
LVL 5

Author Comment

by:dexterhome
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Author Comment

by:dexterhome
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
If there's a loc_reserve_out for every loc_physical, it should be ok.
0
 
LVL 5

Author Closing Comment

by:dexterhome
Comment Utility
Thanks very very much.  does exactly what I need.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this article I will describe the Backup & Restore 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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now