[英]Concatenate two columns with different names
I have two tables, joined on one column, I want to concatenate one of the columns with different names into the same column, I have tried this 我有两个表,连接在一个列上,我想将具有不同名称的列之一连接到同一列中,我已经尝试过
I want whstocks.warehouse
to be concatenated with brnstocks.brachcode
. 我希望将
whstocks.warehouse
与brnstocks.brachcode
连接在一起。
current result (no 900 brachcode from whstocks) 当前结果(没有来自whstocks的900 brachcode)
brachcode | varint | stock --------------------------- 201 |24601121| 2 204 |24601121| 3 197 |24601121| 4
Desired result (bring 900 from whstocks.warehouse into branchcode) 所需的结果(将whstocks.warehouse中的900带入branchcode)
brachcode | varint | stock --------------------------- 201 |24601121| 2 204 |24601121| 3 197 |24601121| 4 900 |24601121| 400
SELECT brnstocks.branchcode , brnstocks.varint , SUM(brnstocks.retail)
FROM dbo.brnstocks
INNER JOIN dbo.whstocks
ON brnstocks.varint = whstocks.varint
GROUP BY brnstocks.branchcode, whstocks.warehouse
,brnstocks.varint
,brnstocks.retail
,whstocks.stock
HAVING (SUM(brnstocks.retail) > 0)
Try this: 尝试这个:
SELECT
P.branchcode , P.varint , SUM(P.retail)
FROM
(SELECT
branchcode , varint , retail
FROM dbo.brnstocks
UNION
SELECT
branchcode , varint , retail
FROM dbo.whstocks
) AS P
GROUP BY P.branchcode , P.varint
HAVING (SUM(P.retail) > 0)
Assuming that whstocks
has the same fields as brnstocks
. 假设
whstocks
具有相同的字段brnstocks
。 If not, substitute the matching fields from whstocks
如果不是,请替换
whstocks
的匹配字段
Since you're already grouping by both, you should be able to concatenate them using the + operator. 由于您已经将两者分组,因此您应该可以使用+运算符将它们串联起来。
SELECT whstocks.warehouse + brnstocks.branchcode [WarehousePlusBranch], brnstocks.varint , SUM(brnstocks.retail)
FROM dbo.brnstocks
INNER JOIN dbo.whstocks
ON brnstocks.varint = whstocks.varint
GROUP BY brnstocks.branchcode
,whstocks.warehouse
,brnstocks.varint
,brnstocks.retail
,whstocks.stock
HAVING (SUM(brnstocks.retail) > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.