Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mayoorsubbu
Yak Posting Veteran
95 Posts |
Posted - 2013-12-25 : 11:19:13
|
Dear all,I have three tables in my SQL server 2000 database like soI have three tables like soItemMasterItemcode PartNo Description12345 KU7 Blah Blah34567 KAU30 Tester98765 UPAS1M SpeedometerItemSecondaryPrimaryItemcode SecondaryCode12345 77656412345 435543 12345 324456 12345 98765 StockMasterItemcode QtyInStk12345 20776564 10 435543 5324456 998765 11I want a result as underitemcode stk inLieuCode12345 55 776564, 435543, 324456, 98765 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-12-25 : 12:47:15
|
[code]SET NOCOUNT ONGOCREATE TABLE dbo.ItemMaster ( ItemCode INT, PartNo VARCHAR(10) )CREATE TABLE dbo.ItemSecondary ( PrimaryItemCode INT, SecondaryCode INT )CREATE TABLE dbo.StockMaster ( ItemCode INT, QtyInStk INT )GOINSERT dbo.ItemMaster ( ItemCode, PartNo )SELECT 12345, 'KU7' UNION ALLSELECT 34567, 'KAU30' UNION ALLSELECT 98765, 'UPAS1M'INSERT dbo.ItemSecondary ( PrimaryItemCode, SecondaryCode )SELECT 12345, 776564 UNION ALLSELECT 12345, 435543 UNION ALL SELECT 12345, 324456 UNION ALLSELECT 12345, 98765INSERT dbo.StockMaster ( ItemCode, QtyInStk )SELECT 12345, 20 UNION ALLSELECT 776564, 10 UNION ALL SELECT 435543, 5 UNION ALLSELECT 324456, 9 UNION ALLSELECT 98765, 11GOCREATE FUNCTION dbo.StringConcat( @Primary INT)RETURNS VARCHAR(8000)ASBEGIN DECLARE @Data VARCHAR(8000) SELECT @Data = ISNULL(@Data + ', ', '') + CAST(SecondaryCode AS VARCHAR(12)) FROM dbo.ItemSecondary WHERE PrimaryItemCode = @Primary ORDER BY SecondaryCode DESC RETURN @DataENDGO/*itemcode stk inLieuCode12345 55 776564, 435543, 324456, 98765*/SELECT m.ItemCode AS itemcode, MIN(v.QtyInStk) + SUM(w.QtyInStk) AS stkINTO #TempFROM dbo.ItemMaster AS mINNER JOIN dbo.StockMaster AS v ON v.ItemCode = m.ItemCodeINNER JOIN dbo.ItemSecondary AS s ON s.PrimaryItemCode = m.ItemCodeINNER JOIN dbo.StockMaster AS w ON w.ItemCode = s.SecondaryCodeGROUP BY m.ItemCodeSELECT itemcode, stk, dbo.StringConcat(itemcode) AS inLieuCodeFROM #TempGODROP FUNCTION dbo.StringConcatDROP TABLE dbo.ItemMaster, dbo.ItemSecondary, dbo.StockMaster, #TempGO[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
mayoorsubbu
Yak Posting Veteran
95 Posts |
Posted - 2013-12-26 : 01:40:23
|
SwePeso,First of thanks for the solution. Could you please explain as to why have you selected MIN(v.QtyInStk) in the query. "Select m.ItemCode AS itemcode, MIN(v.QtyInStk) + SUM(w.QtyInStk) AS stk"Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-26 : 03:50:48
|
[code]CREATE PROC GetDataASIF OBJECT_ID('tempdb..#Result') IS NOT NULL DROP TABLE #ResultSELECT Itemcode,CAST(0 AS int) AS stk,CAST(NULL AS varchar(8000)) AS inLieuCodeINTO #ResultFROM dbo.ItemMasterUPDATE rSET r.Stk = TotalStkFROM #Result rINNER JOIN (SELECT is.PrimaryItemCode,SUM(QtyInStk) AS TotalStkFROM dbo.ItemSecondary isINNER JOIN dbo.StockMaster smON sm.ItemCode = is.SecondaryCodeOR sm.ItemCode = is.PrimaryItemCodeGROUP BY is.PrimaryItemCode)sON s.PrimaryItemCode = r.ItemCodeUPDATE rSET r.inLieuCode = COALESCE(inLieuCode,'') + ',' + CAST(SecondaryCode AS varchar(10)) FROM #Result rINNER JOIN dbo.ItemSecondary iON i.PrimaryItemCode = r.ItemcodeSELECT itemCode,stk,STUFF(inLieuCode,1,1,'') AS inLieuCodeFROM #resultGOThen call it likeEXEC Getdata[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-12-26 : 06:09:04
|
quote: Originally posted by mayoorsubbu Could you please explain as to why have you selected MIN(v.QtyInStk) in the query.
It's the nature of the JOIN. The sum is built of all secondarycodes and the primarycodes (all just once). Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
mayoorsubbu
Yak Posting Veteran
95 Posts |
Posted - 2013-12-26 : 06:43:06
|
Thank you Peso and Visakh. Two different approach. |
|
|
|
|
|
|
|