MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old December 5th, 2003, 09:55 AM
msr msr is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 1 msr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
(Sub?)Query difficulty

I've got a bit of an issue creating a query that has any elegance at all (i.e. done right by someone who actually knows this stuff) due simply to my knowledge limitations.

In a single query I need to accomplish the following:

Table_A contains Item_NO and UPC_NO
Table_B contains Item_NO and Warehouse_ID and Quantity_Available and Quantity_Allocated

Result set needs to be:

UPC_NO, InStock_Flag, Quantity

The problem is that the 'InStock_Flag' is a new requirement (I have a query already that simply joins the tables on Item_NO and groups by UPC_NO).

The Flag is simply 'T' or 'F' depending upon whether the quantity value for a particular UPC_NO is positive or negative. I've accomplished that as well utilizing a CASE statement. However, what I neglected to take into account is that the UPC_NO shows up multiple times in Table_B (once for each warehouse) and as my CASE statement is looping through the records the flag is getting set to 'T' for some records and 'F' for others, thus my grouping will no longer work as those records become unique.

The logic needs to be as follows for the query, but I'm not sure how to structure it:

First join tables, select all records and group Quantity_Available and Quantity_Allocated by UPC_NO where Warehouse_ID <> 'x'

Then take that result set and generate final result as UPC_NO, InStock_Flag, Quantity (Quantity_Available - Quantity_Allocated)
Where Quantity_Available - Quantity_Allocated > 0 then InStock_Flag = 'T'
Where Quantity_Available - Quantity_Allocated < 1 then InStock_Flag ='F' and Quantity = 0

Again, I have a query that does all of this except for the first part...and not sure if this should be done as a subquery (which frankly I have no experience with) or other.

Any help much appreciated.

Reply With Quote
  #2  
Old December 11th, 2003, 04:18 PM
smackmeister smackmeister is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 21 smackmeister User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
try:

select upc_no,
case when sum(Quantity_Available - Quantity_Allocated) > 0 then 'T' else 'F' end,
case when sum(Quantity_Available - Quantity_Allocated) > 0 then quantity else 0 end
from table_a a
join table_b b
on a.item_no = b.item_no
where where Warehouse_ID <> 'x'
group by upc_no

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > (Sub?)Query difficulty


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway