Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

T-SQL: Pulling Different Data from the Same Table

Posted on by Microsoft Employee

Hi:


The first code block below has a field called IM.  This is equal to the FormulaID field of FMItem of the second code.
The ItemKey field of the second code is the FormulaID field of the third code's FMItem table.
Both the second and third codes are of the same table, of course.  The where clauses are slightly different.
The first code that I need modified already contains an IM of 506L.
In addition, I created another field named "IM2".  This field should be returned as item 129.  This is the only item in the ItemKey column that is a formula (i.e. the FMItem table)
Originally, I placed the following restriction at the end of the code:  and FORMULA.ItemKey <> IM2.IM2.
But, I now have it commented out.  (The restriction was preventing my code from returning results.)
How can I modify the syntax of the code to allow for having this restriction, so that IM = 506L and IM2 = 129?

Thank you!

John

select DISTINCT [INVHDRBATCH], 
[FGBATCH], [FG], CONVERT(DECIMAL(10,2), [FGMAT]) as [FGMAT], 
CONVERT(DECIMAL(10,2), [FGLABOR]) as [FGLABOR], CONVERT(DECIMAL(10,2), [FGOH]) as [FGOH], 
CONVERT(DECIMAL(10,2), [FGCOST]) as [FGCOST], 
CONVERT(DECIMAL(10,2),[FGWEIGHT]) as [FGWEIGHT], [MATLB] as [MATLB], 
[LABORLB] as [LABORLB], 
[OHLB] as [OHLB],
rtrim(cast((select DISTINCT IV30300.ITEMNMBR) as varchar)) as [Component],
FORMULA.ItemKey as IM,
rtrim(cast([LOT] as varchar)) as [LOT], CONVERT(DECIMAL(10,2), WIPTEST.[LOTQTY]) as [LOTQTY], 
CONVERT(DECIMAL(10,2), [LOTMATUNITCOST]) as [LOTMATUNITCOST], 
CONVERT(DECIMAL(10,2), [LOTMATWIPCOST]) as [LOTMATWIPCOST], 
case when rtrim(cast([LOT] as varchar)) LIKE rtrim(cast((select DISTINCT IV30300.ITEMNMBR) as varchar))+'%' 
THEN CONVERT(DECIMAL(10,2), [LOTLABORWIPCOST]) else 0 end as [LOTLABORWIPCOST], 
case when rtrim(cast([LOT] as varchar)) LIKE rtrim(cast((select DISTINCT IV30300.ITEMNMBR) as varchar))+'%' 
THEN CONVERT(DECIMAL(10,2), [LOTOHWIPCOST]) else 0 end as [LOTOHWIPCOST], YIELD.[YIELD] as [YIELD]
from WIPTEST
INNER JOIN IV30200 BATCH on WIPTEST.[INVHDRBATCH] = BATCH.BACHNUMB AND WIPTEST.[INVHDRDOC] = BATCH.DOCNUMBR
INNER JOIN IV30300 ON BATCH.TRXSORCE = IV30300.TRXSORCE AND BATCH.DOCNUMBR = IV30300.DOCNUMBR
LEFT OUTER JOIN FMItem FORMULA on IV30300.ITEMNMBR = FORMULA.FormulaID
INNER JOIN IV30400 ON IV30300.TRXSORCE = IV30400.TRXSORCE
INNER JOIN BM_View_SL_BatchTicketAuditLog LOG on BATCH.BACHNUMB = LOG.BatchTicket AND BATCH.DOCNUMBR = LOG.GPDocNo
CROSS JOIN (select
[FGWEIGHT]/SUM([LOTQTY]) as [YIELD] from (
   select DISTINCT 
    CONVERT(DECIMAL(10,2),[FGWEIGHT]) as [FGWEIGHT], 
    CONVERT(DECIMAL(10,2), WIPTEST.[LOTQTY]) as [LOTQTY],
	case when rtrim(cast([LOT] as varchar)) LIKE rtrim(cast((select DISTINCT IV30300.ITEMNMBR) as varchar))+'%' 
THEN CONVERT(DECIMAL(10,2), [LOTLABORWIPCOST]) else 0 end as [LOTLABORWIPCOST], 
case when rtrim(cast([LOT] as varchar)) LIKE rtrim(cast((select DISTINCT IV30300.ITEMNMBR) as varchar))+'%' 
THEN CONVERT(DECIMAL(10,2), [LOTOHWIPCOST]) else 0 end as [LOTOHWIPCOST]
   from WIPTEST
   INNER JOIN IV30200 BATCH on WIPTEST.[INVHDRBATCH] = BATCH.BACHNUMB AND WIPTEST.[INVHDRDOC] = BATCH.DOCNUMBR
   INNER JOIN IV30300 ON BATCH.TRXSORCE = IV30300.TRXSORCE AND BATCH.DOCNUMBR = IV30300.DOCNUMBR
   INNER JOIN IV30400 ON IV30300.TRXSORCE = IV30400.TRXSORCE
   INNER JOIN BM_View_SL_BatchTicketAuditLog LOG on BATCH.BACHNUMB = LOG.BatchTicket AND BATCH.DOCNUMBR = LOG.GPDocNo
   where LOT IS NOT NULL and LOG.FGItem = ''
     and WIPTEST.[FG] = '506' and WIPTEST.[FGBATCH] = '50418G12D' and WIPTEST.[INVHDRBATCH] = '50418G12D' 
 ) as WIP
   where LOTLABORWIPCOST <> 0 AND lotohwipcost <> 0 
   GROUP BY WIP.FGWEIGHT) as YIELD
CROSS JOIN 
(select ItemKey as IM2 
from 
(select ItemKey
   from WIPTEST
   INNER JOIN IV30200 BATCH on WIPTEST.[INVHDRBATCH] = BATCH.BACHNUMB AND WIPTEST.[INVHDRDOC] = BATCH.DOCNUMBR
   INNER JOIN IV30300 ON BATCH.TRXSORCE = IV30300.TRXSORCE AND BATCH.DOCNUMBR = IV30300.DOCNUMBR
   LEFT OUTER JOIN FMItem FORMULA on IV30300.ITEMNMBR = FORMULA.FormulaID
   INNER JOIN IV30400 ON IV30300.TRXSORCE = IV30400.TRXSORCE
   INNER JOIN BM_View_SL_BatchTicketAuditLog LOG on BATCH.BACHNUMB = LOG.BatchTicket AND BATCH.DOCNUMBR = LOG.GPDocNo
   where WIPTEST.LOT IS NOT NULL and LOG.FGItem = '' 
     and WIPTEST.[FG] = '506' and WIPTEST.[FGBATCH] = '50418G12D' and WIPTEST.[INVHDRBATCH] = '50418G12D')
as WIP2)
as IM2
where LOT IS NOT NULL and LOG.FGItem = '' --and FORMULA.ItemKey <> IM2.IM2
and WIPTEST.[FG] = '506' and WIPTEST.[FGBATCH] = '50418G12D' and WIPTEST.[INVHDRBATCH] = '50418G12D'

 

select * from FMItem where FormulaId = '506N'

 

select * from FMItem where FormulaId = '506L'

Categories:
  • RE: T-SQL: Pulling Different Data from the Same Table

    Hey John, maybe here:  social.msdn.microsoft.com/.../home

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: T-SQL: Pulling Different Data from the Same Table

    Hi Deseree:

    I figured out my own solution for this.  None of the other SQL Forums that I go to had a response, after two disappointing days of waiting.

    For future reference, what are the best SQL forums to go to?

    Thanks!

    John

  • RE: T-SQL: Pulling Different Data from the Same Table

    Hello John!  I am wondering if this would be better answered in the SQL Forums instead of here, but let's leave this open just in case someone can offer some assistance.

    Thank you!

    Warm Regards,

    Deseree

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans