혹시 SQL 쿼리 잘하시는 분 하나만 만들어 주세요

moonrepeat의 이미지

일단 테이블은 아래와 같이 있는데.....

select * from member

id  date       com_no
01  2005.7.1  01
02  2005.7.1  02
01  2005.7.2  03
03  2005.7.2  04
04  2005.7.2  05
02  2005.7.3  06
05  2005.7.4  07
06  2005.7.5  08
07  2005.8.9  09

음 그러니깐 실질적인 주키는 com_no 입니다.
원하는 쿼리문 결과는

id  date       com_no
01  2005.7.2  03
03  2005.7.2  04
04  2005.7.2  05
02  2005.7.3  06
05  2005.7.4  07
06  2005.7.5  08

이런식으로 가장 최근의 날짜를(단 오늘 시점을 넘깆 않았다는 조건하에)
가지는 중복되지 않는 id를 출력하는 겁니다.(주키만 받으면 안되고 전부 그대로 출력)

group by 와 in 을 써서 해결할려고 하는데 group by 를 id로 하게 되면 com_no를
반환할 수가 없어서 미치겠습니다.

제발 도와 주세요.[/code]

ssehoony의 이미지

group by id 를 했을 때 com_no 를 반환 할 수 없다는게 무슨 의미인가요?
com_no 값을 알 수 없다는건가요?
무슨 DB 사용하시는데요? MySql 에서는 잘 될텐데.
근데 group by 보다는 distinct 를 이용하시는게 더 좋을 듯 하네요.
(distinct 가 표준인지 MySql 에만 있는지는 모르겠네요. MySql 만 사용해봐서)

moonrepeat의 이미지

흑... DB는 저의 의사와는 관계없이 저주 받은 MSSQL 2000을 사용하고 있습니다.

참고로 MySQL에서는 잘되지만 MSSQL에서는 만약 아래와 같이 쿼리를 날리면

SELECT com_no,id,MAX(date) FROM member GROUP BY id

대략 결과가

서버: 메시지 8120, 수준 16, 상태 1, 줄 1
'member.com_no' 열이 집계 함수나 GROUP BY 절에 없으므로 SELECT 목록에서 사용할 수 없습니다.

라는 메세지가 뜹니다.

즉 오직
select id,max(date) from member group by id
라고 해야지만 먹히지만 com_no 값을 영원히 가지고 올 수 없습니다.

삽질은 계속되어야 한다....... 쭉.........

ssehoony의 이미지

select distinct(id) as id, date, com_no from member order by date desc
이런거 어떤가요? 혹시 될지도...

smoka의 이미지

SELECT Distinct [id], [com_no] FROM [some_table] WHERE DateDiff(d, date, GetDate()) < 1
GROUP BY [id]

moonrepeat의 이미지

음 쉽게 되지가 않는군요.
일단 여기저기에 질문을 올려 놓았고 msdn도 뒤져보고 있으니 언젠가는
성공하겠죠.

삽질은 계속되어야 한다....... 쭉.........

seoleda의 이미지

select distinct id, date, com_no
from member
where exists	(select distinct id 
		from member
		where DateDiff(d, date, GetDate()) < 1 
		)

한번 해보세요.. ^^
익명 사용자의 이미지

select id, substr(data, 1, 8), substr(data, 9)
from (
select id max(date || com_no) data from member
where date <= to_char(sysdate, 'yyyymmdd')
group by id
)

lck7141의 이미지

moonrepeat wrote:

음 그러니깐 실질적인 주키는 com_no 입니다.
원하는 쿼리문 결과는

id  date       com_no
01  2005.7.2  03
03  2005.7.2  04
04  2005.7.2  05
02  2005.7.3  06
05  2005.7.4  07
06  2005.7.5  08

라면.. 다음과같은 쿼리를 날리면 되지 않을까 하는데요..
제가 지금 테스트 할 만한 mssql 을 가지고 있지 않아 잘 모르겠지만.......

SQL :

select id, max(date), max(com_no)
from member
group by id

면 되지 않을까 합니다.

단 조건은 date 가 증가할 수록 com_no 도 증가 한다는 가정 하에서 가능 하겠죠???

안된다면.. 죄송...

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.