从链接服务器交叉连接左连接查询

时间:2017-08-24 15:47:20

标签: sql sql-server sql-server-2008

有一些CROSS JOIN正在进行,UNION ALL并希望获得2列中的百分比。下面你会看到我的剧本。寻求一点帮助。

DECLARE @Now DATETIME,
@Monday DATETIME,
@Friday DATETIME,
@StartTime VARCHAR(16) ,
@EndTime VARCHAR(16)
SET @StartTime = '00:01:00AM'
SET @EndTime = '11:59:59PM'
SET @Now = GETDATE()
SET @Monday = DATEADD(dd, DATEDIFF(dd, 0, @Now), -1)
SET @Friday = DATEADD(dd, DATEDIFF(dd, 0, @Now), -1)

SELECT t1.EventDate,t1.Sorter, t1.Items,t2.Misfire,t3.DisabledTrays
FROM
(
SELECT EventDate = CONVERT(CHAR(15), CURRENT_TIMESTAMP -1, 106)
   , Items = COUNT(SortID) 
   , Sorter = 'TILT_1' 
FROM  [111.11.1.111].[AAA].[dbo].[SortList]
WHERE CreationTime BETWEEN @Monday + ' ' + @StartTime
                  AND @Friday + ' ' + @EndTime 
                    AND WorkstationID = 'T01'
)t1
CROSS JOIN
(
SELECT EventDate = CONVERT(CHAR(15), CURRENT_TIMESTAMP -1, 106) 
   , Misfire = COUNT(StatsUpd) 
   , 'TILT_1' AS Sorter
FROM  [111.11.1.111].[AAA].[dbo].[MisfireLog]
WHERE RecordedPeriod BETWEEN @Monday + ' ' + @StartTime
                    AND @Friday + ' ' + @EndTime
                        AND WorkstationID = 'T01'
)t2
CROSS JOIN
(
SELECT EventDate = CONVERT(CHAR(15), CURRENT_TIMESTAMP -1, 106)
   , DisabledTrays = COUNT(SorterID)
   , 'TILT_1' AS Sorter
FROM [111.11.1.111].[AAA].[dbo].[DisabledCarriers]
WHERE SorterID  = 'TILT_1'
)t3

UNION ALL

SELECT t1.EventDate,t1.Sorter, t1.Items,t2.Misfire,t3.DisabledTrays
FROM
(
SELECT EventDate = CONVERT(CHAR(15), CURRENT_TIMESTAMP -1, 106)
   , Items = COUNT(SortID) 
   , 'TILT_2' AS Sorter
FROM  [111.11.1.111].[AAA].[dbo].[SortList]
WHERE CreationTime BETWEEN @Monday + ' ' + @StartTime
                AND @Friday + ' ' + @EndTime
                    AND WorkstationID = 'T02'
)t1
CROSS JOIN
(
SELECT EventDate = CONVERT(CHAR(15), CURRENT_TIMESTAMP -1, 106)
   , Misfire = COUNT(StatsUpd) 
   , 'TILT_2' AS Sorter
FROM  [111.11.1.111].[AAA].[dbo].[MisfireLog]
WHERE RecordedPeriod BETWEEN @Monday + ' ' + @StartTime
                    AND @Friday + ' ' + @EndTime
                        AND WorkstationID = 'T02'
)t2
CROSS JOIN
(
SELECT EventDate = CONVERT(CHAR(15),CURRENT_TIMESTAMP -1, 106)
   , DisabledTrays = COUNT(SorterID)
   , 'TILT_2' AS Sorter
FROM [111.11.1.111].[AAA].[dbo].[DisabledCarriers]
WHERE SorterID = 'TILT_2'
)t3;

SELECT 'TILT_1' AS Sorter
   , COUNT (StoreID) AS'NI_Misfires'
FROM [111.11.1.111].[AAA].[dbo].[MisfireLog]
WHERE StoreID = 'NI' AND RecordedPeriod BETWEEN @Monday + ' ' + @StartTime
                    AND @Friday + ' ' + @EndTime AND WorkstationID = 'T01' 

----------------------------------------------- -----------------------------

根据您的上述情况,这是我目前得到的结果集。

EventDate       Sorter Items       Misfire     DisabledTrays
--------------- ------ ----------- ----------- -------------
23 Aug 2017     TILT_1 4332        30          1
23 Aug 2017     TILT_2 4246        21          2

(2 row(s) affected)

Sorter NI_Misfires
------ -----------
TILT_1 5
TILT_2 5

(2行(s)受影响)

----------------------------------------------- -------------------------------

我希望看到以下内容。显示Items - Misfire然后除以ItemsMisfire%)以显示99.30%。我正试图实现这一目标。

EventDate       Sorter Items       Misfire     Accuracy%    DisabledTrays
--------------- ------ ----------- ----------- ---------    -------------
23 Aug 2017     TILT_1 4332        30          99.30%       1
23 Aug 2017     TILT_2 4246        21          99.51%       2

(2 row(s) affected)

Sorter NI_Misfires
------ -----------
TILT_1 5
TILT_2 5

(2 row(s) affected)

1 个答案:

答案 0 :(得分:1)

将您的选择线更改为:

SELECT
t1.EventDate,
t1.Sorter, 
t1.Items,
t2.Misfire,
CONVERT(VARCHAR, ROUND((CONVERT(FLOAT, t1.Items) - CONVERT(FLOAT, t2.Misfire))/CONVERT(FLOAT, t1.Items) * 100, 2)) + '%' AS [Accuracy%],
t3.DisabledTrays