[quote author=maximasj link=1150872965/0#14 date=1221218942] Помогите, плиз. не знаю как его правильно написать чтоб работал [/quote]
примерно, так:
[code]SELECT Код, Товар, Штрихкод, ПолнНаименование, Disc, LimDisc, РодКод, РодКод2, РодКод3, ОгрСкид, Колич, вид, isNULL((Select ( select top 1 cast(c3775_vv.value as numeric(14, 2)) from _1sconst as c3775_vv (nolock) where c3775_vv.id = 3775 and c3775_vv.objid = СпрЦ.ID and (c3775_vv.date <= '20080821') order by c3775_vv.date desc, c3775_vv.time desc, c3775_vv.docid desc, c3775_vv.row_id desc ) Цена FROM sc3772 СпрЦ (NOLOCK) WHERE СпрЦ.ParentExt = Товар AND СпрЦ.sp3787 = ' PCEE'),0) AS Цена FROM
(SELECT Tov.code as Код, РегП.sp101 AS Товар, Tov.sp3559 as ШтрихКод, Tov.sp3024 as ПолнНаименование, Tov.sp6622 as Disc, Tov.sp6574 as LimDisc, isNULL(R1.code, '') as РодКод, isNULL(R2.code, '') as РодКод2, isNULL(R3.code, '') as РодКод3, isNULL(UR.ogrSk, 0) as ОгрСкид, РегП.sp102 AS Колич, 0 as Вид FROM rg99 as РегП (NOLOCK) INNER JOIN sc33 Tov (NOLOCK) ON Tov.ID = РегП.sp101 LEFT JOIN sc33 R1 (NOLOCK) ON Tov.ParentID = R1.ID LEFT JOIN sc33 R2 (NOLOCK) ON R1.ParentID = R2.ID LEFT JOIN sc33 R3 (NOLOCK) ON R2.ParentID = R3.ID LEFT JOIN (select u3.id, u3.sp6685 ogrSk from sc33 u3 (NOLOCK) where u3.parentid IN (select u2.id from sc33 u2 (NOLOCK) where u2.parentid IN (Select u1.id from sc33 u1 (NOLOCK) where parentid = ' 0 ')) ) ur ON ur.id = R3.parentid OR ur.id = R2.parentid OR ur.id = R1.parentid WHERE Period = '20080801' AND РегП.sp3603 = ' 0 ' AND РегП.sp100 = ' 14CEE' AND РегП.sp102 >0
UNION
SELECT Tov1.code, Tov1.ID, Tov1.sp3559, Tov1.sp3024, Tov1.sp6622, Tov1.sp6574, '', '', '', 0, 0, 1 FROM sc33 Tov1 (NOLOCK) WHERE ltrim(Tov1.sp689) = 'J4' ) ttt
ORDER BY Вид, Товар[/code] т.е. делаешь обертку Униону и выносишь наружу получение последнего значения.
|