在同一列上多次联接两个表,中间使用一个查找表

时间:2019-01-28 21:51:36

标签: sql sql-server join

这可能已经回答了,但是很难找到这个问题,正如您在我迷惑的标题中所见。

无论如何,我希望这个示例对您有帮助:

tables and desired result

棘手的部分是参数查找表中的一对多关系。

自从我获得每个“价格”的每个“金额”以来,我就尝试使用多个联接和别名导致大量行。

SELECT paraval.month, paraval.value as amount, paraval2.value as price, trade.position
FROM trade
INNER JOIN parameter para on trade.tID=para.tID and para.name = 'amount'
INNER JOIN parametervalues paraval on para.pID=paraval.pID

INNER JOIN parameter para2 on trade.tID=para2.tID and para2.name = 'price'
INNER JOIN parametervalues paraval2 on para2.pID=paraval2.pID
WHERE trade.type = 'cert'

猜测我需要子查询,但不确定将其放在何处。

EDIT添加一些SQL代码结构:

CREATE TABLE #Trade  
(
    tID             int PRIMARY KEY,  
    type            varchar(50),
    position        int
 );  
CREATE TABLE #Parameter  
(
    pID             int PRIMARY KEY, 
    tID             int, 
    name            varchar(50)
);  
CREATE TABLE #ParameterValue  
(
    pID         int,  
    smonth      varchar(50),
    value       varchar(50));  


INSERT INTO #Trade 
SELECT 1, 'stock', 1
UNION
SELECT 2, 'stock', 2
UNION
SELECT 3, 'cert', 3

INSERT INTO #Parameter 
SELECT 1,1,'amount'
UNION
SELECT 2,1,'price'
UNION
SELECT 3,2,'amount'
UNION
SELECT 4,2,'price'
UNION
SELECT 5,3,'amount'
UNION
SELECT 6,3,'price'

INSERT INTO #ParameterValue
SELECT 1,1,'5'
UNION
SELECT 2,1,'500'
UNION
SELECT 3,1,'15'
UNION 
SELECT 4,1,'300'
UNION
SELECT 5,1,'5'
UNION
SELECT 5,2,'10'
UNION
SELECT 5,3,'5'
UNION
SELECT 6,1,'100'
UNION
SELECT 6,2,'200'
UNION 
SELECT 6,3,'300'

-- SELECT * FROM #Trade

-- SELECT * FROM #Parameter

-- SELECT * FROM #ParameterValue


DROP TABLE #Trade
DROP TABLE #Parameter
DROP TABLE #ParameterValue

1 个答案:

答案 0 :(得分:1)

我认为构建例外输出和相关架构的最佳方法是必须将数据透视图与动态sql一起使用,因为在第二天可能会有一些新值,这是结构的主体。

但是我认为这个查询可以响应:

SELECT paraval.month, (case when para. name = 'amount' then max(paraval.value) else null end)as amount, (case when para. name = 'price' then  max(paraval.value) else null end) as price, max(trade.position) as position 
FROM trade
INNER JOIN parameter para on trade.tID=para.tID 
INNER JOIN parametervalues paraval on para.pID=paraval.pID
WHERE trade.type = 'cert'
Group by paraval.month 

修改先前的查询:

CREATE TABLE #Trade  
(
    tID             int PRIMARY KEY,  
    type            varchar(50),
    position        int
 );  
CREATE TABLE #Parameter  
(
    pID             int PRIMARY KEY, 
    tID             int, 
    name            varchar(50)
);  
CREATE TABLE #ParameterValue  
(
    pID         int,  
    smonth      varchar(50),
    value       varchar(50));  


INSERT INTO #Trade 
SELECT 1, 'stock', 1
UNION
SELECT 2, 'stock', 2
UNION
SELECT 3, 'cert', 3

INSERT INTO #Parameter 
SELECT 1,1,'amount'
UNION
SELECT 2,1,'price'
UNION
SELECT 3,2,'amount'
UNION
SELECT 4,2,'price'
UNION
SELECT 5,3,'amount'
UNION
SELECT 6,3,'price'

INSERT INTO #ParameterValue
SELECT 1,1,'5'
UNION
SELECT 2,1,'500'
UNION
SELECT 3,1,'15'
UNION 
SELECT 4,1,'300'
UNION
SELECT 5,1,'5'
UNION
SELECT 5,2,'10'
UNION
SELECT 5,3,'5'
UNION
SELECT 6,1,'100'
UNION
SELECT 6,2,'200'
UNION 
SELECT 6,3,'300'


/***/
-- Perform select 
/***/
SELECT t.tID, paraval.smonth, MAX(case when para.name = 'amount' then paraval.value else null end)as amount, MAX(case when para.name = 'price' then  paraval.value else null end) as price, max(T.position) as position 
FROM #Trade T
INNER JOIN #Parameter para on T.tID=para.tID 
INNER JOIN #ParameterValue paraval on para.pID=paraval.pID
Group by T.tId, paraval.smonth
/***/

DROP TABLE #Trade
DROP TABLE #Parameter
DROP TABLE #ParameterValue

结果:

tID smonth  amount  price   position
1   1       5       500     1
2   1       15      300     2
3   1       5       100     3
3   2       10      200     3
3   3       5       300     3