[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Pull data from two separate tables

Posted on 2010-11-08
7
Medium Priority
?
341 Views
Last Modified: 2012-05-10
Ok, I  know there is a cleaner way to do this and I am just too new and too dumb to figure it out.  Time for some help from the experts.

I have a SQL Database with multiple tables.  Most of the tables are just lists in order to connect the two main data tables with like identifiers.  The problem I am having is that I can't seem to pull the data from both tables correctly to correlate one with another.

The first table pulls qty and sales by zip code, item number and thus are filtered by the lists in order to get the market that correlates with the zip code and variety that correlates with the item number along with the weight multiplier so as to convert qty to pounds.  I have that down and working well.

When I try and add the second table that already has the correct market, the correct pounds and correct variety in it - but I still need to pull the sales and pounds out to compare the two data sets is when I get into trouble.  I can't seem to get the two table to pull the correct information.

In the example it should basically return 52 rows - one for each market with the correct dollars and pounds from one table (MarketShare) and dollars and pounds from the other table (CognosData).

Using MS SQL Server 2005 and Visual Studio 2005

Can anyone help me get this right?
SELECT     Markets.[IRI Market], Varieties.Variety, SUM(MarketShare.[POS Qty] * Varieties.Weight) AS WMTlbs, SUM(MarketShare.[POS Sales]) AS WMTDollars, 
                      SUM(CognosData.Dollars) AS IRIDollars, SUM(CognosData.Pounds) AS IRIlbs, 
FROM         MarketShare INNER JOIN
                      Markets ON MarketShare.[Zip Code] = Markets.[Zip Code] INNER JOIN
                      CognosMonths ON MarketShare.[WM Week] = CognosMonths.[Walmart Weeks] INNER JOIN
                      Qualifier ON MarketShare.[Store Nbr] = Qualifier.Stores INNER JOIN
                      Varieties ON MarketShare.[Item Nbr] = Varieties.[Item No] INNER JOIN
                      CognosData ON Markets.[IRI Market] = CognosData.Market
WHERE     (Qualifier.Qualifier = N'Yes') AND (Varieties.Variety = N'Red') AND (CognosData.Variety = N'Red') AND (CognosData.Year = N'2009')
GROUP BY Varieties.Variety, Markets.[IRI Market], CognosData.Market, CognosData.Variety, CognosData.Year
ORDER BY Markets.[IRI Market]

Open in new window

0
Comment
Question by:austintre
  • 4
  • 3
7 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 34088421
rather than 52 rows what are you returning?
what is missing/added?
Need a bit more info on the results before being able to diagnose the sql
0
 

Author Comment

by:austintre
ID: 34088439
Basically it is pulling multiple rows from the 'CognosData' table for every one row of 'MarketShare' so I am getting hundreds of rows with the same numbers for one table and different numbers for the other data.

From what I can tell I have the first table rolling up correctly and the second row is not rolling up to match if that makes sense.
0
 
LVL 29

Expert Comment

by:QPR
ID: 34088503
basically this will mean that the join is being satisfied > once for the INNER JOIN part.
Can IRI Market have more than one market in the cognos table?
If so then you will receive a row for each "true"
One thing to do is to start your query from scratch adding one join at a time until you get the undesired results.
This way you know exactly where the problem lies and can focus your attention there
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:austintre
ID: 34088702
Believe me when I say that I have started from scratch more times than I want to admit to.  Asking a question on this site is my last resort.  Basically looking for someone who can look at the code and tell me a better and more correct way to write it.
0
 
LVL 29

Expert Comment

by:QPR
ID: 34088724
that would require an intimate knowledge of the data.
As I said... Can IRI Market have more than one market in the cognos table?
0
 

Author Comment

by:austintre
ID: 34088785
The IRI Market is a list of the markets by zipcode.  That way I can reference the market by zipcode for the 'marketshare' table.  Cognos table has five years by month of data that is broken down by market, variety, and year.

So columns in marketshare are
StoreNbr, State, Zipcode, Week, ItemNbr, Qty, Sales

Columns in cognosdata are
Market, Variety, Month, Year, Dollars and Pounds

So I need to combine the two tables so the output looks like:
Market, Variety, MarketShare.Pounds, MarketShare.Dollars, CognosData.Pounds, Cognosdata.Dollars

In order to compare apples to apples - I have one table that finds the market by zipcode, another table that finds the weight and the variety by the marketshare.itemnbr and the week rolls up to months.

Does that help at all or is that just causing more confusion?

0
 
LVL 29

Accepted Solution

by:
QPR earned 2000 total points
ID: 34088816
so Markets.[IRI Market] = CognosData.Market
is returning too many records as the join criteria is being satisfied many times?
In your WHERE clause you specify red and 2009 on the cognos table, would this limit the returned rows if this criteria was part of the join rather than the where?

In other words
INNER JOIN CognosData
     ON Markets.[IRI Market] = CognosData.Market
    AND (CognosData.Variety = N'Red')
    AND (CognosData.Year = N'2009')
WHERE ......

you will need a where to further define the join (or use some other method such as subqueries) in order to remove the duplicates

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview

831 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