从另一台服务器获取其他表的行详细信息并将其合并到您的查询中

时间:2016-06-17 06:02:13

标签: sql sql-server tsql

Select REPLACE(CONVERT(VARCHAR(20),
CAST(abs(SUM(
CASE WHEN [G_L Account No_] in ('5010', '5011','5020','5030')
and [Global Dimension 1 Code] = 'BANQUITO' 
THEN Amount ELSE 0 END)+ 
SUM(
CASE WHEN [G_L Account No_] between '5041' and '5047'
and [Global Dimension 1 Code] = 'BANQUITO'  
THEN Amount ELSE 0 END)) as MONEY),1),'.00','') As Net,
replace(convert(NVARCHAR,[Posting Date],110),'-','/')as PostingDate
from [The Cravings Group 2013$G_L Entry]  
where [Posting Date] between '2016-01-01 00:00:00' and '2016-01-30 00:00:00'  
Group by [Posting Date];

这将导致

 COLA | COLB 
 row  | row

我想在此查询中添加另一列

select count(distinct checkno) as  TCOUNT
from [10.0.0.165].[MenuEngg].[dbo].GNDSale  
where dob between '2016-01-01 00:00:00' and '2016-01-30 00:00:00'
and branchcode in ('037') and branchcode in  ('037')and type in ('31','19')

它应该给我cola colb colc,我不知道如何将[全局维度1代码]与另一个使用[分支代码]的数据库相匹配?获取或合并它们并得出这个结果

COLA | COLB | COLC
row  | row  | row

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT a.Net,a.PostingDate,
    (
      SELECT count(DISTINCT checkno) AS TCOUNT
       FROM [10.0.0.165].[MenuEngg].[dbo].GNDSale
       WHERE dob BETWEEN '2016-01-30 00:00:00' AND '2016-01-30 00:00:00'
         AND branchcode IN ('037')
         AND branchcode IN ('037')
         AND TYPE IN ('31','19') 
    ) as COLC
FROM
  ( SELECT REPLACE(CONVERT(VARCHAR(20), 
        CAST(abs(SUM( CASE WHEN [G_L Account No_] IN ('5010', '5011','5020','5030')
            AND [Global Dimension 1 Code] = 'BANQUITO' THEN Amount ELSE 0 END)+ SUM( CASE WHEN [G_L Account No_] BETWEEN '5041' AND '5047'
                AND [Global Dimension 1 Code] = 'BANQUITO' THEN Amount ELSE 0 END)) AS MONEY),1),'.00',''
                   ) AS Net,
           replace(convert(NVARCHAR,[Posting Date],110),'-','/') AS PostingDate
   FROM [The Cravings Group 2013$G_L Entry]
   WHERE [Posting Date] BETWEEN '2016-01-30 00:00:00' AND '2016-01-30 00:00:00'
   GROUP BY [Posting Date] 
   ) a