Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 导致出现次数最少的多个查询_Sql_Sql Server - Fatal编程技术网

Sql 导致出现次数最少的多个查询

Sql 导致出现次数最少的多个查询,sql,sql-server,Sql,Sql Server,我需要根据exp_条件找到任何主题检索到的最小数量的文档。subjects表中的Exp_条件包含“1”和“2”列 以下是表格: 主题表: +-----------------+--------------+ | Field | Type | +-----------------+--------------+ | username | varchar(255) | | user_type | varchar(10) | | year

我需要根据exp_条件找到任何主题检索到的最小数量的文档。subjects表中的Exp_条件包含“1”和“2”列

以下是表格: 主题表:

+-----------------+--------------+
| Field           | Type         |
+-----------------+--------------+
| username        | varchar(255) |
| user_type       | varchar(10)  |
| years           | int          |
| low_grade       | int          |
| high_grade      | int          |
| on_line         | varchar(10)  |
| on_line_sources | varchar(255) |
| location        | varchar(5)   |
| exp_condition   | int          |
+-----------------+--------------+
tasks table:

+------------+--------------+
| Field      | Type         |
+------------+--------------+
| username   | varchar(255) |
| task       | varchar(5)   |
| confidence | int          |
| sim_helpd  | int          |
+------------+--------------+
文档表:

+--------------+--------------+
| Field        | Type         |
+--------------+--------------+
| username     | varchar(255) |
| task         | varchar(5)   |
| doc_type     | varchar(10)  |
| used_tool    | int          |
| relevant     | int          |
| motivational | int          |
| concepts     | int          |
| background   | int          |
| grade_level  | int          |
| hands_on     | int          |
| attachments  | int          |
+--------------+--------------+
我能够为两个exp_条件值生成主题数和文档数。我可以使用多个查询,但我不确定如何使用

为exp_条件1和2生成主题数的代码:

select count(distinct(t2.username)) 
from tasks as t1 
inner join subjects as t2 
on t1.username = t2.username group by exp_condition;
select count(*), exp_condition 
from docs as t1 
left join subjects as t2 
on t1.username = t2.username 
group by exp_condition;
为exp_条件1和2生成文档数的代码:

select count(distinct(t2.username)) 
from tasks as t1 
inner join subjects as t2 
on t1.username = t2.username group by exp_condition;
select count(*), exp_condition 
from docs as t1 
left join subjects as t2 
on t1.username = t2.username 
group by exp_condition;
预期输出:两个独立的数字表示任何主题根据exp_条件检索到的最小文档数


提前感谢。

您可以使用子查询或CTE

子查询

SELECT exp_condition, MIN(A) as Tasks, MIN(B) as Docs FROM (
    SELECT exp_condition, COUNT(DISTINCT t2.username) A, COUNT(DISTINCT (t3.username) B
    FROM subjects s
    LEFT JOIN tasks T2 ON s.username = t2.username
    LEFT JOIN docs T3 ON s.username = t3.username
    GROUP BY exp_condition
) A
GROUP BY ex_condition 
CTE

不相关,但是:distinct不是一个函数。不同的a与不同的a完全相同。