Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I work on sql server 2012 and i need to get top 10000 revision_id and count parts related that having highest parts from parts.Rohs ?

this table have one million rows of parts but parts are repeated .

so that i need to count parts related to every revision it then get top 10000 have counts




SQL
Result Expected
revision_ID  COUNTPARTS  COMPANY
1              30        KMCOMPANY
2              20        WANDERCOMPANY
3              18         WILIAMCOMPANY





SQL
CREATE TABLE [Parts].[ROHS](
	[ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
	[ZPartID] [int] NULL,
        [Revision_ID] [bigint] NULL,
	
PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON 

[PRIMARY]
====================
CREATE TABLE [Parts].[Nop_Part](
	[PartID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
	[PartName] [nvarchar](70) NOT NULL,
	[CompanyID] [int] NOT NULL,
	
 CONSTRAINT [PK_Nop_Part] PRIMARY KEY CLUSTERED 
(
	[PartID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON 

[PRIMARY]
=============
CREATE TABLE [Parts].[Company](
	[CompanyID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
	[CompanyName] [nvarchar](70) NOT NULL,

	
 CONSTRAINT [PK_Company] PRIMARY KEY CLUSTERED 
(
	[CompanyID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON 

[PRIMARY]


What I have tried:

SQL
select distinct  top  10000 Revision_ID,count( ZPartID)as CountParts into #temprev from    [Parts].[ROHS] r    group by 

Revision_ID having count( ZPartID)>1 ORDER BY CountParts DESC
	 
			select distinct   v.Revision_ID,CountParts,c.CompanyName from  #temprev v 
			 inner join [Parts].[ROHS] r on v.Revision_ID=r.Revision_ID
			 inner join [Parts].[Nop_Part] p on p.PartID=r.ZPartID
			 inner join [Parts].[Company] c on c.CompanyID=p.CompanyID 
order by CountParts desc
Posted
Updated 2-Dec-19 21:04pm
v2
Comments
ahmed_sa 2-Dec-19 22:02pm    
sample data to expected result
revision_ID COUNTPARTS COMPANY
1000 3 KMCOMPANY
2000 2 WANDERCOMPANY
3000 1 WILIAMCOMPANY

parts.rohs
zprtid forign key for table [Parts].[Nop_Part]
id zpartid revisionid
9000 2 1000
9001 90 1000
9002 50 1000
9003 70 2000
9004 75 2000
9005 40 3000
[Parts].[Nop_Part] represent main parts
partid not repeated
PartID PartName CompanyID
2 trans 10
90 resis 10
50 speaker 10
70 screen 40
75 lcd 40
40 converter 70


CompanyID CompanyName
10 KMCOMPANY
40 WANDERCOMPANY
70 WILIAMCOMPANY
Herman<T>.Instance 3-Dec-19 9:15am    
Why you are using a temporary table for this? I do not understand your choice.
ZurdoDev 3-Dec-19 11:05am    
What's wrong with the first SQL statement? Except don't put it in a temp table.
ahmed_sa 3-Dec-19 21:17pm    
i get repeated datan
i see repeated company and revision

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