What would be the best way of writing out Header Line and Sub line to a csv file

Christopher Jack 1,616 Reputation points
2021-02-01T14:40:30.02+00:00

Hi,

I want to be able to write out a header line then a sub line then a header line sub etc

To a CSV file for example

CREATE TABLE Sales_Header(
    sales_order_num int PRIMARY KEY,
    sales_date datetime ,
    customer_name nvchar(50),
);

CREATE TABLE Sales_item(
    sales_order_num int PRIMARY KEY,
    product_code nvchar(20),
    product_name nvchar(20),
    product_price int,
);

insert into Sales_Header
values (2039489, 25-01-2021, Mrs Smith),
            (2039460, 25-01-2021, Mr Smith);

insert into Sales_item
values (2039489, SKU987, Kitchen Utensils, £3.00),
           (2039489, SKU934, Knife, £5.00),
           (2039460, SKU47, Calendar, £6.00),
           (2039460, SKU87, Stationary, £2.00);

Desire output is

2039489, 25-01-2021, Mrs Smith
2039489, SKU987, Kitchen Utensils, £3.00
2039489, SKU934, Knife, £5.00
2039460, 25-01-2021, Mr Smith
2039460, SKU47, Calendar, £6.00
2039460, SKU87, Stationary, £2.00

What would be the best way of going about this?

Thanks

SQL server version 2016

SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,066 questions
Windows for business | Windows Server | User experience | PowerShell
Sysinternals
Sysinternals
Advanced system utilities to manage, troubleshoot, and diagnose Windows and Linux systems and applications.
1,258 questions
SQL Server | Other
{count} votes

Accepted answer
  1. Rich Matheisen 47,906 Reputation points
    2021-02-01T22:47:23.2+00:00

    If we can agree that the data source contains one row for each item sold(?), and each row contains all the data, then something like this will produce the desired result:

    $dataSource = Import-CSV C:\Junk\DPD.csv
    
    $OutFile = 'C:\Junk\DPD.txt'
    $key = ""
    $OneShot = $true
    For ($i=0; $i -lt $dataSource.Length; $i++){
        If($OneShot){
            $key = $dataSource[$i].Order
            $OneShot = $false
            $dataSource[$i].Order,$dataSource[$i].Date,$dataSource[$i].Name -join "," | Out-File $OutFile
        }
        if ($key -ne $dataSource[$i].Order){
            $key = $dataSource[$i].Order
            $dataSource[$i].Order,$dataSource[$i].Date,$dataSource[$i].Name -join "," | Add-Content $OutFile
        }
        $dataSource[$i].Order,$dataSource[$i].SKU,$dataSource[$i].Desc,$dataSource[$i].Price -join "," | Add-Content $OutFile
    }
    

    The input to that code was this file:

    Order,Date,Name,SKU,Desc,Price
    2039489,25-01-2021,Mrs Smith,SKU987,Kitchen Utensils,£3.00
    2039489,25-01-2021,Mrs Smith,SKU934,Knife,£5.00
    2039460,25-01-2021,Mr Smith,SKU47,Calendar,£6.00
    2039460,25-01-2021,Mr Smith,SKU87,Stationary,£2.00
    

    And the output was:

    2039489,25-01-2021,Mrs Smith
    2039489,SKU987,Kitchen Utensils,£3.00
    2039489,SKU934,Knife,£5.00
    2039460,25-01-2021,Mr Smith
    2039460,SKU47,Calendar,£6.00
    2039460,SKU87,Stationary,£2.00
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 122.3K Reputation points MVP Volunteer Moderator
    2021-02-01T22:41:27.38+00:00

    The file would have to be created by a client program which runs two queries, one for the headers and one for the details. You loop over the headers and print them, and then you lookup the details on the order id or whatever that number is.

    As for what to write the program, just pick the language you are most comfortable with.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.