Well, that'll obviously be tgd.Constrained . . .
Main Topics
Browse All TopicsHere is what I have:
SELECT DISTINCT tbm.MaterialNo, tgm.GroupingID, tbm.MaterialDescription, tbd.PrettyName, tgd.Constrained, tgd.DefaultSelect
FROM TOPS_GROUP_KEY_DETAIL LEFT OUTER JOIN
TOPS_GROUPING_MASTER AS tgm ON TOPS_GROUP_KEY_DETAIL.Grou
TOPS_BOM_MASTER AS tbm ON tgm.MaterialID = tbm.MaterialNo LEFT OUTER JOIN
TOPS_BOM_DETAIL AS tbd ON tgm.MaterialID = tbd.MaterialNo LEFT OUTER JOIN
TOPS_GROUPING_DETAIL AS tgd ON tgm.GroupingID = tgd.GroupingID
WHERE (TOPS_GROUP_KEY_DETAIL.Gro
It returns:
F145 5331 C_FJS_FL_FR_EA_OPNG_PLL PARALLEL OPENING NULL
F145 5331 C_FJS_FL_FR_EA_OPNG_PLL PARALLEL OPENING X NULL
I need only 1 record to show up. The field 'Constrained' which is represented by the 'X' flag in the above result set does not exist for every MaterialNo. How can I limit this to 1 record? If the constrained flag exists I want just that record else give me the other record.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Oh, sorry . . . last one . . . if you use TOP 1, you don't need distinct. Nota bene, this will only work if you really did mean that you want only one record. If you want to return a set of these rows which differ, but you want the constrained rows to take precedence, then you'll have to do it a different way. In that case, I'd probably just use a recursive join and some isnulls.
TNC
Yes I'll multiple records. The actual query is actually:
SELECT DISTINCT tbm.MaterialNo, tgm.GroupingID, tbm.MaterialDescription, tbd.PrettyName, tgd.Constrained, tgd.DefaultSelect
FROM TOPS_GROUP_KEY_DETAIL LEFT OUTER JOIN
TOPS_GROUPING_MASTER AS tgm ON TOPS_GROUP_KEY_DETAIL.Grou
TOPS_BOM_MASTER AS tbm ON tgm.MaterialID = tbm.MaterialNo LEFT OUTER JOIN
TOPS_BOM_DETAIL AS tbd ON tgm.MaterialID = tbd.MaterialNo LEFT OUTER JOIN
TOPS_GROUPING_DETAIL AS tgd ON tgm.GroupingID = tgd.GroupingID
WHERE (TOPS_GROUP_KEY_DETAIL.Gro
I'd probably use the join then. In pseudo-code . . .
SELECT DISTINCT
ID,
Description = ISNULL(Constrained.Descrip
Group = ISNULL(Constrained.Group, NotConstrained.Group)
FROM MyTable AS NotConstrained
LEFT JOIN MyTable AS Constrained
ON NotConstrained.ID = Constrained.ID
WHERE
Constrained.Constrained = 'x' AND
NotConstrained.Constrained
Does that make sense?
TNC
like this ?
SELECT distinct tbm.MaterialNo
, tgm.GroupingID
, tbm.MaterialDescription
, tbd.PrettyName
, case when tgd.const is null then ' ' else 'x' end as Constrained
, case when tgd.const is null then tgd.Unconst else tgd.const end as DefaultSelect
FROM TOPS_GROUP_KEY_DETAIL
LEFT OUTER JOIN TOPS_GROUPING_MASTER AS tgm
ON TOPS_GROUP_KEY_DETAIL.Grou
LEFT OUTER JOIN TOPS_BOM_MASTER AS tbm
ON tgm.MaterialID = tbm.MaterialNo
LEFT OUTER JOIN TOPS_BOM_DETAIL AS tbd
ON tgm.MaterialID = tbd.MaterialNo
LEFT OUTER JOIN
(select groupingID
,max(case when constrained = 'x' then defaultselect else null end) as const
,max(case when constrained = 'x' then null else defaultselect end) as UNconst
From TOPS_GROUPING_DETAIL
group by groupingid
) AS tgd
ON tgm.GroupingID = tgd.GroupingID
WHERE TOPS_GROUP_KEY_DETAIL.Grou
AND tbm.MaterialNo = 'F145'
btw the
AND tbm.MaterialNo = 'F145'
is in effect making the tbm join into an inner join... is that what you intended?
Business Accounts
Answer for Membership
by: tncbbthositgPosted on 2007-06-20 at 12:08:11ID: 19327369
SELECT TOP 1 DISTINCT tbm.MaterialNo, tgm.GroupingID, tbm.MaterialDescription, tbd.PrettyName, tgd.Constrained, tgd.DefaultSelect pKeyID = tgm.GroupKeyID LEFT OUTER JOIN upDetailID = '775') AND (tbm.MaterialNo = 'F145')
FROM TOPS_GROUP_KEY_DETAIL LEFT OUTER JOIN
TOPS_GROUPING_MASTER AS tgm ON TOPS_GROUP_KEY_DETAIL.Grou
TOPS_BOM_MASTER AS tbm ON tgm.MaterialID = tbm.MaterialNo LEFT OUTER JOIN
TOPS_BOM_DETAIL AS tbd ON tgm.MaterialID = tbd.MaterialNo LEFT OUTER JOIN
TOPS_GROUPING_DETAIL AS tgd ON tgm.GroupingID = tgd.GroupingID
WHERE (TOPS_GROUP_KEY_DETAIL.Gro
ORDER BY CASE WHEN Constrained = 'X' THEN 0 ELSE 1 END
TNC