您可以檢查這個方法只是相同的數據。
declare @mytable table (compid int,compname varchar(20),itemid int, itemdesc varchar(20))
insert into @mytable
values
(1,'Company A',100,'Nestle'),
(1,'Company A',200,'UniLever'),
(2,'Company B',300,'Citrix'),
(2,'Company B',400,'SQL'),
(2,'Company B',500,'Oracle'),
(1,'Company B',600,'Microsoft')
select
iif(left(m1.ord_id,1)>1,NULL,m.compid) [CompID],
iif(left(m1.ord_id,1)>1,NULL,m.compname) [CompName],
m.itemid,
m.itemdesc
from @mytable m
inner join (
select distinct compid,row_number() over (partition by compid order by itemid) [ord_id], itemid
from @mytable) m1
on m.compid = m1.compid and m.itemid = m1.itemid
或CTE
;with cte as
(
select distinct compid,row_number() over (partition by compid order by itemid) [ord_id], itemid
from @mytable
)
select
iif(left(m1.ord_id,1)>1,NULL,m.compid) [CompID],
iif(left(m1.ord_id,1)>1,NULL,m.compname) [CompName],
m.itemid,
m.itemdesc
from @mytable m
inner join cte m1
on m.compid = m1.compid and m.itemid = m1.itemid
,如果你不快樂與空值替換
iif(left(m1.ord_id,1)>1,'',cast(m.compid as varchar)) [CompID],
iif(left(m1.ord_id,1)>1,'',m.compname) [CompName],
結果
CompID CompName itemid itemdesc
1 Company A 100 Nestle
200 UniLever
600 Microsoft
2 Company B 300 Citrix
400 SQL
500 Oracle
或此等值的EntityFramework –
你混淆的領域數據無線顯示器呈現。您在SSMS的結果選項卡中看到的呈現不是最終用戶應該看到的內容(除非您計劃對應用程序進行建模以使其看起來像SSMS)。強制數據庫執行通常爲應用程序保留的作業,一旦你接觸到應用程序只會導致悲傷。 –