从 sql 服务器中的多个 table 获取数据
Get Data from mutile table in sql server
我有 3 个 table,我想使用连接从那些 table 中获取数据。
这是我的 table 结构和这 3 table 中的数据。
我正在使用 MS Sql 服务器。
Year Month TotalNewCaseAmount TotalNewCaseCount TotalClosingAmount TotalReturnCount TotalClosingAmount TotalReturnCount
2016 Januray 146825.91 1973 54774.41 147 299.35 41
2016 Fabuary 129453.30 5384 46443.99 7 7568.21 123
2016 March 21412.07 3198 Null Null 78.83 73
2016 April 0.00 5 Null Null Null NULL
我不知道哪个连接会得到这个结果,我试过 CROSS 连接,但它会给我 36 行。
这样试试,
select
t1.*,
t2.TotalClosingAmount,
t2.TotalClosingCount,
t3.TotalRetrunAmount,
t3.TotalReturnCount
from
Table1 t1
left join
Table2 t2 on t1.year=t2.year
and t1.month=t2.month
left join
Table3 t3 on t1.year=t3.year
and t1.month=t3.month
请试试这个
select year,month, totalnewcasesamount,totalnewcasescount,
totalclosingamount,totalclosingcount,totlareturnamopunt,totalreturncount
from table1 full outer join
table2
on table1.year=table2.year
and table1.month=table2.month
full outer join table3
on table1.year=table3.year
and table1.month=table3.month
where table1.year is not null
and table1.month is not nulll
这样就可以了:
;WITH Table1 AS (
SELECT *
FROM (VALUES
(2016, 'Januray', 146825.91, 1973),
(2016, 'Fabuary', 129453.30, 5384),
(2016, 'March', 21412.07, 3198),
(2016, 'April', 0.00, 5)
) as t ([Year], [Month], TotalNewCaseAmount, TotalNewCaseCount)
), Table2 AS (
SELECT *
FROM (VALUES
(2016, 'Januray', 54774.41, 147),
(2016, 'Fabuary', 46443.99, 7)
) as t ([Year], [Month], TotalClosingAmount, TotalClosingCount)
), Table3 AS (
SELECT *
FROM (VALUES
(2016, 'Januray', 299.35, 41),
(2016, 'Fabuary', 7568.21, 123),
(2016, 'March', 78.83, 73)
) as t ([Year], [Month], TotalReturnAmount, TotalReturnCount)
)
SELECT t1.[Year],
t1.[Month],
t1.TotalNewCaseAmount,
t1.TotalNewCaseCount,
t2.TotalClosingAmount,
t2.TotalClosingCount,
t3.TotalReturnAmount,
t3.TotalReturnCount
FROM table1 t1
LEFT JOIN table2 t2
ON t1.[Year] = t2.[Year] AND t1.[Month] = t2.[Month]
LEFT JOIN table3 t3
ON t1.[Year] = t3.[Year] AND t1.[Month] = t3.[Month]
输出:
Year Month TotalNewCaseAmount TotalNewCaseCount TotalClosingAmount TotalClosingCount TotalReturnAmount TotalReturnCount
2016 Januray 146825.91 1973 54774.41 147 299.35 41
2016 Fabuary 129453.30 5384 46443.99 7 7568.21 123
2016 March 21412.07 3198 NULL NULL 78.83 73
2016 April 0.00 5 NULL NULL NULL NULL
只需将 table1
、table2
和 table3
更改为您的实际 table 名称即可。
我有 3 个 table,我想使用连接从那些 table 中获取数据。 这是我的 table 结构和这 3 table 中的数据。
我正在使用 MS Sql 服务器。
Year Month TotalNewCaseAmount TotalNewCaseCount TotalClosingAmount TotalReturnCount TotalClosingAmount TotalReturnCount
2016 Januray 146825.91 1973 54774.41 147 299.35 41
2016 Fabuary 129453.30 5384 46443.99 7 7568.21 123
2016 March 21412.07 3198 Null Null 78.83 73
2016 April 0.00 5 Null Null Null NULL
我不知道哪个连接会得到这个结果,我试过 CROSS 连接,但它会给我 36 行。
这样试试,
select
t1.*,
t2.TotalClosingAmount,
t2.TotalClosingCount,
t3.TotalRetrunAmount,
t3.TotalReturnCount
from
Table1 t1
left join
Table2 t2 on t1.year=t2.year
and t1.month=t2.month
left join
Table3 t3 on t1.year=t3.year
and t1.month=t3.month
请试试这个
select year,month, totalnewcasesamount,totalnewcasescount,
totalclosingamount,totalclosingcount,totlareturnamopunt,totalreturncount
from table1 full outer join
table2
on table1.year=table2.year
and table1.month=table2.month
full outer join table3
on table1.year=table3.year
and table1.month=table3.month
where table1.year is not null
and table1.month is not nulll
这样就可以了:
;WITH Table1 AS (
SELECT *
FROM (VALUES
(2016, 'Januray', 146825.91, 1973),
(2016, 'Fabuary', 129453.30, 5384),
(2016, 'March', 21412.07, 3198),
(2016, 'April', 0.00, 5)
) as t ([Year], [Month], TotalNewCaseAmount, TotalNewCaseCount)
), Table2 AS (
SELECT *
FROM (VALUES
(2016, 'Januray', 54774.41, 147),
(2016, 'Fabuary', 46443.99, 7)
) as t ([Year], [Month], TotalClosingAmount, TotalClosingCount)
), Table3 AS (
SELECT *
FROM (VALUES
(2016, 'Januray', 299.35, 41),
(2016, 'Fabuary', 7568.21, 123),
(2016, 'March', 78.83, 73)
) as t ([Year], [Month], TotalReturnAmount, TotalReturnCount)
)
SELECT t1.[Year],
t1.[Month],
t1.TotalNewCaseAmount,
t1.TotalNewCaseCount,
t2.TotalClosingAmount,
t2.TotalClosingCount,
t3.TotalReturnAmount,
t3.TotalReturnCount
FROM table1 t1
LEFT JOIN table2 t2
ON t1.[Year] = t2.[Year] AND t1.[Month] = t2.[Month]
LEFT JOIN table3 t3
ON t1.[Year] = t3.[Year] AND t1.[Month] = t3.[Month]
输出:
Year Month TotalNewCaseAmount TotalNewCaseCount TotalClosingAmount TotalClosingCount TotalReturnAmount TotalReturnCount
2016 Januray 146825.91 1973 54774.41 147 299.35 41
2016 Fabuary 129453.30 5384 46443.99 7 7568.21 123
2016 March 21412.07 3198 NULL NULL 78.83 73
2016 April 0.00 5 NULL NULL NULL NULL
只需将 table1
、table2
和 table3
更改为您的实际 table 名称即可。