Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7077013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:22:13+00:00 2026-05-28T06:22:13+00:00

I have a SQL query Question, I am trying to nest two separate queries

  • 0

I have a SQL query Question, I am trying to nest two separate queries which run perfectly fine individually.


The first query is

Select DISTINCT Leads.EmailAddress,ClientCustomFields.CommonName , LeadSources.Name, CONVERT(VARCHAR(10), leads.LeadStatusDate,101) AS [MM/DD/YYYY],Leads.Age,Leads.State, Leads.ProgramOfInterest, Leads.HighestEducationLevel, Leads.LeadNumber from LeadSources inner join Leads on Leads.LeadSourceNumber=LeadSources.LeadSourceNumber join ClientCustomFieldPrograms on Leads.ClientCustomFieldProgramsNumber=ClientCustomFieldPrograms.ClientCustomFieldProgramsNumber join  ClientCustomFields on ClientCustomFieldPrograms.ClientCustomFieldsNumber=ClientCustomFields.ClientCustomFieldsNumber where Leads.EmailAddress in ('beloved1965@charter.net') AND Year (Leads.LeadDate)=2011 and ClientCustomFields.CommonName LIKE '%Ashford%' and Leads.LeadStatus='sent' order by  Leads.EmailAddress

The second query

 /****** Script for SelectTopNRows command from SSMS  ******/ 
SELECT 
CASE
   WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Mr.' THEN   'Male'
WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Mrs.' THEN 'Female'
WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Ms.' THEN 'Female'
ELSE 'Unknown' END as Gender, dbo.ClientLeadDistribution.LeadNumber, dbo.ClientLeadDistribution.Postdate, dbo.ClientLeadDistribution.ClientCustomFieldsNumber, dbo.ClientCustomFields.CommonName, dbo.Leads.LeadSourceNumber, dbo.LeadSources.Name, dbo.Leads.ProgramOfInterest, dbo.Leads.EmailAddress from dbo.ClientLeadDistribution join dbo.Leads on dbo.ClientLeadDistribution.LeadNumber = dbo.Leads.LeadNumber join dbo.LeadSources on dbo.Leads.LeadSourceNumber = dbo.LeadSources.LeadSourceNumber join dbo.ClientCustomFields on dbo.ClientLeadDistribution.ClientCustomFieldsNumber = dbo.ClientCustomFields.ClientCustomFieldsNumber where CustomFieldsXML.exist('/Salutation') = 1 and YEAR (postdate) = 2012 and MONTH (postdate)=1 --and dbo.ClientLeadDistribution.ClientCustomFieldsNumber in (1810,1820,1830,2750,2760,2770) order by PostDate, ClientCustomFieldsNumber

The combination is

    Select DISTINCT ClientLeadDistribution.ClientCustomFieldsNumber,   Leads.EmailAddress,ClientCustomFields.CommonName , LeadSources.Name, CONVERT(VARCHAR(10), leads.LeadStatusDate,101) AS [MM/DD/YYYY],Leads.Age,Leads.State, Leads.ProgramOfInterest, Leads.HighestEducationLevel, Leads.LeadNumber from LeadSources inner join Leads  on Leads.LeadSourceNumber=LeadSources.LeadSourceNumber join ClientCustomFieldPrograms on Leads.ClientCustomFieldProgramsNumber=ClientCustomFieldPrograms.ClientCustomFieldProgramsNumber join  ClientCustomFields on ClientCustomFieldPrograms.ClientCustomFieldsNumber=ClientCustomFields.ClientCustomFieldsNumber join ClientLeadDistribution on ClientCustomFields.ClientCustomFieldsNumber=ClientLeadDistribution.ClientCustomFieldsNumber where Leads.EmailAddress in ('beloved1965@charter.net') AND Year (Leads.LeadDate)=2011  and ClientCustomFields.CommonName LIKE '%Fortis%' and Leads.LeadStatus='sent' and ClientLeadDistribution.ClientCustomFieldsNumber =(SELECT ClientLeadDistribution.CustomFieldsXML,CASE
WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Mr.' THEN 'Male'
WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Mrs.' THEN 'Female'
WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Ms.' THEN 'Female'
ELSE 'Unknown' END as Gender from dbo.ClientLeadDistribution join dbo.Leads on dbo.ClientLeadDistribution.LeadNumber = dbo.Leads.LeadNumber join dbo.LeadSources on dbo.Leads.LeadSourceNumber = dbo.LeadSources.LeadSourceNumber join dbo.ClientCustomFields on dbo.ClientLeadDistribution.ClientCustomFieldsNumber = dbo.ClientCustomFields.ClientCustomFieldsNumber where CustomFieldsXML.exist('/Salutation') = 1 AND YEAR (postdate) = 2012 and MONTH (postdate)=1) --and dbo.ClientLeadDistribution.ClientCustomFieldsNumber in (1810,1820,1830,2750,2760,2770)--order by ClientCustomFields.CommonName--order by  Leads.EmailAddress

And Finally I get this error I tried stuff but nothing seems to work. Any help or suggestions appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:22:13+00:00Added an answer on May 28, 2026 at 6:22 am

    Your subquery is returning 2 fields: “CustomFieldsXML” and “gender”.

    The error is telling you that you cannot have it return 2 fields. I think you are only after the “gender” so remove the first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL query that I'm currently solving by doing two queries. I
i have a general question about how sql server evaluates the joins.The query is
I have SQL query, which is working nice on Oracle and MSSQL. Now I'm
As I had written in title, I have SQL query, run on Oracle DB,
I have a SQL query (MS Access) and I need to add two columns,
I have a SQL query that takes a very long time to run on
I have a question on a SQL query and im wondering where to start.
I have a query in SQL that I'm trying to translate into Pig Latin
I have the following Linq-to-SQL query. On line #5 I'm trying to get the
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.