sql在没有条件的情况下连接两个单元格

sql在没有条件的情况下连接两个单元格,sql,Sql,在SSMS中,当我运行 select 1 as A, 2 as B select avg(column1) from table1 where company like '%3m%'and Column2!=0 select avg(column3) from table1 where company like '%3m%'and Column4!=0 我得到一张一行两格的桌子 当我跑的时候 select 1 as A, 2 as B select avg(col

在SSMS中,当我运行

      select 1 as A, 2 as B
select avg(column1) from table1 where company like '%3m%'and Column2!=0
select avg(column3) from table1 where company like '%3m%'and Column4!=0
我得到一张一行两格的桌子

当我跑的时候

      select 1 as A, 2 as B
select avg(column1) from table1 where company like '%3m%'and Column2!=0
select avg(column3) from table1 where company like '%3m%'and Column4!=0
我得到一个数字x

当我跑的时候

      select 1 as A, 2 as B
select avg(column1) from table1 where company like '%3m%'and Column2!=0
select avg(column3) from table1 where company like '%3m%'and Column4!=0
我得到另一个数字y

我想知道如何使用单个查询将x和y放在一个单行表中

谢谢你的建议

试试这个:

SELECT 
    (SELECT AVG(column1) FROM table1 WHERE company LIKE '%3m%'and Column2!=0) AS A,
    (SELECT AVG(column3) FROM table1 WHERE company LIKE '%3m%'and Column4!=0) AS B
SELECT 
    AVG(CASE WHEN Column2 !=0 THEN Column1 END) AS A,
    AVG(CASE WHEN Column4 !=0 THEN Column3 END) AS B
FROM Table1
WHERE Company LIKE '%3m%' AND (Column2!=0 OR Column4 !=0)
试试这个:

SELECT 
    AVG(CASE WHEN Column2 !=0 THEN Column1 END) AS A,
    AVG(CASE WHEN Column4 !=0 THEN Column3 END) AS B
FROM Table1
WHERE Company LIKE '%3m%' AND (Column2!=0 OR Column4 !=0)
试试这个:

SELECT 
    AVG(CASE WHEN Column2 !=0 THEN Column1 END) AS A,
    AVG(CASE WHEN Column4 !=0 THEN Column3 END) AS B
FROM Table1
WHERE Company LIKE '%3m%' AND (Column2!=0 OR Column4 !=0)
试试这个:

SELECT 
    AVG(CASE WHEN Column2 !=0 THEN Column1 END) AS A,
    AVG(CASE WHEN Column4 !=0 THEN Column3 END) AS B
FROM Table1
WHERE Company LIKE '%3m%' AND (Column2!=0 OR Column4 !=0)
是的,你可以

SELECT avg(case when Column2!=0 then column1 else NULL end)
     , avg(case when Column4!=0 then column3 else NULL end)
FROM   table1
WHERE  company LIKE '%3m%'
是的,你可以

SELECT avg(case when Column2!=0 then column1 else NULL end)
     , avg(case when Column4!=0 then column3 else NULL end)
FROM   table1
WHERE  company LIKE '%3m%'
是的,你可以

SELECT avg(case when Column2!=0 then column1 else NULL end)
     , avg(case when Column4!=0 then column3 else NULL end)
FROM   table1
WHERE  company LIKE '%3m%'
是的,你可以

SELECT avg(case when Column2!=0 then column1 else NULL end)
     , avg(case when Column4!=0 then column3 else NULL end)
FROM   table1
WHERE  company LIKE '%3m%'

当执行
select
语句时,将得到一个结果集,而不是一个表。对于您的问题,您可以尝试使用
case
函数构造一个合适的判别函数,如下所示:

select avg(case when column2 != 0 then column1 else null end) as col1_average ,
       avg(case when column4 != 0 then column3 else null end) as col3_average
from table1
where company like '%3m%'

当执行
select
语句时,将得到一个结果集,而不是一个表。对于您的问题,您可以尝试使用
case
函数构造一个合适的判别函数,如下所示:

select avg(case when column2 != 0 then column1 else null end) as col1_average ,
       avg(case when column4 != 0 then column3 else null end) as col3_average
from table1
where company like '%3m%'

当执行
select
语句时,将得到一个结果集,而不是一个表。对于您的问题,您可以尝试使用
case
函数构造一个合适的判别函数,如下所示:

select avg(case when column2 != 0 then column1 else null end) as col1_average ,
       avg(case when column4 != 0 then column3 else null end) as col3_average
from table1
where company like '%3m%'

当执行
select
语句时,将得到一个结果集,而不是一个表。对于您的问题,您可以尝试使用
case
函数构造一个合适的判别函数,如下所示:

select avg(case when column2 != 0 then column1 else null end) as col1_average ,
       avg(case when column4 != 0 then column3 else null end) as col3_average
from table1
where company like '%3m%'

非常感谢。那很简单,很快!非常感谢。那很简单,很快!非常感谢。那很简单,很快!非常感谢。那很简单,很快!