문제
어떻게 선택할 수 있습니까? count(*)
두 개의 다른 테이블에서 전화하십시오 (전화하십시오 tab1
그리고 tab2
) 결과 : 결과 :
Count_1 Count_2
123 456
나는 이것을 시도했다 :
select count(*) Count_1 from schema.tab1 union all select count(*) Count_2 from schema.tab2
그러나 내가 가진 것은 다음과 같습니다.
Count_1
123
456
해결책
SELECT (
SELECT COUNT(*)
FROM tab1
) AS count1,
(
SELECT COUNT(*)
FROM tab2
) AS count2
FROM dual
다른 팁
추가 정보로 SQL Server에서 동일한 작업을 수행하려면 쿼리의 "듀얼"부분을 제거하면됩니다.
약간 다르기 때문에 :
SELECT 'table_1' AS table_name, COUNT(*) FROM table_1
UNION
SELECT 'table_2' AS table_name, COUNT(*) FROM table_2
UNION
SELECT 'table_3' AS table_name, COUNT(*) FROM table_3
그것은 대답을 바꾸는 답변 (한 열 대신 테이블 당 한 행)을 제공합니다. 그렇지 않으면 크게 다르다고 생각합니다. 나는 성능면에서 그들이 동등해야한다고 생각합니다.
제 경험은 SQL Server에 대한 것이지만 다음과 같은 일을 할 수 있습니다.
select (select count(*) from table1) as count1,
(select count(*) from table2) as count2
SQL Server에서는 결과를 얻습니다.
다른 약간 다른 방법 :
with t1_count as (select count(*) c1 from t1),
t2_count as (select count(*) c2 from t2)
select c1,
c2
from t1_count,
t2_count
/
select c1,
c2
from (select count(*) c1 from t1) t1_count,
(select count(*) c2 from t2) t2_count
/
다른 답변을 볼 수 없으므로 이것을 제기하십시오.
만약에 당신은 하위 쿼리를 좋아하지 않습니다 그리고 각 테이블에 기본 키가있어서이를 수행 할 수 있습니다.
select count(distinct tab1.id) as count_t1,
count(distinct tab2.id) as count_t2
from tab1, tab2
그러나 성능 현명한 Quassnoi의 솔루션이 더 좋고 내가 사용할 수있는 솔루션이 더 좋다고 생각합니다.
select (select count(*) from tab1) count_1, (select count(*) from tab2) count_2 from dual;
SELECT (SELECT COUNT(*) FROM table1) + (SELECT COUNT(*) FROM table2) FROM dual;
여기 나에게서 공유합니다
옵션 1- 다른 테이블에서 동일한 도메인에서 계산
select distinct(select count(*) from domain1.table1) "count1", (select count(*) from domain1.table2) "count2"
from domain1.table1, domain1.table2;
옵션 2- 같은 테이블의 다른 도메인에서 계산
select distinct(select count(*) from domain1.table1) "count1", (select count(*) from domain2.table1) "count2"
from domain1.table1, domain2.table1;
옵션 3- "Union All"과 동일한 테이블에 대해 다른 도메인에서 계산하여 카운트 행을 가질 수 있습니다.
select 'domain 1'"domain", count(*)
from domain1.table1
union all
select 'domain 2', count(*)
from domain2.table1;
SQL을 즐기십시오. 나는 항상합니다 :)
약간의 완전성을 위해 -이 쿼리는 주어진 소유자에 대한 모든 테이블 수를 제공하는 쿼리를 만듭니다.
select
DECODE(rownum, 1, '', ' UNION ALL ') ||
'SELECT ''' || table_name || ''' AS TABLE_NAME, COUNT(*) ' ||
' FROM ' || table_name as query_string
from all_tables
where owner = :owner;
출력은 같은 것입니다
SELECT 'TAB1' AS TABLE_NAME, COUNT(*) FROM TAB1
UNION ALL SELECT 'TAB2' AS TABLE_NAME, COUNT(*) FROM TAB2
UNION ALL SELECT 'TAB3' AS TABLE_NAME, COUNT(*) FROM TAB3
UNION ALL SELECT 'TAB4' AS TABLE_NAME, COUNT(*) FROM TAB4
그런 다음 횟수를 얻기 위해 달릴 수 있습니다. 때때로 주변에있는 편리한 대본 일뿐입니다.
빠른 찌르기가 시작되었습니다.
Select (select count(*) from Table1) as Count1, (select count(*) from Table2) as Count2
참고 : SQL Server에서 이것을 테스트했습니다. From Dual
필요하지 않습니다 (따라서 불일치).
select
t1.Count_1,t2.Count_2
from
(SELECT count(1) as Count_1 FROM tab1) as t1,
(SELECT count(1) as Count_2 FROM tab2) as t2
테이블 (또는 적어도 키 열)이 동일한 유형 인 경우 먼저 조합을 만들고 계산합니다.
select count(*)
from (select tab1key as key from schema.tab1
union all
select tab2key as key from schema.tab2
)
또는 만족을 가져다가 그 주위에 또 다른 합을 넣으십시오.
select sum(amount) from
(
select count(*) amount from schema.tab1 union all select count(*) amount from schema.tab2
)
Declare @all int
SET @all = (select COUNT(*) from tab1) + (select count(*) from tab2)
Print @all
또는
SELECT (select COUNT(*) from tab1) + (select count(*) from tab2)
--============= FIRST WAY (Shows as Multiple Row) ===============
SELECT 'tblProducts' [TableName], COUNT(P.Id) [RowCount] FROM tblProducts P
UNION ALL
SELECT 'tblProductSales' [TableName], COUNT(S.Id) [RowCount] FROM tblProductSales S
--============== SECOND WAY (Shows in a Single Row) =============
SELECT
(SELECT COUNT(Id) FROM tblProducts) AS ProductCount,
(SELECT COUNT(Id) FROM tblProductSales) AS SalesCount
다른 테이블과 결합하십시오
SELECT COUNT(*) FROM (
SELECT DISTINCT table_a.ID FROM table_a JOIN table_c ON table_a.ID = table_c.ID );
select (select count))에서 tab1에서 field
'value'와 같은) + (count select count ()에서 tab2에서 field
'value')처럼
select @count = sum(data) from
(
select count(*) as data from #tempregion
union
select count(*) as data from #tempmetro
union
select count(*) as data from #tempcity
union
select count(*) as data from #tempzips
) a