How to retrieve Min(startDate) and Max(endDate) for different groups of data? (sql server 2000)

My sample dataset (below) contains 3 groups -- 'a', 'b', 'c'.  I need to retrieve the Min(startDate) and Max(EndDate) for each group so that the output looks something like this (date format not an issue):
fk   minStart       maxEnd
a    1/13/1985    12/31/2003
b    2/14/1986    12/31/2003
c    4/26/1987    12/31/2002
What is the Tsql to perform this type of operation?  Note:  the actual data resides in a sql server 2000 DB.  If the Tsql is different between version 2000 and the later versions -- I would be grateful for both versions of the Tsql
--I noticed that multiple lines of Insert values doesn't work in Sql Server 2000 -- this sample is in Sql Server 2008
create table #tmp2(rowID int Identity(1,1), fk varchar(1), startDate datetime, endDate datetime)
insert into #tmp2
values
('a', '1/13/1985', '12/31/1999'),
('a', '3/17/1992', '12/31/1997'),
('a', '4/21/1987', '12/31/2003'),
('b', '2/14/1986', '12/31/2003'),
('b', '5/30/1993', '12/31/2001'),
('b', '6/15/1994', '12/31/2003'),
('b', '7/7/2001', '12/31/2003'),
('c', '4/26/1987', '12/31/1991'),
('c', '8/14/1992', '12/31/1998'),
('c', '9/10/1995', '12/31/2002'),
('c', '10/9/1996', '12/31/2000')
Thanks
Rich P

Rich
It is unclear what you are trying to achieve, you said that it is SQL Server 2000 but provide a sample data with SQL Server 2008 syntax
Is it possible to use UNION ALL for your queries to make its one 
select * from
select * from #tmp2 t1 where exists
(select * from (select top 1 * from #tmp2 t2 where t2.fk = t1.fk order by t2.startdate) x where x.rowID = t1.rowID)
UNION ALL
select * from #tmp2 t1 where exists
(select * from (select top 1 * from #tmp2 t2 where t2.fk = t1.fk order by t2.Enddate desc) x where x.rowID = t1.rowID)
 as  der order by fk
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence

Similar Messages

  • How to build a report in web Intelligence using Store procedure under Microsoft SQL Server 2000

    Post Author: ltkin
    CA Forum: WebIntelligence Reporting
    Hi,
    How to build a report in web Intelligence using Store procedure under Microsoft SQL Server 2000 ?
    Regards,

    Hi ltkin,
    Unfortunately, it is not possible in Xir2 to create Webi reports from stored procedures.
    Webi reports can only be created from Universe. So in Business Objects XIR3 we can create a special universe that enables Web Intelligence user's to access stored procedures residing in the database. This is the only way that Web Intelligence user's can access stored procedures.
    Please let me know if the above information helps.
    Regards,
    Pavan

  • TSQL Query to display First and Last Values of a GROUP as Columns - SQL Server 2008

    Could someone please help with the TSQL to achieve the following: Every Employee has 2 records (From & To). Output has to display one record for each employee with From & To as columns.
    Input
    Empid
    Type
    Date
    100
    From
    5/4/2004
    100
    To
    6/2/2008
    101
    From
    6/12/2003
    101
    To
    6/2/2013
    102
    From
    12/12/2012
    102
    To
    5/3/2014
    Output
    EmpID
    From
    To
    100
    5/4/2004
    6/2/2008
    101
    6/12/2003
    6/2/2013
    102
    12/12/2012
    5/3/2014
    Thanks, Ashish Singh

    SELECT empid,
    MAX(CASE WHEN Type='From' THEN date END) from,
    MAX(CASE WHEN Type='To' THEN date END) to
    FROM tbl GROUP BY empid
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • How to connett via hdmi and VGA adapter for my Toshibe 3LCD data projector

    My  HP Pavilion running windos 8.1  can not converse via HDMI A to VGA adapter to Toshibe TLP470Z data projector

    Checking the manual for the Toshiba projector, found HERE, it states:
    The projector cannot be connected to a computer that does not have
    an analog RGB output terminal. For details, refer to the computer
    manual.
    • You may not be able to connect some computers to the projector.
    The model number of your Pavilion PC would be most helpful.
    {---------- Please click the "Thumbs Up" to say thanks for helping.
    Please click "Accept As Solution" if my help has solved your problem. ----------}
    This is a user supported forum. I am a volunteer and I do not work for HP.

  • How to calculate size of a folder located on different machine(Remote) in sql server 2008?

    DECLARE @TenantId UNIQUEIDENTIFIER ='A79ED820-2E5E-4A9D-B930-B7597DC97081'
    DECLARE @BusinessName VARCHAR(200)
    declare @line  varchar(255) =''
    declare @path varchar(255)=''
    declare @command varchar(255)=''
    DECLARE @folderSizeGB decimal (16,8) = 0
    DECLARE @AttachmentDiscSpaceUsed decimal (16,8) = 0
    -- Code to calculate folder size in GB BEGINs
                IF OBJECT_ID('tempdb..#temp') IS NOT NULL
                drop table #temp
                create table #temp (line varchar(255))
                --set @path = '\\ewp-dev18\\c$\\Attachments\\' +  CONVERT(varchar(50),@tenantId)
                set @path = '-S ewp-dev18 -E -i ewp-dev18\\c$\\Attachments\\' +  CONVERT(varchar(50),@tenantId)
                set @command = 'dir "' + @path +'"'                               
                insert into #temp
                exec master.dbo.xp_cmdshell @command
                select
                @line=
                ltrim(replace(substring(line, charindex(')', line)+1,
                len(line)), ',', ''))
                from #temp where line like '%File(s)%bytes'
                SET @line=
                (Case WHEN @line IS NULL or @line ='' THEN '0' ELSE @line END)
                set @folderSizeGB  = Cast(Replace(@line,'bytes', '') as decimal)/1073741824
                if (@folderSizeGB is null )
                set @folderSizeGB =0
                SET @AttachmentDiscSpaceUsed = @folderSizeGB
                SET @path=''
                SET @command=''
                SET @line=0
                -- Drop #temp table
                IF OBJECT_ID('tempdb..#temp') IS NOT NULL
                drop table #temp
                -- Code to calculate folder size in GB ENDs
                

    hello RSingh,
    Once again thanks for your cooperation,
    I applied the script you posted, but it is not giving me the size of the folder which is located at a remote machine. Please note the " I need to calculate the size of the folder which is located at a remote machine".and when i applied its path in your script,
    it raises "Access is denied" error message.
    My path is - '\\ewp-dev18\c$\Attachments'
    EXEC getFileProperties '\\ewp-dev18\c$\Attachments'
    and this folder does not have any sub folders, it contains only some txt files, image files or some doc files. so no issue of sub folder.
    and about my remote server 'ewp-dev18', i have all access and rights, but still raising "Access is denied". why?
    do i need to pass any login credential with it ???

  • How to replace a character at a random position of a column in SQL Server 2000?

    Hello everyone,
    I'm trying to export the data of a table into a flat file. However, when I try to export it, a column which has a specific character (".") is being loaded as a new column into the file rather than the same column.
    Here is the DDL/DML:
    CREATE TABLE [dbo].[EXT_Name](
    [A_Name] [VARCHAR](4),
    [U_Name] [VARCHAR](256),
    [U_Desc] [VARCHAR](256),
    [Acc_Desc] [VARCHAR](256),
    [S_Type] [VARCHAR](50))
    Its just that's it. No more Keys and Indexes on this table.
    Here is the bcp command I'm running:
    EXEC [master].[dbo].[xp_cmdshell] 'bcp "SELECT * FROM [dbo].[Ext_Name]" queryout C:\Data\Ext_Name.dat -c -t, -T -S'
    Data:
    GEHG,/User/Personal/Project/Click.do,Search for User Project.,See Summary,Summaries
    GEHG,/User/Personal/Project/Click.do,Search for User Project.Detail,null,Summaries
    Instead for the data to be loaded as the above, its loading into the file as
    GEHG,/User/Personal/Project/Click.do,Search for User Project.,See Summary,Summaries
    GEHG,/User/Personal/Project/Click.do,Search for User Project.Detail
    ,null,Summaries
    This issue occurs only when [Acc_Desc] column has the "." character in between the string. Could experts in this forum help me please?
    Thank you,
    Bangaaram
    Known is a DROP, Unknown is an OCEAN.

    That is most likely not your problem. Your problem is most likely the source data has a CR and/or LF after it in the database.
    Run this to remove them from Acc_Desc:
    UPDATE [dbo].[EXT_Name]
    SET [Acc_Desc] = REPLACE(REPLACE([Acc_Desc],CHAR(13),''),CHAR(10),'')
    Perfect! Thank you Tom :)
    Known is a DROP, Unknown is an OCEAN.

  • How to compare 2 dates in SQL Server 2000 DB using Java?

    How do you compare 2 dates in SQL Server 2000 DB using Java?
    Let's say we have two fields: Date Reported and Target Finish Date.
    Date Reported is 09-10-2004 09:55:55: PM
    Target Finish Date is 09-12-2004 11:59:59: PM
    What i want to happen is i want to convert both dates to days and get the difference of the two.
    can SQL Server 2000 DB do this?

    it doesnt wrk.
    ok here's what i did:
    iv tested a simple code for this case.
    I created a table name tblDate which has 3 columns namely date_ID, date_From (datatype datetime), date_To (datatype datetime).
    I inserted 1 row: date_ID has the value 1, date_From has the value 10/22/2004, and date_To has the value 10/24/2004.
    i run the java code below:
    int days = 0;
              String query = "SELECT date_From, date_To cast(date_From-date_To AS int) AS Diff FROM tblZoo WHERE date_ID = '1'";
              try
                   DBConnect db = new DBConnect();
                   db.openCon();
                   ResultSet rs = db.execute(query);
                   while(rs.next())
                        days = rs.getInt("Diff");
                   db.closeCon();                         
              catch (Exception ex)
                   System.out.println("Error on Execution: " + ex);
              return days;___________
    an error occurred: Error on Execution: java.lang.nullpointerexception

  • Basic Deployment problem, SQL Server 2000 and wanna create setup

    I have just finished making a small app using Java and using JDBC driver to connect to an SQL Server 2000 that is installed on my PC.
    I now want to create a setup (using NSIS) of this app and distribute among friends. But my problem is that I have created the database on my PC on the SQL Server 2000. Do all the people who want to use my app have to have Sql Server 2000 installed ?? How do they get the data from the database if they dont have Sql Server installed??
    I really havent tried much of apps that use databases...soo dont know much, but then any help would be appreciated!!

    They have to connect to an instance of SQL Server 2000 that has the database and schema that your app requires, so the host machine has to be visible in their network, with SQL Server installed, listening on the port in your URL connection string (default is 1433), with a username and password that you've set up for a user with the appropriate permissions GRANTed.
    As you can see, it's not trivial.
    If all the people you're distributing the program to can see your machine's IP address, they can simply connect to your database.
    If not, you should provide all the scripts necessary to create the database and create the tables, set up the permissions, and populate the data.
    %

  • How to dynamically set Min count and Max Count values in Adobe Print Form?

    How to set the Min Count and Max Count values dynamically in a Print Form?
    The values when set should over ride the values set in Binding Tab.
    This is all needed to print multiple Material Number labels on a single page - and the number of labels to be printed needs to be under user control - something like an Avery Address labels
    Please advise.
    Thanks,

    Here is a work around that works for me and may not be an intelligent solution .
    I kept the Min Count to 1 on the subform binding properties.
    Per the number of labels required, I appended the same item to the internal table so many times and passed it to the label.
    it works fine for my requirement.
    However - leaving out the post for a while to see if this can be done at the form level via scripting.
    Thanks,

  • How to retrieve the path and the name of the destination file in a link annotation

    Hi,
    In C++:
    How to retrieve the path and the name of the destination file in a link annotation and a Launch action.
    Sample of my code:
    //Determine if the annotation is a Link annotation
    if (PDAnnotGetSubtype(annot) == ASAtomFromString("Link")) {
    //Determine if the action is a Launch action
    if (PDActionGetSubtype(action) == ASAtomFromString("Launch")) {
    // What is the method ?
    Regards
    David G

    In general, get the annotation as a Cos object and examine it, in
    accordance with the PDF Reference.
    Aandi Inston

  • How to save and retrieve an image in JSP/Java and MS SQL/server 2000?

    Hi All,
    I am uploading an image from the JSP page and want to store it in MS SQL server. I have made a column by the name "COMPANY_LOGO" with data type as image MS SQL/server 2000. How do I save the full image in the table and how do I display the thumb nail image on the JSP page???
    Regards,
    Raj

    BEFORE YOU POST A TOPIC HERE: Please be sure your topic is related to features or functionality of this site. This forum is not for general technology questions. Not sure which forum to use? Try searching for your topic in the "Search Forums" element on the left panel.
    Post your question in
    http://forum.java.sun.com/forum.jspa?forumID=45
    or
    http://forum.java.sun.com/forum.jspa?forumID=48
    Do not reply here

  • How to get the lock and block occured for the full day

    Hi,
         How to monitor the lock and blocks occurred for the full day? i need to get it by end of the day. is there any script for this?

    Hi,
         How to monitor the lock and blocks occurred for the full day? i need to get it by end of the day. is there any script for this?
    Why would you like to collect information about locks and let me tell you, you cannot collect information about ALL locks which were taken and even thinking about it is pointless.
    Coming to blocking if you have heavy system with huge concurrency there is bound to be blocking which is necessary and good for RDBMS but if the blocking remains for long time then this is a issue.
    There is DMV sys.dm_tran_locks which tell you what all locks are being taken. You can see various examples mentioned in DMV online documentation and can schedule the query to run every 5
    mins and insert its record in table which you can refer at end of the day.
    This task would be made very easy if you have Monitoring tool which gives you information in your mail every time blocking occurs I have Spotlight in my environment.
    If you are looking for doc for troubleshooting blocking  below is one you can refer
    http://support.microsoft.com/kb/224453
    Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it.
    My TechNet Wiki Articles

  • How to know Sales Organization and distribution channel for a material

    How to know Sales Organization and distribution channel for a created material

    Hi,
    For a single material means u can go mm03 7 select the material number 7 select the sales views then press f4 the details u can see.
    For any number of materials u can go to se11 & give table name MVKE then select the required parameters & execute.
    pandari

  • Can anybody tell me how to extract sales value and cost value for an materi

    dear all,
    Can anybody tell me how to extract sales value and cost value for an material sold .
    All the values of cost value and sale values are getting  stored in bseg-wrbtr field .
    How to identify them uniquely.
    Or there is another way to find the sale and cost values.
    Regards
    Mave

    Hi Mave!
    Normally there is no condition change, when something is sold.
    But sales price depends on sales conditions (not only price, but also surcharges, discounts, taxes - if customized).
    Nevertheless, after selling you have a invoice document, where correct values <i>should</i> be stored (see VBRP fields mentioned above). If here is a problem, then customizing of price determination is in question, not a programming of user-exits with MARD selection for costs (or some other tries to get a price).
    When you specify your requirements more in detail, you might get better answers - but currently only general hints are possible ('see in invioce / G/L accounts').
    Regards,
    Christian

  • Storing and retrieving arabic in SQL Server 2000

    hello, i realize this problem has been posted lots before but i read in the forum and i couldn't find a solution to fix my problem
    i am developing a desktop application in java and sql server 2000, and i'm using JBuilder for writing my java codes
    here's the problem
    i can MANUALLY store arabic text in my database (directly in SQL Sever Query Analyzer) and can read it in arabic properly in SQL Server, but if i want to retrieve the arabic text in a query in JAVA, for example: select * from person i see weird characters (not question marks) but not the original arabic text
    and if i type arabic text in a java textfield and use the value entered in an sql query, for example:
    sqlQuery = "insert into person (fname) values ('" + jtfFName.getText() + "')";it is inserted in SQL Server as ?????s and when i retrieve the values from the table i also see it as ?????s
    even when i converted all the data types in the database to unicode data types (*nchar* instead of char and nvarchar instead of varchar) and modified the sql query to the following:
    sqlQuery = "insert into person (fname) values (N'" + jtfFName.getText() + "')";(N outside the single quotation mark since according to rules on msdn)
    it still didn't solve the problem
    please i need urgent help with this issue!

    problem was solved by setting the charSet encoding to "UTF-8" in the connection to sql server using the jdbc-odbc bridge

Maybe you are looking for

  • How to position the Swing button on javaFx applet

    Hello, I am putting a swing button on javaFx page as: SwingButton {                 text: "Go"                 action: function() {                     widgetManager.openDirectAlertId(homeBean.getAlert(1).getAlertId());             }This button alway

  • Importing xml page changed the language used

    hi, i am having an issue on my customized oaf page which is an advanced search page. We are using french in OA. Evrything in the page is in French including the results table page i exported an oaf xml page (using xmlexporter without specifing the la

  • FRM-41067: Cannot find menu item: invalid Id

    Hello, I have a menu, when i try to execute query by entering a value i received this message. i checked all my code, everything seems correct. someone can tell me what it could be wrong in my menu?

  • MacBook Pro 10.5.2 cannot see Hard Reset Snow to administer

    Can someone help me revive my hard-reset Snow base station? I've tried the new AirPort Admin Utility for Graphite and Snow 4.2.5, the old Airport Admin Utility 4.2 and Airport Utility (which I knew wouldn't work anyway). The old Airport Admin Utiliti

  • Trouble Updating my Apps

    I have a Mac, iPod, & very recently an iPhone. Since I've got the phone I can't update my updates. When I log in it will either say wrong password or it will accept the password & then nothing happens. I think I may have somehow confused something by