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

时间:2018-07-24 05:39:05

标签: sql-server-2012

我有一个说A的表,它有一些行和'Id'列作为主键。和其他表B一样,它具有'TabAId'并引用表A id列。 想要获得一个如所附图片所示的报告。 说明 我'sql server'数据库   从表A中选择一行,然后检查表B中的ID,如果存在,则将表B行添加为下一行(如果存在多行,则将这些行数也添加为下一行),否则继续进行。

尝试将case语句追加到该行,而不添加为下一行。 使用join也只会发生相同的情况。

通过php之类的编程语言或诸如jquery和ajax之类的脚本可能很容易,但是我只希望通过sql server。它可以帮助我满足进一步的要求。

所以请有人帮我。 enter image description here

编辑:

$flag = 0;
if(!empty($_POST['agent_wise'])){ 
        $flag = 1;    
        $agent_wise = $_POST['agent_wise'];
        $queryCondition .= "allusers.sno ='$agent_wise'";
     } 

if(!empty($_POST['status_wise'])){      
        $status_wise = $_POST['status_wise'];
        if($flag == 1){
        $queryCondition .= " AND st_application.admin_status_crs ='$status_wise'";
        }
      else {
        $queryCondition .= " st_application.admin_status_crs ='$status_wise'";
         $flag = 1;
  }
    }   
    if(!empty($_POST['course_wise'])){      
        $course_wise = $_POST['course_wise'];
       if ($flag == 1){
        $queryCondition .= " AND st_application.prg_name1 ='$course_wise'";
      }
       else {
           $queryCondition .= " st_application.prg_name1 ='$course_wise'";
    }
    }


$result2 = "SELECT * FROM st_application INNER JOIN allusers on st_application.user_id = allusers.sno where " . $queryCondition . " ";

输出应如下所示

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')

1 个答案:

答案 0 :(得分:2)

根据要求,我已执行以下操作。

    ;

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

结果如下 enter image description here