(ÇØ°á¿Ï·á) Äõ¸® Áú¹®µå¸³´Ï´Ù ¤Ð¤Ð

   Á¶È¸ 1244   Ãßõ 0    

 좌측테이블 데이터에서 우측테이블처럼 출력하고자합니다(2번째 행만 없어졌다 보시면됩니다)

Select MAX(Coldate) as ColDate, ColName From Table Group by ColName으로하면




여기까지는 출력해냈습니다.

이러저러해도 뒤엣 3, 4번째 컬럼을 출력하기 어려워 조언을 구합니다 ㅠㅠ



ªÀº±Û Àϼö·Ï ½ÅÁßÇÏ°Ô.
Àϱ¹ 2022-11
¿øÇÏ½Ã´Â°Ô ¸Â´Â°¡ ¸ð¸£°Ú½À´Ï´Ù.

WITH TEMP_TABLE AS
(SELECT '2022-11-29' COLDATE, '»óÇ°A' COLNAME, 14 COLWIDTH,  9 COLHEIGHT FROM DUAL UNION ALL
 SELECT '2022-11-28' COLDATE, '»óÇ°A' COLNAME, 15 COLWIDTH,  9 COLHEIGHT FROM DUAL UNION ALL
 SELECT '2022-11-28' COLDATE, '»óÇ°B' COLNAME, 52 COLWIDTH, 52 COLHEIGHT FROM DUAL UNION ALL
 SELECT '2022-11-28' COLDATE, '»óÇ°C' COLNAME, 27 COLWIDTH, 27 COLHEIGHT FROM DUAL UNION ALL
 SELECT '2022-11-28' COLDATE, '»óÇ°D' COLNAME, 15 COLWIDTH,  9 COLHEIGHT FROM DUAL UNION ALL
 SELECT '2022-11-2E' COLDATE, '»óÇ°E' COLNAME, 14 COLWIDTH,  9 COLHEIGHT FROM DUAL)
SELECT A.COLDATE, A.COLNAME, A.COLWIDTH, A.COLHEIGHT
  FROM TEMP_TABLE A
 INNER JOIN (
      SELECT COLNAME, MAX(COLDATE) COLDATE
        FROM TEMP_TABLE
        GROUP BY COLNAME
            ) B ON A.COLDATE = B.COLDATE AND A.COLNAME = B.COLNAME;
     
MSSQL 2000¿¡¼­´Â Áߺ¹Ç׸ñÀÌ ³ª¿Í¼­ distinct½á¼­ Á¦°ÅÇÏ°í È®ÀÎÁßÀÔ´Ï´Ù!
´ëÃæ ÀÌ·±½ÄÀ̸é ÇØ°á °¡´ÉÇÕ´Ï´Ù.
DB Á¾·ù, ¹öÀü¿¡ µû¶ó ´Ù¸¦ ¼ö ÀÖÀ¸´Ï
Äõ¸® Áú¹®Àº »ç¿ëÇÏ´Â DB Á¾·ù ¹öÀüÀ» ±âº»À¸·Î Ç¥±âÇØ¾ß Á¤È®ÇÑ ´äº¯À» ¹ÞÀ» ¼ö ÀÖ½À´Ï´Ù.

SELECT ColDate, ColName, ColWidth, ColHeight
FROM (
SELECT rank() over (partition by ColName order by ColDate DESC) rk
      ,*
FROM Å×À̺í¸í
) lst
WHERE rk=1

µ¥ÀÌÅÍ°¡ ¸¹°í ColDate, ColName ÀÌ PK(¶Ç´Â À妽º°¡ ¼³Á¤µÈ)ÀÎ Å×À̺íÀÇ °æ¿ì À§¿¡ Àϱ¹´ÔÀÌ ¼³¸íÇϽŠ¹æ¹ýÀ¸·Î Çؾß
¼Óµµ°¡ ºü¸§´Ï´Ù.
     
±âº»ÀûÀ롃 Á¦°¡ ±ôºýÇß±º¿ä ²ûÂïÇÏ°Ôµµ MSSQL 2000À» ¾²°íÀ־ ¿©·¯°¡Áö ³»ÀåÇÔ¼ö°¡ ¾ø½À´Ï´Ù ¤Ð¤Ð
MSSQL 2000 ÀÌ½Ã¸é ¾Æ·¡ Äõ¸®·Î ÇØ°á µÉ µí ÇÕ´Ï´Ù.

WITH TEMP_TABLE AS (
 SELECT '2022-11-29' COLDATE, '»óÇ°A' COLNAME, 14 COLWIDTH,  9 COLHEIGHT  UNION ALL
 SELECT '2022-11-28' COLDATE, '»óÇ°A' COLNAME, 15 COLWIDTH,  9 COLHEIGHT  UNION ALL
 SELECT '2022-11-28' COLDATE, '»óÇ°B' COLNAME, 52 COLWIDTH, 52 COLHEIGHT  UNION ALL
 SELECT '2022-11-28' COLDATE, '»óÇ°C' COLNAME, 27 COLWIDTH, 27 COLHEIGHT  UNION ALL
 SELECT '2022-11-28' COLDATE, '»óÇ°D' COLNAME, 15 COLWIDTH,  9 COLHEIGHT  UNION ALL
 SELECT '2022-11-27' COLDATE, '»óÇ°E' COLNAME, 14 COLWIDTH,  9 COLHEIGHT
)

SELECT ColDate, ColName, ColWidth, ColHeight
FROM (
SELECT ( select count(*)
from TEMP_TABLE counter
where counter.ColName = TEMP_TABLE.ColName -- Partition
and counter.ColDate >= TEMP_TABLE.ColDate  -- Order
) as rk
  ,*
FROM TEMP_TABLE
) lst
WHERE rk=1
     
³î¶øµµ·Ï Á¤»óÀûÀ¸·Î ÀÛµ¿ÇÕ´Ï´Ù ´ë´ÜÇÑ ³»°øÀ̽ʴϴÙ...!


QnA
Á¦¸ñPage 364/5679
2015-12   1478969   ¹é¸Þ°¡
2014-05   4942160   Á¤ÀºÁØ1
2016-07   5134   ÇÞ»ìÇѽºÇ¬
06-29   1238   Sakura24
2022-11   1434   ¸¶À½ÀÌ°¥´ë
2014-02   4043   °¡ºü·Î±¸³ª
2016-07   4073   ÁØÈ£
2019-12   2287   ¶¥ºÎÀÚ
2016-07   7701   żº±â±è
2022-11   1467   giddus
2018-11   2964   ¼öÇÊó·³
2015-07   4988   ³ª¸ô¶ó1
2022-11   1245   ·¹¹ÝÅͽº
2014-03   5776   ¾Æ¸§´Ù¿î³ëÀ»
2014-03   3398   ¹æoÈ¿o¹®
2016-08   3796   ½Ì±¹³¯°­µµ
2022-12   1567   ±è¹ÎöGC
2021-04   1608   ±èµ¿Çõ1
2016-08   4221   myang512
2018-11   4523   ha2mil
2016-08   4535   ¼­¹Ì
2014-03   3537   metaljw