简体   繁体   中英

SQL select first value in a row then insert that value into another column

I'm not very good with sql so I'm not sure if this is possible. Or maybe even in excel?? I'm trying to select the very first value and ignore duplicates from Product_ID and then add that first value for that row to the Title column.

Also note that my product list is over 25,000+ items.

So take this:

+---------------+------------+-------+-------------+-------+
| Product_Count | Product_ID | Title | _Color_Name | _Size |
+---------------+------------+-------+-------------+-------+
| 2             | 14589      |       | Black       | 00    |
| 3             | 14589      |       | Black       | 0     |
| 4             | 14589      |       | Black       | 2     |
| 5             | 14589      |       | Black       | 4     |
| 6             | 14589      |       | Black       | 6     |
| 11            | 14589      |       | Dark Coral  | 00    |
| 12            | 14589      |       | Dark Coral  | 0     |
| 13            | 14589      |       | Dark Coral  | 2     |
| 14            | 14589      |       | Dark Coral  | 4     |
| 15            | 14589      |       | Dark Coral  | 6     |
| 129           | 15027      |       | Aqua        | 00    |
| 130           | 15027      |       | Aqua        | 0     |
| 131           | 15027      |       | Aqua        | 2     |
| 132           | 15027      |       | Aqua        | 4     |
| 133           | 15027      |       | Aqua        | 6     |
| 138           | 15027      |       | Black       | 00    |
| 139           | 15027      |       | Black       | 0     |
| 140           | 15027      |       | Black       | 2     |
| 141           | 15027      |       | Black       | 4     |
| 142           | 15027      |       | Black       | 6     |
+---------------+------------+-------+-------------+-------+

And turn it into this:

+---------------+------------+-------+-------------+-------+
| Product_Count | Product_ID | Title | _Color_Name | _Size |
+---------------+------------+-------+-------------+-------+
| 2             | 14589      | 14589 | Black       | 00    |
| 3             | 14589      |       | Black       | 0     |
| 4             | 14589      |       | Black       | 2     |
| 5             | 14589      |       | Black       | 4     |
| 6             | 14589      |       | Black       | 6     |
| 11            | 14589      |       | Dark Coral  | 00    |
| 12            | 14589      |       | Dark Coral  | 0     |
| 13            | 14589      |       | Dark Coral  | 2     |
| 14            | 14589      |       | Dark Coral  | 4     |
| 15            | 14589      |       | Dark Coral  | 6     |
| 129           | 15027      | 15027 | Aqua        | 00    |
| 130           | 15027      |       | Aqua        | 0     |
| 131           | 15027      |       | Aqua        | 2     |
| 132           | 15027      |       | Aqua        | 4     |
| 133           | 15027      |       | Aqua        | 6     |
| 138           | 15027      |       | Black       | 00    |
| 139           | 15027      |       | Black       | 0     |
| 140           | 15027      |       | Black       | 2     |
| 141           | 15027      |       | Black       | 4     |
| 142           | 15027      |       | Black       | 6     |
+---------------+------------+-------+-------------+-------+

You can use PARTITION to window the ProductIds and then identify the first row in each partition with ROW_NUMBER() :

SELECT 
  ProductID, 
  Product_Count, 
  CASE WHEN rn = 1 THEN ProductID else null END AS Title, 
  Color_Name, 
  Size
FROM
(
    SELECT ProductID, Product_Count, Color_Name, Size,
    ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY Product_Count) AS rn
    FROM product_stock
) AS X;

SqlFiddle here

try this,

Declare @t table (col1 int,col2 int,col3 int)
insert into @t values(2,3,null),(2,4,null),(5,3,null),(5,4,null)

;with CTE as
(select *,ROW_NUMBER()over(partition by col1 order by col1)rn from @t

)

select a.col1,a.col2,case when a.rn=1 then b.col1 end col3 from CTE a
left join (select col1 from cte where rn>1) b on a.col1=b.col1

Create the table with UNIQUE.

CREATE TABLE recentDetails"
                    + "(_id integer primary key autoincrement,"
                    + "fileName TEXT, filePath TEXT UNIQUE ,fileSize TEXT,fileDate TEXT);

SELECT * from TABLE_NAME WHERE Product_count IN (SELECT MIN(Product_COUNT) FROM TABLE_NAME where Product_ID IN (SELECT DISTINCT Product_ID FROM TABLE_NAME)) AND Product_ID IN (SELECT DISTINCT Product_ID FROM TABLE_NAME)

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