?
Solved

Pull data from two separate tables

Posted on 2010-11-08
7
Medium Priority
?
336 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
[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
  • 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

765 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