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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:15:12+00:00 2026-05-12T01:15:12+00:00

With an Orders table: (OrderID, date, customerID, status, etc) and an OrderDetails table: (ParentID,

  • 0

With an Orders table:
(OrderID, date, customerID, status, etc)

and an OrderDetails table:
(ParentID, itemID, quantity, price, etc)

I would like to create a SQL Query that will export a CSV flat file with Order and OrderDetail rows interspersed. For instance, output might look like this (H and D indicate “Header” and “Detail” respectively.):

"H",2345,"6/1/09",856,"Shipped"
"D",2345,52,1,1.50
"D",2345,92,2,3.25
"D",2345,74,1,9.99
"H",2346,"6/1/09",474,"Shipped"
"D",2346,74,1,9.99
"D",2346,52,1,1.50

Not sure where to even start with this. Any ideas? TIA.

  • 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-12T01:15:12+00:00Added an answer on May 12, 2026 at 1:15 am

    You’ll want to take advantage of the fact that union all will honor the order by clause at the end on the entire result set. Therefore, if you order by the second column (2!) ascending, and the first column (1!) descending, you’ll get the header row, then the detail rows underneath that.

    Also, make sure that you have the same number of columns in the two queries. They don’t have to be of the same data type, since you’re exporting to CSV, but they do have to be the same number. Otherwise, the union all won’t be able to pile them onto each other. Sometimes, you’ll just have to pad columns with null if you need extra ones, or '' if you don’t want the word null in your CSV.

    select
        'H',
        OrderID,
        Date,
        CustomerID,
        Status
    from
        Headers
    union all
    select
        'D',
        ParentID,
        ItemID,
        Quantity,
        Price
    from
        Details
    order by
        2 asc, 1 desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a table Orders with fields CustomerID , OrderID and OrderDate ,
In MS Transact SQL, let's say I have a table (Orders) like this: Order
I have one table orders with a foreing key ProductID. I want to show
I am using Spring and JDBCTemplate. The scenario is a CUSTOMER table and ORDERS
I have two Tables. Order - With Columns OrderID, OrderStatusID OrderStatus - With Columns
I wish to order a table: Firstly by Field1=3 Then by Field2 DESC I
In order to perform a case-sensitive search/replace on a table in a SQL Server
Say I have 2 tables: Customers and Orders. A Customer can have many Orders.
I have a table of records in mySql. I need to maintain an order
For example, in my WCF, if I have a Customer table and an Order

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.