简体   繁体   中英

How can I remove Null value from first column but keep the value of the 2nd and thirds columns

I am Omar, a new learner of SQL.

I have a large excel sheet that I want to analyze by SQL. It has the following columns (Manufacturers, Products, sales) the problem is, in the first column 'Manufacturers,' the manufacturer name has only been entered once per one manufacturer. while for the rest of the below rows, the cells are empty until the next manufacturer. Please refer to the attached image for more understanding.

How can I remove these null values in my query results while keeping the values of the product column value?

thank you

在此处输入图像描述

The main problem you have is that SQL tables represent unordered sets. So, if you have only your specified columns, you cannot reconstruct the Excel format.

To solve this, you want to load the data into a table that has an identity or auto-incremented column, in order to preserve the insertion order. The exact details depend on the database. Let me call this column id .

Then you can "spread" the value where it is missing. One method is:

select t.*,
       max(manufacturer) over (partition by manufacturer_grp) as imputed_manufacturer
from (select t.*,
             count(manufacturer) over (order by id) as manufacturer_grp
      from t
     ) t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM