Sql Server从临时表中删除某些重复记录

时间:2015-05-26 00:43:18

标签: sql sql-server

我正在尝试创建一个返回股票清单的查询,如果某个维度上方没有股票,那么它将返回0股票,但如果在该维度上方返回股票,则会排除该股票没有股票的条目,

但我也想保留记录,其中有两个不同的位置,包含库存I.E在输出中的颜色" Cinder Slate"

到目前为止,我有:

DECLARE @T1 TABLE(
    brand   VARCHAR(500), 
    Colour  VARCHAR(500), 
    finish  VARCHAR(50),
    Length  INT, 
    width   INT, 
    QTY     INT, 
    loc     VARCHAR(10) 
)
DECLARE @length INT,
        @width  INT,
        @loc    VARCHAR(10),
        @Qty    INT 

SET @length = 0
SET @width  = 0 
SET @Qty    = 0
SET @loc    = 'None' 

INSERT INTO @T1
SELECT  DISTINCT 
    tblbrand.brand,
    tblcolour.colour,
    tblfinish.finish, 
    @length,
    @width, 
    @qty,
    @loc
FROM  tblcolour 
INNER JOIN tbloffcut
    ON tblcolour.colourid = tbloffcut.colourid 
INNER JOIN tblfinish 
    ON tblcolour.finishid = tblfinish.finishid 
INNER JOIN tblbrand
    ON tblcolour.brandid = tblbrand.brandid
WHERE
    tbloffcut.length < 3600
    AND tbloffcut.width < 1500
    AND  tblbrand.brand = 'Arborite'

INSERT INTO @t1
SELECT 
    tblbrand.brand,
    tblcolour.colour,
    tblfinish.finish, 
    tbloffcut.length,
    tbloffcut.width,
    tbloffcut.quantity,
    tbloffcut.location
FROM tblbrand
INNER JOIN tblcolour
    ON tblbrand.brandid = tblcolour.brandid 
INNER JOIN tbloffcut 
    ON tblcolour.colourid = tbloffcut.colourid 
INNER JOIN tblfinish
    ON tblbrand.brandid = tblfinish.brandid
    AND tblcolour.finishid = tblfinish.finishid
WHERE
    tblbrand.brand = 'Arborite'
    AND tbloffcut.length >= 3600
    AND tbloffcut.width >= 1500
ORDER BY 
    tblcolour.colour

SELECT * 
FROM @t1 
ORDER BY 
    Colour,
    Length DESC,
    width DESC

enter image description here

0 个答案:

没有答案