Recent Discussions
Import from Excel changes Header Names
I have a wide table in Excel with column names like Q1.1, Q1.2, etc. However, when I import this table using SSMS, the column names are renamed to Q1#1#, Q1#2#, and so on. This renaming is visible when you look at Column Mapping before actually importing. Is there a way to prevent this renaming? If not, can I write some code to rename the columns afterward? I have 96 columns in this dataset, which I need to upload monthly. This issue is becoming a nuisance because I want SQL to clean up this dataset before pivoting it and appending it to my final table. Currently, I manually clean up the data in Excel, pivot it, and then upload a tall file with only 4 columns. This method avoids the renaming issue because all the question numbers become values in a column rather than column names.Solved45Views0likes2CommentsCatch Error in a SQL Server Logon Trigger
I have written a Logon trigger in a SQL Server to control logon authorization. ALTER TRIGGER [LOG_TRG_01] ON ALL SERVER WITH EXECUTE AS 'SA' FOR LOGON AS BEGIN if ORIGINAL_LOGIN() = 'sa' begin return; end; if ORIGINAL_LOGIN() = 'OMEGACAEVDEV1' begin -- not allowed to logon rollback; end; -- Insert Trail EXEC [OmegaCoreAudit].[OMEGACA].[P_ACC_UNF_TRAIL] 0, 'trail_details', 'ip', 'server', 'db', 0 ; END GO It does (as expected): OMEGACAEVDEV1 is not allowed to logon OMEGACAEVDEV2 is allowed to logon An audit event is inserted by proc P_ACC_UNF_TRAIL in a table for both users. All three above I want to stay this way ! But I need to have error handling in it, so that in case of whatever error the login is allowed to login - but keeping a record on another table named SYS_ERROR_LOG (with error details). In this trigger I have intentionally provoked an error by "select 10/0" The new trigger is: ALTER TRIGGER [LOG_TRG_02] ON ALL SERVER WITH EXECUTE AS 'SA' FOR LOGON AS BEGIN BEGIN TRY if ORIGINAL_LOGIN() = 'sa' begin return; end; --provoke error select 10/0; if ORIGINAL_LOGIN() = 'OMEGACAEVDEV1' begin -- not allowed to logon rollback; end; -- Insert Trail EXEC [OmegaCoreAudit].[OMEGACA].[P_ACC_UNF_TRAIL] 0, 'trail_details', 'ip', 'server', 'db', 0 ; END TRY BEGIN CATCH Insert into OmegaCoreAudit.OMEGACA.SYS_ERROR_LOG ([LOGIN_USER_NAME], [DB_USER_NAME], [USERHOST], [IP_ADDRESS], [OS_USER], [BG_JOB_ID], [ERROR_DATA]) values ('LOGIN_NAME', 'USER_NAME', 'USER_HOST', 'IP', NULL, NULL, 'ERROR_MESSAGE'); END CATCH END GO In the above code "if ORIGINAL_LOGIN() = 'OMEGACAEVDEV1'" represents a simplified version of a wider authorization process. Problem: CATCH is not working. Both users are not allowed to logon ("Logon failed for login '[user]' due to trigger execution") No record is written on table SYS_ERROR_LOG. I was expecting one for each user. What can I do to fix this problem? best regards AltinSolved146Views0likes13CommentsHelp in Query to calculate the % from last two timelines
I want to compare the FAmt from last recent two lines and if difference is more than 80% then it should be in the result. The group/index keys are A,B,C,D, Edate. Create table #AR (A int, B char(10), C char(10), D char(10), Edate datetime, FAmt decimal (6,2), G char(2)) Insert into #AR values ('AA','CCC','1','E', '2022-01-01',12.60,'2') Insert into #AR values ('AA','CCC','1','E', '2023-01-01',15.80,'2') Insert into #AR values ('AA','CCC','1','E', '2024-01-01',18.60,'2') Insert into #AR values ('BB','DCC','1','E', '2022-01-01',11.40,'2') Insert into #AR values ('BB','DCC','1','E', '2024-01-01',15.60,'2') Insert into #AR values ('CC','DCC','1','E', '2021-01-01',12.60,'2') Insert into #AR values ('CC','GCC','2','E', '2022-01-01',15.60,'2') Insert into #AR values ('CC','GCC','2','E', '2023-04-01',18.60,'2') Insert into #AR values ('CC','GCC','2','E', '2024-04-01',34.80,'2') --Note: This FAmt is more than 80% from last timeline and it should be in the expected result Expected result: A B C D Edate FAmt G Comments CC GCC 2 E 2024-04-01 34.80 2 Current Amount is > 80% from last timelineSolved59Views0likes6CommentsSQL Help : Time difference for events within a specific group that resets based on an anchor event
Hi All, Need help with an SQL query to find the time difference between events that happen in sequence, where an anchor event resets the calculation. Below is the sample data and the expected output. thanksSolved84Views0likes5CommentsError when importing CSV file
Hi, I am new at MS SQL and have the community edition in my local machine. Trying to import a CSV file but getting the following error message: Total Defect Qty:=SUM([Defect Qty]) TITLE: Microsoft SQL Server Management Studio ------------------------------ Error inserting data into table. (Microsoft.SqlServer.Import.Wizard) ------------------------------ ADDITIONAL INFORMATION: Error inserting data into table. (Microsoft.SqlServer.Prose.Import) ------------------------------ The given value '[name of the column]' of type String from the data source cannot be converted to type nvarchar for Column 4 [Label]. (Microsoft.Data.SqlClient) ------------------------------ String or binary data would be truncated in table '[dbo].[name of the file]', column 'Label'. Truncated value: '[name of the column '. (Microsoft.Data.SqlClient) ------------------------------ BUTTONS: OK ------------------------------ Do you think you can help me please? Thanks, NaveenSolved78Views0likes5CommentsError 0x80004005 during SSIS package execution with SSAS source
Hi. I've created a SSIS package utilizing a SSAS source (VS 2022 Developer Edition, SSAS 2019 15.0.35.48). I can confirm that I can connect to my SSAS source through OLE DB connector and run MDX queries: When I run the package, "Connection Test" step passes (it runs a simple MDX query against the SSAS source to ensure we can connect to source): But data flow task fails on OLE DB Source as follows: My problem is that there is nothing in error message which can help me where is the exact problem. I've seen many posts on this problem, but none of them has worked for me as each focus on a part of error message following the above message, which doesn't exist in my case (e.g. "Communication link failure" or "TCP Provider: The specified network name is no longer available"). Please help. Thanks in advance.Solved31Views0likes1CommentHelp with SQL Code
Hi, I have a table called FACT_DELAYS which contains the following columns: ID, START_DATE, END_DATE, FILTERED_MINUTES If an event is still active then END_DATE IS NULL. Example Data: 1, 20/10/24, null, 25 2, 07/11/24, null, 67 3, 02/08/23, 10/11/24, 43 4, 01/01/22, null, 20 The problem I have got is that it gives me an as of now value when I run the sql. I want to be able to dynamically show the previous 7 days and if a delay covers multiple days then it shows up in each day. My desired output would be: DATE, COUNT(FILTERED_MINUTES) 11/11/24, 3 10/11/24, 4 09/11/24, 4 08/11/24, 4 07/11/24, 4 06/11/24, 3 05/11/24, 3 Any Help will be much appreciated! ThanksSolved68Views0likes4Commentsduplicate nodes for XML Auto
Hi, I have this xml in SQL Server 2022. select cust.cust_Id, id, timestamp, uid, [user], visible, [version], changeset, [action] , (SELECT distinct ref FROM [nd] as nd where nd.cust_Id = cust.cust_Id and tag.cust_Id = ND.cust_Id FOR XML raw('nd'), type ), k , v from cust as cust join [dbo].[nd] as nd on ND.cust_Id = cust.cust_Id join cust_tags as tag on tag.cust_Id = cust.cust_Id where cust.cust_Id=23165 for xml auto and it gives me this result. As you can see it get duplicates. Apart from the dups in <tag> it''s fine. <cust cust_Id="23165" id="1132454103" timestamp="2023-01-16T14:25:54" uid="5612131" user="k" visible="1" version="1" changeset="131338301"> <nd ref="10239832277" /> <nd ref="10559433751" /> <nd ref="10559433752" /> <nd ref="10559433753" /> <tag k="highcust" v="residential" /> <tag k="highcust" v="residential" /> <tag k="highcust" v="residential" /> <tag k="highcust" v="residential" /> <tag k="name" v="Artemis" /> <tag k="name" v="Artemis" /> <tag k="name" v="Artemis" /> <tag k="name" v="Artemis" /> <tag k="source" v="maxar" /> <tag k="source" v="maxar" /> <tag k="source" v="maxar" /> <tag k="source" v="maxar" /> </cust> Now I have modified it like this: select cust.cust_Id, id, timestamp, uid, [user], visible, [version], changeset, [action], (SELECT ref FROM [nd] as nd where nd.cust_Id = cust.cust_Id FOR XML raw('nd'), type ) , (SELECT k , v FROM cust_tags as tag where tag.cust_Id = cust.cust_Id FOR XML raw('tag'), TYPE ) from cust as cust join cust_tags as tag on tag.cust_Id = cust.cust_Id join [GIS].[dbo].[nd] as nd on ND.cust_Id = cust.cust_Id for xml auto This gives me the structure I want but the node appears multiple times as a duplicate , 5+ times due to the joins <cust cust_Id="23165" id="1132454103" timestamp="2023-01-16T14:25:54" uid="5612131" user="k" visible="1" version="1" changeset="131338301"> <nd ref="10239832277" /> <nd ref="10559433751" /> <nd ref="10559433752" /> <nd ref="10559433753" /> <tag k="highcust" v="residential" /> <tag k="name" v="Artemis" /> <tag k="source" v="maxar" /> </cust> <cust cust_Id="23165" id="1132454103" timestamp="2023-01-16T14:25:54" uid="5612131" user="k" visible="1" version="1" changeset="131338301"> <nd ref="10239832277" /> <nd ref="10559433751" /> <nd ref="10559433752" /> <nd ref="10559433753" /> <tag k="highcust" v="residential" /> <tag k="name" v="Artemis" /> <tag k="source" v="maxar" /> </cust> <cust cust_Id="23165" id="1132454103" timestamp="2023-01-16T14:25:54" uid="5612131" user="k" visible="1" version="1" changeset="131338301"> <nd ref="10239832277" /> <nd ref="10559433751" /> <nd ref="10559433752" /> <nd ref="10559433753" /> <tag k="highcust" v="residential" /> <tag k="name" v="Artemis" /> <tag k="source" v="maxar" /> </cust> Any idea how i can get rid of the duplicates. Either in the fist sql or the second. Thank you!Solved32Views0likes2Commentscompare cell 2 with cell 1 and show result
Dear All, I need your help. I have below sample data and want to compare Cell F (column Snap_Id) with previous row value. e.g. row_num 2 should compare with row_num 1 and if it is matching it should say "No" and if it is not matching like row_num 3 compare with row_num 2 then it should say "Yes". Can you please help me to write SQL statement to achieve it. I tried self join but then it is returning me 12 records with incorrect data. The final output I am expecting is as below, Hope you can help me with this. Thanks in advance. Regards, AbhishekSolved169Views0likes2CommentsHow do we get DB Creation Date along with Tables/Views/SPs last access history details
I need below details when i connect to my SQL Server. DB Creation Date Tables/Views/SPs Creation Date Tables/Views/SPs Last used/accessed ==> Need history of one/two years when they accessed connecting to applications host I want all above details in one SQL Query. Is that possible to get all the above details in one single query..?Solved256Views0likes3CommentsAttach a database containing file streams
My original database environment has a problem, can not be backed up, fortunately there are mdf, ldf and StreamFile files of the database; After I redeployed the database, I found that I could not attach the database. The error is as follows =================================== 服务器 "--------------" 的 附加数据库 失败。 (Microsoft.SqlServer.Smo) ------------------------------ 有关帮助信息,请单击: https://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=17.100.40.0&Evtsrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=附加数据库+Server&LinkId=20476 ------------------------------ 程序位置: 在 Microsoft.SqlServer.Management.Smo.Server.AttachDatabaseWorker(String name, StringCollection files, String owner, AttachOptions attachOptions) 在 Microsoft.SqlServer.Management.Smo.Server.AttachDatabase(String name, StringCollection files) 在 Microsoft.SqlServer.Management.SqlManagerUI.AttachDatabaseData.PrimaryFile.Attach() 在 Microsoft.SqlServer.Management.SqlManagerUI.AttachDatabase.SendDataToServer() =================================== 执行 Transact-SQL 语句或批处理时发生了异常。 (Microsoft.SqlServer.ConnectionInfo) ------------------------------ 程序位置: 在 Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType, Boolean retry) 在 Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType, Boolean retry) 在 Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries, Boolean retry) 在 Microsoft.SqlServer.Management.Smo.Server.AttachDatabaseWorker(String name, StringCollection files, String owner, AttachOptions attachOptions) =================================== 一个或多个文件与数据库的主文件不匹配。如果是尝试附加数据库,请使用正确的文件重试该操作。如果这是现有数据库,则文件可能已损坏,应该从备份进行还原。 出现文件激活错误。物理文件名 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\*****_*****_StreamFile' 可能不正确。请诊断并更正其他错误,然后重试此操作。 无法打开新数据库 '*****_*****'。CREATE DATABASE 中止。 (Framework Microsoft SqlClient Data Provider) ------------------------------ 有关帮助信息,请单击: https://docs.microsoft.com/sql/relational-databases/errors-events/mssqlserver-5173-database-engine-error ------------------------------ 服务器名称: *************** 错误号: 5173 严重性: 16 状态: 3 行号: 1 ------------------------------ 程序位置: 在 Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 在 Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 在 Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 在 Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 在 Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) 在 Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) 在 Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在 Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException) 在 Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType, Boolean retry)Solved341Views0likes3CommentsMake Organization
I have a table with organizational structure. It has an ID column called ID. The table also has a column called MOTHER_ID. This refers to the level (MOR) above in the organizational structure. It also has a row that says which level in the organization the row belongs to. We can have 6 levels. Table looks like this: ID Name MOTHER_ID Level 10020027 Level 1 name NULL 1 10020028 Level 2 name 10020027 2 10020032 Level 3 Name 10020028 3 10020035 Level 4 Name 10020032 4 10020166 Level 5 Name 10020035 5 10020038 Level 6 Name 10020166 6 10020083 Level 3 Name 10020028 3 10020188 Level 4 Name 10020083 4 I want to "flatten it out" so it has this structure: Level 1 ID Level 1 Name Level 2 ID Level 2 Name Level 3 ID Level 3 Name Level 4 ID Level 4 Name Level 5 ID Level 5 Name Level 6 ID Level 6 Name And the result should be like this: Level 1 ID Level 1 Name Level 2 ID Level 2 Name Level 3 ID Level 3 Name Level 4 ID Level 4 Name Level 5 ID Level 5 Name Level 6 ID Level 6 Name 10020027 Level 1 name 10020027 Level 1 name 10020027 Level 1 name 10020027 Level 1 name 10020027 Level 1 name 10020027 Level 1 name 10020027 Level 1 name 10020028 Level 2 name 10020028 Level 2 name 10020028 Level 2 name 10020028 Level 2 name 10020028 Level 2 name 10020027 Level 1 name 10020028 Level 2 name 10020032 Level 3 Name 10020032 Level 3 Name 10020032 Level 3 Name 10020032 Level 3 Name 10020027 Level 1 name 10020028 Level 2 name 10020032 Level 3 Name 10020035 Level 4 Name 10020035 Level 4 Name 10020035 Level 4 Name 10020027 Level 1 name 10020028 Level 2 name 10020032 Level 3 Name 10020035 Level 4 Name 10020166 Level 5 Name 10020166 Level 5 Name 10020027 Level 1 name 10020028 Level 2 name 10020032 Level 3 Name 10020035 Level 4 Name 10020166 Level 5 Name 10020038 Level 6 Name 10020027 Level 1 name 10020028 Level 2 name 10020083 Level 3 Name 10020083 Level 3 Name 10020083 Level 3 Name 10020083 Level 3 Name 10020027 Level 1 name 10020028 Level 2 name 10020083 Level 3 Name 10020188 Level 4 Name 10020188 Level 4 Name 10020188 Level 4 Name TThe rows that are on level 6 should have a different id in each column. But rows that is in higher levels are going to mirror the value on the last level into all the missing levels. Example Level 1 row should mirror Level1 values into all columns. Level 2 rows will look like this (Level 1 on top, then level 2 on the rest): Level 1 ID Level 1 Name Level 2 ID Level 2 Name Level 3 ID Level 3 Name Level 4 ID Level 4 Name Level 5 ID Level 5 Name Level 6 ID Level 6 Name 10020027 Level 1 name 10020028 Level 2 name 10020028 Level 2 name 10020028 Level 2 name 10020028 Level 2 name 10020028 Level 2 name Level 3rows will look like this (Level 1 on top, then level 2 and then level 3 on the rest): Level 1 ID Level 1 Name Level 2 ID Level 2 Name Level 3 ID Level 3 Name Level 4 ID Level 4 Name Level 5 ID Level 5 Name Level 6 ID Level 6 Name 10020027 Level 1 name 10020028 Level 2 name 10020032 Level 3 Name 10020032 Level 3 Name 10020032 Level 3 Name 10020032 Level 3 Name I tried a code like this: WITH OrgCTE AS ( SELECT ID AS Level1_ID, Name AS Level1_Name, ID AS Level2_ID, Name AS Level2_Name, ID AS Level3_ID, Name AS Level3_Name, ID AS Level4_ID, Name AS Level4_Name, ID AS Level5_ID, Name AS Level5_Name, ID AS Level6_ID, Name AS Level6_Name, ID, MOTHER_ID, Level FROM Tabell1 WHERE Level = 1 UNION ALL SELECT c.Level1_ID, c.Level1_Name, CASE WHEN p.Level = 2 THEN p.ID ELSE c.Level2_ID END, CASE WHEN p.Level = 2 THEN p.Name ELSE c.Level2_Name END, CASE WHEN p.Level = 3 THEN p.ID ELSE c.Level3_ID END, CASE WHEN p.Level = 3 THEN p.Name ELSE c.Level3_Name END, CASE WHEN p.Level = 4 THEN p.ID ELSE c.Level4_ID END, CASE WHEN p.Level = 4 THEN p.Name ELSE c.Level4_Name END, CASE WHEN p.Level = 5 THEN p.ID ELSE c.Level5_ID END, CASE WHEN p.Level = 5 THEN p.Name ELSE c.Level5_Name END, CASE WHEN p.Level = 6 THEN p.ID ELSE c.Level6_ID END, CASE WHEN p.Level = 6 THEN p.Name ELSE c.Level6_Name END, p.ID, p.MOTHER_ID, p.Level FROM Tabell1 p INNER JOIN OrgCTE c ON p.MOTHER_ID = c.ID ) SELECT Level1_ID, Level1_Name, Level2_ID, Level2_Name, Level3_ID, Level3_Name, Level4_ID, Level4_Name, Level5_ID, Level5_Name, Level6_ID, Level6_Name FROM OrgCTE ORDER BY Level1_ID, Level2_ID, Level3_ID, Level4_ID, Level5_ID, Level6_ID; But this did not mirror the lowest ID for the row. It just put Level1 ID for the missing levels. Like this: Suggestions?Solved401Views0likes4CommentsUnpatched known vulnerabilities SQL Server 2019 GDR
We are running an installation of SQL Server 2019GDR (version 15.0.2116.2) and I see we have these vulnerable commons-collections-3.2.1.jar-files present in the following 2 locations: C:\Program Files\Microsoft SQL Server\150\DTS\Extensions\Common\Jars C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Extensions\Common\Jars The following CVE's need to be mitigated: -CVE-2015-6420 -CVE-2017-15708 How can we address this? We have installed the latest security updateKB5040986Solved537Views0likes4CommentsDefault SQL Server Connection for SSMS
SQL 2019 - SSMS19.3.4.0 I was always wrongly under the impression that SSMS required a server connection in the Object Explorer to run a script against. We have databases with the same names on 2 servers as we're preparing for migration and I accidentally ran a script on server B, even though there appeared to be no connection open to server B. Only Server A was connected in the object explorer. I was then shocked to find that any new sql script I opened was connected to server B which had been closed out in Object Explorer. What controls the default server for a script when opening via File / Open in SSMS? What is the best way to lock a script to specific server or make it more obvious which server this is being applied to. I may need to get used to looking in the bottom right where it displays the SQL server, but I'd like to make it more fool proof. I see activating SQLCMD Mode on the Query Menu is one option, but I wonder what the downside to this might be such that it is not default behaviour.Solved375Views0likes2Commentshow to calculate rolling 12 months average for 3 years
Hi, I am trying to calculate rolling 12 months average for 3 years for every ID and my result is not what i am expecting. Below is the example of my dataset. --drop table #table1 create table #table1( ID int, MonthYear varchar(50), netvalue_1 float, netvalue_2 float, netvalue_3 float ) insert into #table1 select 1,'006.2021','8000.56','1001.00','1002.00' UNION ALL select 1,'009.2023','2345.67','1010.00','1003.00' UNION ALL select 1,'004.2024','2367.78','1100.00','2000.00' UNION ALL select 1,'006.2024','1234.56','3000.00','1010.00' UNION ALL select 2,'010.2023','3478.56','4000.00','2000.00' UNION ALL select 2,'001.2024','8947.56','2500.00','4500.00' UNION ALL select 2,'001.2021','3000.56','2590.00','4560.00' My code is: select ID,MonthYear, AVG(netvalue_1) OVER (ORDER BY MonthYear ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) , AVG(netvalue_2) OVER (ORDER BY MonthYear ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) , AVG(netvalue_3) OVER (ORDER BY MonthYear ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) from #table1 order by ID,MonthYear firstly, Monthyear is not sorting and avg is not been calculating correctly. Can anyone please let me know what I am missing here?Solved235Views0likes2CommentsCONCAT String with NULL - Changing default behavior of SSMS
We are on Sybase ASE and are in the processing migrating to MS SQL Server. In Sybase ASE select null + 'DBMS' ==> DBMS In MS SQL Server (with SSMS) In SSMS QUERY Options has by default "CONCAT_NULL_YIELDS_NULL" set to ON. select null + 'DBMS' ==> NULL If we override every session with "SET CONCAT_NULL_YIELDS_NULL" to OFF then we get select null + 'DBMS' ==> DBMS We are looking for changing the default behavior of SSMS. Wondering what is the right means of accomplishing this. Though the example here is about SSMS, we want all clients to have CONCAT_NULL_YIELDS_NULL to be turned off. We do not know the default behavior of different drivers yet. Thanks for your help!Solved324Views0likes1CommentPartition rows by id and DateColumn or DateColumn + 1
Hello all. I'm trying to partition out data by patient stay and select the stay with the greatest discharge date. The only thing I'm struggling with is, multiple stays where the admit date is the same or 1 day later have to be treated as the same, and again I take the one with the latest discharge date. SQL server 2016 create table #HospitalStays ( PatientId int ,AdmitDate date ,DischargeDate date ,DaysToFollowUp int ) GO insert into #HospitalStays Values (123456, '2024-08-01', '2024-08-01', 14) ,(123456, '2024-08-02', '2024-08-05', 30) ,(123456, '2024-08-07', '2024-08-08', 30) select patientId ,AdmitDate ,DischargeDate ,DaysToFollowUp ,row_number() over (partition by patientId, AdmitDate order by DischargeDate desc, DaysToFollowUp) as rownum from #HospitalStays So in the above how do I make Admit date either admitDate or dateadd(dd, 1, admitDate) I'm doing it with a cursor, but I have a feeling my architect is not going to want a cursor in the proc. Much appreciated!Solved425Views1like3CommentsTo find the Missing rows
Every CEO should have both 'SEYT' and 'ZZZZ' records in Acc_Personal. I want to find any CEO record if anyone is missing 'ZZZZ' record. See table below. To more clear: if CEO has 'SAYT' in Acc_Personal but not 'ZZZZ' then this record should show in the desired output. This condition should be in the query to grab the correct result. If CEO has both 'SAYT' and 'ZZZZ' in Acc_Personal then this CEO number should not be in the desired output. This condition should be in the query to grab the correct result. CREATE TABLE DBO.TMP (CEO VARCHAR(20), Acc_Personal VARCHAR(20), AMT INT, DATEA DATETIME, STAT VARCHAR(1)) INSERT INTO DBO.TMP VALUES ('10001','SEYT',78, '2024-04-09','N' INSERT INTO DBO.TMP VALUES ('10001','ZZZZ',12, '2024-03-09','N' INSERT INTO DBO.TMP VALUES ('10002','SEYT',45, '2024-06-02','N' INSERT INTO DBO.TMP VALUES ('10002','ZZZZ',55, '2024-07-07','D' INSERT INTO DBO.TMP VALUES ('10003','SEYT',76, '2024-08-09','N' INSERT INTO DBO.TMP VALUES ('10004','SEYT',45, '2024-04-02','C' INSERT INTO DBO.TMP VALUES ('10004','ZZZZ',21, '2024-07-09','N' INSERT INTO DBO.TMP VALUES ('10005','SEYT',57, '2024-04-01','N' INSERT INTO DBO.TMP VALUES ('10006','ZZZZ',59, '2024-04-01','B' INSERT INTO DBO.TMP VALUES ('10006','SEYT',47, '2024-02-01','A' INSERT INTO DBO.TMP VALUES ('10007','SEYT',59, '2024-04-09','N' INSERT INTO DBO.TMP VALUES ('10010','AAAA',59, '2024-04-01','B' INSERT INTO DBO.TMP VALUES ('10010','ZZZZ',47, '2024-02-01','A' Note: CEO-10010 should not be in the result because this CEO has no record of SEYT in Acc_personal. Desired Output ----------------- CEO Acc_personal AMT DATEA STAT 1003 1005 1007Solved567Views0likes4CommentsHelp to find in sequence issue in groups records
Hi, Everygroup_cost has different sequence of PP_Seq but it should not be any gap in the sequence of any group_cost rcords. It should always be increment by 1. I want to see any record/row of any group_cost if it has any issue in pp_seq and not increment by 1. I want to see when the issue of sequence is started then it will be corrected with other next rows. Create table #Cost (group_cost char(10), PP_Seq int, date1 datetime, ST char(2)) insert into #cost ('T1',5,'2023-01-01','A') insert into #cost ('T1',6,'2023-02-01','A') insert into #cost ('T1',7,'2023-03-01','A') insert into #cost ('T2',12,'2023-01-01','A') insert into #cost ('T2',14,'2023-02-01','B') -- This pp_seq is not increment by 1. Should be in output insert into #cost ('T2',15,'2023-03-01','A') insert into #cost ('T3',25,'2023-01-02','A') insert into #cost ('T3',26,'2023-02-01','A') insert into #cost ('T3',27,'2023-03-05','A') insert into #cost ('T5',65,'2023-01-01','A') insert into #cost ('T5',66,'2023-02-06','A') insert into #cost ('T5',67,'2023-03-04','W') insert into #cost ('T5',69,'2023-04-01','A')-- This pp_seq is not increment by 1. Should be in output --Expected result group_cost PP_Seq date1 ST Comments T2 14 2023-02-01 B PP_Seq is not in seq. Also check next rows to correct. T5 69 2023-04-01 A PP_Seq is not in seq. Also check next rows to correct.Solved272Views0likes2Comments
Events
Recent Blogs
- The SQL Tools team is excited to announce the upcoming release of SQL Server Management Studio (SSMS) 21 Preview 1, and the upcoming Private Preview of Copilot in SSMS.Dec 20, 202412KViews22likes24Comments
- The SQL Tools team, in partnership with Visual Studio team, is thrilled to announce the release of SQL Server Management Studio 21 Preview 1, which is based on Visual Studio 2022 (17.13.0 Previe...Dec 19, 202415KViews15likes31Comments