Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two queries and i am listing them below make a single query from them if any one can do

First Query:
SQL
SELECT sum(cte.TARGET) "target" 
     , sum(cte.ACHIEVED) "achieved" 
FROM (
    SELECT sum(DISTINCT( QT.QuestionTarget+QT.QT_FormMen )) AS TARGET ,
           sum(S.QTotal) AS ACHIEVED 
    FROM QuestionTarget QT
    LEFT OUTER JOIN AnswersNew AS AN ON AN.Question_ID=QT.Question_ID
    GROUP BY QT.Question_ID
)cte



Second Query:
SQL
SELECT 
    QT.QuestionTarget As Target,
    QT.Question_ID,
    Q.QuestionText,AN.QTotal As Achieved,
    QT.QT_ForWomen AS TW,
    AN.id,
    QT.QT_FormMen AS TM,
    AN.QT_ForMen AS AM,
    AN.QT_ForWomen AS AW,
    QT.QT_ForGirl AS TG,
    AN.QT_ForGirl AS AG,
    QT.QT_ForBoy AS TB,
    AN.QT_ForBoy AS AB,
    V.VillageName,
    AN.CdfPErsonName,
    QT.District,AN.Year,
    AN.Quarter,D.DistrictName,
    QU.Quarter_Name,
    CC.CommunityCodeName,
    G.Group_Name,
    Y.Year_Name,
    AN.Date_Created,
    AN.UserTypes,
    AN.AnswerID,
    U.IsActive,
    AN.Status 
from AnswersNew AN
INNER JOIN QuestionTarget QT 
           ON QT.Question_ID= AN.Question_ID 
          AND QT.Year=AN.Year 
          AND QT.Quarter=AN.Quarter 
          AND AN.DistrictId=QT.District
INNER JOIN Villages V ON V.VlgID=AN.VillagesId
INNER JOIN Quarter Qu ON Qu.Quarter_ID=AN.Quarter
INNER JOIN Users U ON U.UserId=AN.CDF_ID
INNER JOIN Questions Q ON Q.QuestionIds=QT.Question_ID
INNER JOIN CommunityCodes CC ON CC.CommunityCodeID =AN.CommunityCodes
INNER JOIN Groups G ON G.Group_ID=AN.UserTypes
INNER JOIN Year Y ON Y.YEAR_ID=AN.Year
INNER JOIN Districts D on D.DistrictId=U.DistrictId
WHERE U.IsVisible = 1
Posted
Updated 25-Aug-15 6:12am
v4
Comments
Maciej Los 25-Aug-15 9:11am    
Sorry, but we can't see your screen and we can't read in your mind. Please, be more specific and provide more details about your issue? What have you tried? Where are you stuck?
Malikdanish 25-Aug-15 10:46am    
check the query both first and second i want to make a single one query i have nested select statements , I want to make them a one query with results
Advay Pandya 25-Aug-15 9:14am    
Your question is not clear. Which kind of result you want ? Do you want to join rows/columns ? or you want the result in 2 different tables ?
Malikdanish 25-Aug-15 12:14pm    
The question is super easy . as above I have two queries both are using same tables I want to make a single query returning the result (combination of one and two) means return all result set of select command
Andy Lanng 25-Aug-15 9:31am    
Sometime I want to put in the "What did you change" box: "Ah! My eyes are bleeding."

1 solution

In the first query, cte is not a good name because it is not a Common Table Expression. I would use a real CTE to match your queries:

SQL
with cte as (
    SELECT sum(DISTINCT( F.TargetValue+F.Target_ForMen)) AS TARGET ,
           sum(S.Achieved) AS ACHIEVED ,
           F.Question_ID,
           F.Year, 
          F.Quarter, 
          F.DistrictId, 
          F.QT_ForWomen ,
          F.QT_FormMen,
          F.QT_ForGirl ,
          F.QT_ForBoy 
    FROM QuestionTarget F
    LEFT OUTER JOIN AnswersNew AS S ON S.Question_ID=F.Question_ID
    GROUP BY 
          F.Question_ID,
          F.Year, 
          F.Quarter, 
          F.DistrictId, 
          F.QT_ForWomen ,
          F.QT_FormMen,
          F.QT_ForGirl ,
          F.QT_ForBoy  
)
SELECT 
    QT.Target As Target,
    QT.Question_ID,
    Q.QuestionText,AN.QTotal As Achieved,
    QT.QT_ForWomen AS TW,
    AN.id,
    QT.QT_FormMen AS TM,
    AN.QT_ForMen AS AM,
    AN.QT_ForWomen AS AW,
    QT.QT_ForGirl AS TG,
    AN.QT_ForGirl AS AG,
    QT.QT_ForBoy AS TB,
    AN.QT_ForBoy AS AB,
    V.VillageName,
    AN.CdfPErsonName,
    QT.District,AN.Year,
    AN.Quarter,D.DistrictName,
    QU.Quarter_Name,
    CC.CommunityCodeName,
    G.Group_Name,
    Y.Year_Name,
    AN.Date_Created,
    AN.UserTypes,
    AN.AnswerID,
    U.IsActive,
    AN.Status 
from AnswersNew AN
INNER JOIN CTE QT 
           ON QT.Question_ID= AN.Question_ID 
          AND QT.Year=AN.Year 
          AND QT.Quarter=AN.Quarter 
          AND AN.DistrictId=QT.District
INNER JOIN Villages V ON V.VlgID=AN.VillagesId
INNER JOIN Quarter Qu ON Qu.Quarter_ID=AN.Quarter
INNER JOIN Users U ON U.UserId=AN.CDF_ID
INNER JOIN Questions Q ON Q.QuestionIds=QT.Question_ID
INNER JOIN CommunityCodes CC ON CC.CommunityCodeID =AN.CommunityCodes
INNER JOIN Groups G ON G.Group_ID=AN.UserTypes
INNER JOIN Year Y ON Y.YEAR_ID=AN.Year
INNER JOIN Districts D on D.DistrictId=U.DistrictId
WHERE U.IsVisible = 1


UPDATE: from comments:

This is how you write a cte:
SQL
with cte1 as (
	SELECT 
		Question_ID , 
		Sum(QuestionTarget) AS "Target" 
	FROM QuestionTarget 
	GROUP BY Question_ID
	),
cte2 as (
    SELECT 
		Sum(QTotal) AS Achieved , 
		SUM(DISTINCT(Target)) AS Target 
	FROM AnswersNew AS S
	RIGHT OUTER Join cte1 ON cte1.Question_ID=S.Question_ID
    GROUP BY CTE.Question_ID),
cte3 as(
	SELECT 
		SUM(Target) Target , 
		Sum(Achieved) Achieved 
	FROM cte2)
--I don't know how you want this joined to your main query, but this is how you use it:

SELECT 
	QT.Question_ID,
	Q.QuestionText,AN.QTotal As Achieved,
	QT.QT_ForWomen AS TW,
	AN.id,
	QT.QT_FormMen AS TM,
	AN.QT_ForMen AS AM,
	AN.QT_ForWomen AS AW,
	QT.QT_ForGirl AS TG,
	AN.QT_ForGirl AS AG,
	QT.QT_ForBoy AS TB,
	AN.QT_ForBoy AS AB,
	V.VillageName,
	AN.CdfPErsonName,
	QT.District,AN.Year,
	AN.Quarter,D.DistrictName,
	QU.Quarter_Name,
	CC.CommunityCodeName,
	G.Group_Name,
	Y.Year_Name,
	AN.Date_Created,
	AN.UserTypes,
	AN.AnswerID,
	U.IsActive,
	AN.Status 
from AnswersNew AN
INNER JOIN QuestionTarget QT ON QT.Question_ID= AN.Question_ID 
                             AND QT.Year=AN.Year 
                             AND QT.Quarter=AN.Quarter 
                             AND AN.DistrictId=QT.District
INNER JOIN Villages V ON V.VlgID=AN.VillagesId
INNER JOIN Quarter Qu ON Qu.Quarter_ID=AN.Quarter
INNER JOIN Users U ON U.UserId=AN.CDF_ID
INNER JOIN Questions Q ON Q.QuestionIds=QT.Question_ID
INNER JOIN CommunityCodes CC ON CC.CommunityCodeID =AN.CommunityCodes
INNER JOIN Groups G ON G.Group_ID=AN.UserTypes
INNER JOIN Year Y ON Y.YEAR_ID=AN.Year
INNER JOIN Districts D on D.DistrictId=U.DistrictId
--HERE - just like a table
INNER JOIN cre3 ON --something
WHERE U.IsVisible = 1


That's all I can tell you. There is no more info you have given me

UPDATE2: from comments:

Like this?
SQL
with cte1 as (
	SELECT 
		Question_ID , 
		Sum(QuestionTarget) AS "Target" 
	FROM QuestionTarget 
	GROUP BY Question_ID
	),
cte2 as (
    SELECT 
		Sum(QTotal) AS Achieved , 
		SUM(DISTINCT(Target)) AS Target ,
                S.VillagesId
	FROM AnswersNew AS S
	RIGHT OUTER Join cte1 ON cte1.Question_ID=S.Question_ID
    GROUP BY CTE.Question_ID,
                S.VillagesId),
cte3 as(
	SELECT 
		SUM(Target) Target , 
		Sum(Achieved) Achieved ,
                VillagesId
	FROM cte2
        Group By VillagesId)
select * from cte3
INNER JOIN Villages V ON V.VlgID=cte3.VillagesId
 
Share this answer
 
v3
Comments
Maciej Los 25-Aug-15 10:50am    
Andy, let me know if OP accept your answer. I promise to upvote it.
Andy Lanng 25-Aug-15 10:58am    
Thanks ^_^, but I don't think we'll hear from him again >_<
Maciej Los 25-Aug-15 11:04am    
We'll see ;)
Andy Lanng 26-Aug-15 5:52am    
well, he did come back >_<
Malikdanish 25-Aug-15 12:10pm    
Your modification Returns wrong result..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900