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
.
current result (no 900 brachcode from whstocks)
brachcode | varint | stock --------------------------- 201 |24601121| 2 204 |24601121| 3 197 |24601121| 4
Desired result (bring 900 from whstocks.warehouse into 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
. If not, substitute the matching fields from 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.