再談 Sql 兩行變一行 例項

2021-05-21 19:40:01 字數 1566 閱讀 6150

要求: 按 lct1, lct2  排序後的前兩條紀錄,顯示為下列結果:

item_cd1   item_cd2            lct

01             a                 a01-3  b01-1

建表準備:

if exists (select table_name from information_schema.tables where table_name = 'm_lct_service')

drop table m_lct_service

gocreate table m_lct_service (

item_cd1 nvarchar(5)  not null 

,item_cd2 nvarchar(10)  not null 

,seq_no  int   not null 

,lct1  nvarchar(3)  not null 

,lct2  nvarchar(3)  not null 

,stk_qty numeric(15,4) 

,ins_emp_cd nvarchar(4)  not null 

,ins_date datetime  not null 

,upd_emp_cd nvarchar(4)  not null 

,upd_date datetime  not null 

,constraint pk_m_lct_service primary key (

item_cd1

,item_cd2

,seq_no

,lct1

,lct2))

go資料準備:

item_cd1 item_cd2 seq_no lct1 lct2 stk_qty ins_emp_cd ins_date upd_emp_cd upd_date

01 a 1 a01 3 20.0000 fan 2010-01-02 00:00:00.000 fan 2010-01-02 00:00:00.000

01 a 2 b01 1 20.0000 fan 2010-01-02 00:00:00.000 fan 2010-01-02 00:00:00.000

01 a 3 b01 4 20.0000 fan 2010-01-02 00:00:00.000 fan 2010-01-02 00:00:00.000

實際sql:

select item_cd1,item_cd2,

case  when (aa <> bb)

then aa + ' ' + bb

else aa

end as lct

from(

select item_cd1,item_cd2,min(lct) as aa,max(lct) as bb

from (select top 2 item_cd1, item_cd2,(lct1 + '-' + lct2) as lct from m_lct_service order by lct1 asc, lct2 asc) as t

group by item_cd1,item_cd2

) as t1

用sql實現兩行記錄合併為一行記錄

偶然遇到同學問了個sql的問題,閒著沒事搗鼓了一下。他的問題是 如何把下面這樣的資料,每兩行資料按照第乙個字段合併為一行。create table tp user user no varchar2 2char user name varchar2 10 char user no user name ...

關於左邊兩行右邊一行的布局

給出3個div,要做乙個側品字形布局,如下圖 這裡利用的是負邊距的方法,即div3的margin top 200px 來實現的 還有一種方法是在div2的css裡加上clear left來實現,可以這樣理解,在沒加clear屬性前,雖然div1和div2也是分行顯示的,但那只是因為一行裡不能同時容下...

一行省略,兩行省略

一行省略 1 得是以配置為inline block的元素,或缺省以block呈現的div p元素 2 強制不換行 3 固定寬度 4 超出部分隱藏 5 超出部分以 結尾 width 140px display inline block white space nowrap overflow hidde...