sql server - SQL Select inside Select using an existing column as reference, -


i have table looks this.

| itemcode | avgprice | pricelist | complimentaryitemcode | | al01     | 22       | 1         | al02                  | | al02     | 19       | 1         | al03                  |  | al03     |  7       | 1         | al01                  | | ba01     | 50       | 1         | null                  | | ba01     | 60       | 1         | ba01                  | 

i want create column in query show me avgprice of complimentaryitemcode, this;

| itemcode | avgprice | pricelist | complimentaryitemcode | avgpricecomplimentary | al01     | 22       | 1         | al02                  |   19  | al02     | 19       | 1         | al03                  |    7 | al03     |  7       | 1         | al01                  |   22 | ba01     | 50       | 1         | null                  | null | ba01     | 60       | 1         | ba01                  |   50 

so far tried this, no luck;

select     a.itemcode, a.avgprice,  t.pricelist,  a.complimentaryitemcode,                           (select     avgprice                                      materials                                  (itemcode = complimentaryitemcode)) avgpricecomplimentary         materials left outer join                       prices t on t.itemcode = a.itemcode , t.pricelist = 1     (t.pricelist <> 107) , (t.pricelist <> 108) 

any wonderful!

you should use window functions this:

select m.itemcode, m.avgprice, p.pricelist, m.complimentaryitemcode,        avg(avgprice) on (partition complimentaryitemcode)  avgpricecomplimentary materials m left outer join      prices p on t.itemcode = m.itemcode , t.pricelist = 1 p.pricelist not in (107, 108) ; 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -