0

I'm used to mysql when you can do that with no problems. I would like to run the following statement in SQL Server however it doesn't see the column C_COUNT.

SELECT 
   A.customers AS CUSTOMERS, 
  (SELECT COUNT(ID) FROM Partners_customers B WHERE A.ID = B.PIID) AS C_COUNT 
FROM Partners A
WHERE CUSTOMERS <> [C_COUNT]

Is it possible to utilize any mathematical operations in the SELECT area like

SELECT (CUSTOMERS - C_COUNT) AS DIFFERENCE 
4

3 回答 3

5

SQL Server does not allow you to use aliases in the WHERE clause. You'll have to have something like this:

SELECT *, Customers - C_COUNT "Difference"
FROM  (
    SELECT 
    A.customers AS CUSTOMERS, 
    (SELECT COUNT(ID) 
    FROM Partners_customers B WHERE A.ID = B.PIID) 
    AS C_COUNT FROM Partners A
) t
WHERE CUSTOMERS <> [C_COUNT]

Or, better yet, eliminating an inline count:

select A.customers, count(b.id)
FROM Partners A
LEFT JOIN Partners_customers B ON A.ID = B.PIID
Group By A.ID
having a.customers <> count(b.id)
于 2013-07-01T16:04:24.160 回答
0
WITH A AS
(
    SELECT 
       A.customers AS CUSTOMERS, 
      (SELECT COUNT(ID) FROM Partners_customers B WHERE A.ID = B.PIID) AS C_COUNT 
    FROM Partners A
    WHERE CUSTOMERS <> [C_COUNT]
)
SELECT 
    *, 
    (CUSTOMERS - C_COUNT) AS DIFFERENCE 
FROM A 
于 2013-07-01T16:07:13.300 回答
0

Completely untested....

(select * from TabA 
 minus
 select * from TabB) -- Rows in TabA not in TabB
union all
( 
 select * from TabB 
 minus
 select * from TabA
) -- rows in TabB not in TabA
于 2013-07-01T16:07:35.050 回答