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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Complex Sql Query help

Author  Topic 

direrayne
Starting Member

30 Posts

Posted - 2011-09-11 : 14:10:13
The code below gives me exactly what i need at this time.
all of the fields in the query are displayed along with the "locncode" which is the location number of a given store.
however i was just asked to combine the results of 3 of the locations into one. Example, locncode 2 & 3 & 4 rolled into one, as locncode 2.
i am having a very rough time trying to do this. may someone have a look and help if they can?


SELECT  x.* ,
vi.VNDITNUM ,
vi.LSTORDDT ,
vi.LSRCPTDT
FROM ( SELECT
DISTINCT ISNULL([BuyerID], 'ND') AS BUYERID ,
[VENDORID] ,
[LOCNCODE] ,
[ITEMNMBR] ,
[ITEMDESC] ,
MAX([LASTSOLD]) AS LASTSOLD ,
SUM([QTYSOLD]) AS QTYSOLD ,
MAX([QTYOHND]) AS QTYOHND ,
MAX([QTYONORD]) AS QTYONORD ,
SUM([XTNDPRCE]) AS XTNDPRCE ,
SUM([EXTDCOST]) AS EXTDCOST ,
MAX([COSTUNIT]) AS COSTUNIT ,
MAX([RETAILPRICE]) AS RETAILPRICE ,
( ( ( SUM([XTNDPRCE]) - SUM([EXTDCOST]) )
/ CASE WHEN SUM([XTNDPRCE]) = 0 THEN 1
ELSE SUM([XTNDPRCE])
END ) * 100 ) AS MARGIN ,
MAX([ReceiptDate]) AS ReceiptDate ,
SALE AS SALE
FROM ITEMCONSOLIDATION_v2
WHERE ( docdate >= @FromDocumentDate
AND docdate <= @ToDocumentDate
)
AND ( @VendorID = 'ALL'
OR VendorID = @VendorID
)
AND ( @BuyerID = 'ALL'
OR BuyerID = @BuyerID
)
AND ( @Store = 'ALL'
OR Locncode = @Store
)
AND ( @Sale = 'ALL'
OR Sale = @Sale
)
GROUP BY [VENDORID] ,
[LOCNCODE] ,
[BuyerID] ,
[ITEMNMBR] ,
[ITEMDESC] ,
SALE
UNION
SELECT
DISTINCT ISNULL(DEST.BUYERID, 'NOT DEFINED') AS BUYERID ,
DEST.VENDORID AS VENDORID ,
DEST.LOCNCODE AS LOCNCODE ,
DEST.ITEMNMBR AS ITEMNMBR ,
DEST.ITEMDESC AS ITEMDESC ,
0 AS LASTSOLD ,
0 AS QTYSOLD ,
MAX(DEST.QTYONHND) AS QTYOHND ,
MAX(DEST.QTYONORD) AS QTYONORD ,
0 AS XTNDPRCE ,
0 AS EXTDCOST ,
MAX(DEST.CURRCOST) AS UNITCOST ,
MAX(DEST.RETAILPRICE) AS RETAILPRICE ,
0 AS Margin ,
MAX(ISNULL(P.ReceiptDate, '1900-01-01')) AS ReceiptDate ,
DEST.SALE AS SALE
FROM dbo.r1_NoSales DEST
LEFT OUTER JOIN dbo.R2a_Purchasev10_Grouping AS P ON DEST.ITEMNMBR = P.itemnmbr
AND DEST.LOCNCODE = P.locncode
WHERE ( ( DEST.LOCNCODE <> '' )--AND DEST.QTYONHND>0)
AND NOT EXISTS ( SELECT 1
FROM dbo.R1_PurchaseSalesAnalysisv10_v2 S
WHERE ( DEST.LOCNCODE = S.LOCNCODE
AND DEST.ITEMNMBR = S.ITEMNMBR
)
AND ( S.docdate >= @FromDocumentDate
AND S.docdate <= @ToDocumentDate
) )
)
AND ( @VendorID = 'ALL'
OR VendorID = @VendorID
)
AND ( @BuyerID = 'ALL'
OR DEST.BuyerID = @BuyerID
)
AND ( @Store = 'ALL'
OR DEST.Locncode = @Store
)
AND ( @Sale = 'ALL'
OR DEST.SALE = @Sale
)
GROUP BY DEST.BUYERID ,
DEST.VENDORID ,
DEST.LOCNCODE ,
DEST.ITEMNMBR ,
DEST.ITEMDESC ,
DEST.SALE
) x
LEFT OUTER JOIN ICL.dbo.IV00103 AS vi ON x.VENDORID = vi.VENDORID
AND x.ItemNMBR = vi.ITEMNMBR

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-12 : 00:51:36
replace the occurances of [LOCNCODE] in SELECT and GROUP BY in above query with below and it should work as you expect

CASE WHEN [LOCNCODE] IN (2,3,4) THEN 2 ELSE [LOCNCODE] END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -