tsql

exclude all rows for an ID if 1 row meets condition

一个人想着一个人 提交于 2021-01-27 19:29:26
问题 I am trying to select certain customers from a contacts table if they do not have a guardian listed. ClientId | ContactId | Guardian 123 | 1 | Y 123 | 2 | N 123 | 3 | N 456 | 4 | N 456 | 5 | N 456 | 6 | N Desired output: ClientId | ContactId | Guardian 456 | 4 | N 456 | 5 | N 456 | 6 | N So my goal is that Client 456 would show up in my query results, but not Client 123. I have written the following: select * from Contacts where Guardian <> (case when Guardian = 'Y' then Guardian else '' end)

Get the column names matching a multiple column search

笑着哭i 提交于 2021-01-27 18:15:41
问题 For an application, I need to show search results based upon multiple columns. Searching in multiple columns is not that hard, but I also want to know which column(s) the search query matches the text. Imagine following 'Blabla' table: --------------------------------------------------------------------------------------- | column1 | column2 | column 3 | --------------------------------------------------------------------------------------- | Animals are beautiful | Some text regarding music

Find ID of parent where all children exactly match

两盒软妹~` 提交于 2021-01-27 14:50:37
问题 The Scenario Let's suppose we have a set of database tables that represent four key concepts: Entity Types (e.g. account, client, etc.) Entities (e.g. instances of the above Entity Types) Cohorts (a named group) Cohort Members (the Entities that form up the membership of a Cohort) The rules around Cohorts are: A Cohort always has at least one Cohort Member. A Cohorts Members must be unique to that Cohort (i.e. Entity 5 cannot be a member of Cohort 3 twice, though it could be a member of

t-sql Different datatype possible in a case?

旧街凉风 提交于 2021-01-27 11:44:39
问题 I have this query SELECT CASE WHEN dbo.CFE_PPHY.P77 IS NOT NULL OR dbo.CFE_PPHY.P77 <>'' THEN MONTH(dbo.CFE_PPHY.P77) WHEN dbo.CFE_PPHY.P70 IS NOT NULL OR dbo.CFE_PPHY.P70 <>'' THEN MONTH(dbo.CFE_SERVICE_EVTS.C10_2) ELSE COALESCE(CONVERT(VARCHAR,dbo.CFE_PPHY.P77)+ CONVERT(VARCHAR,dbo.CFE_SERVICE_EVTS.C10_2),'toto') END AS CFELiasse_DateEffetEIRL_MM_N FROM CFE_PPHY LEFT JOIN CFE_SERVICE_EVTS ON CFE_PPHY.colA = CFE_SERVICE_EVTS.colB The ELSE part is giving me headaches. The columns CFE_PPHY.P77

SQL pivot to flatten rows into columns

感情迁移 提交于 2021-01-27 08:55:43
问题 I have a source table that looks like this: +--------------+----------+------+------------+-----------+ | vehicleindex | parentid | year | make | model | +--------------+----------+------+------------+-----------+ | 1 | 1 | 2007 | TOYOTA | SIENNA LE | | 2 | 1 | 2005 | VOLKSWAGEN | JETTA GLS | +--------------+----------+------+------------+-----------+ I'd like to select from this table such that the output looks like this: +-------+--------+-----------+-------+------------+-----------+ |

Timestamp discrepancies between SQL server and Java

不打扰是莪最后的温柔 提交于 2021-01-27 07:54:16
问题 I need to replicate a simple procedure from Java code to SQL Server stored procedure. It will go into a SQL Azure db in production, but I'm testing it against my local SQL Express 12 install. A part of this stored procedure is to concatenate some values into a string. This is my example Java code: import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import com.google.common.base.Strings; public static String concat() { /

Timestamp discrepancies between SQL server and Java

。_饼干妹妹 提交于 2021-01-27 07:53:09
问题 I need to replicate a simple procedure from Java code to SQL Server stored procedure. It will go into a SQL Azure db in production, but I'm testing it against my local SQL Express 12 install. A part of this stored procedure is to concatenate some values into a string. This is my example Java code: import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import com.google.common.base.Strings; public static String concat() { /

Generate list of new unique random numbers in T-SQL

倖福魔咒の 提交于 2021-01-27 07:38:23
问题 I need a stored procedure to generate @n records, each with a unique random 8 digit number. This number must not be incremental and must not exist already in the table. CREATE TABLE Codes ( ID UNIQUEIDENTIFIER PRIMARY KEY, Code INT, CONSTRAINT UQ_Code UNIQUE(Code) ); I can generate random numbers: DECLARE @min int = 0, @max int = 99999999, @n INT = 100; SELECT TOP (@n) FLOOR(CAST(CRYPT_GEN_RANDOM(4) AS BIGINT) / 4294967296 * ((@max - @min) + 1)) + @min FROM sys.all_objects s1 CROSS JOIN sys

Sort by most recent but keep together by another ID column

余生颓废 提交于 2021-01-27 07:22:51
问题 I am trying to get some sorting and keep together (not really grouping) working. In my sample data I would like to keep the DealerIDs together, sorted by IsPrimaryDealer DESC, but show the group (ok maybe it is grouping) of dealers by the ones with the most recent entry. Result set 2 is the closest, but Grant and his brother should be displayed as the first two rows, in that order. (Grant should be row 1, Grants Brother row 2 because Grants Brother was the most recently added) DECLARE @temp

Sort by most recent but keep together by another ID column

蓝咒 提交于 2021-01-27 07:22:24
问题 I am trying to get some sorting and keep together (not really grouping) working. In my sample data I would like to keep the DealerIDs together, sorted by IsPrimaryDealer DESC, but show the group (ok maybe it is grouping) of dealers by the ones with the most recent entry. Result set 2 is the closest, but Grant and his brother should be displayed as the first two rows, in that order. (Grant should be row 1, Grants Brother row 2 because Grants Brother was the most recently added) DECLARE @temp