sql server - SQL TOP and Join challenge -


i have problem top , join in sql.

i have 2 tables inventtable , ikmtechspecprod.

inventtable contains products have.

ikmtechspecprod contains technical specifications products.

there can many technical specifications single product. want export products , 12 first technical specs each product, , want listed on 1 line per product

example:

itemid, itemname, spec1name, spec1value, spec2name, spec2value, spec3name, spec3 value..... 

i have tried sql query below, gives me:

itemid, itemname, spec1name, spec2value itemid, itemname, spec2name, spec2value  itemid, itemname, spec3name, spec3value 

query:

select      invent.itemid, itemname,      [techspec].name, [techspec].value      [inventtable] invent cross apply      (select top 12           [ikmtechspecprod].name, ikmtechspecprod.value      [ikmtechspecprod]      [ikmtechspecprod].itemid = invent.itemid) techspec 

anyone know how solve this?

you can write query as:

 select distinct     t2.itemid,     stuff     (         (             select top 12 ',' + specifications             @ikmtechspecprod t1             t1.itemid = t2.itemid             order specifications             xml path('')         ), 1, 1, ''     ) specs     @inventtable t2 

demo


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -