Solved

Pull data from two separate tables

Posted on 2010-11-08
7
331 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:austintre
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

8 Experts available now in Live!

Get 1:1 Help Now