Hi SET, You have done it this time too.... Your SQL worked with a small amount of change in below statement DECLARE VARIABLE FK_BOM2 VARCHAR(2000);
Because FK_BOM2 is of GUID type. You are awesome as usual. I would like to say Thank You from bottom of HEART.... Thanks Once Again SET... You Are my ROCKSTAR..... Thanking You And With Best Regards. Vishal On Friday, 3 July 2015 11:33 PM, "Svein Erling Tysvær svein.erling.tysv...@kreftregisteret.no [firebird-support]" <firebird-support@yahoogroups.com> wrote: >Hi All, > >I have below SQL, which is concatenating the ShortCode column data, but >without ordering as per the ORDER_NUMBER column in ABC table in "WITH" clause. > >Please help. > >With TBL_SHORT_CODE (SHORT_CODE, FK_KEY) As >( >SELECT Distinct(XYZ.SHORT_CODE) As SHORT_CODE, ABC.FK_KEY >From ABC Join XYZ On ABC.PK_KEY = XYZ.FK_KEY >where XYZ.FK_KEY = '{009DA0F8-51EE-4207-86A6-7E18F96B983A}' And >ABC.STATUS_CODE = 1 >Order By ABC.ORDER_NUMBER >) > >SELECT LIST(Distinct(TBL_SHORT_CODE.SHORT_CODE), '' ), ABC.FK_BOM >From ABC >Join XYZ ON ABC.FK_KEY = XYZ.PK_KEY >Join TBL_SHORT_CODE On TBL_SHORT_CODE.FK_KEY = ABC.FK_KEY >where ABC.FK_BOM = '{009DA0F 8-51EE-4207-86A6-7E18F96B983A}' And >ABC.STATUS_CODE = 1 >Group By ABC.FK_BOM I've had a similar problem once, Vishal. Then I think I solved it by using EXECUTE BLOCK. You could try something similar: EXECUTE BLOCK RETURNS (SHORT_CODES VARCHAR(2000), FK_BOM INTEGER AS DECLARE VARIABLE SHORT_CODE VARCHAR(20); DECLARE VARIABLE FK_BOM2 INTEGER; DECLARE VARIABLE DUMMY INTEGER; BEGIN FK_BOM = NULL; FOR With TBL_SHORT_CODE (SHORT_CODE, FK_KEY, ORDER_NUMBER) As (SELECT XYZ.SHORT_CODE, ABC.FK_KEY, min(ABC.ORDER_NUMBER) From ABC Join XYZ On ABC.PK_KEY = XYZ.FK_KEY where XYZ.FK_KEY = '{009DA0F8-51EE-4207-86A6-7E18F96B983A}' And ABC.STATUS_CODE = 1 group by 1, 2) SELECT ABC.FK_BOM, tsc.SHORT_CODE, min(tsc.ORDER_NUMBER) From ABC Join XYZ ON ABC.FK_KEY = XYZ.PK_KEY Join TBL_SHORT_CODE tsc On tsc.FK_KEY = ABC.FK_KEY where ABC.FK_BOM = '{009DA0F 8-51EE-4207-86A6-7E18F96B983A}' And ABC.STATUS_CODE = 1 Group By 1, 2 ORDER BY 1, 3 into :FK_BOM2, :SHORT_CODE, :DUMMY do begin if (FK_BOM2 > FK_BOM) then suspend; if (FK_BOM2 is distinct from FK_BOM) then begin FK_BOM = FK_BOM2; SHORT_CODES = ''; end SHORT_CODES = SHORT_CODES ||', '||SHORT_CODE; end suspend; end This should order the SHORT_CODES by the lowest ORDER_NUMBER within each FK_BOM. To also order the FK_BOMs by the lowest ORDER_NUMBER amongst themselves would add more complexity, hopefully you don't need that. HTH, Set #yiv3332182013 #yiv3332182013 -- #yiv3332182013ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv3332182013 #yiv3332182013ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv3332182013 #yiv3332182013ygrp-mkp #yiv3332182013hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv3332182013 #yiv3332182013ygrp-mkp #yiv3332182013ads {margin-bottom:10px;}#yiv3332182013 #yiv3332182013ygrp-mkp .yiv3332182013ad {padding:0 0;}#yiv3332182013 #yiv3332182013ygrp-mkp .yiv3332182013ad p {margin:0;}#yiv3332182013 #yiv3332182013ygrp-mkp .yiv3332182013ad a {color:#0000ff;text-decoration:none;}#yiv3332182013 #yiv3332182013ygrp-sponsor #yiv3332182013ygrp-lc {font-family:Arial;}#yiv3332182013 #yiv3332182013ygrp-sponsor #yiv3332182013ygrp-lc #yiv3332182013hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv3332182013 #yiv3332182013ygrp-sponsor #yiv3332182013ygrp-lc .yiv3332182013ad {margin-bottom:10px;padding:0 0;}#yiv3332182013 #yiv3332182013actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv3332182013 #yiv3332182013activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv3332182013 #yiv3332182013activity span {font-weight:700;}#yiv3332182013 #yiv3332182013activity span:first-child {text-transform:uppercase;}#yiv3332182013 #yiv3332182013activity span a {color:#5085b6;text-decoration:none;}#yiv3332182013 #yiv3332182013activity span span {color:#ff7900;}#yiv3332182013 #yiv3332182013activity span .yiv3332182013underline {text-decoration:underline;}#yiv3332182013 .yiv3332182013attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv3332182013 .yiv3332182013attach div a {text-decoration:none;}#yiv3332182013 .yiv3332182013attach img {border:none;padding-right:5px;}#yiv3332182013 .yiv3332182013attach label {display:block;margin-bottom:5px;}#yiv3332182013 .yiv3332182013attach label a {text-decoration:none;}#yiv3332182013 blockquote {margin:0 0 0 4px;}#yiv3332182013 .yiv3332182013bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv3332182013 .yiv3332182013bold a {text-decoration:none;}#yiv3332182013 dd.yiv3332182013last p a {font-family:Verdana;font-weight:700;}#yiv3332182013 dd.yiv3332182013last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv3332182013 dd.yiv3332182013last p span.yiv3332182013yshortcuts {margin-right:0;}#yiv3332182013 div.yiv3332182013attach-table div div a {text-decoration:none;}#yiv3332182013 div.yiv3332182013attach-table {width:400px;}#yiv3332182013 div.yiv3332182013file-title a, #yiv3332182013 div.yiv3332182013file-title a:active, #yiv3332182013 div.yiv3332182013file-title a:hover, #yiv3332182013 div.yiv3332182013file-title a:visited {text-decoration:none;}#yiv3332182013 div.yiv3332182013photo-title a, #yiv3332182013 div.yiv3332182013photo-title a:active, #yiv3332182013 div.yiv3332182013photo-title a:hover, #yiv3332182013 div.yiv3332182013photo-title a:visited {text-decoration:none;}#yiv3332182013 div#yiv3332182013ygrp-mlmsg #yiv3332182013ygrp-msg p a span.yiv3332182013yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv3332182013 .yiv3332182013green {color:#628c2a;}#yiv3332182013 .yiv3332182013MsoNormal {margin:0 0 0 0;}#yiv3332182013 o {font-size:0;}#yiv3332182013 #yiv3332182013photos div {float:left;width:72px;}#yiv3332182013 #yiv3332182013photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv3332182013 #yiv3332182013photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv3332182013 #yiv3332182013reco-category {font-size:77%;}#yiv3332182013 #yiv3332182013reco-desc {font-size:77%;}#yiv3332182013 .yiv3332182013replbq {margin:4px;}#yiv3332182013 #yiv3332182013ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv3332182013 #yiv3332182013ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv3332182013 #yiv3332182013ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv3332182013 #yiv3332182013ygrp-mlmsg select, #yiv3332182013 input, #yiv3332182013 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv3332182013 #yiv3332182013ygrp-mlmsg pre, #yiv3332182013 code {font:115% monospace;}#yiv3332182013 #yiv3332182013ygrp-mlmsg * {line-height:1.22em;}#yiv3332182013 #yiv3332182013ygrp-mlmsg #yiv3332182013logo {padding-bottom:10px;}#yiv3332182013 #yiv3332182013ygrp-msg p a {font-family:Verdana;}#yiv3332182013 #yiv3332182013ygrp-msg p#yiv3332182013attach-count span {color:#1E66AE;font-weight:700;}#yiv3332182013 #yiv3332182013ygrp-reco #yiv3332182013reco-head {color:#ff7900;font-weight:700;}#yiv3332182013 #yiv3332182013ygrp-reco {margin-bottom:20px;padding:0px;}#yiv3332182013 #yiv3332182013ygrp-sponsor #yiv3332182013ov li a {font-size:130%;text-decoration:none;}#yiv3332182013 #yiv3332182013ygrp-sponsor #yiv3332182013ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv3332182013 #yiv3332182013ygrp-sponsor #yiv3332182013ov ul {margin:0;padding:0 0 0 8px;}#yiv3332182013 #yiv3332182013ygrp-text {font-family:Georgia;}#yiv3332182013 #yiv3332182013ygrp-text p {margin:0 0 1em 0;}#yiv3332182013 #yiv3332182013ygrp-text tt {font-size:120%;}#yiv3332182013 #yiv3332182013ygrp-vital ul li:last-child {border-right:none !important;}#yiv3332182013