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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:49:59+00:00 2026-06-14T13:49:59+00:00

I got a table containing 3 columns: NameColumn CategoryColumn QuantityColumn Name1 Category1 5 Name2

  • 0

I got a table containing 3 columns:

NameColumn  CategoryColumn  QuantityColumn
Name1       Category1   5
Name2       Category1   8
Name3       Category1   10
Name4       Category2   3
Name5       Category2   15
Name6       Category2   7

I need to write a query to convert the above data into the following result set:

NameColumn  CategoryColumn  QuantityColumn
Category1   NULL        NULL
Name1       NULL        5
Name2       NULL        8
Name3       NULL        10
Category2   NULL        NULL
Name4       NULL        3
Name5       NULL        15
Name6       NULL        7

Is there anyway to do this without using cursors?
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-06-14T13:50:00+00:00Added an answer on June 14, 2026 at 1:50 pm
    SELECT NameColumn, CategoryColumn, QuantityColumn
    FROM
    (
        SELECT CategoryColumn AS NameColumn, NULL AS CategoryColumn, NULL AS QuantityColumn,
            CategoryColumn AS _cat, 1 AS _iscat
        FROM myTable
        GROUP BY CategoryColumn
        UNION ALL
        SELECT NameColumn, NULL AS CategoryColumn, QuantityColumn, 
            CategoryColumn AS _cat, 0 AS _iscat
        FROM myTable
    ) x
    ORDER BY _cat, _iscat DESC
    

    SQL Fiddle example

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

Sidebar

Related Questions

I've got a table containing some duplicates (defined as some specific columns contain the
I've got a table containing many rows. The rows are guaranteed to have been
Sorry for vague question title. I've got a table containing huge list of, say,
I've got a SQL query that joins a pricing table to a table containing
I've got a table, each row containing 3 <td> . Now, I assigned a
I've got table with two columns: name and grade. It looks sth like this:
I've got a MySQL table containing 115 fields like these: v1q1 v1q2 v2q1 v2q2
I have got a table containing rows of related data which I need to
I've got a standard table to store form input, with columns firstName,lastName,email. There are
I got one HTML file containing a table: src.Html: <table id=all> <tr> <th>Row 1</th>

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.