如何比较SQL Server 2014中同一个表的记录?

时间:2017-05-08 23:45:46

标签: sql-server sql-server-2014

我有一个表格,显示项目进入和退出仓库。摄像机1和摄像机2分别记录该项目的进入时间和退出时间。然后摄像机在物品进入时对物品进行分类,并在激光的帮助下离开检查点。例如:大盒子:5级,中型盒子:3级,小盒子:2级。

有时,相机分类彼此不匹配。例如:入口处的分类可以是中等框,退出时可以是小框。

我需要找到相同TransactionDetail的类不匹配的事务数,然后在特定时间范围内找到所有事务中这些类不匹配的百分比。

我的表看起来有点像这样:

---------------------------------------------------------------------------
| AVDetailID | TransDetailID | AVClassID | CamID | CreatedDate            |
---------------------------------------------------------------------------
| 20101522   | 54125478      | 5         | 1     | 2017-05-08 10:15:01:560|
| 20101523   | 54125478      | 5         | 2     | 2017-05-08 10:15:01:620|
| 20101524   | 54125479      | 3         | 1     | 2017-05-08 10:15:03:120|
| 20101525   | 54125479      | 2         | 2     | 2017-05-08 10:15:03:860|
| 20101526   | 54125480      | 4         | 1     | 2017-05-08 10:15:06:330|
| 20101527   | 54125480      | 4         | 2     | 2017-05-08 10:15:06:850|
---------------------------------------------------------------------------

因此,在上面的例子中,类在记录3和4中从3变为2.这是类更改的一个事务。我需要获得每个摄像机之间班级变化的所有交易的百分比。

我到目前为止使用的代码如下。我只需要找到一种方法来获得总交易量的百分比。

DECLARE @MinDate DATE = '20170406',
        @MaxDate DATE = '20170407';

SELECT  COUNT(tdBefore.TransDetailId) TD
        --,SUM((COUNT(*) OVER() / allRecords.Count) * 100) AS DiffPercent

FROM    AVTransDetail AS tdBefore
INNER JOIN AVTransDetail AS tdAfter
    ON tdBefore.TransDetailID = tdAfter.TransDetailID 
    AND tdBefore.ACClassID = 1
    AND tdAfter.ACClassID = 2
CROSS APPLY
(
    SELECT COUNT(*) AS [Count]
    FROM AVTransDetail
    WHERE tdBefore.DateCreated >= @MinDate
        AND tdAfter.DateCreated <= @MaxDate
) AS allRecords
WHERE   tdBefore.AVCClassId <> tdAfter.AVCClassId 
        AND tdBefore.DateCreated >= @MinDate
        AND tdAfter.DateCreated <= @MaxDate

如何为总交易百分比创建列?

3 个答案:

答案 0 :(得分:2)

这适用于您的样本数据。

DECLARE @MinDate DATETIME = '5/8/2017 12:00AM';
DECLARE @MaxDate DATETIME = '5/8/2017 11:59PM';

WITH cam1 AS ( 
    SELECT TransDetailID,AVClassID
    FROM AVTransDetail
    WHERE CreatedDate BETWEEN @MinDate AND @MaxDate
    AND
    CamID = 1),

cam2 AS (
    SELECT TransDetailID,AVClassID
    FROM AVTransDetail
    WHERE CreatedDate BETWEEN @MinDate AND @MaxDate
    AND
    CamID = 2)

SELECT COUNT(*)'Total',SUM(CASE WHEN c1.AVClassID = c2.AVClassID THEN 0 ELSE 1 END)'NonMatch',
       SUM(CASE WHEN c1.AVClassID = c2.AVClassID THEN 0 ELSE 1 END) * 100.00/COUNT(*)'Percentage'
FROM cam1 c1
     JOIN cam2 c2 ON c1.TransDetailID=c2.TransDetailID

答案 1 :(得分:1)

尝试以下SQL脚本。

首先我们LAG找到差异。然后,我们得到每笔交易以及是否存在差异。最后,我们得到了百分比。

Option Explicit 'force all variables to be declared

On Error Resume Next
Const ForWriting = 2
Dim objFSO
Set objFSO = CreateObject(Scripting.FileSystemObject)

Dim objTS 'Text Stream Object
Set objTS = objFSO.OpenTextFile("output_path", ForWriting, True)

Recurse objFSO.GetFolder("location_of_files")
objTS.Close()

Sub Recurse(objFolder)
    Dim objFile, objSubFolder

    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile.Name)) = "exe" Then
            objTS.WriteLine(objfile.Path)
        End If
    Next

    For Each objSubFolder In objFolder.SubFolders
        Recurse objSubFolder
    Next
End Sub

希望这有帮助。

答案 2 :(得分:0)

我添加了更多样本行以获得更好的结果

create table #trans (
    AVDetailID int,
    TransDetailID int,
    AVClassID int,
    CamID int,
    CreatedDate datetime
)

insert into #trans values 
( 20101522, 54125478, 5, 1, '2017-05-08 10:15:01:560'),
( 20101523, 54125478, 5, 2, '2017-05-08 10:15:01:620'),
( 20101524, 54125479, 3, 1, '2017-05-08 10:15:03:120'),
( 20101525, 54125479, 2, 2, '2017-05-08 10:15:03:860'),
( 20101526, 54125480, 4, 1, '2017-05-08 10:15:06:330'),
( 20101527, 54125480, 4, 2, '2017-05-08 10:15:06:850'),
( 20101528, 54125481, 4, 1, '2017-05-08 10:15:07:850'),
( 20101529, 54125481, 5, 2, '2017-05-08 10:15:09:850'),
( 20101530, 54125482, 4, 1, '2017-05-08 10:15:07:850'),
( 20101531, 54125482, 5, 3, '2017-05-08 10:15:09:850')

;with diff as (
    -- select records that have different class 
    select CamID as Ent_CamID, count(*) diff_Count
    from #trans ent 
        outer apply (
            select top 1 AVClassID as x_AVClassID, CamID as x_CamID from #trans 
            where CreatedDate > ent.CreatedDate and TransDetailID = ent.TransDetailID
            order by CamID, CreatedDate desc
        ) ext
    where ent.AVClassID <> ext.x_AVClassID
    group by ent.CamID, ext.x_CamID
    union
    select ext.x_CamID as Ext_CamID, count(*) diff_Count
    from #trans ent 
        outer apply (
            select top 1 AVClassID as x_AVClassID, CamID as x_CamID from #trans 
            where CreatedDate > ent.CreatedDate and TransDetailID = ent.TransDetailID
            order by CamID, CreatedDate desc
        ) ext
    where ent.AVClassID <> ext.x_AVClassID
    group by ent.CamID, ext.x_CamID
)
, perc as (
    select Ent_CamID as CamID, sum(diff_Count) Total_Error
    , (select count(*) 
       from #trans where CamID = diff.Ent_CamID 
       group by CamID) AS Total_Capture
    from diff
    group by Ent_CamID
)
select CamID, Total_Error, Total_Capture, 100*(Total_Error)/Total_Capture Error_Percentage
from perc

结果:

CamID   Total_Error Total_Capture   Error_Percentage
1       3           5               60
2       2           4               50
3       1           1               100