如何在链接服务器上使用Join

时间:2016-01-06 12:18:12

标签: sql-server

我正在尝试远程从两个链接的服务器获取客户的结果。我需要总结每个cust_id的点,但我的查询有问题

SELECT  sum(cust_point) as total
FROM [192.168.23.9].[POSDBV4].[dbo].[loyal_summery_branch] where cust_id='0100015388' 
INNER JOIN [192.168.13.4].[POSDBV4].[dbo].[loyal_summery_branch]
ON cust_id.[192.168.23.9].[POSDBV4].[dbo].[loyal_summery_branch]=cust_id.[192.168.13.4].[POSDBV4].[dbo].[loyal_summery_branch];

3 个答案:

答案 0 :(得分:1)

我认为您的查询语法有点混乱。试试这个。

SELECT sum(cust_point) as total 
FROM [192.168.23.9].[POSDBV4].[dbo].[loyal_summery_branch] A
INNER JOIN [192.168.13.4].[POSDBV4].[dbo].[loyal_summery_branch] B ON A.cust_id=B.cust_id
WHERE cust_id='0100015388' 

答案 1 :(得分:1)

如果你想要两个表的cust_point的总和。请在下面找到查询

Select( (SELECT  sum(cust_point)
        FROM [192.168.23.9].[POSDBV4].[dbo].[loyal_summery_branch] where cust_id='0100015388') +  
    (SELECT  sum(cust_point)
        FROM [192.168.13.4].[POSDBV4].[dbo].[loyal_summery_branch] where cust_id='0100015388') ) as total

答案 2 :(得分:0)

如果您愿意,可以随时在此使用UNION ALL ..如果您添加GROUP BY

,也可以选择其他字段
SELECT  SUM(cust_point) AS total
FROM    (
            SELECT  cust_point
            FROM    [192.168.23.9].[POSDBV4].[dbo].[loyal_summery_branch]
            WHERE   cust_id = '0100015388'
            UNION ALL
            SELECT  cust_point
            FROM    [192.168.13.4].[POSDBV4].[dbo].[loyal_summery_branch]
            WHERE   cust_id = '0100015388'
        ) t