SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert a query to a View


Convert a query to a View

Author
Message
ordnance1
ordnance1
Old Hand
Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

Group: General Forum Members
Points: 311 Visits: 108
First and foremost I am not a db programmer, Dispatcher by trade. So if I do not belong here do not hesitate to say so. With the form someone on another form I have created the following query to return the minimum temperatue for each day and the time it first occurred. My problem is that I want this as a View. I plan to use another program as a front end to display my data.

  • use tempdb;
    GO

    CREATE TABLE #Readings (
    EventDate DATE,
    EventTime TIME,
    Temp TINYINT
    );
    GO
    -- some sample data
    INSERT INTO #Readings
    SELECT EventDate
    ,EventTime
    ,[Temp_Out]
    FROM [YewSt_Weather_WD].[dbo].[WX_Data_WD]

    -- get the min(EventTime) of the Min(Temp)
    SELECT mt.EventDate
    , mt.MinimumTemp
    , MIN(r.EventTime) AS EarliestTime
    FROM
    (SELECT EventDate
    , MIN(Temp) AS MinimumTemp
    FROM #Readings
    GROUP BY EventDate) mt
    INNER JOIN #Readings r
    ON (mt.EventDate = r.EventDate
    AND mt.MinimumTemp = r.Temp)
    GROUP BY
    mt.EventDate
    , mt.MinimumTemp;

  • Jacob Wilkins
    Jacob Wilkins
    SSC-Insane
    SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

    Group: General Forum Members
    Points: 23494 Visits: 12942
    A simpler way to get the information you're looking at would be this:

    WITH CTE AS 
    (
    SELECT EventDate,EventTime,Temp, rn=ROW_NUMBER() OVER (PARTITION BY EventDate ORDER BY Temp ASC, EventTime ASC)
    FROM mytable --Change to your table name, of course
    )

    SELECT EventDate, EventTime, Temp
    FROM CTE
    WHERE rn=1;


    Turning that into a view is as simple as adding the following line at the beginning:

    CREATE VIEW myview AS --Again, change to the desired name for the view as necessary


    Cheers!
    pietlinden
    pietlinden
    SSC-Forever
    SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)SSC-Forever (48K reputation)

    Group: General Forum Members
    Points: 48824 Visits: 16131
    Yeah, I figured that out after taking a break for a while... (That's my crummy code he was posting about.)
    Took me a minute to figure out I could use ROW_NUMBER() OVER () PARTITION... to do it. Turns out leaving myself hints worked pretty well.
    ordnance1
    ordnance1
    Old Hand
    Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)Old Hand (311 reputation)

    Group: General Forum Members
    Points: 311 Visits: 108
    Thank you for your help.
    Go


    Permissions

    You can't post new topics.
    You can't post topic replies.
    You can't post new polls.
    You can't post replies to polls.
    You can't edit your own topics.
    You can't delete your own topics.
    You can't edit other topics.
    You can't delete other topics.
    You can't edit your own posts.
    You can't edit other posts.
    You can't delete your own posts.
    You can't delete other posts.
    You can't post events.
    You can't edit your own events.
    You can't edit other events.
    You can't delete your own events.
    You can't delete other events.
    You can't send private messages.
    You can't send emails.
    You can read topics.
    You can't vote in polls.
    You can't upload attachments.
    You can download attachments.
    You can't post HTML code.
    You can't edit HTML code.
    You can't post IFCode.
    You can't post JavaScript.
    You can post emoticons.
    You can't post or upload images.

    Select a forum








































































































































































    SQLServerCentral


    Search