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 6798569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:44:46+00:00 2026-05-26T18:44:46+00:00

I have this Type of Query, select StateName ,countryName from country_master left join state_master

  • 0

I have this Type of Query,

select StateName ,countryName
from country_master left join state_master on country_master.countryID = state_master.countryID

which produce following result,

Gujarat  India
Delhi  India
HR    India
MP    India
NY    USA
LA    USA
Chicago  USA
WDC  USA
London  Uk

I want this result in single row with its relation like,

India –   Gujarat  Delhi   HR   MP
USA
–   NY  LA  Chicago Uk –    London

Have Any Idea For Building this Query
Suggest Me,
Thanks,

  • 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-26T18:44:46+00:00Added an answer on May 26, 2026 at 6:44 pm

    This question is essentially a duplicate of Concatenate many rows into a single text string?.

    Here is one method that works in Sql Server 2008:

    WITH Ranked ( countryID, rnk, stateName )  
             AS ( SELECT countryID,
                         ROW_NUMBER() OVER( PARTITION BY countryID ORDER BY countryID ),
                         CAST( stateName AS VARCHAR(8000) ) 
                    FROM state_master),
    AnchorRanked ( countryID, rnk, stateName )  
             AS ( SELECT countryID, rnk, stateName 
                    FROM Ranked
                   WHERE rnk = 1 ),
    RecurRanked ( countryID, rnk, stateName ) 
             AS ( SELECT countryID, rnk, stateName 
                    FROM AnchorRanked
                   UNION ALL 
                  SELECT Ranked.countryID, Ranked.rnk,
                         RecurRanked.stateName + ', ' + Ranked.stateName
                    FROM Ranked
                   INNER JOIN RecurRanked 
                      ON Ranked.countryID = RecurRanked.countryID 
                     AND Ranked.rnk = RecurRanked.rnk + 1 )
    SELECT countryName, MAX( stateName ) 
      FROM RecurRanked
      INNER JOIN country_master on RecurRanked.countryID = country_master.countryID
     GROUP BY countryName;
    

    You can see several methods for pulling off this kind of technique here: https://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/

    You’ll need to be careful if your concatenated list of cities is going to be longer than 8000 characters, though you might be able to get away with doing something like varchar(max) if that’s a concern (aside from the fact that it probably wouldn’t be a very good way to look at the data).

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

Sidebar

Related Questions

I have this query select count(*) from sl_list where type = 'pantry' which returns
I have this query: SELECT a.id, b.discount FROM (a LEFT JOIN b ON b.id_a
I have this query SELECT products_list.id_cat_unique, products_categories_list.*, COUNT(products_list.id_cat_unique) as counter FROM products_categories_ids LEFT JOIN
I have used this query before: SELECT * FROM stone_list WHERE type IN ('ALEXANDRITE','AMETHYST')
I have this straight forward query: SELECT f.id, f.format_type, h.newspaper_id FROM newspapers_formats f LEFT
I have this query: SELECT COUNT(*) AS `numrows` FROM (`tbl_A`) JOIN `tbl_B` ON `tbl_A`.`B_id`
I have this query: SELECT *, default_insurance.company AS company FROM (default_insurance) JOIN default_gruppe_rel ON
So I have this query: select ens_use_new_models_bit from cfo_transaction inner join dbo.cfo_trans_entity_rel on te_tr_transaction_id=tr_transaction_id
So, I have this query: SELECT count(*) AS count FROM table1 INNER JOIN some
I have this query: SELECT foo.*, COUNT(archive.pkey) as archivedCount FROM ( SELECT type, count(active.pkey)

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.