Guest User

Untitled

a guest
Jul 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. SELECT
  2. [addresstype].name As [Type],
  3. [address].city As [City], address.statecode As [State],
  4. [address].postalcode As [Zip],
  5. [address].addressid As [Id]
  6. FROM
  7. [address]
  8. LEFT OUTER JOIN [contact_address] ON [address].addressid = [contact_address].addressid
  9. LEFT OUTER JOIN [addresstype] ON [addresstype].addresstypeid = [contact_address].addresstypeid
  10. LEFT OUTER JOIN [clientcontact] ON dbo.contact_address.contactid = [clientcontact].contactid
  11. WHERE
  12. [contact_address].contactid = 12538
  13. ORDER BY
  14. [address].name, [address].statecode, [address].city
  15.  
  16. SELECT
  17. dbo.clientcontact.clientcontactid ,
  18. dbo.clientcontact.clientid ,
  19. dbo.clientcontact.contactid
  20. --[addresstype].name As [Type],
  21. --[address].city As [City], address.statecode As [State],
  22. --[address].postalcode As [Zip],
  23. --[address].addressid As [Id]
  24. FROM
  25. [address]
  26. LEFT OUTER JOIN [contact_address] ON [address].addressid = [contact_address].addressid
  27. LEFT OUTER JOIN [addresstype] ON [addresstype].addresstypeid = [contact_address].addresstypeid
  28. right JOIN [clientcontact] ON dbo.contact_address.contactid = [clientcontact].contactid
  29. WHERE
  30. [contact_address].contactid = 12538
  31. ORDER BY
  32. [address].name, [address].statecode, [address].city
  33.  
  34. SELECT
  35. client_addressexternal.address_table_type As [Address Record Type],
  36. addresstype.name As [Type],
  37. CASE WHEN client_addressexternal.address_table_type = 'CLIENT Address' THEN '<a href="/ClientServices/ManageClients/ClientDetails/ClientAddresses.aspx?Id=' + CONVERT(VARCHAR,client_addressexternal.addressid) + '&ClientId=' + CONVERT(VARCHAR,client_addressexternal.client_id) + '&SourceClientId=14103">' + address.name + '</a>' + '<br /><b>Client Name:</b> ' + client_addressexternal.client_full_name ELSE client_addressexternal.contact_full_name END As [Address Name],
  38. dbo.limssubstring(dbo.LIMSNullString(address1) + '<br />' + dbo.LIMSNullString(address2), 84) As [Address],
  39. address.city As [City], address.statecode As [State],
  40. address.postalcode As [Zip],
  41. CASE client.clientid WHEN 14103 THEN '' ELSE client.name END As [From Parent Client],
  42. address.addressid As [Id]
  43. FROM
  44. address
  45. JOIN (
  46.  
  47. SELECT client_address.clientid, client_address.addressid, client_address.addresstypeid, depth, 'CLIENT Address' AS 'address_table_type', '' as 'contact_full_name', client.name as 'client_full_name', client_address.clientid as 'client_id', '' as 'contact_id'
  48. FROM dbo.fnClientRelatives(14103, 0, 1, 0) relatives
  49. inner join client_address on client_address.clientid = relatives.clientid
  50. LEFT OUTER JOIN client ON relatives.clientid = dbo.client.clientid
  51.  
  52. UNION
  53.  
  54. SELECT clientcontact.clientid, contact_address.addressid, contact_address.addresstypeid, 999 [depth], 'CONTACT Address' AS 'address_table_type', address.name + '<br /><b>Contact Name:</b> ' + LTRIM(RTRIM(ISNULL(contact.firstname, '') + ISNULL(' ' + contact.middleinitial + ' ', ' ') + ISNULL(contact.lastname, ''))), '' as 'client_full_name', clientcontact.clientid as 'client_id', clientcontact.contactid as 'contact_id'
  55. from clientcontact
  56. inner join contact_address ON contact_address.contactid=clientcontact.contactid and clientcontact.clientid=14103
  57. LEFT OUTER JOIN [contact] ON [clientcontact].contactid = [contact].contactid
  58. LEFT OUTER JOIN [address] ON contact_address.addressid = address.addressid
  59.  
  60. ) AS client_addressexternal ON client_addressexternal.addressid = address.addressid
  61. JOIN client ON client.clientid = client_addressexternal.clientid
  62. JOIN addresstype on addresstype.addresstypeid = client_addressexternal.addresstypeid
  63. ORDER BY
  64. depth,address.statecode, address.city, address.name
  65.  
  66. GO
  67. /****** Object: UserDefinedFunction [dbo].[fnClientRelatives] Script Date: 07/29/2011 12:48:24 ******/
  68. SET ANSI_NULLS OFF
  69. GO
  70. SET QUOTED_IDENTIFIER ON
  71. GO
  72. --your basic recursive tree searcher.
  73. --childrennotparents = 1 means you'll get children. = 0 means you'll get parents
  74. --@recursive = 1 means it finds all children, grandchildren, etc... or whatever
  75. -- The depth is the base level to start incrementing each level, if set to zero, the @clientid will also be part of the results
  76. ALTER FUNCTION [dbo].[fnClientRelatives]
  77. (
  78. @clientId INT,
  79. @childrenNotParents BIT,
  80. @recursive bit,
  81. @depth int
  82. )
  83. RETURNS @clientids TABLE (clientid INT primary key clustered, depth int)
  84. AS
  85. begin
  86.  
  87. -- Add the parent client id if the depth is zero
  88. if @depth = 0
  89. begin
  90. INSERT INTO @clientids VALUES (@clientid, @depth)
  91. end
  92. set @depth = @depth + 1
  93.  
  94. IF @childrenNotParents = 1
  95. begin
  96. DECLARE clientids CURSOR FOR
  97. SELECT clientid
  98. FROM client
  99. where parentclientid = @clientId
  100. END--/if childrennotparents
  101. ELSE--if not childrennotparents
  102. BEGIN
  103. DECLARE clientids CURSOR FOR
  104. SELECT parentclientid
  105. FROM client
  106. where clientid = @clientid
  107. END--/if not childrennotparents
  108.  
  109. OPEN clientids
  110. DECLARE @nextClientID INT
  111. FETCH clientids INTO @nextClientID
  112. --@nextClientID may be null if we're loading parents, and the
  113. --current client has null for a parent id.
  114. WHILE @@FETCH_STATUS = 0 AND @nextClientID IS NOT NULL
  115. BEGIN
  116. INSERT INTO @clientids
  117. VALUES (@nextclientid, @depth)
  118.  
  119. IF @recursive = 1
  120. BEGIN
  121. INSERT INTO @clientids
  122. SELECT * FROM dbo.fnClientRelatives(@nextclientid, @childrenNotParents, @recursive, @depth)
  123. END--IF @recursive = 1
  124. FETCH clientids INTO @nextclientid
  125. END--WHILE @@FETCH_STATUS = 0
  126.  
  127. CLOSE clientids
  128. DEALLOCATE clientids
  129.  
  130. RETURN
  131. END--/IssueRelatives
  132.  
  133. GROUP BY [addresstype].name,
  134. [address].city,
  135. [adress].statecode,
  136. [address].postalcode,
  137. [address].addressid
  138.  
  139. SELECT DISTINCT
  140. [addresstype].name As [Type],
  141. [address].city As [City], address.statecode As [State],
  142. [address].postalcode As [Zip],
  143. [address].addressid As [Id],
  144. [address].name
  145. FROM
  146. [address]
  147. LEFT OUTER JOIN [contact_address] ON [address].addressid = [contact_address].addressid
  148. LEFT OUTER JOIN [addresstype] ON [addresstype].addresstypeid = [contact_address].addresstypeid
  149. LEFT OUTER JOIN [clientcontact] ON dbo.contact_address.contactid = [clientcontact].contactid
  150. WHERE
  151. [contact_address].contactid = 12538
  152. ORDER BY
  153. [address].name, [address].statecode, [address].city
  154.  
  155. GROUP BY ([addresstype].name,[address].city,
  156. address.statecode,[address].postalcode,[address].addressid)
  157.  
  158. SELECT DISTINCT
  159. [address].name as [Address],
  160. [addresstype].name As [Type],
  161. [address].city As [City], address.statecode As [State],
  162. [address].postalcode As [Zip],
  163. [address].addressid As [Id]
  164. FROM
  165. [address]
  166. LEFT OUTER JOIN [contact_address] ON [address].addressid = [contact_address].addressid
  167. LEFT OUTER JOIN [addresstype] ON [addresstype].addresstypeid = [contact_address].addresstypeid
  168. LEFT OUTER JOIN [clientcontact] ON dbo.contact_address.contactid = [clientcontact].contactid
  169. WHERE
  170. [contact_address].contactid = 12538
  171. ORDER BY
  172. [address].name, [address].statecode, [address].city
Add Comment
Please, Sign In to add comment