Sql server 2012 从一个表中获取一行,并检查另一个表中的对应行(如果存在),然后将此行添加到第一个表行的下一行

Sql server 2012 从一个表中获取一行,并检查另一个表中的对应行(如果存在),然后将此行添加到第一个表行的下一行,sql-server-2012,Sql Server 2012,我有一个表,比如a,它有一些行和'Id'列作为主键。和其他表B,它有“TabAId”并引用表A id列。 希望获得如所附图像所示的报告。 解释 I“sql server”数据库 从表A中选择一行并检查表B中的Id,如果存在,则将表B行添加为下一行(如果存在多行,则也将这些行数添加为下一行),否则继续 尝试使用case语句,该语句追加到行,而不是作为下一行添加。 和join发生的情况也一样 通过php之类的编程语言或jquery和ajax之类的脚本编写可能很容易,但我只希望通过sql server

我有一个表,比如a,它有一些行和'Id'列作为主键。和其他表B,它有“TabAId”并引用表A id列。 希望获得如所附图像所示的报告。 解释 I“sql server”数据库 从表A中选择一行并检查表B中的Id,如果存在,则将表B行添加为下一行(如果存在多行,则也将这些行数添加为下一行),否则继续

尝试使用case语句,该语句追加到行,而不是作为下一行添加。 和join发生的情况也一样

通过php之类的编程语言或jquery和ajax之类的脚本编写可能很容易,但我只希望通过sql server实现。这有助于我满足进一步的需求

所以请有人帮帮我。

编辑:

create table tabA(id int not null primary key,
name varchar(20) null,age int null)

insert into tabA values(1,'Sudeep',35),
(2,'Darshan',34)

create table tabB(A_id int not null,nickname varchar(20) null )

insert into tabB values(1,'Kiccha'),
(1,'Nalla'),
(2,'Boss')
输出应如下所示

    Id | name        | age  |
    ------------------------
    1  | Sudeep      | 35   |
    ------------------------
       | *Kichha     |      |
    ------------------------
       | *Nalla      |      |
    ------------------------
    2  | Darshan     | 34   |
    ------------------------
       | *Boss       |      |
    ------------------------

根据要求,我做了以下工作

    ;

WITH cte
AS (
    SELECT *
        ,DENSE_RANK() OVER (
            ORDER BY id
            ) dn
        ,ROW_NUMBER() OVER (
            PARTITION BY id ORDER BY age DESC
            ) rn
    FROM (
        SELECT *
        FROM tabA a

        UNION ALL

        SELECT *
            ,NULL
        FROM tabB b where exists (select 1 from taba a where a.id=b.A_id)
        ) a
    )
SELECT iif(rn = 1, cast(id AS VARCHAR(50)), '') ID
    ,CONCAT (
        iif(rn = 1, '', '*')
        ,name
        ) NAME
    ,iif(rn = 1, cast(age AS VARCHAR(50)), '') AGE
FROM cte
如果需要补充什么,请告诉我

编辑:根据要求显示基于偏移的结果

if OBJECT_ID('tempdb..#cte_results') is not null
drop table #cte_results
/*
in order to achieve the second goal we need to store in results in a table then use that table to display
results
*/
;

WITH cte
AS (
    SELECT *
        ,DENSE_RANK() OVER (
            ORDER BY id
            ) dn
        ,ROW_NUMBER() OVER (
            PARTITION BY id ORDER BY age DESC
            ) rn
        ,ROW_NUMBER() over( order by id asc,age desc) off_set
    FROM (
        SELECT *
        FROM tabA a

        UNION ALL

        SELECT *
            ,NULL
        FROM tabB b where exists (select 1 from taba a where a.id=b.A_id)
        ) a
    )
SELECT iif(rn = 1, cast(id AS VARCHAR(50)), '') ID
    ,CONCAT (
        iif(rn = 1, '', '*')
        ,name
        ) NAME
    ,iif(rn = 1, cast(age AS VARCHAR(50)), '') AGE,off_set,rn,max(rn) over(partition by id) max_rn,id idrrr
    into #cte_results
FROM cte


/*
the following query is used to display the results in the screen dynamically
*/
declare @pre_offset  int=0, @post_offset int =2

set @post_offset=( select top 1  max(max_rn)-max(rn) 
from #cte_results
where off_Set 
between @pre_offset and @post_offset
group by idrrr
order by idrrr desc
)+@post_offset


select id,name,age from #cte_results
where off_Set 
between @pre_offset and @post_offset
结果如下

根据要求,我做了以下工作

    ;

WITH cte
AS (
    SELECT *
        ,DENSE_RANK() OVER (
            ORDER BY id
            ) dn
        ,ROW_NUMBER() OVER (
            PARTITION BY id ORDER BY age DESC
            ) rn
    FROM (
        SELECT *
        FROM tabA a

        UNION ALL

        SELECT *
            ,NULL
        FROM tabB b where exists (select 1 from taba a where a.id=b.A_id)
        ) a
    )
SELECT iif(rn = 1, cast(id AS VARCHAR(50)), '') ID
    ,CONCAT (
        iif(rn = 1, '', '*')
        ,name
        ) NAME
    ,iif(rn = 1, cast(age AS VARCHAR(50)), '') AGE
FROM cte
如果需要补充什么,请告诉我

编辑:根据要求显示基于偏移的结果

if OBJECT_ID('tempdb..#cte_results') is not null
drop table #cte_results
/*
in order to achieve the second goal we need to store in results in a table then use that table to display
results
*/
;

WITH cte
AS (
    SELECT *
        ,DENSE_RANK() OVER (
            ORDER BY id
            ) dn
        ,ROW_NUMBER() OVER (
            PARTITION BY id ORDER BY age DESC
            ) rn
        ,ROW_NUMBER() over( order by id asc,age desc) off_set
    FROM (
        SELECT *
        FROM tabA a

        UNION ALL

        SELECT *
            ,NULL
        FROM tabB b where exists (select 1 from taba a where a.id=b.A_id)
        ) a
    )
SELECT iif(rn = 1, cast(id AS VARCHAR(50)), '') ID
    ,CONCAT (
        iif(rn = 1, '', '*')
        ,name
        ) NAME
    ,iif(rn = 1, cast(age AS VARCHAR(50)), '') AGE,off_set,rn,max(rn) over(partition by id) max_rn,id idrrr
    into #cte_results
FROM cte


/*
the following query is used to display the results in the screen dynamically
*/
declare @pre_offset  int=0, @post_offset int =2

set @post_offset=( select top 1  max(max_rn)-max(rn) 
from #cte_results
where off_Set 
between @pre_offset and @post_offset
group by idrrr
order by idrrr desc
)+@post_offset


select id,name,age from #cte_results
where off_Set 
between @pre_offset and @post_offset
结果如下

您能分享样本数据吗……这将有助于answer@Smart003事实上,我已经为上面显示的图像参考了多个表。我可以创建示例表并给出吗?.Yes@Mahantesh….@Smart003我已经编辑了我的问题,请检查一次…@Smart003感谢您的ans,是否可以按照要求对CTE内的查询应用分页:如果我希望每页获得10行,并且如果最后一行的选项卡中存在任何昵称,则获取所有昵称行。?所以每页的总行数会动态增加。您可以共享示例数据吗…这将有助于answer@Smart003事实上,我已经为上面显示的图像参考了多个表。我可以创建示例表并给出吗?.Yes@Mahantesh….@Smart003我已经编辑了我的问题,请检查一次…@Smart003感谢您的ans,是否可以按照要求对CTE内的查询应用分页:如果我希望每页获得10行,并且如果最后一行的选项卡中存在任何昵称,则获取所有昵称行。?所以每页的总行数会动态增加。谢谢。。。我从你的ans那里了解了很多。谢谢你。。。我从你的ans那里了解了很多。