简体   繁体   中英

How to get data of a table filled from a text file with csv format in sql server

This is the data I get from the bulk import and I have in a table called Drinks:

| Descrip       |  10/02/20 07   |  17/02/20 08  |  24/02/20 09 |
| SL AGUA       |     NULL       |     NULL      |     2.861    |
| ALHAMBRA IPA  |     350        |     NULL      |     NULL     |
| Carlsberg     |     800        |     2.800     |     2.800    |
| CASIMIRO MH   |     NULL       |      55       |     NULL     |

I need to do a query to obtain the data like this:

| SL AGUA       |      20      |    09    |   2861   |
| ALHAMBRA IPA  |      20      |    07    |    350   |
| Carlsberg     |      20      |    07    |    800   |
| Carlsberg     |      20      |    08    |   2800   |
| Carlsberg     |      20      |    09    |   2800   |
| CASIMIRO MH   |      20      |    08    |     55   |

The first row has data like header (Descrip) but has also data I need like date and number of the week.

Is it possible to do what I need? Thanks.

Load the data into a staging table, with columns from the first row.

Then unpivot, I would recommend using apply :

select s.descrip, v.col1, vol2, v.val
from staging s cross apply
     (values ([10/02/20 07], 20, 7),
             ([10/02/20 08], 20, 8),
             ([10/02/20 09], 20, 9)
     ) v(val, col1, col2)
where v.val is not null;  -- or perhaps v.val <> ''

Follow the below step:

  1. Table creation & insert Data

create table Drinks
(
    Head1 nvarchar(100),
    Head2 nvarchar(100),
    Head3 nvarchar(100),
    Head4 nvarchar(100)
)

insert into Drinks values('Descrip','10/02/20 07','17/02/20 08','24/02/20 09'),
                    ('SL AGUA',NULL,NULL,'2.861'),
                    ('ALHAMBRA IPA','350',NULL, NULL),
                    ('Carlsberg','800','2.800','2.800'),
                    ('CASIMIRO MH',NULL,'55',NULL)
  1. Result Query

;with tempCTE
as (
select head1,head2,head3,head4,
head5=(case when head2 is not null then head5 else '' end) ,
head6=(case when head3 is not null then head6 else '' end) ,
head7=(case when head4 is not null then head7 else '' end) 
from Drinks
cross apply (
    select  
    head5=right(Head2,2) + '|' + substring(Head2,7,2),
    head6=right(Head3,2)+'|'+substring(Head3,7,2),
    head7=right(Head4,2)+'|'+substring(Head4,7,2) 
     from Drinks
    where head1='Descrip'
) as tbl
cross apply (
        select 1 slno union select 2
) as tbl2
where head1<>'Descrip'

), resultCTE
 as
(
    select distinct head1,SplitHead1=left(head5,2),SplitHead2=right(head5,2),head2 from tempCTE where head2 is not null
    union
    select distinct head1,left(head6,2) ,right(head6,2),head3 from tempCTE where head3 is not null
    union
    select distinct head1,left(head7,2),right(head7,2),head4 from tempCTE where head4 is not null
)
select * from resultCTE

输出

Fiddle DB Output

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