存储过程以删除重复项但保留一些数据

huangapple go评论100阅读模式
英文:

Stored Procedure to remove duplicates but keep some data

问题

I'll provide the translated code portions as requested:

  1. -- SET NOCOUNT ON added to prevent extra result sets from
  2. -- interfering with SELECT statements.
  3. SET NOCOUNT ON;
  4. with PCTE AS
  5. (
  6. SELECT Count(*) as Count, Companies.Id as companyid, Branches.id as branchid, c.name as InventoryCenter
  7. FROM dbo.InventoryItems INNER JOIN
  8. dbo.inventoryCategories on InventoryItems.inventoryCategoryID = inventoryCategories.id Inner JOIN
  9. dbo.InventoryCenters c on inventoryCategories.InventoryCenterId = c.Id INNER JOIN
  10. dbo.Branches ON InventoryItems.BranchId = Branches.Id INNER JOIN
  11. dbo.Companies ON Branches.CompanyId = Companies.Id
  12. WHERE
  13. InventoryItems.Deleted = 'False'AND
  14. Branches.IsDeleted= 'False' AND
  15. Companies.IsDeleted = 'False' AND
  16. IsShownToMembers = 'True' AND
  17. Companies.Id = 20
  18. group by Companies.Id, Branches.id , c.name
  19. )
  20. , CompanySummaryCTE AS
  21. (
  22. select companyid, branchid, [Part Center], [Equipment Center], [Attachment Center], [Powertrain Center], [Salvage Center]
  23. from PCTE
  24. PIVOT (Sum(Count) for InventoryCenter IN ([Part Center], [Equipment Center], [Attachment Center], [Powertrain Center], [Salvage Center] )) as P
  25. )
  26. , cteInvoices AS (
  27. select t.CompanyId, t.FirstRun, t.NextRun, f.DisplayedAs as Frequency
  28. , l.Description as InvoiceLineDescription, l.Quantity as InvoiceLineQuantity, l.DiscountAmount as InvoiceLineDiscount
  29. , l.ServiceId as InvoiceLineServiceId
  30. from RecurringInvoiceTemplates t
  31. inner join RecurringInvoiceFrequencies f on t.RecurringInvoiceFrequencyId = f.Id
  32. inner join RecurringInvoiceLines l on l.RecurringInvoiceTemplateId = t.Id
  33. )
  34. , cteFinal AS (
  35. SELECT Companies.Id as CompanyId, Companies.Name AS CompanyName, p.Name as MembershipType,Branches.Name as BranchName, I.InvoiceLineDescription
  36. FROM
  37. CompanySummaryCTE cte
  38. JOIN Companies ON cte.companyid = companies.Id
  39. JOIN Branches on Branches.CompanyId = Companies.Id
  40. JOIN CompanyProducts cp on companies.id = cp.companyid
  41. JOIN Products p on cp.ProductId = p.Id
  42. LEFT JOIN cteInvoices I on Companies.Id = i.CompanyId
  43. where
  44. Companies.IsDeleted = 0
  45. and Branches.IsDeleted = 0
  46. and p.ProductTypeId = 2
  47. )
  48. --Original data with magic numbers added based on partition functions
  49. SELECT DISTINCT * ,
  50. DENSE_RANK() OVER (PARTITION BY BranchName ORDER BY BranchName, InvoiceLineDescription ) DescriptionGroup,
  51. DENSE_RANK() OVER (PARTITION BY InvoiceLineDescription ORDER BY BranchName) BranchGroup
  52. FROM cteFinal
  53. WHERE 1=1
  54. order by CompanyId
  55. --Insert into
  56. -- Once you have the magic numbers worked out, just filter out the rows you don't want
  57. SELECT * FROM (
  58. SELECT * ,
  59. ROW_NUMBER() OVER (PARTITION BY BranchName ORDER BY BranchName, InvoiceLineDescription ) DescriptionGroup,
  60. ROW_NUMBER() OVER (PARTITION BY InvoiceLineDescription ORDER BY BranchName) BranchGroup
  61. FROM cteFinal
  62. ) F
  63. WHERE F.BranchGroup = 1 OR F.DescriptionGroup = 1
  64. --ORDER BY F.BranchName, F.InvoiceLineDescription
  65. END

Please note that this translation may not include specific variables, object names, or database configurations, as requested.

英文:

I am working on SP to help return a list a records to assist with invoicing for certain types of membership...

(I have cut the SP down to work with just one company until I get it going - there will be multiple MembershipType - this example we are using 'Buyer Membership')

So each company will have multiple branches, and each branch will have 3 records relating to the 'invoice line description' which are currently returned on the SP...

InvoiceLineDescription

  1. Branch Monthly Membership
  2. Buyer Monthly Membership
  3. Carrying Charge

like so:

  1. Company Invoice Line
  2. Id CompanyName MembershipType BranchName Description
  3. 20 DeluxPainting BuyerMemberShip Branch1 Branch MonthlyMembership
  4. 20 DeluxPainting BuyerMemberShip Branch1 Buyer Monthly Membership
  5. 20 Deluxainting BuyerMemberShip Branch1 Carrying Charge
  6. 20 DeluxPainting BuyerMemberShip Branch2 Branch MonthlyMembership
  7. 20 DeluxPainting BuyerMemberShip Branch2 Buyer Monthly Membership
  8. 20 DeluxPainting BuyerMemberShip Branch2 Carrying Charge
  9. 20 DeluxPainting BuyerMemberShip Branch3 Branch MonthlyMembership
  10. 20 DeluxPainting BuyerMemberShip Branch3 Buyer Monthly Membership
  11. 20 DeluxPainting BuyerMemberShip Branch3 Carrying Charge

I want to break this down further...

So only returning one instance of
'Buyer Monthly Membership',(for each company)

1 instance of 'Carrying Charge'(for each company)

and then list 'Branch Monthly Membership' for each different branch

So the above data should change to

  1. Company Invoice Line
  2. Id CompanyName MembershipType BranchName Description
  3. 20 DeluxPainting BuyerMemberShip Branch1 Buyer Monthly Membership
  4. 20 Deluxainting BuyerMemberShip Branch1 Carrying Charge
  5. 20 DeluxPainting BuyerMemberShip Branch1 Branch MonthlyMembership
  6. 20 DeluxPainting BuyerMemberShip Branch2 Branch MonthlyMembership
  7. 20 DeluxPainting BuyerMemberShip Branch3 Branch MonthlyMembership

I just cant seem to get over the line for this final request

(removing Buyer Monthly Membership & Carrying Charge for branch 2 and 3)

Is this even possible?

Any help please?

UPDATE
Thank you for helping me with this... I added in what you suggested and it worked for the first part showing DescriptionGroup and BranchGroup but the second select however is flagging with cteFinal - invalid object name.(your @T) Do I need to declare it globally or how can I get it to use the results?

I tried to put it into a temp variable but then just copied the contents of the SP over to a new table and was able to do a simple select statment...but I would like to get it working as you have shown in your exmaple.. please could you suggest how I can fix the below...

  1. BEGIN
  2. -- SET NOCOUNT ON added to prevent extra result sets from
  3. -- interfering with SELECT statements.
  4. SET NOCOUNT ON;
  5. with PCTE AS
  6. (
  7. SELECT Count(*) as Count, Companies.Id as companyid, Branches.id as branchid, c.name as InventoryCenter
  8. FROM dbo.InventoryItems INNER JOIN
  9. dbo.inventoryCategories on InventoryItems.inventoryCategoryID = inventoryCategories.id Inner JOIN
  10. dbo.InventoryCenters c on inventoryCategories.InventoryCenterId = c.Id INNER JOIN
  11. dbo.Branches ON InventoryItems.BranchId = Branches.Id INNER JOIN
  12. dbo.Companies ON Branches.CompanyId = Companies.Id
  13. WHERE
  14. InventoryItems.Deleted = 'False'AND
  15. Branches.IsDeleted= 'False' AND
  16. Companies.IsDeleted = 'False' AND
  17. IsShownToMembers = 'True' AND
  18. Companies.Id = 20
  19. group by Companies.Id, Branches.id , c.name
  20. )
  21. , CompanySummaryCTE AS
  22. (
  23. select companyid, branchid, [Part Center], [Equipment Center], [Attachment Center], [Powertrain Center], [Salvage Center]
  24. from PCTE
  25. PIVOT (Sum(Count) for InventoryCenter IN ([Part Center], [Equipment Center], [Attachment Center], [Powertrain Center], [Salvage Center] )) as P
  26. )
  27. , cteInvoices AS (
  28. select t.CompanyId, t.FirstRun, t.NextRun, f.DisplayedAs as Frequency
  29. , l.Description as InvoiceLineDescription, l.Quantity as InvoiceLineQuantity, l.DiscountAmount as InvoiceLineDiscount
  30. , l.ServiceId as InvoiceLineServiceId
  31. from RecurringInvoiceTemplates t
  32. inner join RecurringInvoiceFrequencies f on t.RecurringInvoiceFrequencyId = f.Id
  33. inner join RecurringInvoiceLines l on l.RecurringInvoiceTemplateId = t.Id
  34. )
  35. , cteFinal AS (
  36. SELECT Companies.Id as CompanyId, Companies.Name AS CompanyName, p.Name as MembershipType,Branches.Name as BranchName, I.InvoiceLineDescription
  37. FROM
  38. CompanySummaryCTE cte
  39. JOIN Companies ON cte.companyid = companies.Id
  40. JOIN Branches on Branches.CompanyId = Companies.Id
  41. JOIN CompanyProducts cp on companies.id = cp.companyid
  42. JOIN Products p on cp.ProductId = p.Id
  43. LEFT JOIN cteInvoices I on Companies.Id = i.CompanyId
  44. where
  45. Companies.IsDeleted = 0
  46. and Branches.IsDeleted = 0
  47. and p.ProductTypeId = 2
  48. )
  49. --Original data with magic numbers added based on partition functions
  50. SELECT DISTINCT * ,
  51. DENSE_RANK() OVER (PARTITION BY BranchName ORDER BY BranchName, InvoiceLineDescription ) DescriptionGroup,
  52. DENSE_RANK() OVER (PARTITION BY InvoiceLineDescription ORDER BY BranchName) BranchGroup
  53. FROM cteFinal
  54. WHERE 1=1
  55. order by CompanyId
  56. --Insert into
  57. -- Once you have the magic numbers worked out, just filter out the rows you don't want
  58. SELECT * FROM (
  59. SELECT * ,
  60. ROW_NUMBER() OVER (PARTITION BY BranchName ORDER BY BranchName, InvoiceLineDescription ) DescriptionGroup,
  61. ROW_NUMBER() OVER (PARTITION BY InvoiceLineDescription ORDER BY BranchName) BranchGroup
  62. FROM cteFinal
  63. ) F
  64. WHERE F.BranchGroup = 1 OR F.DescriptionGroup = 1
  65. --ORDER BY F.BranchName, F.InvoiceLineDescription
  66. END

答案1

得分: 1

以下是已翻译的内容:

这是类似于您所寻找的东西。使用这种方法,您可以将分组简化为更简单的表达式。这适用于您的特定情况,但您可能需要调整它以适应您的实际数据,包括为要保留在其他分支中的“Description”添加标识符。在此逻辑中,它只保留最后一个字母顺序的描述。

-- 请注意,在您的问题中提供此类数据可以加快答案速度
DECLARE
@T TABLE (ID INT, CompanyName VARCHAR(100), MembershipType VARCHAR(100), BranchName VARCHAR(100), Description VARCHAR(100))

INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch1','Branch MonthlyMembership')
INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch1','Buyer Monthly Membership')
INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch1','Carrying Charge')
INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch2','Branch MonthlyMembership')
INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch2','Buyer Monthly Membership')
INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch2','Carrying Charge')
INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch3','Branch MonthlyMembership')
INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch3','Buyer Monthly Membership')
INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch3','Carrying Charge')

-- A. Original data with magic numbers added based on partition functions
-- THIS IS FOR ILLUSTRATION ONLY. It is not needed for the final answer
SELECT * ,
ROW_NUMBER() OVER (PARTITION BY BranchName ORDER BY BranchName, Description ) DescriptionGroup,
ROW_NUMBER() OVER (PARTITION BY Description ORDER BY BranchName) BranchGroup
FROM @T

-- B. Once you have the magic numbers worked out, just filter out the rows you don't want
SELECT * FROM (
SELECT * ,
ROW_NUMBER() OVER (PARTITION BY BranchName ORDER BY BranchName, Description ) DescriptionGroup,
ROW_NUMBER() OVER (PARTITION BY Description ORDER BY BranchName) BranchGroup
FROM @T
) F
WHERE F.BranchGroup = 1 OR F.DescriptionGroup = 1
ORDER BY F.BranchName, F.Description

英文:

Here is something like what you area after. Using this approch you boil grouping down into much simpler expressions. This works for your particaular case but you'll likely need to fiddle with it for your real data, including adding an identifier for which "Description" you want to keep for remaining branches. In this logic, it just keeps the last alphabetical one.

  1. -- Note that providing data like this in your question speeds up the answer
  2. DECLARE
  3. @T TABLE (ID INT, CompanyName VARCHAR(100), MembershipType VARCHAR(100), BranchName VARCHAR(100), Description VARCHAR(100))
  4. INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch1','Branch MonthlyMembership')
  5. INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch1','Buyer Monthly Membership')
  6. INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch1','Carrying Charge')
  7. INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch2','Branch MonthlyMembership')
  8. INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch2','Buyer Monthly Membership')
  9. INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch2','Carrying Charge')
  10. INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch3','Branch MonthlyMembership')
  11. INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch3','Buyer Monthly Membership')
  12. INSERT INTO @T VALUES(20,'DeluxPainting','BuyerMemberShip','Branch3','Carrying Charge')
  13. -- A. Original data with magic numbers added based on partition functions
  14. -- THIS IS FOR ILLUSTRATION ONLY. It is not needed for the final answer
  15. SELECT * ,
  16. ROW_NUMBER() OVER (PARTITION BY BranchName ORDER BY BranchName, Description ) DescriptionGroup,
  17. ROW_NUMBER() OVER (PARTITION BY Description ORDER BY BranchName) BranchGroup
  18. FROM @T
  19. -- B. Once you have the magic numbers worked out, just filter out the rows you don't want
  20. SELECT * FROM (
  21. SELECT * ,
  22. ROW_NUMBER() OVER (PARTITION BY BranchName ORDER BY BranchName, Description ) DescriptionGroup,
  23. ROW_NUMBER() OVER (PARTITION BY Description ORDER BY BranchName) BranchGroup
  24. FROM @T
  25. ) F
  26. WHERE F.BranchGroup = 1 OR F.DescriptionGroup = 1
  27. ORDER BY F.BranchName, F.Description

huangapple
  • 本文由 发表于 2023年6月13日 10:23:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定