help with select

  • Hi,

    i've a table with 2 columns one log_event_num (int) and one log_event_time (datetime).

    how i can write a select that will show me every day only one log_event_num per event?

    THX

  • Mad-Dog (8/9/2011)


    Hi,

    i've a table with 2 columns one log_event_num (int) and one log_event_time (datetime).

    how i can write a select that will show me every day only one log_event_num per event?

    THX

    Do you mean "only one log_event_time per event"? If so, then which one?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • the table hold a lot of events from the same event per day.

    i need to extract only one event per event per day from every event.

    THX

  • How about a little sample data, say 10 rows split across two day boundaries?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • with cte as (

    select ROW_NUMBER () Over (partition by (convert(varchar(256),eventtime ,103)) , eventid order by (convert(varchar(256),eventtime ,103) )) as rowid ,

    convert(varchar(256),eventtime ,103) as dates , eventid from eventtb

    )

    select * from cte where rowid = 1

    Remove event id and tweak as required .

    Jayanth Kurup[/url]

  • this syntax give me the right results but can it be change some how to start with a select first and not with "with cte as"?

    THX

  • Yes, but why?

    John

  • i need to put the results in here

    +N'<TD>' + ISNULL( + '<p class="datarows">' + CONVERT(NVARCHAR(MAX),[eventtime ]),'<p class="datarowserror">Not Avail') + N'</p></TD>'

    +N'<TD>' + ISNULL( + '<p class="datarows">' + CONVERT(NVARCHAR(MAX),[eventid]),'<p class="datarowserror">Not Avail') + N'</p></TD>'

  • OK, so instead of saying SELECT *, say SELECT N'<TD>' + ISNULL( + '<p class="datarows">' + CONVERT(NVARCHAR(MAX),[eventtime ]),'<p class="datarowserror">Not Avail') + N'</p></TD>'

    +N'<TD>' + ISNULL( + '<p class="datarows">' + CONVERT(NVARCHAR(MAX),[eventid]),'<p class="datarowserror">Not Avail') + N'</p></TD>'

    That's if I'm understanding you correctly. If you provide table DDL in the form of CREATE TABLE statements, sample data in the form of INSERT statements and expected results, we wouldn't have to guess, and you'd get a tested solution.

    John

  • Mad-Dog (8/9/2011)


    this syntax give me the right results but can it be change some how to start with a select first and not with "with cte as"?

    THX

    As John says, Yes - a simple SELECT with GROUP BY. But we can't test without sample data.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • some sample data from table,i need to extract with select

    LOG_NUMBER,EVENT_TIME

    002,2011-08-07 20:29:05.787

    002,2011-08-07 20:29:05.787

    002,2011-08-06 20:29:05.787

    002,2011-08-05 20:29:05.787

    002,2011-08-05 21:29:05.787

    003,2011-08-05 21:29:05.787

    003,2011-08-05 20:29:05.787

    004,2011-08-08 20:29:05.787

    004,2011-08-08 23:29:05.787

    005,2011-08-06 20:29:05.787

  • Chooes the column you want, min or max (or average, if you wish):

    DROP TABLE #Sample

    CREATE TABLE #Sample (LOG_NUMBER CHAR(3), EVENT_TIME DATETIME)

    INSERT INTO #Sample (LOG_NUMBER, EVENT_TIME)

    SELECT '002','2011-08-07 20:29:05.787' UNION ALL

    SELECT '002','2011-08-07 20:29:05.787' UNION ALL

    SELECT '002','2011-08-06 20:29:05.787' UNION ALL

    SELECT '002','2011-08-05 20:29:05.787' UNION ALL

    SELECT '002','2011-08-05 21:29:05.787' UNION ALL

    SELECT '003','2011-08-05 21:29:05.787' UNION ALL

    SELECT '003','2011-08-05 20:29:05.787' UNION ALL

    SELECT '004','2011-08-08 20:29:05.787' UNION ALL

    SELECT '004','2011-08-08 23:29:05.787' UNION ALL

    SELECT '005','2011-08-06 20:29:05.787'

    SELECT LOG_NUMBER,

    MAX_EVENT_TIME = MAX(EVENT_TIME),

    MIN_EVENT_TIME = MIN(EVENT_TIME)

    FROM #Sample

    GROUP BY LOG_NUMBER

    ORDER BY LOG_NUMBER

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (8/9/2011)


    Chooes the column you want, min or max (or average, if you wish):

    DROP TABLE #Sample

    CREATE TABLE #Sample (LOG_NUMBER CHAR(3), EVENT_TIME DATETIME)

    INSERT INTO #Sample (LOG_NUMBER, EVENT_TIME)

    SELECT '002','2011-08-07 20:29:05.787' UNION ALL

    SELECT '002','2011-08-07 20:29:05.787' UNION ALL

    SELECT '002','2011-08-06 20:29:05.787' UNION ALL

    SELECT '002','2011-08-05 20:29:05.787' UNION ALL

    SELECT '002','2011-08-05 21:29:05.787' UNION ALL

    SELECT '003','2011-08-05 21:29:05.787' UNION ALL

    SELECT '003','2011-08-05 20:29:05.787' UNION ALL

    SELECT '004','2011-08-08 20:29:05.787' UNION ALL

    SELECT '004','2011-08-08 23:29:05.787' UNION ALL

    SELECT '005','2011-08-06 20:29:05.787'

    SELECT LOG_NUMBER,

    MAX_EVENT_TIME = MAX(EVENT_TIME),

    MIN_EVENT_TIME = MIN(EVENT_TIME)

    FROM #Sample

    GROUP BY LOG_NUMBER

    ORDER BY LOG_NUMBER

    it's not give me the correct results.

    LOG_NUMBER '002' needs to be

    00205/08/2011

    00206/08/2011

    00207/08/2011

  • Mad-Dog (8/9/2011)


    ...

    it's not give me the correct results.

    LOG_NUMBER '002' needs to be

    00205/08/2011

    00206/08/2011

    00207/08/2011

    Apologies. Actually it should appear three times:

    SELECT LOG_NUMBER,

    MAX_EVENT_TIME = MAX(EVENT_TIME),

    MIN_EVENT_TIME = MIN(EVENT_TIME)

    FROM #Sample

    GROUP BY LOG_NUMBER, DATEDIFF(dd,0,EVENT_TIME)

    ORDER BY LOG_NUMBER

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (8/9/2011)


    Mad-Dog (8/9/2011)


    ...

    it's not give me the correct results.

    LOG_NUMBER '002' needs to be

    00205/08/2011

    00206/08/2011

    00207/08/2011

    Apologies. Actually it should appear three times:

    SELECT LOG_NUMBER,

    MAX_EVENT_TIME = MAX(EVENT_TIME),

    MIN_EVENT_TIME = MIN(EVENT_TIME)

    FROM #Sample

    GROUP BY LOG_NUMBER, DATEDIFF(dd,0,EVENT_TIME)

    ORDER BY LOG_NUMBER

    Thanks it is working good.

Viewing 15 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply