sql-server-2005

Extract Unique Pairs from Table Containing Reciprocal Relationships

吃可爱长大的小学妹 提交于 2019-12-25 04:33:11
问题 Consider a SQL Server 2005 database with a bunch of data on a lot of people, a large chunk of whom are married . To track relationships among people, a Relationship table exists that serves to link the ID of one person to the ID of another. A Relationship Type on each Relationship record serves to indicates the type of relationship exists between the two people in question (duh): "is married to", "is son of", "is daughter of", etc. For married people, there are TWO relationship records in the

Update query on millions of rows fills the transaction log

一笑奈何 提交于 2019-12-25 04:29:41
问题 I need to update millions of rows as part of my next release, but doing so fills the transaction log and fails. I have a few ideas but I'm not a SQL expert so I'm sure there will be gotchas that I'm not aware of. Pertinent points: I need to hand a script over to the operations team so need a T-SQL method with no manual intervention. Apparently the transaction log gets recycled every 15 minutes. (I've thought about writing a loop with a try-catch with WAITFOR DELAY '00:15:00' in the catch

Sql convert data into one row from multiple columns

与世无争的帅哥 提交于 2019-12-25 04:28:20
问题 I have a table and I want to convert it into one row for each customer number something like this: Can someone point me to the right place , example where I can do similar thing. 回答1: You need to use PIVOT. Something like the following query should help. SELECT CustomerNumber, CASE WHEN [1] > 0 THEN 'Y' ELSE 'N' END [Sony], CASE WHEN [2] > 0 THEN 'Y' ELSE 'N' END [LG], CASE WHEN [3] > 0 THEN 'Y' ELSE 'N' END [Samsung] FROM (SELECT Product1, CustomerNumber FROM Table) AS SourceTable PIVOT (

Sql query to join/union two tables

情到浓时终转凉″ 提交于 2019-12-25 04:11:50
问题 I am looking for some suggestions to build a query to join table A & table B to get the output as table C. It looks like I need to be able to union the two tables and build a cross tab query. The data shown below are just examples as the dates in the two tables would be changing every month. Note: the dates in the two tables will not overlap. For example, below Table B will never have FutureDates as June or July. and vice-versa Table A Product Location HistoryDate HistorySales ... more

How to Re Format DateTime in Sql with Ceiling or Flooring method?

你。 提交于 2019-12-25 04:10:52
问题 i need this table look please TABLE2 Table1 VisitingCount Date 1-------------------15:09 3-------------------15:10 7-------------------15:15 1-------------------15:39 2-------------------15:40 3-------------------15:47 How can i change this table below table Table2 VisitingCount Date 11-------------------15:00-15:30 6-------------------15:30-16:00 so, i writed some sql user-defined functions look below please: create FUNCTION [dbo].[fn_GetActivityLogsArranger] ( @time AS nvarchar(max) )

Including NULL results after join

允我心安 提交于 2019-12-25 04:07:11
问题 I am trying to do a report which shows all payments we have received and for the report I have to show names of patients who pay, but this table also contains checks from payers (insurance companies) and after I do a join all of the payers are excluded. I have tried every join version I know left, right, outer, inner, and combinations of the two. SQL Server 2005. UPDATE The line that is causing the left join not to work is select p.*, max(episode_id) over (partition by patient_id) as maxei

Multiple Criteria In Case Statement [duplicate]

落爺英雄遲暮 提交于 2019-12-25 04:06:52
问题 This question already has answers here : How do I do multiple CASE WHEN conditions using SQL Server 2008? (8 answers) Closed 5 years ago . I know you can do a 1 to 1 relationship in a case statement like such Select case userID when '12345' Then '12' Else userID End from userInformation and I don't think you can add 2 criteria in here, but is it possible (if not how could i add a 2nd criteria) to say Select case userID when '12345' And status is 'Active' Then '12' Else userID End from

SQL: Turn a subquery into a join: How to refer to outside table in nested join where clause?

匆匆过客 提交于 2019-12-25 03:55:15
问题 I am trying to change my sub-query in to a join where it selects only one record in the sub-query. It seems to run the sub-query for each found record, taking over a minute to execute: select afield1, afield2, ( select top 1 b.field1 from anothertable as b where b.aForeignKey = a.id order by field1 ) as bfield1 from sometable as a If I try to only select related records, it doesn't know how to bind a.id in the nested select. select afield1, afield2, bfield1 from sometable a left join ( select

SQL: Turn a subquery into a join: How to refer to outside table in nested join where clause?

喜你入骨 提交于 2019-12-25 03:55:03
问题 I am trying to change my sub-query in to a join where it selects only one record in the sub-query. It seems to run the sub-query for each found record, taking over a minute to execute: select afield1, afield2, ( select top 1 b.field1 from anothertable as b where b.aForeignKey = a.id order by field1 ) as bfield1 from sometable as a If I try to only select related records, it doesn't know how to bind a.id in the nested select. select afield1, afield2, bfield1 from sometable a left join ( select

MS SQL Server 2005 Express x86 - its port is unreachable - help

℡╲_俬逩灬. 提交于 2019-12-25 03:54:45
问题 I have just installed the MS SQL Server 2005 Express and, of course, I am trying to connect it with my test app but the thing is ... for some reason I cannot connect it I have already checked its port with the CP "netstat -an" but the outcoming list does not contain any 1433 port at all :( The SQL service is running I could check it with the OS Services but its port is unavailable :( I have installed MS SQL Server 2005 Express Management Studio but for some reason it connected the MS SQL