Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
NEED TO CREATE NORMATIVE FOR PRODUCTION
I HAVE PEACES: tbl.pieces
1. D1234
2. C1234

FOR PIECE 1 NEED MATERIAL: tbl.normativo
1. TUBE
2. SHEET METAL

FOR PIECE 2 NEED MATERIAL:tbl.normativo
2.BAR

Result in final table (DATAGRID) need to be

1. D1234
1. TUBE
2. SHEET METAL
2. C1234
1. BAR

What I have tried:

I TRY TO UNDERSTAND LOGIC OF MS SQL

SELECT FROM dbo.pieces

Result:
id drawing_name
1. D1234
2. C1234

Select * from dbo.normativo

Result:
Nr Material drawing
1. TUBE D1234
2. SHEET METAL D1234
1. BAR C1234
Posted
Updated 15-Jul-22 5:02am

That is an unusual way to get results from a database. Normally you would do something like this
SQL
SELECT p.id, p.drawing_name, n.nr, n.Material
from pieces p
inner join normativo n on p.drawing_name = n.drawing
order by p.id
which would give the results
id	drawing_name	nr	Material
1	D1234	1		TUBE
1	D1234	2		SHEET METAL
2	C1234	1		BAR
If you want to display that in a different way then that is a job for the display layer of your application.

There is a way to kludge it together e.g.
SQL
;with cte1 as 
(
	SELECT p.id, p.drawing_name, n.nr, n.Material
	from pieces p
	inner join normativo n on p.drawing_name = n.drawing
), cte2 as
(
	select id, nr, Material, drawing_name 
	from cte1
	union
	select p.id, null as nr, null as Material, p.drawing_name
	from pieces p
)
select 
	 isnull(nr,id)
	,isnull(Material, drawing_name)
from cte2
order by id, case when material is null then 0 else 1 end
Which gives
(No column name)	(No column name)
1						D1234
1						TUBE
2						SHEET METAL
2						C1234
1						BAR
Similar to your expected results but not the same - largely because I don't think your expected results match the data you have given us, and we have nothing really to determine the order Edit: Corrected my error in the ORDER BY clause
 
Share this answer
 
v3
Comments
Goran Bibic 15-Jul-22 5:56am    
Solution is close of result:

Result need to be

(No column name) (No column name)
1 D1234
1 TUBE
2 SHEET METAL
2 C1234
1 BAR
CHill60 15-Jul-22 11:38am    
I've fixed my bug in the order by clause
0x01AA 15-Jul-22 12:00pm    
Ugly request, ugly solution. I did similar ugly in s2 :) Have my 5.
Very ugly approach with union and cte, I don't like that realy.
;WITH cte1 AS
(
SELECT CAST(NULL AS INT) Nr
	  ,[id]           Id 
      ,[drawing_name] Desig
FROM [CP].[dbo].[pieces]
UNION
SELECT p.[id] Id
      ,n.[Nr] Nr
	  
      ,[Material] Desig
FROM [CP].[dbo].[pieces] p
LEFT JOIN [CP].[dbo].[normativo] n ON n.drawing = p.drawing_name
)
SELECT 
	 COALESCE(Nr, Id)
	,Nr,Id
	,Desig
FROM cte1
ORDER BY 1,2

The result will be:
(NoName)	Nr		Id		Desig
1			NULL	1		D1234                                             
1			1		1		TUBE                                              
1			1		2		SHEET METAL                                       
2			NULL	2		C1234                                             
2			2		1		BAR                                               

It's up to you then how to hide the first two column in the GUI.

[Edit]
Hiding first two columns seems to be easy:

;WITH cte1 AS
(
SELECT CAST(NULL AS INT) Nr
	  ,[id]           Id 
      ,[drawing_name] Desig
FROM [CP].[dbo].[pieces]
UNION
SELECT p.[id] Id
      ,n.[Nr] Nr
	  
      ,[Material] Desig
FROM [CP].[dbo].[pieces] p
LEFT JOIN [CP].[dbo].[normativo] n ON n.drawing = p.drawing_name
)
SELECT 
     Id
	,Desig
FROM cte1
ORDER BY COALESCE(cte1.Nr, cte1.Id), cte1.Nr


Result:
Id	Desig
1	D1234                                             
1	TUBE                                              
2	SHEET METAL                                       
2	C1234                                             
1	BAR                                               
 
Share this answer
 
v2
Comments
0x01AA 17-Jul-22 8:52am    
CHill60's answer also deserve to be accepted ;)

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