简体   繁体   中英

How to assign a variable in the WHERE clause of a sub-query in SQL Server

SELECT 
    Total_Males, SETTLEMENT_ID, URI, _PARENT_AURI    
FROM
    (SELECT 
         COUNT(t2.SEX) as Total_Males, t1.SETTLEMENT_ID, t1.URI, 
         t2._PARENT_AURI 
     FROM 
         PScData t1 
     INNER JOIN 
         PscMemberData t2 ON t2._PARENT_AURI  = t1.URI
     WHERE 
         t1.SETTLEMENT_ID = '11102002002001' 
         AND t2.SEX = '1'
     GROUP BY 
         t1.URI, t2._PARENT_AURI, t1.SETTLEMENT_ID) tmp

This is the query that I have written. In the WHERE clause, where I have given the exact value - '11102002002001' - I want to compare with each SETTLEMENT_ID from t1 . How can I do this? Please provide some solution.

Further, the query is returning count in two rows ie: count against two different ids right now. How can I add the result of two different ids to get the sum of these two rows returned by the sub-query?

Do you mean you want to compare against each Settlement_id from table PScData?.

From my understanding of the question, you might do a self join on the table PScData or use the below query.

This query will compare the settlement_id against all settlement_ids from the same table. I dont understand why you want to do this.

Please check the script and let me know if I get you wrong.

SELECT 
        SUM(Total_Males) [Total_males],  SETTLEMENT_ID,URI, _PARENT_AURI    
    FROM
        (SELECT 
             COUNT(t2.SEX) as Total_Males, t1.SETTLEMENT_ID, t1.URI, 
             t2._PARENT_AURI 
         FROM 
             PScData t1 
         INNER JOIN 
             PscMemberData t2 ON t2._PARENT_AURI  = t1.URI
         WHERE 
             t1.SETTLEMENT_ID in (SELECT DISTINCT SETTLEMENT_ID FROM PScData)
             AND t2.SEX = '1'
         GROUP BY 
             t1.URI, t2._PARENT_AURI, t1.SETTLEMENT_ID) tmp
group by
     SETTLEMENT_ID,URI, _PARENT_AURI

A sub-query is void here. Just perform the join and filter out the settlements if you don't need all of them. But it is not clear from your question if you want to filter them out or just match them. For the purpose of matching use joins.

SELECT COUNT(t2.SEX) as Total_Males, t1.SETTLEMENT_ID, t1.URI, t2._PARENT_AURI 
FROM PScData t1 
    INNER JOIN PscMemberData t2 ON t2._PARENT_AURI  = t1.URI
WHERE t2.SEX = '1'
GROUP BY t1.URI, t2._PARENT_AURI, t1.SETTLEMENT_ID

If you need to filter them out, you need to prepare the list of wanted settlements and use IN in the WHERE clause. You can use a Common Table Expression to spread out the list of wanted settlements and pass it in the WHERE clause:

WITH cteSettlements AS (
    SELECT SETLLEMENT_ID
    FROM PscData
    WHERE ? -- apply some criteria
)
SELECT COUNT(t2.SEX) as Total_Males, t1.SETTLEMENT_ID, t1.URI, t2._PARENT_AURI 
FROM PScData t1 
    INNER JOIN PscMemberData t2 ON t2._PARENT_AURI  = t1.URI
WHERE t1.SETTLEMENT_ID IN cteSettlements AND t2.SEX = '1'
GROUP BY t1.URI, t2._PARENT_AURI, t1.SETTLEMENT_ID

As for the second question - How can I add the result of two different ids to get the sum of these two rows ? - you need to play with the GROUP BY clause. As far as I understand, you actually don't need to group be SETTLEMENT_ID so just remove it from the GROUP BY . Just remember that you need to remove it from the SELECT list too.

I found the solution to the query the result can be retrieved and summed up like this:

SELECT Total_Males, SETTLEMENT_ID
FROM (
    SELECT COUNT(t2.SEX) as Total_Males, t1.SETTLEMENT_ID 
    FROM PScData t1  
        INNER JOIN PscMemberData t2 ON t2._PARENT_AURI  = t1.URI
    WHERE t1.SETTLEMENT_ID in (SELECT DISTINCT SETTLEMENT_ID FROM PScData)
      AND t2.SEX = '1'
    GROUP BY t1.SETTLEMENT_ID
) tmp

Try With ,

Select 
Total_Males, SETTLEMENT_ID , URI, _PARENT_AURI    
from
(Select 
COUNT(t2.SEX) as Total_Males ,t1.SETTLEMENT_ID  , t1.URI, t2._PARENT_AURI    
from 
PScData t1 
INNER JOIN 
PscMemberData t2 ON t2._PARENT_AURI  = t1.URI
 where
  t1.SETTLEMENT_ID in (SELECT DISTINCT SETTLEMENT_ID FROM PScData)
  AND t2.SEX = '1'
 Group By 
t1.SETTLEMENT_ID ) tmp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM