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 |
|
williadw1955
Starting Member
3 Posts |
Posted - 2007-08-14 : 17:48:38
|
| I am definate newbie at all this.. I have writting this sql code and it works.. for small amounts of data.. but for larger amounts.. it takes quite a bit of time.Basically.. what I am trying to do is to find all the postion numbers for a machine that was created at the same time (this is a time that is incremented every 15 mins and stored in a database table, EX: 9:15, 9:30, etc) for the same machine and increment one position number for the next position number (up to 11 from the orig pos number) of a production machine. If there is no data for that join (doffnum and position), I want it to be blank, but show all the other positions for that doffnum. The part of the SQL that is slow.. is below. The #TempTab table is data from a database table and the data I put into that is quick. Select DISTINCT TT.HDR_FromDateTime, TT.HDR_ToDateTime, TT.HDR_Machine, TT.MinPos, TT.MaxPos, TT.DTL_Merge, TT.DTL_Ref, TT.DTL_DoffNum, TT1.DTL_BobbinNumber as BobbinNumber1, TT1.DTL_SpinProdPos as SpinPos1, TT1.DTL_DoffDate as Doffdate1, TT1.DTL_OrigLen as OrigLen1, TT1.DTL_BobnHeldFlg as HeldFlag1, TT1.DTL_BobnSize as BobnSize1, TT2.DTL_BobbinNumber as BobbinNumber2, TT2.DTL_SpinProdPos as SpinPos2, TT2.DTL_DoffDate as Doffdate2, TT2.DTL_OrigLen as OrigLen2, TT2.DTL_BobnHeldFlg as HeldFlag2, TT2.DTL_BobnSize as BobnSize2, TT3.DTL_BobbinNumber as BobbinNumber3, TT3.DTL_SpinProdPos as SpinPos3, TT3.DTL_DoffDate as Doffdate3, TT3.DTL_OrigLen as OrigLen3, TT3.DTL_BobnHeldFlg as HeldFlag3, TT3.DTL_BobnSize as BobnSize3, TT4.DTL_BobbinNumber as BobbinNumber4, TT4.DTL_DoffDate as Doffdate4, TT4.DTL_OrigLen as OrigLen4, TT4.DTL_BobnHeldFlg as HeldFlag4, TT4.DTL_BobnSize as BobnSize4, TT5.DTL_BobbinNumber as BobbinNumber5, TT5.DTL_DoffDate as Doffdate5, TT5.DTL_OrigLen as OrigLen5, TT5.DTL_BobnHeldFlg as HeldFlag5, TT5.DTL_BobnSize as BobnSize5, TT6.DTL_BobbinNumber as BobbinNumber6, TT6.DTL_DoffDate as Doffdate6, TT6.DTL_OrigLen as OrigLen6, TT6.DTL_BobnHeldFlg as HeldFlag6, TT6.DTL_BobnSize as BobnSize6, TT7.DTL_BobbinNumber as BobbinNumber7, TT7.DTL_SpinProdPos as SpinPos7, TT7.DTL_DoffDate as Doffdate7, TT7.DTL_OrigLen as OrigLen7, TT7.DTL_BobnHeldFlg as HeldFlag7, TT7.DTL_BobnSize as BobnSize7, TT8.DTL_BobbinNumber as BobbinNumber8, TT8.DTL_DoffDate as Doffdate8, TT8.DTL_OrigLen as OrigLen8, TT8.DTL_BobnHeldFlg as HeldFlag8, TT8.DTL_BobnSize as BobnSize8, TT9.DTL_BobbinNumber as BobbinNumber9, TT9.DTL_DoffDate as Doffdate9, TT9.DTL_OrigLen as OrigLen9, TT9.DTL_BobnHeldFlg as HeldFlag9, TT9.DTL_BobnSize as BobnSize9, TT10.DTL_BobbinNumber as BobbinNumber10, TT10.DTL_DoffDate as Doffdate10, TT10.DTL_OrigLen as OrigLen10, TT10.DTL_BobnHeldFlg as HeldFlag10, TT10.DTL_BobnSize as BobnSize10, TT11.DTL_BobbinNumber as BobbinNumber11, TT11.DTL_DoffDate as Doffdate11, TT11.DTL_OrigLen as OrigLen11, TT11.DTL_BobnHeldFlg as HeldFlag11, TT11.DTL_BobnSize as BobnSize11, TT12.DTL_BobbinNumber as BobbinNumber12, TT12.DTL_DoffDate as Doffdate12, TT12.DTL_OrigLen as OrigLen12, TT12.DTL_BobnHeldFlg as HeldFlag12, TT12.DTL_BobnSize as BobnSize12Into #LastTabFrom #TempTab TT Left Outer Join #TempTab as TT1 on TT.DTL_DoffNum = TT1.DTL_DoffNum and TT1.DTL_SpinProdPos = TT.MinPos Left Outer Join #TempTab as TT2 on TT.DTL_DoffNum = TT2.DTL_DoffNum and TT2.DTL_SpinProdPos = (TT.MinPos + 1) Left Outer Join #TempTab as TT3 on TT.DTL_DoffNum = TT3.DTL_DoffNum and TT3.DTL_SpinProdPos = (TT.MinPos + 2) Left Outer Join #TempTab as TT4 on TT.DTL_DoffNum = TT4.DTL_DoffNum and TT4.DTL_SpinProdPos = (TT.MinPos + 3) Left Outer Join #TempTab as TT5 on TT.DTL_DoffNum = TT5.DTL_DoffNum and TT5.DTL_SpinProdPos = (TT.MinPos + 4) Left Outer Join #TempTab as TT6 on TT.DTL_DoffNum = TT6.DTL_DoffNum and TT6.DTL_SpinProdPos = (TT.MinPos + 5) Left Outer Join #TempTab as TT7 on TT.DTL_DoffNum = TT7.DTL_DoffNum and TT7.DTL_SpinProdPos = (TT.MinPos + 6) Left Outer Join #TempTab as TT8 on TT.DTL_DoffNum = TT8.DTL_DoffNum and TT8.DTL_SpinProdPos = (TT.MinPos + 7) Left Outer Join #TempTab as TT9 on TT.DTL_DoffNum = TT9.DTL_DoffNum and TT9.DTL_SpinProdPos = (TT.MinPos + 8) Left Outer Join #TempTab as TT10 on TT.DTL_DoffNum = TT10.DTL_DoffNum and TT10.DTL_SpinProdPos = (TT.MinPos + 9) Left Outer Join #TempTab as TT11 on TT.DTL_DoffNum = TT11.DTL_DoffNum and TT11.DTL_SpinProdPos = (TT.MinPos + 10) Left Outer Join #TempTab as TT12 on TT.DTL_DoffNum = TT12.DTL_DoffNum and TT12.DTL_SpinProdPos = (TT.MinPos + 11)Order By TT.DTL_DoffNum |
|
|
mattyblah
Starting Member
49 Posts |
Posted - 2007-08-14 : 18:27:57
|
| perhaps this?Selecttt_a.HDR_FromDateTime,tt_a.HDR_ToDateTime,tt_a.HDR_Machine,tt_a.MinPos,tt_a.MaxPos,tt_a.DTL_Merge,tt_a.DTL_Ref,tt_a.DTL_DoffNum,case when tt_b.DTL_SpinProdPos = tt_a.MinPos then tt_b.DTL_BobbinNumber end as BobbinNumber1,case when tt_b.DTL_SpinProdPos = tt_a.MinPos then tt_b.DTL_SpinProdPos end as SpinPos1,case when tt_b.DTL_SpinProdPos = tt_a.MinPos then tt_b.DTL_DoffDate end as Doffdate1,case when tt_b.DTL_SpinProdPos = tt_a.MinPos then tt_b.DTL_OrigLen end as OrigLen1,case when tt_b.DTL_SpinProdPos = tt_a.MinPos then tt_b.DTL_BobnHeldFlg end as HeldFlag1,case when tt_b.DTL_SpinProdPos = tt_a.MinPos then tt_b.DTL_BobnSize end as BobnSize1,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 1) then tt_b.DTL_BobbinNumber end as BobbinNumber2,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 1) then tt_b.DTL_SpinProdPos end as SpinPos2,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 1) then tt_b.DTL_DoffDate end as Doffdate2,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 1) then tt_b.DTL_OrigLen end as OrigLen2,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 1) then tt_b.DTL_BobnHeldFlg end as HeldFlag2,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 1) then tt_b.DTL_BobnSize end as BobnSize2,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 2) then tt_b.DTL_BobbinNumber end as BobbinNumber3,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 2) then tt_b.DTL_SpinProdPos end as SpinPos3,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 2) then tt_b.DTL_DoffDate end as Doffdate3,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 2) then tt_b.DTL_OrigLen end as OrigLen3,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 2) then tt_b.DTL_BobnHeldFlg end as HeldFlag3,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 2) then tt_b.DTL_BobnSize end as BobnSize3,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 3) then tt_b.DTL_BobbinNumber end as BobbinNumber4,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 3) then tt_b.DTL_DoffDate end as Doffdate4,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 3) then tt_b.DTL_OrigLen end as OrigLen4,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 3) then tt_b.DTL_BobnHeldFlg end as HeldFlag4,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 3) then tt_b.DTL_BobnSize end as BobnSize4,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 4) then tt_b.DTL_BobbinNumber end as BobbinNumber5,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 4) then tt_b.DTL_DoffDate end as Doffdate5,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 4) then tt_b.DTL_OrigLen end as OrigLen5,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 4) then tt_b.DTL_BobnHeldFlg end as HeldFlag5,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 4) then tt_b.DTL_BobnSize end as BobnSize5,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 5) then tt_b.DTL_BobbinNumber end as BobbinNumber6,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 5) then tt_b.DTL_DoffDate end as Doffdate6,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 5) then tt_b.DTL_OrigLen end as OrigLen6,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 5) then tt_b.DTL_BobnHeldFlg end as HeldFlag6,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 5) then tt_b.DTL_BobnSize end as BobnSize6,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 6) then tt_b.DTL_BobbinNumber end as BobbinNumber7,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 6) then tt_b.DTL_SpinProdPos end as SpinPos7, case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 6) then tt_b.DTL_DoffDate end as Doffdate7,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 6) then tt_b.DTL_OrigLen end as OrigLen7,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 6) then tt_b.DTL_BobnHeldFlg end as HeldFlag7,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 6) then tt_b.DTL_BobnSize end as BobnSize7,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 7) then tt_b.DTL_BobbinNumber end as BobbinNumber8,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 7) then tt_b.DTL_DoffDate end as Doffdate8,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 7) then tt_b.DTL_OrigLen end as OrigLen8,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 7) then tt_b.DTL_BobnHeldFlg end as HeldFlag8,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 7) then tt_b.DTL_BobnSize end as BobnSize8,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 8) then tt_b.DTL_BobbinNumber end as BobbinNumber9,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 8) then tt_b.DTL_DoffDate end as Doffdate9,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 8) then tt_b.DTL_OrigLen end as OrigLen9,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 8) then tt_b.DTL_BobnHeldFlg end as HeldFlag9,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 8) then tt_b.DTL_BobnSize end as BobnSize9,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 9) then tt_b.DTL_BobbinNumber end as BobbinNumber10,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 9) then tt_b.DTL_DoffDate end as Doffdate10,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 9) then tt_b.DTL_OrigLen end as OrigLen10,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 9) then tt_b.DTL_BobnHeldFlg end as HeldFlag10,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 9) then tt_b.DTL_BobnSize end as BobnSize10,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 10) then tt_b.DTL_BobbinNumber end as BobbinNumber11,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 10) then tt_b.DTL_DoffDate end as Doffdate11,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 10) then tt_b.DTL_OrigLen end as OrigLen11,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 10) then tt_b.DTL_BobnHeldFlg end as HeldFlag11,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 10) then tt_b.DTL_BobnSize end as BobnSize11,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 11) then tt_b.DTL_BobbinNumber end as BobbinNumber12,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 11) then tt_b.DTL_DoffDate end as Doffdate12,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 11) then tt_b.DTL_OrigLen end as OrigLen12,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 11) then tt_b.DTL_BobnHeldFlg end as HeldFlag12,case when tt_b.DTL_SpinProdPos = (tt_a.MinPos + 11) then tt_b.DTL_BobnSize end as BobnSize12Into #LastTabFrom #TempTab tt_ainner join #temptab tt_b on tt_a.DTL_DoffNum = tt_b.DTL_DoffNum and (tt_b.DTL_SpinProdPos - tt_a.MinPos) between 0 and 11without seeing actual data it's kinda hard to shoot in the dark. let me know how it goes. |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-08-15 : 07:08:01
|
| Normalization would also be a very good idea![Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
williadw1955
Starting Member
3 Posts |
Posted - 2007-08-15 : 10:26:45
|
| Mattyblah,Not exactly the same results..The original way, yields 13 rows where you have all the bobbin numbers ( example 1 to 12 ) on a single row for each doffnum.Your sql returned 1872 rows, with one bobbin number per row per doffnum. So 12 rows for each doffnum, as opposed to one.I may can work with that in the report writer by grouping it, if the SQL runs faster..maybe. |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2007-08-15 : 10:43:39
|
As previously mentioned, you should really normalize the schema.This should work with your current table:SELECT HDR_FromDateTime ,HDR_ToDateTime ,HDR_Machine ,MinPos ,MaxPos ,DTL_Merge ,DTL_Ref ,DTL_DoffNum ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_BobbinNumber END) AS BobbinNumber1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_SpinProdPos END) AS SpinPos1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_DoffDate END) AS Doffdate1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_OrigLen END) AS OrigLen1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_BobnHeldFlg END) AS HeldFlag1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_BobnSize END) AS BobnSize1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_BobbinNumber END) AS BobbinNumber2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_SpinProdPos END) AS SpinPos2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_DoffDate END) AS Doffdate2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_OrigLen END) AS OrigLen2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_BobnHeldFlg END) AS HeldFlag2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_BobnSize END) AS BobnSize2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_BobbinNumber END) AS BobbinNumber3 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_SpinProdPos END) AS SpinPos3 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_DoffDate END) AS Doffdate3 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_OrigLen END) AS OrigLen3 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_BobnHeldFlg END) AS HeldFlag3 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_BobnSize END) AS BobnSize3 -- etcFROM #TempTabGROUP BY HDR_FromDateTime ,HDR_ToDateTime ,HDR_Machine ,MinPos ,MaxPos ,DTL_Merge ,DTL_Ref ,DTL_DoffNum |
 |
|
|
williadw1955
Starting Member
3 Posts |
Posted - 2007-08-15 : 12:05:08
|
Ifor, This sql you posted...Runs in 3 seconds. And it returns just what I wanted.It is nothing short of amazing. (Do you teach this stuff.. I need some training.) LOLWe have many older sql reports that I need to optimize.Thanks mucho for this!quote: Originally posted by Ifor As previously mentioned, you should really normalize the schema.This should work with your current table:SELECT HDR_FromDateTime ,HDR_ToDateTime ,HDR_Machine ,MinPos ,MaxPos ,DTL_Merge ,DTL_Ref ,DTL_DoffNum ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_BobbinNumber END) AS BobbinNumber1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_SpinProdPos END) AS SpinPos1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_DoffDate END) AS Doffdate1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_OrigLen END) AS OrigLen1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_BobnHeldFlg END) AS HeldFlag1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos THEN DTL_BobnSize END) AS BobnSize1 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_BobbinNumber END) AS BobbinNumber2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_SpinProdPos END) AS SpinPos2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_DoffDate END) AS Doffdate2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_OrigLen END) AS OrigLen2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_BobnHeldFlg END) AS HeldFlag2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 1 THEN DTL_BobnSize END) AS BobnSize2 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_BobbinNumber END) AS BobbinNumber3 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_SpinProdPos END) AS SpinPos3 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_DoffDate END) AS Doffdate3 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_OrigLen END) AS OrigLen3 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_BobnHeldFlg END) AS HeldFlag3 ,MAX(CASE WHEN DTL_SpinProdPos = MinPos + 2 THEN DTL_BobnSize END) AS BobnSize3 -- etcFROM #TempTabGROUP BY HDR_FromDateTime ,HDR_ToDateTime ,HDR_Machine ,MinPos ,MaxPos ,DTL_Merge ,DTL_Ref ,DTL_DoffNum
|
 |
|
|
mattyblah
Starting Member
49 Posts |
Posted - 2007-08-16 : 01:20:30
|
| Ugh, forgot the max for the pivot, and the group by. but Ifor's query is much cleaner and if it works thats all that matters!! :) |
 |
|
|
|
|
|
|
|