English 中文(简体)
Consolidating a COUNT query
原标题:

I have a page where I am running an initial SQL query to get a list of subjects, then I loop over this query and run two additional queries for each record returned from the original subjects query (I happen to be doing this in ColdFusion, but not sure that really matters). These two additional queries do COUNTs for that specific subject and then output the results (print the Subject name, then the two counts for that subject). I was trying to improve performance for this page, and wanted to know if I could somehow combine this into a single query.

Initial Query:

   SELECT subject_ID, subject_name
   FROM Subjects
   ORDER BY subject_name

Queries inside the loop of the initial query:

   SELECT COUNT(test_ID) as priority_count
   FROM Test_Queue
   WHERE priority_flag = 1 AND subject_ID = #SubjectQuery.subject_ID#

   SELECT COUNT(test_ID) as locked_count
   FROM Test_Queue
   WHERE locked = 1 AND subject_ID = #SubjectQuery.subject_ID#

Suggestions on how these might be optimized? DB is MS SQL 2008. Thanks.

最佳回答
SELECT 
    subject_ID, 
    subject_name,
    priority_count = (select count(test_id) from test_queue where priority_flag = 1),
    locked_count = (select count(test_id) from test_queue where locked = 1)
FROM Subjects
ORDER BY subject_name

or, if the counts are supposed to incorporate subject_id (just a guess), then

SELECT 
    s.subject_ID, 
    s.subject_name,
    priority_count = (select count(test_id) from test_queue t where priority_flag = 1 and t.subject_id = s.subject_id),
    locked_count = (select count(test_id) from test_queue t where locked = 1 and t.subject_id = s.subject_id)
FROM Subjects s
ORDER BY subject_name
问题回答

This should do it, presuming that the join column from subjects to test_queue is correct, you haven t specified which column in test_queue references subjects

select
    subjects.subject_id
   ,subjects.subject_name
   ,sum(case when test_queue.priority_flag=1 THEN 1 ELSE 0 END) as priority_count
   ,sum(case when test_queue.locked=1 THEN 1 ELSE 0 END) as locked_count
  from
    subjects
    left join test_queue
       on subjects.subject_id=test_queue.subject_id
  group by subjects.subject_id, subjects.subject_name
  order by subjects.subject_name

something like this? (you may need to cast to INT if priority_flag or locked is of a type that SUM doesn t like)

SELECT
  subject_ID
, subject_name
, SUM(priority_flag) AS priority_count
, SUM(locked) AS locked_count
FROM Subjects s, Test_Queue tq
GROUP BY subject_ID, subject_name
ORDER BY subject_name
;

I ll take a punt that Test_Queue.Subject_ID = Subjects.Subject_ID

SELECT s.subject_ID, s.subject_name, COUNT(t1.*) as priority_count, COUNT(t2.*) as locked_count
  FROM Subjects s
  LEFT OUTER JOIN Test_Queue t1
    ON s.Subject_ID = t1.Subject_ID
   AND t1.priority_flag = 1
  LEFT OUTER JOIN Test_Queue t2
    ON s.Subject_ID = t2.Subject_ID
   AND t2.locked = 1
 GROUP by s.subject_ID, s.subject_name
 ORDER BY s.subject_name




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签