austintre
asked on
Pull data from two separate tables
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?
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]
ASKER
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.
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.
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
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
ASKER
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.
that would require an intimate knowledge of the data.
As I said... Can IRI Market have more than one market in the cognos table?
As I said... Can IRI Market have more than one market in the cognos table?
ASKER
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?
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?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
what is missing/added?
Need a bit more info on the results before being able to diagnose the sql