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

Christopher Jack 1,611 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
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,645 questions
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.
2,791 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
Sysinternals
Sysinternals
Advanced system utilities to manage, troubleshoot, and diagnose Windows and Linux systems and applications.
1,082 questions
{count} votes

Accepted answer
  1. Rich Matheisen 44,621 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 100.8K Reputation points MVP
    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