Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL脚本以特定方式在2列上突出显示模式_Sql_Sql Server_Tsql - Fatal编程技术网

SQL脚本以特定方式在2列上突出显示模式

SQL脚本以特定方式在2列上突出显示模式,sql,sql-server,tsql,Sql,Sql Server,Tsql,我试图找到一种方法,对2列运行一些SQL,这将突出显示Col1值,其中,对于给定的Col1值,Col2值不直接显示在其Col2等价值的旁边。 最后一部分很重要,即Col1值4000的5921是8888的两倍,因此4000是有效的 但是Col2注意,我用**用粗体突出显示1011是单独的,而不是在列表后面的1011值旁边,这是我需要突出显示的 一开始我觉得很容易,然后又很挣扎,一直在构建循环临时表等,我想一定有更简单的方法来实现这一点,谢谢阅读 注:示例数据很小,要获得解决方案的正确逻辑,只有以下

我试图找到一种方法,对2列运行一些SQL,这将突出显示Col1值,其中,对于给定的Col1值,Col2值不直接显示在其Col2等价值的旁边。 最后一部分很重要,即Col1值4000的5921是8888的两倍,因此4000是有效的

但是Col2注意,我用**用粗体突出显示1011是单独的,而不是在列表后面的1011值旁边,这是我需要突出显示的

一开始我觉得很容易,然后又很挣扎,一直在构建循环临时表等,我想一定有更简单的方法来实现这一点,谢谢阅读

注:示例数据很小,要获得解决方案的正确逻辑,只有以下内容。解决方案将突出显示给定的Col1值,以及组中缺少的重复散乱者

Col1    Col2 
4000    5921
4000    5921
4000    8888
4284    6359
4284    6359
4284    6359
4284    5921
4284    **1011**
4284    5921
4284    4970
4284    4970
4284    1011
4284    1011
4284    1011
4284    1011
4284    1011
4284    1011
4284    1011
4284    1011
我走了这么远。。注意:Col1是PremiseProviderID,Col2是CustomerID

declare @PremiseProviderId int;declare @CustomerId int; 
declare @result table
(
    RowID int not null identity(1,1) primary key,
    PremiseProviderVersionId    int,
    assigndate        DateTime,   
    EffectiveDate      DateTime,
    effectivedateend DateTime,
    PremiseProviderId int,
    customerid int,comments varchar(max), D2044_RVTransition bit); declare @Spids table (RowID int not null identity(1,1) primary key,PremiseProviderId    int);

insert into @result (PremiseProviderVersionId,assigndate,EffectiveDate,effectivedateend,PremiseProviderId,customerid,comments,D2044_RVTransition)  
select PremiseProviderVersionId,assigndate,EffectiveDate,effectivedateend,PremiseProviderId,customerid,comments,D2044_RVTransition from 
PremiseProviderVersions where   TransactionDateTimeEnd is null order by PremiseProviderId desc, EffectiveDate desc, PremiseProviderVersionId desc;

insert into @Spids (PremiseProviderId) select distinct PremiseProviderId from @result;

declare @i int
select @i = min(RowID) from @Spids
declare @max int
select @max = max(RowID) from @Spids

while @i <= @max begin
     set @PremiseProviderId = (select PremiseProviderId from @result where PremiseProviderId = @i) ;
     set @CustomerId = (select CustomerId from @result where PremiseProviderId = @i) ;

        -- I got to here and realised, must be easier way

    set @i = @i + 1
end

但7541还可以,因为它不是7542。因此,我认为逻辑在整个第2列上进行检测,并且在查看7541 col1时不会重置。

好的,这与此非常相似,但我理解可能很难将其转换为您的情况,因此,现在开始:

首先,您确实需要一个一致的排序列,我想您可能有一个,但您的数据没有反映它

其次,如果根据数据的实际情况分配行号,并根据其应如何分配行号,并且实际行号大于应如何分配行号的任何情况都会给出错误行

declare @Test table (col1 int, col2 int, col3 int);

insert into @Test (col1, col2, col3)
values
(4000,8888,1),
(4000,5921,2),
(4000,5921,3),
(4284,5921,4),
(4284,1011,5),
(4284,4970,6),
(4284,1011,7);

with cte as (
  select *
    -- "Should Be" row number
    , row_number() over (partition by col1 order by col2 desc, col3 asc) rn1
    -- "Actual" row number
    , row_number() over (partition by col1 order by col3 asc) rn2
  from @Test
)
select col1, col2 --, col3, rn1, rn2
from cte
-- Comment out the following line and uncomment out the additional columns in the select to see the working
where rn1 > rn2
order by col1, col3;
返回

可乐 可乐 4284 1011
您似乎希望行的值小于下一行的值。只要使用铅:


请注意,您的示例数据与您编写的代码不一致,这使得问题很难理解。以上假设rowid是指定行顺序的列。

根据问题指南,请说明您尝试了什么,并告诉我们您在本网站或其他地方发现了什么,以及为什么它不能满足您的需要。请通过添加其他示例进一步澄清您的数据,并向我们展示您的实际期望输出。您还必须有第三列进行排序,否则您只需按Col1、Col2排序,它将始终有效。数据的方向有关系吗?因为您已经显示了递增和递减数据。一旦你澄清了所有这些,你可能会寻找一个窗口函数。这是一个数据分析的事情,数据需要在一个特定的顺序,然后检查。查找损坏的双时态数据很好,但您的示例数据没有显示其顺序。。。这对解决这个问题至关重要。正如我在上面提到的,你的一些数据是上升的,一些是下降的,这使事情变得复杂。其中一个答案对你有帮助吗?顺序不能重新排序,它对解决方案至关重要。这两列都是主键,其值不在较小的位置,而是在与第一列相同的行组中重新显示时。第二列只能同时具有一个或多个值,对于给定的左列值,该值是否会重新出现?或者只是不按顺序出现?当col2值重新出现,但给定col1时,它可以连续重新出现,但如果它发生变化,则不会出现,然后重新出现。起初,它似乎起作用,但是,它不是,但它已关闭。我将编辑并显示why@John当我运行您的测试编号时,我得到一个75421136的结果,它看起来是正确的,因为它的顺序不正确。我确实认为你需要澄清你的问题,因为你似乎想要的东西比你的样本数据显示的要多。
declare @Test table (col1 int, col2 int, col3 int);

insert into @Test (col1, col2, col3)
values
(4000,8888,1),
(4000,5921,2),
(4000,5921,3),
(4284,5921,4),
(4284,1011,5),
(4284,4970,6),
(4284,1011,7);

with cte as (
  select *
    -- "Should Be" row number
    , row_number() over (partition by col1 order by col2 desc, col3 asc) rn1
    -- "Actual" row number
    , row_number() over (partition by col1 order by col3 asc) rn2
  from @Test
)
select col1, col2 --, col3, rn1, rn2
from cte
-- Comment out the following line and uncomment out the additional columns in the select to see the working
where rn1 > rn2
order by col1, col3;
select r.*
from (select r.*,
             lead(col2) over (partition by col1 order by rowid) as next_col2
      from @results r
     ) r
where next_col2 > col2;