-2

Please look at my current summary query result :

id      name mch
127664  ML  2
127666  ML  2
127667  ML  2
127670  ML  2
127671  ML  2
127672  ML  2
127674  ML  2
127675  ML  2
127678  ML  1
127680  ML  1
127665  ML  2

I want to merge row which has same value.. Just merge the name column. And then here is my expected query :

id      name    mch
127664  ML      2
127666          2
127667          2
127670          2
127671          2
127672          2
127674          2
127675          2
127678          1
127680          1
127665          2

I already look for some problem but still not found. I hope you want to guide me to handle this..

Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
RANDY
  • 31
  • 8

2 Answers2

4

Well you could try a ROW_NUMBER trick here:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY name ORDER BY id) rn
    FROM yourTable
)

SELECT
    id,
    CASE WHEN rn = 1 THEN name ELSE '' END AS name,
    mch
FROM cte
ORDER BY
    name,
    id;

But typically this type of requirement would be better handled in your presentation layer (e.g. PHP or Java).

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

Your query should be like this :

WITH t AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY name ORDER BY id) counter
    FROM table //nameofyourtable
)

SELECT
    id,CASE WHEN counter = 1 THEN name ELSE '' END AS name, mch
FROM t ORDER BY
    name,
    id;
Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60