1

I'm using SQL Server, and I have this data:

NO_PO_MYSAP ID_PLANNING_DETAIL ITEM BL
5501        13683              6    2019-11-28
5501        13683              7    2019-11-28
1101        13685              6    2019-12-10
1101        13685              6    2019-12-11
1101        13685              7    2019-12-10
1101        13685              7    2019-12-11

You can see that they have different no. of rows under the same NO_PO_MYSAP, because when I. select distinct, BL for 13685 has different values.

What I want is, if BL returns different values under similar NO_PO_MYSAP, I want those values to be sticked together so I still get 2 rows of data as in ID_PLANNING_DETAIL = 13683 (no. of rows depend on no. of ITEM, in this case they are 6 and 7).

So the return data will look like this:

NO_PO_MYSAP ID_PLANNING_DETAIL ITEM BL
5501        13683              6    2019-11-28
5501        13683              7    2019-11-28
1101        13685              6    2019-12-10, 2019-12-11
1101        13685              7    2019-12-10, 2019-12-11
Jov
  • 45
  • 4
  • Does this answer your question? [SQL Server : GROUP BY clause to get comma-separated values](https://stackoverflow.com/questions/12668528/sql-server-group-by-clause-to-get-comma-separated-values) – Ravi Makwana Feb 05 '20 at 05:38
  • 1
    How is this any different from your previous question [SQL: Select row of data based on several conditions from other columns](https://stackoverflow.com/questions/60069553/sql-server-select-row-of-data-based-on-several-conditions-from-other-columns) – Nick Feb 05 '20 at 05:41
  • https://stackoverflow.com/questions/8868604/sql-group-concat-function-in-sql-server – Ravi Makwana Feb 05 '20 at 05:46

1 Answers1

0

here this demo can help SQLFiddle

SELECT distinct NO_PO_MYSAP, 
                ID_PLANNING_DETAIL,
                ITEM,  
                BL = STUFF((SELECT ', ' + bl_date
               FROM Table_bl bl  -- your table name from bl_date --
               WHERE bl.ID = A.ID
              FOR XML PATH('')), 1, 2, '')
FROM   v_temp_iseecargo_planning A 
       LEFT JOIN tbl_iseecargo_trx_planning_detail B 
              ON A.id_planning = B.id_planning  AND B.CHANGEID <> 'D'
       LEFT JOIN tbl_iseecargo_trx_lifting C 
              ON A.id_planning = C.id_planning AND C.CHANGEID <> 'D'
       LEFT JOIN tbl_iseecargo_trx_lifting_detail D 
              ON C.id_lifting = D.id_lifting AND D.CHANGEID <> 'D'
       LEFT JOIN tbl_iseecargo_md_port E 
              ON B.id_port_loading = E.id_port AND E.CHANGEID <> 'D'
       LEFT JOIN tbl_iseecargo_md_port F 
              ON B.id_port_discharge = F.id_port AND F.CHANGEID <> 'D'
       LEFT JOIN tbl_iseecargo_md_item G 
              ON B.id_item = G.id_item AND G.CHANGEID <> 'D'
       LEFT JOIN tbl_iseecargo_trx_vessel_acceptance H 
              ON C.id_lifting = H.id_lifting AND H.CHANGEID <> 'D'
       LEFT JOIN tbl_iseecargo_md_vessel I 
              ON H.id_vessel = I.id_vessel AND I.CHANGEID <> 'D'
       LEFT JOIN tbl_iseecargo_md_uom J 
              ON B.id_uom = J.id_uom AND J.CHANGEID <> 'D'
       LEFT JOIN tbl_iseecargo_md_shipping_type K 
              ON id_shipping_type_temp = K.id_shipping_type AND K.CHANGEID <> 'D'
       LEFT JOIN (SELECT DISTINCT id_planning_detail, 
                                  no_po_mysap 
                  FROM 
       tbl_iseecargo_trx_procurement_detail
       where CHANGEID <> 'D') L 
              ON b.id_planning_detail = L.id_planning_detail 
WHERE  A.ID_PLANNING in (13683, 13685) and B.id_uom = '3'
order by ALD_AWAL desc;
Ravi Makwana
  • 2,782
  • 1
  • 29
  • 41
  • Hi, it is working to return 2 rows now, but, returned value only one value (not concatenated). The origin type is date, i convert to varchar. Still, only one value return, not as expected with comma between – Jov Feb 05 '20 at 07:40
  • can you create a demo in sqlFiddle? – Ravi Makwana Feb 05 '20 at 07:43
  • HI I've made the demo.. you can see its showing strings of concatenated date, but its duplicating. In my real case, its not even showing concatenated string, only one value per row data.. http://sqlfiddle.com/#!18/e8d29/11/0 – Jov Feb 05 '20 at 10:39
  • logically yes.. but still not working in real db :((( – Jov Feb 05 '20 at 10:58
  • sorry without testing data it's hard for anyone to find logical bug – Ravi Makwana Feb 05 '20 at 11:00
  • Hm.. can you help me again? What if the data looks like this http://sqlfiddle.com/#!18/66bc39/2. I want the data to look like as before i changed the data structure (2 rows with concated date) – Jov Feb 05 '20 at 11:59
  • I think output is correct as Data here check with your example data http://sqlfiddle.com/#!18/5b3d7e/1 – Ravi Makwana Feb 05 '20 at 12:09
  • I've changed the data again now.. please check http://sqlfiddle.com/#!18/43dd6/1. Here under PO_NO_MYSAP 1101 i still get 4 rows.. – Jov Feb 05 '20 at 12:30
  • as i said before as per data it's showing correct! do you also want to merge ID_PLAN/ITEM? – Ravi Makwana Feb 05 '20 at 12:36
  • Sorry.. i dont understand. I am hoping that under NO_PO_MYSAP 1101, i would only get 2 rows containing NO_PO_MYSAP 1101;ITEM 6;BL DATE A,B and NO_PO_MYSAP 1101;ITEM 7;BL DATE A,B. I dont know if maybe we can just disregard ID_PLAN, but yes it is different. How? – Jov Feb 06 '20 at 04:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207325/discussion-between-jov-and-ravi-makwana). – Jov Feb 06 '20 at 06:54
  • No need to accept answer if it's not resolve your problem :) – Ravi Makwana Feb 07 '20 at 05:40