Search Results

Search found 28900 results on 1156 pages for 'sql 2005'.

Page 120/1156 | < Previous Page | 116 117 118 119 120 121 122 123 124 125 126 127  | Next Page >

  • Querying for a unique value based on the aggregate of another value while grouping on a third value

    - by Justin Swartsel
    So I know this problem isn't a new one, but I'm trying to wrap my head around it and understand the best way to deal with scenarios like this. Say I have a hypothetical table 'X' that looks like this: GroupID ID (identity) SomeDateTime -------------------------------------------- 1 1000 1/1/01 1 1001 2/2/02 1 1002 3/3/03 2 1003 4/4/04 2 1004 5/5/05 I want to query it so the result set looks like this: ---------------------------------------- 1 1002 3/3/03 2 1004 5/5/05 Basically what I want is the MAX SomeDateTime value grouped by my GroupID column. The kicker is that I DON'T want to group by the ID column, I just want to know the 'ID' that corresponds to the MAX SomeDateTime. I know one pseudo-solution would be: ;WITH X1 as ( SELECT MAX(SomeDateTime) as SomeDateTime, GroupID FROM X GROUP BY GroupID ) SELECT X1.SomeDateTime, X1.GroupID, X2.ID FROM X1 INNER JOIN X as X2 ON X.DateTime = X2.DateTime But this doesn't solve the fact that a DateTime might not be unique. And it seems sloppy to join on a DateTime like that. Another pseudo-solution could be: SELECT X.GroupID, MAX(X.ID) as ID, MAX(X.SomeDateTime) as SomeDateTime FROM X GROUP BY X.GroupID But there are no guarantees that ID will actually match the row that SomeDateTime comes from. A third less useful option might be: SELECT TOP 1 X.GroupID, X.ID, X.SomeDateTime FROM X WHERE X.GroupID = 1 ORDER BY X.SomeDateTime DESC But obviously that only works with a single, known, GroupID. I want to be able to join this result set on GroupID and/or ID. Does anyone know of any clever solutions? Any good uses of windowing functions? Thanks!

    Read the article

  • Dynamically set the result of a TSQL query using CASE WHEN

    - by Name.IsNullOrEmpty
    SELECT MyTable.Name,(SELECT CASE WHEN ISNULL(SUM(TotalDays), 0) <= 0 THEN 0 ELSE SUM(TotalDays) END AS Total FROM Application AS Applications WHERE (ID = MyTable.id)) - MIN(Assignments) AS Excesses FROM MyTable The above TSQL statement is a subquery in a main query. When i run it, if TotalDays is NULL or <=0, then Total is set to 0 (zero). What i would like to do here is to set the result of the whole query(Excesses) to 0. I want (Excesses) which is the result of Total - Min(Assignments) to be set to 0 if its NULL or <=0. I want the CASE WHEN to apply to the whole query but am struggling to get it right.

    Read the article

  • Sql query number of occurance

    - by phenevo
    Hi, I wanna to have query: Select cars.* from cars where cars.code in ( select carCode from articles where numberofrecords with this car (it is not a column) >1 and Accepted=1 order by date ) How to write it?

    Read the article

  • SQL Server PIVOT on key-value table

    - by Zenox
    I have a table that has attributes based on a key-value. Example: CREATE TABLE ObjectAttributes ( int objectId, key nvarchar(64), value nvarchar(512) ) When I select from this I get: objectId key value ---------------------------- 1 Key 1 Value 1 1 Key 2 Value 2 I was wondering if I could use the PIVOT syntax to turn this into: objectId Key 1 Key 2 --------------------------- 1 Value 1 Value 2 I know all of my tables will have the same keys. (Unfortunately I cannot easily change the table structure. This is what is leading me to attempt using PIVOTS). The big issue here though is that pivots require an aggression function to be used. Is there a way to avert this? Am I completely wrong attempting this? Or is there a better solution?

    Read the article

  • need help understanding a function.

    - by Adam McC
    i had previously asked for help writing/improving a function that i need to calculate a premium based on differing values for each month. the premium is split in to 12 months and earned on a percentage for each month. so if the policy start in march and we are in jan we will have earned 10 months worth. so i need to add up the monthly earning to give us the total earned. wach company wil have differeing earnings values for each month. my original code is Here. its ghastly and slow hence the request for help. and i was presented with the following code. the code works but returns stupendously large figures. begin set @begin=datepart(month,@outdate) set @end=datepart(month,@experiencedate) ;with a as ( select *, case calmonth when 'january' then 1 when 'february' then 2 when 'march' then 3 when 'april' then 4 when 'may' then 5 when 'june' then 6 when 'july' then 7 when 'august' then 8 when 'september' then 9 when 'october' then 10 when 'november' then 11 when 'december' then 12 end as Mnth from tblearningpatterns where clientname=@client and earningpattern=@pattern ) , b as ( select earningvalue, Mnth, earningvalue as Ttl from a where Mnth=@begin union all select a.earningvalue, a.Mnth, cast(b.Ttl*a.earningvalue as decimal(15,3)) as Ttl from a inner join b on a.Mnth=b.Mnth+1 where a.Mnth<=@end ) select @earningvalue= Ttl from b inner join ( select max(Mnth) as Mnth from b ) c on b.Mnth=c.Mnth option(maxrecursion 12) SET @earnedpremium = @earningvalue*@premium end can someone please help me out?

    Read the article

  • [SQL] Select 3 lastest order for each customer

    - by Ratiug
    Hi Here is my table CusOrder that collect customer order OrderID Cus_ID Product_ID NumberOrder OrderDate 1 0000000001 9 1 6/5/2553 0:00:00 2 0000000001 10 1 6/5/2553 0:00:00 3 0000000004 9 2 13/4/2553 0:00:00 4 0000000004 9 1 17/3/2553 0:00:00 5 0000000002 9 1 22/1/2553 0:00:00 7 0000000005 9 1 16/12/2552 0:00:00 8 0000000003 9 3 13/12/2552 0:00:00 10 0000000001 9 2 19/11/2552 0:00:00 11 0000000003 9 2 10/11/2552 0:00:00 12 0000000002 9 1 23/11/2552 0:00:00 I need to select 3 lastest order for each customer and I need all customer so it will show each customer and his/her 3 lastest order how can I do it sorry for my bad english

    Read the article

  • Optimising a query for Top 5% of users

    - by Nai
    On my website, there exists a group of 'power users' who are fantastic and adding lots of content on to my site. However, their prolific activities has led to their profile pages slowing down a lot. For 95% of the other users, the SPROC that is returning the data is very quick. It's only for these group of power users, the very same SPROC is slow. How does one go about optimising the query for this group of users? You can assume that the right indexes have already been constructed. EDIT: Ok, I think I have been a bit too vague. To rephrase the question, how can I optimise my site to enhance the performance for these 5% of users. Given that this SPROC is the same one that is in use for every user and that it is already well optimised, I am guessing the next steps are to explore caching possibilities on the data and application layers?

    Read the article

  • Error when trying to open SQL Maintenance Plan - SSMS 2008

    - by alex
    If I open SSMS on my client machine, connect to our SQL server, and try and open a maintenance plan on there, I get this error: TITLE: Microsoft SQL Server Management Studio Could not load file or assembly 'msddsp, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. (Microsoft.DataTransformationServices.Design) ------------------------------ BUTTONS: OK If i try the same thing directly on the server, nothing happens (no errors or anything)

    Read the article

  • SQL Server installation leaves odd hotfix folders on the root

    - by Nick
    I have about a dozen weirdly named folders, for example 5e812807b3e1b04248ed046794a01b, on my d: drive after installing SQL Server 2008. Inside each folder are two subfolders: 1033 and hotfixexpress \1033\eula.txt \1033\finalsql2005information.rtf \1033\hotfix.rll \1033\sqlhotfix.chm \1033\sqlse.rll \hotfixexpress\files\sqlexpr.exe I get access denied whenever I try to delete any of these files / folders. I tried again after stopping all SQL server services, without luck. Any ideas?

    Read the article

  • SQL Server 2008 - "Enhanced read-ahead and scan"?

    - by Chris J
    Hi all, Can anyone point me to, or does anyone know what this "feature" of SQL Server is? Googling for it (also for "Super scan" which seems to be a synonym for the feature) reveals no information other than all the edition comparison pages that exist out there for SQL Server. I'm also not sure whether this is a stackoverflow or a serverfault question. I'll try here on serverfault first, but if folk think stackoverflow may be a better forum, drop me a comment to that effect - ta :-)

    Read the article

  • Don't see job schedule added by sp_add_jobschedule in SQL Mgmt UI

    - by Ariel
    I'm running a script like below on a SQL Server box and, even though it finishes correctly, then when, on SQL Mgmt UI, I right click on that job's properties, go to Schedules, I cannot see the schedule just added... what am I missing? (I'm using the right job_name param, etc) thanks! BEGIN TRY BEGIN TRAN EXEC msdb.dbo.sp_add_jobschedule @job_name = 'Job name', @name=N'Job schedule name', @enabled = 0, @freq_type=1, @active_start_date=20100525, @active_start_time=60000 COMMIT TRAN END TRY BEGIN CATCH SELECT ERROR_Message(), ERROR_Line(); ROLLBACK TRAN END CATCH

    Read the article

  • Run a script prior to start of SQL instance via Windows clusters

    - by Shahryar G. Hashemi
    Hi, We have a Windows 2008 cluster with several SQL 2008 instance. We would like to run a script that modifies 4 registry keys prior to the startup of SQL. I do not know if there is a way to have a script run through Windows 2008 clustering that does that. I have a VBS script to do it and tried to add a Generic Script to an existing cluster group, but it failed saying it could not be registered. Any ideas?

    Read the article

  • SQL Agent Command Line Not Saved

    - by Greg_the_Ant
    I have a SSIS package I am trying to schedule. I create a new job under SQL Server Agent. On the Command line tab of the jobstep, I choose "Edit the command-line manually". The changes are retained as I switch from tab to tab within the job step but whenever I exit and save the job, the changes are lost. Any ideas what's going on? I'm on SQL Server 2008.

    Read the article

  • Don't see job schedule in SQL Mgmt UI added by sp_add_jobschedule

    - by Ariel
    I'm running a script like below on a SQL Server box and, even though it finishes correctly, then when, on SQL Mgmt UI, I right click on that job's properties, go to Schedules, I cannot see the schedule just added... what am I missing? (I'm using the right job_name param, etc) thanks! BEGIN TRY BEGIN TRAN EXEC msdb.dbo.sp_add_jobschedule @job_name = 'Job name', @name=N'Job schedule name', @enabled = 0, @freq_type=1, @active_start_date=20100525, @active_start_time=60000 COMMIT TRAN END TRY BEGIN CATCH SELECT ERROR_Message(), ERROR_Line(); ROLLBACK TRAN END CATCH

    Read the article

  • Resources for SQL Server programming?

    - by Undh
    I have tried to search from the web resources for SQL Server programming. Basically I'm trying to search good tutorial for programming SQL Server (creating procedures, triggers, cursors etc.). Can you give some helping hand and show some links for good tutorials?

    Read the article

  • Tracking SQL Server 2008 Timeout Errors

    - by SDReyes
    we got some connections timeouts running a stored procedure in a SQL Server 2008 instance. after a while, the DB started to work as usual, and there weren't any additional timeout errors. So I'm trying to get additional info about the error. Does SQL server 2008 have any kind of error log that I can look into? Do exist any auditing/logging features that I can activate to trace the error (if it happens again)?

    Read the article

< Previous Page | 116 117 118 119 120 121 122 123 124 125 126 127  | Next Page >