Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 481.6k

How to restrict duplicate values in a column in sql server record?

May 2 2024 6:05 AM

Hi Team

I have duplicate record from my table, reason being i notice its because there is more than one FarmName with different CustomerName uses same tripsheetno that is why its doing that. How can i restrict this so there could be unique trip sheet number per each FarmName?

USE [Batcher]
GO
/****** Object:  StoredProcedure [dbo].[GetTripWeightReport]    Script Date: 2024/04/30 15:19:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetTripWeightReport] --'2024-04-24', '2024-04-25'

(
@StartDate Date, @EndDate Date
)
AS



BEGIN


SELECT  DISTINCT 
    t.TripsheetNo, 
    t.ProductWeight AS 'TripSheetReport Weight'
	,
    cu.Customer,
	f.FarmID
INTO #FINISHEDPRODUCT
FROM [dbo].[Tripsheet] t
LEFT JOIN  [dbo].[WeighbridgeTicket] wt ON t.TripsheetNo = wt.trip
LEFT JOIN  [dbo].[Customer] cu ON cu.CustomerID = wt.CustomerID
LEFT JOIN [dbo].[Farm] f ON f.FarmID = cu.Farmid
WHERE 
    CONVERT(DATE, t.Tripdate, 103) BETWEEN @StartDate AND @EndDate And topseal <> '' and bottomseal <> ''


SELECT DISTINCT *  
INTO #SALES   
FROM
(
    SELECT DISTINCT
        ProdDay =  CASE 
                        WHEN CAST(WT.LoadedTime AS Time) > '00:00:00' AND CAST(WT.LoadedTime AS Time) < '06:00:00' 
                        THEN DATEADD(day, -1, Convert(DATE, WT.LoadedDate, 103)) 
                        ELSE Convert(DATE, WT.LoadedDate, 103) 
                    END,
        tps.TripsheetNo,
        CASE 
            WHEN tps.TripsheetNo = 229080 AND WT.LoadedDate = @StartDate THEN 28020 
            ELSE SUM(tk.productweight) 
        END AS 'SalesReport Weight', 
        WT.LoadedTime,
        WT.LoadedDate  
    FROM [Batcher].[dbo].[Tripsheet] tps 
    INNER JOIN [Batcher].[dbo].[WeighbridgeTicket] tk on tps.tripsheetno = tk.trip
    LEFT JOIN (
        SELECT trip, 
               LoadedDate = MIN(WeighTime), 
               LoadedTime = SUBSTRING(CONVERT(VARCHAR, MIN(Weightime), 108),12,8)
        FROM [Batcher].[dbo].[WeighbridgeTicket] 
        WHERE Compartment <> 'truck-empty' 
        GROUP BY trip
    ) WT on tk.trip = WT.trip
    LEFT JOIN Batcher.dbo.TripReturnRedelivery TRR ON TRR.Ticket = TK.ticketnumber
    WHERE tk.productweight > 0
    GROUP BY 
        CASE 
            WHEN CAST(WT.LoadedTime AS Time) > '00:00:00' AND CAST(WT.LoadedTime AS Time) < '06:00:00' 
            THEN DATEADD(day, -1, Convert(DATE, WT.LoadedDate, 103)) 
            ELSE Convert(DATE, WT.LoadedDate, 103) 
        END, 
        tps.TripsheetNo, 
        WT.LoadedTime,  
        WT.LoadedDate   
) Core
WHERE CONVERT(datetime, Core.LoadedDate, 103) BETWEEN @StartDate AND @EndDate;

SELECT DISTINCT
    s.ProdDay, 
    s.Tripsheetno,
    s.[SalesReport Weight],
    ISNULL(cu.Customer, '') as Customer, 
	ISNULL(f.FarmID, '') as FarmName,
    ISNULL(f.[TripSheetReport Weight], 0) as [TripSheetReport Weight], 
    ISNULL(f.[TripSheetReport Weight], 0) - s.[SalesReport Weight] AS Discrepancy, 
    CASE
        WHEN ISNULL(f.[TripSheetReport Weight], 0) != s.[SalesReport Weight] THEN 'Warning'
        WHEN s.[SalesReport Weight] = f.[TripSheetReport Weight] THEN 'PASS'
    END AS ErrorWarning
FROM #SALES s
LEFT JOIN #FINISHEDPRODUCT f ON s.Tripsheetno = f.TripsheetNo
LEFT JOIN  [dbo].[Customer] cu ON cu.CustomerID = cu.CustomerID
LEFT JOIN [dbo].[Farm] fa ON f.FarmID = cu.Farmid



END

 


Answers (5)