需要生成一个select语句来从多个表中获取数据

时间:2014-05-10 12:07:02

标签: sql-server-2008 join distinct

我正在做一个有产品反馈的项目,我似乎已经走到了尽头,我需要通过从4个不同的表中收集数据并在产品名称下合并/分组来生成产品活动报告。我的表格如下:

+------------+
| ProductUSP |
+------------+
| intID      |
| intProdID  |
| chrProdUSP |
| chrRemarks |
+------------+
+------------------+
|  ProductDetails  |
+------------------+
| intID            | --this is the same as the intProdID in the other tables
| chrProdName      |
+------------------+
+---------------------+
|   ProductActivity   |
+---------------------+
| intID               |
| intProdID           |
| chrSalesActivity    |
| chrPurchaseActivity |
| chrNewLauches       |
+---------------------+
+-------------------+
|  ProductVariety   |
+-------------------+
| intID             |
| intProdID         |
| chrHighEndVariety |
| chrMidVariety     |
| chrLowVariety     |
+-------------------+
+----------------+
| ProductPricing |
+----------------+
| intID          |
| intProdID      |
| chrHighPricing |
| chrMidPricing  |
| chrLowPricing  |
+----------------+

问题是第一个表有一个产品ID的多个记录,所有其他表都有一个产品ID的记录,因此我在select语句中不断获取重复值。

我得到的结果如下:

ProdUSP1  |    SalesActivity    |   HighVariety  |   HighPrice
ProdUSP1  |    PurchaseActivity |   MidVariety   |   MidPrice
ProdUSP1  |    NewLaunches      |   LowVariety   |   LowPrice
ProdUSP2  |    SalesActivity    |   HighVariety  |   HighPrice
ProdUSP2  |    PurchaseActivity |   MidVariety   |   MidPrice
ProdUSP2  |    NewLaunches      |   LowVariety   |   LowPrice
ProdUSP3  |    SalesActivity    |   HighVariety  |   HighPrice
ProdUSP3  |    PurchaseActivity |   MidVariety   |   MidPrice
ProdUSP3  |    NewLaunches      |   LowVariety   |   LowPrice

我想按如下方式打印结果,只使用一个查询。

ProdUSP1   |   SalesActivity     |  HighVariety  |   HighPrice
ProdUSP2   |   PurchaseActivity  |  MidVariety   |   MidPrice
ProdUSP3   |   NewLaunches       |  LowVariety   |   LowPrice

有任何变通方法吗?可能吗? 我希望我已经清楚地解释了......

提前致谢

0 个答案:

没有答案