SQL Server Combine 2 different queries with different columns

mjqavswn  于 2023-05-16  发布在  其他
关注(0)|答案(2)|浏览(159)

How can I combine the 2 queries below in order to have the result in the same table as 4 columns Table name, last create date, last update date and columns count?

select distinct 
    'abc.gcc_case' Table, 
    max(create_date) as Last_Create_Date, 
    max(update_date) as Last_Update_Date 
from abc.gcc_case

select 
    count(*) as Columns_Count 
from information_schema.columns
where Table_name = 'gcc_case'

I was trying to use a join but I am not sure what to use to give the result I was looking for.

kkbh8khc

kkbh8khc1#

Here is an option

The distinct is redundant

select 'abc.gcc_case' Table
         ,max(create_date) as Last_Create_Date
         ,MAX(update_date) as Last_Update_Date 
         ,(select count(*) as Columns_Count 
            from information_schema.columns
           where Table_name = 'gcc_case'
          ) as column_count
     from abc.gcc_case
jv4diomz

jv4diomz2#

Something I noticed is that you want the output of these two queries to be in the a row

select * 
from  (

             select distinct 
                'abc.gcc_case' TableTableName, 
                max(create_date) as Last_Create_Date, 
                MAX(update_date) as Last_Update_Date 
            from abc.gcc_case
)List
cross apply (  
               select 
                    count(*) as Columns_Count 
                from information_schema.columns
                where Table_name = 'gcc_case'
        )cot

result:

TableNameLast_Create_DateLast_Update_DateColumns_Count
abc.gcc_case2020-02-012020-02-0110

相关问题