tsql

SQL query for parent with specific values in child set

无人久伴 提交于 2021-01-28 12:52:39
问题 I am trying to return just the parent where the set of children contain multiple specific records. Given this table: Product State ------- ----- 111 AZ 111 CA 111 UT 222 AZ 222 WA 333 CA I want find the list of Product that have both a AZ child record and a CA child record, even if it also has other records. Which would return ... Product ------- 111 回答1: select product from table where state in ('az','ca') group by product having count(distinct(state)) = 2 select distinct product from table

SQL query for parent with specific values in child set

瘦欲@ 提交于 2021-01-28 12:41:45
问题 I am trying to return just the parent where the set of children contain multiple specific records. Given this table: Product State ------- ----- 111 AZ 111 CA 111 UT 222 AZ 222 WA 333 CA I want find the list of Product that have both a AZ child record and a CA child record, even if it also has other records. Which would return ... Product ------- 111 回答1: select product from table where state in ('az','ca') group by product having count(distinct(state)) = 2 select distinct product from table

Group rows by ID and find max/min(date_from, date_to) with date gaps

こ雲淡風輕ζ 提交于 2021-01-28 11:55:39
问题 I need to group data by id and find max/min (date_from, date_to). But if there is date gap it should be new row. I have following data: SYS_ID ITEM_ID DATE_FROM DATE_TO 1 1 01.01.2019 20.01.2019 1 1 15.01.2019 10.02.2019 1 1 15.02.2019 20.02.2019 1 1 18.02.2019 10.03.2019 1 1 10.03.2019 22.03.2019 1 2 01.01.2019 10.01.2019 1 2 15.01.2019 25.01.2019 Result should be: SYS_ID ITEM_ID DATE_FROM DATE_TO 1 1 01.01.2019 10.02.2019 1 1 15.02.2019 22.03.2019 1 2 01.01.2019 10.01.2019 1 2 15.01.2019 25

Extract substring from a string in SQL Server

萝らか妹 提交于 2021-01-28 11:01:09
问题 I need to extract a part of substring from string which follows as per below. YY_12.Yellow ABC_WSA.Thisone_A SS_4MON.DHHE_A_A I need to extract the string as per below Yellow Thisone DHHE 回答1: You could use something like this: declare @tbl table (col nvarchar(100)); insert @tbl values ('YY_12.Yellow'), ('ABC_WSA.Thisone_A'), ('SS_4MON.DHHE_A_A') select * , charindex('_', col2, 0) , left(col2, case when charindex('_', col2, 0) - 1 > 0 then charindex('_', col2, 0) - 1 else len(col2) end)

How can I get the definition of a view with the correct name?

可紊 提交于 2021-01-28 10:18:33
问题 I have a database with several views that have been manually renamed by users in the past. If I try to get the definition of the view so that I can script its creation elsewhere, the name comes out wrong. I can get a list of databases with "wrong" names behind-the-scenes using: SELECT OBJECT_NAME(object_id), definition FROM sys.sql_modules WHERE convert(nvarchar(200),definition) not like ('%'+OBJECT_NAME(object_id)+'%') Is there any way to get the correct definition of the view, in that it

How can I get the definition of a view with the correct name?

馋奶兔 提交于 2021-01-28 10:17:31
问题 I have a database with several views that have been manually renamed by users in the past. If I try to get the definition of the view so that I can script its creation elsewhere, the name comes out wrong. I can get a list of databases with "wrong" names behind-the-scenes using: SELECT OBJECT_NAME(object_id), definition FROM sys.sql_modules WHERE convert(nvarchar(200),definition) not like ('%'+OBJECT_NAME(object_id)+'%') Is there any way to get the correct definition of the view, in that it

SQL Daily Outstanding Sales, Rolling Aggregate?

烈酒焚心 提交于 2021-01-28 09:08:17
问题 With the below test data I am trying to show how many sales have not been actioned by a colleague, an outstanding sale is any record that is still showing as "New", I have been using the below to identify those. select saleID, count(*) group by saleID having count(*)=1 The trouble I have is that I find it easy to show overall how many are still outstanding to date but I can't figure out how to show how many were outstanding say 3 days ago. The idea is that this will show a trend when charted

T-SQL hierarchy - get breadcrumbs using query

五迷三道 提交于 2021-01-28 08:52:36
问题 I have virtual folder structure saved in database and I want to get the breadcrumbs from the current folder to the root. The data can be unsorted (but better will be sorted) and I want the parent folders of the current folder only. The table definition is: DECLARE Folders TABLE ( FOL_PK INT IDENTITY(1,1) NOT NULL, FOL_Name VARCHAR(200) NOT NULL, FOL_FOL_FK INT NULL -- Foreign key to parent ) And this is my solution: DECLARE @FOL_PK INT = 5 -- Current folder PK DECLARE @breadcrumbs TABLE ( FOL

Must declare the table variable

落花浮王杯 提交于 2021-01-28 08:27:01
问题 I come across an error while writing some SQL statements for data imports. As I am doing data port, I need to declare some temporary table variables. I declared a table variable at the beginning of the file and performed some manipulations (while loops, insertions and updates) on the table variable. Later in the middle of the scripts in another while loop if I access this table variable the script parsing giving below error Must declare the table variable @temptable Appreciate your help. 回答1:

Finding next row in SQL query and deleting it only if previous row matches

♀尐吖头ヾ 提交于 2021-01-28 08:07:46
问题 I have a table like this. |-DT--------- |-ID------| |5/30 12:00pm |10 | |5/30 01:00pm |30 | |5/30 02:30pm |30 | |5/30 03:00pm |50 | |5/30 04:30pm |10 | |5/30 05:00pm |10 | |5/30 06:30pm |10 | |5/30 07:30pm |10 | |5/30 08:00pm |50 | |5/30 09:30pm |10 | I want to remove any duplicate rows only if the previous row has the same ID as the following row. I want to keep the duplicate row with the datetime furthest in the future. For example the above table would look like this. |-DT--------- |-ID---