sql-server-2008-r2

Selecting the maximum count from a GROUP BY operation

自作多情 提交于 2021-02-04 18:50:11
问题 Forgive my SQL knowledge, but I have a Person table with following data - Id Name ---- ------ 1 a 2 b 3 b 4 c and I want the following result - Name Total ------ ------ b 2 If I use the GROUP BY query - SELECT Name, Total=COUNT(*) FROM Person GROUP BY Name It gives me - Name Total ------ ------ a 1 b 2 c 1 But I want only the one with maximum count. How do I get that? 回答1: If you want ties SELECT top (1) with ties Name, COUNT(*) AS [count] FROM Person GROUP BY Name ORDER BY count(*) DESC 回答2:

SQL agent job failed with an error 0x80131904

☆樱花仙子☆ 提交于 2021-01-29 07:23:50
问题 I have one SQL agent job that executing the SSIS package. When the job is running through schedule then it is giving an error: Failed to execute IS server package because of error 0x80131904. Server: XXXXX Package path: "XXXX" Environment reference Id: 2. Description: The operation failed because the execution timed out. But when I manually running the job by right clicking on it then the job runs successfully. Could anyone help me on this to find out the root cause and solution of the issue.

Fulltext show the matching word in a multi-word search condition with OR

大兔子大兔子 提交于 2021-01-28 18:54:16
问题 I have a full-text indexed table and a column that is full-text indexed which contains a list of ids and some other information. Example: SearchInformation 100, 101, 102, 103, 104, Mike 200, 201, 202, 203, 204, John And my full-text query (a simplified version) is: SELECT searchInformation FROM Table T1 INNER JOIN CONTAINSTABLE(SearchTable, SearchInformation, '"100" OR "110" OR "Mick"') k ON T1.ID = k.[key] Now, this query identifies the correct row due to the "100" value being matched, but

Count Function on Multiple Columns by Date (SQL Server)

爱⌒轻易说出口 提交于 2021-01-28 08:15:46
问题 I'm using SQL Server 2008 R2 and trying to do multiple counts on different columns and can't figure out how to get it to work properly. I have two queries that work independently, but having trouble when trying to combine them. Trying to count the number of tickets opened and closed during each individual date while passing through an @StartDate and @EndDate. For example, here's my first query: (we assign certain codes to differentiate between different reasons tickets are created, just

SSIS and sending query with date to Oracle

跟風遠走 提交于 2021-01-28 06:26:59
问题 I am trying to create a flow to pull data from an Oracle table into a SQL Server table. I am sending the following query to Oracle to get the data: select distinct CHLD.id, nvl(chld_c_spl, 'N'), to_char(chld_d_start, 'YYYY-MM-DD') chld_d_start, to_char(chld_d_end, 'YYYY-MM-DD') chld_d_end from child chld, picture ptct where CHLD.id = PTCT.chld_id and nvl(chld_d_end, sysdate) >= to_date('01-JAN-2014') and chld_c_veri in ('HC','DR') and nvl(ptct_term, ptct_end) >= to_date('01-JAN-2014') When I

Performance issue in merge statement

空扰寡人 提交于 2021-01-28 06:05:33
问题 I have a merge statement like below MERGE DESTINATION AS DST USING ( SELECT <Some_Columns> FROM TABLEA WITH(NOLOCK) INNER JOIN TableB ..... ) AS SRC ON( <some conditions> ) WHEN MATCHED THEN UPDATE SET column1 = src.column1 ............... ,Modified_By = @PackageName ,Modified_Date = GETDATE() WHEN NOT MATCHED THEN INSERT (<Some_Columns>) VALUES(<Some_Columns>) OUTPUT $action, inserted.key'inserted' INTO @tableVar ; For the first set of records (around 300,000) it is working perfectly and

Performance issue in merge statement

五迷三道 提交于 2021-01-28 05:57:52
问题 I have a merge statement like below MERGE DESTINATION AS DST USING ( SELECT <Some_Columns> FROM TABLEA WITH(NOLOCK) INNER JOIN TableB ..... ) AS SRC ON( <some conditions> ) WHEN MATCHED THEN UPDATE SET column1 = src.column1 ............... ,Modified_By = @PackageName ,Modified_Date = GETDATE() WHEN NOT MATCHED THEN INSERT (<Some_Columns>) VALUES(<Some_Columns>) OUTPUT $action, inserted.key'inserted' INTO @tableVar ; For the first set of records (around 300,000) it is working perfectly and

Is concatenated string on where clause SARGable?

依然范特西╮ 提交于 2021-01-28 03:00:43
问题 Let's say I have a nonclustered index on two nvarchar columns, A and B. If my query looks something like this: SELECT Columns FROM Table WHERE A + B = '1234' Can the query effectively use the index? Or should I separate the columns in where clause SELECT Columns FROM Table WHERE A = '12' AND B = '34' I've found pretty surprising results from my testings. Both produced an identical query plan, but the costs were different. Most of the time, the concatenated query would be faster but from time

SQL Server 2008 R2: Concatenate alias name with column value

强颜欢笑 提交于 2021-01-28 00:01:20
问题 I have Product table with columns QtyID , Qty and Year_ID . I also have a table tbl_Years with ID and Year . I have a simple SELECT statement with SUM calculation of Qty : SELECT SUM(Qty) AS /*Sum_of_year_2016*/ FROM Product p INNER JOIN tbl_Years ty ON p.Year_ID = ty.Year_ID WHERE ty.Year_ID = 6; I want to define an alias name of Sum_of_year_2016 for the SUM(Qty) value. Note: the year should be fetched from the tbl_Years table. My attempt: SELECT SUM(Qty) AS 'Sum_of_year_' + ty.Year FROM

Getting most recent distinct records

北慕城南 提交于 2021-01-27 17:36:51
问题 Considering the following table: User CreatedDateTime Quantity ----- ----------------- -------- Jim 2012-09-19 01:00 1 Jim 2012-09-19 02:00 5 Jim 2012-09-19 03:00 2 Bob 2012-09-19 02:00 2 Bob 2012-09-19 03:00 9 Bob 2012-09-19 05:00 1 What query would return the most recent rows (as defined by CreatedDateTime ) for each User , so that we could determine the associated Quantity . i.e. the following records User CreatedDateTime Quantity ----- ----------------- -------- Jim 2012-09-19 03:00 2 Bob