Duplicate Rows -- Join Columns?
I'm trying to find our top selling product, and then use that information
to find other things. However, we recently moved information to a new
table in our database. The new table has new item numbers but only some of
the old originals. When I join the old table to get all, they double up a
lot because the new table has more data than the old.
I'm really wordy and confusing so here's an example:
ItemName ItemNo ItemNo2 Size Age Type Count Sales
YearNew
Item1 AA123 AA123 ST NULL Accessories 1234
12334.56 2008
Item2 BB123 BB123 ST Adult Accessories 5643
98765.43 2006
Item3 CC123 CC123 ST NULL Accessories 1111
11111.11 2007
Item3b CC123 CC123 ST Adult Accessories 1111
11111.11 2007
Item4 DD123 DD123 L Kids Costumes 333
7891.12 2011
Item4 DD123 DD123 L Teen Costumes 333
7891.12 2011
(these numbers are made up, obviously)
Note that for Item3 and Item3b the only difference is the ItemName and the
Age. Everything else is the same. But because that's different and I'm
grouping by every field, it's splitting them into 2 mostly duplicate
lines.
Is there a way I can join the ItemNo and ItemNo2 columns? Or another
method of solving this?
But also note that I have situations where every single column is the
same, except for the type or the age. DD123 in this example is the same in
every area except for Age, which causes it to split to two lines as well.
Other times, it's all identical except for the ItemName. It varies all over.
It was suggested to me that I ask programming to simply add the old item
numbers they left out back into the new table. Would that be the best
method or is there something I can do on my end?
I have read-only access to the database so I can't make any changes myself.
For fun, here's the query I'm using at the moment:
select PM.ProductMasterName as ItemName,PM.ItemNumber as ItemNo,
op.ItemNumber_obsolete as ItemNo2,
OP.Size_obsolete as Size,Age.AgeSegment as Age,
PT.ProductTypeDescription as Type,PM.Price,
COUNT(PM.ItemNumber) as 'Count',
(PM.Price*COUNT(PM.ItemNumber)) as Sales,
YEAR(PS.DateCreated) as YearNew
from dbo.tblOrder O with (nolock)
left outer join dbo.tblOrderProduct OP with (nolock)
on O.OrderID=op.OrderID
left outer join dbo.tblProductSize PS
on OP.ItemNumber_obsolete=PS.ItemNumber_obsolete
and OP.Size_obsolete=PS.Size_obsolete
left outer join Product.ProductMaster PM with (nolock)
on PM.ItemNumber=OP.ItemNumber_obsolete
and PM.Price=OP.Price
left outer join Product.ProductType PT
on PM.ProductTypeID=PT.ProductTypeID
left outer join Product.AgeSegmentToProductMaster ASPM
on PM.ProductMasterID=ASPM.ProductMasterID
left outer join Product.AgeSegment Age
on ASPM.AgeSegmentID=Age.AgeSegmentID
where O.OrderDate >= '2012-01-01'
and O.OrderDate < '2013-01-01'
group by PM.ProductMasterName,PM.ItemNumber,op.ItemNumber_obsolete,
OP.Size_obsolete,PM.Price,Age.AgeSegment,
PT.ProductTypeDescription,YEAR(PS.DateCreated)
order by Sales desc
No comments:
Post a Comment