Forum Widgets
Latest Discussions
Help 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 timelineSolvedKenny_GuaNov 29, 2024Copper Contributor54Views0likes6CommentsSQL 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. thanksSolvedsukiNov 20, 2024Copper Contributor77Views0likes5CommentsError 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, NaveenSolvednaveen73Nov 16, 2024Copper Contributor68Views0likes5CommentsError 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.SolvedmohammadjavaheryNov 13, 2024Copper Contributor29Views0likes1CommentHelp 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! ThanksSolvedleerjones85Nov 11, 2024Copper Contributor66Views0likes4Commentsduplicate 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!Solvedpanos2024bNov 07, 2024Copper Contributor32Views0likes2Commentscompare 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, AbhishekSolvedAjaiswal84Oct 25, 2024Copper Contributor167Views0likes2CommentsHow 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..?SolvedGani_TpTSep 24, 2024Copper Contributor252Views0likes3CommentsAttach 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)Solved1711643472Sep 18, 2024Copper Contributor340Views0likes3CommentsMake 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?Solvedfrankno1105Sep 09, 2024Copper Contributor398Views0likes4Comments
Resources
Tags
- Data Warehouse64 Topics
- Integration Services58 Topics
- sql server43 Topics
- Reporting Services41 Topics
- SQL36 Topics
- Business Intelligence35 Topics
- Analysis Services29 Topics
- Business Apps22 Topics
- Analytics18 Topics
- Big Data11 Topics