0

I've MYSQL Query with SUM & CROSS JOIN, its working fine.

MYSQL QUERY:

SELECT SUM(totexec/(pass_count+fail_count))/d.total  COUNT FROM test_schedule CROSS JOIN ( SELECT COUNT(*) total FROM test_schedule WHERE project='JupiterQA' ) d WHERE project='JupiterQA'

But I'm trying to execute this same query in MSSQL in showing "Column 'd.total' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."

After that I added GROUP BY a.total in end of the query, ERROR IS: Divide by zero error encountered.

I've records in all the table,Is Query wrong?

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
user3114967
  • 639
  • 5
  • 15
  • 38

2 Answers2

0

i'm not askin why you use a cross join :D just add a group by clause in your query

SELECT SUM(totexec(pass_count+fail_count))d.total  COUNT FROM test_schedule CROSS JOIN ( SELECT COUNT(*) total FROM test_schedule WHERE project='JupiterQA' ) d WHERE project='JupiterQA' GROUP BY d.total
Alexis Peters
  • 1,583
  • 1
  • 10
  • 17
0

This should be equivalent to the MYSQL query:

SELECT tSum / dTotal AS COUNT
FROM (
   SELECT SUM(totexec / (pass_count+fail_count)) AS tSum, d.total AS dTotal 
   FROM test_schedule 
   CROSS JOIN ( SELECT COUNT(*) total 
                FROM test_schedule 
                WHERE project='JupiterQA' ) d 
   WHERE project='JupiterQA' ) t

I don't see a need for a CROSS JOIN though. This should also do the job:

SELECT tSum / ( SELECT COUNT(*) 
                FROM test_schedule 
                WHERE project='JupiterQA' ) AS COUNT
FROM (
   SELECT SUM(totexec / (pass_count+fail_count)) AS tSum 
   FROM test_schedule 
   WHERE project='JupiterQA' ) t
Giorgos Betsos
  • 71,379
  • 9
  • 63
  • 98