December 13, 2006 at 6:08 pm
Hi All
I have a SQL statement that returns 3 values: ItemNumber, Product Line Number, Product Line Description.
(
SELECT dbo.IM1_InventoryMasterfile.ItemNumber as ItemNumber, dbo.IM1_InventoryMasterfile.ProductLine as PL, dbo.IMA_ProductLine.ProductLineDescription as PLDescription
FROM dbo.IM1_InventoryMasterfile INNER JOIN
dbo.IMA_ProductLine ON dbo.IM1_InventoryMasterfile.ProductLine = dbo.IMA_ProductLine.ProductLineCode LEFT OUTER JOIN
dbo.IM_90_UDF_IMMasterfile ON dbo.IM1_InventoryMasterfile.ItemNumber = dbo.IM_90_UDF_IMMasterfile.ItemNumber
WHERE (dbo.IM_90_UDF_IMMasterfile.Web_Exclude <> 'Y' or dbo.IM_90_UDF_IMMasterfile.Web_Exclude is null)
union
SELECT dbo.Items.ItemNumber, dbo.Items.PL, dbo.IMA_ProductLine.ProductLineDescription
FROM dbo.Items INNER JOIN
dbo.IMA_ProductLine ON dbo.Items.PL = dbo.IMA_ProductLine.ProductLineCode
)
Order By PLDescription, ItemNumber ASC
The data it returns looks like this:
ItemNumber1, PL001, PLDescription1
ItemNumber2, PL001, PLDescription1
ItemNumber3, PL001, PLDescription1
ItemNumber4, PL001, PLDescription1
ItemNumber5, PL002, PLDescription2
ItemNumber6, PL003, PLDescription2
Is there a way to group the data so it will return the data like this:
PL001, PLDescription1
ItemNumber1
ItemNumber2
ItemNumber3
ItemNumber4
PL002, PLDescription3
ItemNumber5
ItemNumber6
Thanks
Oren Levy
December 13, 2006 at 6:12 pm
Simple question:
How many columns you expect in returning resultset?
_____________
Code for TallyGenerator
December 13, 2006 at 6:15 pm
ItemNumber2
ItemNumber3
ItemNumber4
PLDescription2
ItemNumber5
ItemNumber6
December 13, 2006 at 6:53 pm
SELECT ReportCol
FROM (
select PLNumber, NULL as SEQUENCE, PLDescription as ReportCol
FROM {YOUR TABLES}
GROUP BY PLNumber, PLDescription
UNION
select PLNumber, ItemNumber, ItemNumber
FROM {YOUR TABLES}
) DT
ORDER BY PLNumber, SEQUENCE
_____________
Code for TallyGenerator
December 13, 2006 at 7:31 pm
FROM (
select PL, NULL as SEQUENCE, PLDescription as ReportCol
FROM
{
--Select all Items from MAS200 excludeing then ones that are Web Exclude
SELECT dbo.IM1_InventoryMasterfile.ItemNumber as ItemNumber, dbo.IM1_InventoryMasterfile.ProductLine as PL, dbo.IMA_ProductLine.ProductLineDescription as PLDescription
FROM dbo.IM1_InventoryMasterfile INNER JOIN
dbo.IMA_ProductLine ON dbo.IM1_InventoryMasterfile.ProductLine = dbo.IMA_ProductLine.ProductLineCode LEFT OUTER JOIN
dbo.IM_90_UDF_IMMasterfile ON dbo.IM1_InventoryMasterfile.ItemNumber = dbo.IM_90_UDF_IMMasterfile.ItemNumber
WHERE (dbo.IM_90_UDF_IMMasterfile.Web_Exclude <> 'Y' or dbo.IM_90_UDF_IMMasterfile.Web_Exclude is null)
SELECT dbo.Items.ItemNumber, dbo.Items.PL, dbo.IMA_ProductLine.ProductLineDescription
FROM dbo.Items INNER JOIN
dbo.IMA_ProductLine ON dbo.Items.PL = dbo.IMA_ProductLine.ProductLineCode
)
GROUP BY PL, PLDescription
UNION
select PL, PLDescription, ItemNumber
FROM
{
(
--Select all Items from MAS200 excludeing then ones that are Web Exclude
SELECT dbo.IM1_InventoryMasterfile.ItemNumber as ItemNumber, dbo.IM1_InventoryMasterfile.ProductLine as PL, dbo.IMA_ProductLine.ProductLineDescription as PLDescription
FROM dbo.IM1_InventoryMasterfile INNER JOIN
dbo.IMA_ProductLine ON dbo.IM1_InventoryMasterfile.ProductLine = dbo.IMA_ProductLine.ProductLineCode LEFT OUTER JOIN
dbo.IM_90_UDF_IMMasterfile ON dbo.IM1_InventoryMasterfile.ItemNumber = dbo.IM_90_UDF_IMMasterfile.ItemNumber
WHERE (dbo.IM_90_UDF_IMMasterfile.Web_Exclude <> 'Y' or dbo.IM_90_UDF_IMMasterfile.Web_Exclude is null)
SELECT dbo.Items.ItemNumber, dbo.Items.PL, dbo.IMA_ProductLine.ProductLineDescription
FROM dbo.Items INNER JOIN
dbo.IMA_ProductLine ON dbo.Items.PL = dbo.IMA_ProductLine.ProductLineCode
)
) DT
ORDER BY PL, SEQUENCE
December 13, 2006 at 8:35 pm
Remove "{" and "}"
_____________
Code for TallyGenerator
December 13, 2006 at 9:21 pm
Did that but still no luck, I'm getting the following error:
Server: Msg 156, Level 15, State 1, Line 26
Incorrect syntax near the keyword 'GROUP'.
Server: Msg 170, Level 15, State 1, Line 49
Line 49: Incorrect syntax near ')'.
I really appriciate this, thank you very much.
Here is the code:
SELECT ReportCol
FROM (
select PL, NULL as SEQUENCE, PLDescription as ReportCol
FROM
(
--Select all Items from MAS200 excludeing then ones that are Web Exclude
SELECT dbo.IM1_InventoryMasterfile.ItemNumber as ItemNumber, dbo.IM1_InventoryMasterfile.ProductLine as PL, dbo.IMA_ProductLine.ProductLineDescription as PLDescription
FROM dbo.IM1_InventoryMasterfile INNER JOIN
dbo.IMA_ProductLine ON dbo.IM1_InventoryMasterfile.ProductLine = dbo.IMA_ProductLine.ProductLineCode LEFT OUTER JOIN
dbo.IM_90_UDF_IMMasterfile ON dbo.IM1_InventoryMasterfile.ItemNumber = dbo.IM_90_UDF_IMMasterfile.ItemNumber
WHERE (dbo.IM_90_UDF_IMMasterfile.Web_Exclude <> 'Y' or dbo.IM_90_UDF_IMMasterfile.Web_Exclude is null)
union
--Select all Items SemiStore DB
SELECT dbo.Items.ItemNumber, dbo.Items.PL, dbo.IMA_ProductLine.ProductLineDescription
FROM dbo.Items INNER JOIN
dbo.IMA_ProductLine ON dbo.Items.PL = dbo.IMA_ProductLine.ProductLineCode
)
GROUP BY PL, PLDescription
UNION
select PL, PLDescription, ItemNumber
FROM
(
--Select all Items from MAS200 excludeing then ones that are Web Exclude
SELECT dbo.IM1_InventoryMasterfile.ItemNumber as ItemNumber, dbo.IM1_InventoryMasterfile.ProductLine as PL, dbo.IMA_ProductLine.ProductLineDescription as PLDescription
FROM dbo.IM1_InventoryMasterfile INNER JOIN
dbo.IMA_ProductLine ON dbo.IM1_InventoryMasterfile.ProductLine = dbo.IMA_ProductLine.ProductLineCode LEFT OUTER JOIN
dbo.IM_90_UDF_IMMasterfile ON dbo.IM1_InventoryMasterfile.ItemNumber = dbo.IM_90_UDF_IMMasterfile.ItemNumber
WHERE (dbo.IM_90_UDF_IMMasterfile.Web_Exclude <> 'Y' or dbo.IM_90_UDF_IMMasterfile.Web_Exclude is null)
union
--Select all Items SemiStore DB
SELECT dbo.Items.ItemNumber, dbo.Items.PL, dbo.IMA_ProductLine.ProductLineDescription
FROM dbo.Items INNER JOIN
dbo.IMA_ProductLine ON dbo.Items.PL = dbo.IMA_ProductLine.ProductLineCode
)
) DT
ORDER BY PL, SEQUENCE
Thanks
Oren
December 13, 2006 at 9:27 pm
SELECT ReportCol
FROM (
select PL, NULL as SEQUENCE, PLDescription as ReportCol
FROM
(
--Select all Items from MAS200 excludeing then ones that are Web Exclude
SELECT dbo.IM1_InventoryMasterfile.ItemNumber as ItemNumber, dbo.IM1_InventoryMasterfile.ProductLine as PL, dbo.IMA_ProductLine.ProductLineDescription as PLDescription
FROM dbo.IM1_InventoryMasterfile INNER JOIN
dbo.IMA_ProductLine ON dbo.IM1_InventoryMasterfile.ProductLine = dbo.IMA_ProductLine.ProductLineCode LEFT OUTER JOIN
dbo.IM_90_UDF_IMMasterfile ON dbo.IM1_InventoryMasterfile.ItemNumber = dbo.IM_90_UDF_IMMasterfile.ItemNumber
WHERE (dbo.IM_90_UDF_IMMasterfile.Web_Exclude 'Y' or dbo.IM_90_UDF_IMMasterfile.Web_Exclude is null)
union
--Select all Items SemiStore DB
SELECT dbo.Items.ItemNumber, dbo.Items.PL, dbo.IMA_ProductLine.ProductLineDescription
FROM dbo.Items INNER JOIN
dbo.IMA_ProductLine ON dbo.Items.PL = dbo.IMA_ProductLine.ProductLineCode
) T1
GROUP BY PL, PLDescription
UNION
select PL, PLDescription, ItemNumber
FROM
(
--Select all Items from MAS200 excludeing then ones that are Web Exclude
SELECT dbo.IM1_InventoryMasterfile.ItemNumber as ItemNumber, dbo.IM1_InventoryMasterfile.ProductLine as PL, dbo.IMA_ProductLine.ProductLineDescription as PLDescription
FROM dbo.IM1_InventoryMasterfile INNER JOIN
dbo.IMA_ProductLine ON dbo.IM1_InventoryMasterfile.ProductLine = dbo.IMA_ProductLine.ProductLineCode LEFT OUTER JOIN
dbo.IM_90_UDF_IMMasterfile ON dbo.IM1_InventoryMasterfile.ItemNumber = dbo.IM_90_UDF_IMMasterfile.ItemNumber
WHERE (dbo.IM_90_UDF_IMMasterfile.Web_Exclude 'Y' or dbo.IM_90_UDF_IMMasterfile.Web_Exclude is null)
union
--Select all Items SemiStore DB
SELECT dbo.Items.ItemNumber, dbo.Items.PL, dbo.IMA_ProductLine.ProductLineDescription
FROM dbo.Items INNER JOIN
dbo.IMA_ProductLine ON dbo.Items.PL = dbo.IMA_ProductLine.ProductLineCode
) T2
) DT
ORDER BY PL, SEQUENCE
_____________
Code for TallyGenerator
December 14, 2006 at 10:48 am
December 15, 2006 at 6:27 am
Hi oren,
FYI, Ten Centuries is a "title" (depends basically on number of posts - e.g. it says "Newbie" at your posts, because you only posted a few times), user name can be found a bit higher, in the post header
Otherwise you're right, Sergiy is one of the true SQL gurus of these forums.
December 15, 2006 at 3:38 pm
December 15, 2006 at 7:03 pm
December 16, 2006 at 4:39 am
Good Work my friends.........But there is also..........
Another Approch!
Finally I would like to say U can do it with using GROUP BY ....... WITH ROLLUP also. And I think it will not be too big Query for so simple requirement.
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply