Click here to Skip to main content
15,888,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
problem

How to make group by Revision_ID and when repeated display last check date separated by stick | ?

I need to group data by Revision_ID that make count to all zpartid

every revision_ID Have group of parts .

and when revision id repeated two time then lastcheckdate is firstdate | seconddate

and if more than two time then display text multi date

so How to do that please ?

SQL
CREATE TABLE [Parts].[LifeCycleMaster](
	[LifeCycleID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
	[ZPartID] [bigint] NOT NULL,
	[LastCheckDate] [date] NULL,
	[Revision_ID] [bigint] NULL,
 CONSTRAINT [PK_LifeCycleMaster_LifeCycleID] PRIMARY KEY CLUSTERED 
(
	[LifeCycleID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
 CONSTRAINT [UK_PartID] UNIQUE NONCLUSTERED 
(
	[ZPartID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Revision_ID	ZPartID	LastCheckDate
12	          10	    12/12/2015
15	          120	    12/01/2014
15	          130	    05/05/2016
20	          170	    09/03/2013
20	          200	    09/05/2016
20	          300	    09/08/2017
		
	FinalResult	
Revision_ID  CountParts  LastCheckDate
12	         1	  12/12/2015
15	         2	  05/05/2016 |12/01/2014
20	         3	  Multi date


What I have tried:

SELECT lcm.Revision_ID,count(lcm.ZPartID)as CountParts,lcm.LastCheckDate  
FROM parts.LifeCycleMaster lcm group by Revision_ID
Posted
Updated 17-Jan-20 20:07pm

1 solution

I would advice not to concatenate the dates into a single field when fetching the data. This makes parsing the data unnecessary complex on the client side.

Instead, why not just fetch the data in separate columns and add the logic to interpret the result to the calling program. This way the query is simple and you don't need to split the data from a single text field which may cause several kinds of problems, starting from date formats.

To fetch the data in separate columns, perhaps something like
SQL
WITH RevAmounts AS (
   SELECT lcm.Revision_ID,
          COUNT(lcm.ZPartID)                 AS CountParts,
          MAX(lcm.LastCheckDate)             AS LatestCheck,
          COUNT( DISTINCT lcm.LastCheckDate) AS DistinctCheckDates
   FROM parts.LifeCycleMaster lcm 
   GROUP BY Revision_ID
)
SELECT 
   ra.Revision_ID,
   ra.CountParts,
   ra.LatestCheck,
   (  SELECT MAX(lcm2.LastCheckDate)
      FROM parts.LifeCycleMaster lcm2
      WHERE lcm2.Revision_ID = ra.Revision_Id
      AND lcm2.LastCheckDate < ra.LatestCheck) AS PreviousCheck,
   ra.DistinctCheckDates
FROM RevAmounts ra
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900