我怎样才能删除组结果SQL

时间:2019-05-09 09:43:16

标签: sql sql-server

我有一个SQL查询,但它给了我更多我想要的结果。例如,当我运行查询时,我将从2种产品中获得20条结果。每个产品都有一个 <SELECT id=alternateIds[2].type.description class=smalltext name=alternateIds[2].type.code value=""> <OPTION selected></OPTION> <OPTION value=AML>AML</OPTION> <OPTION value=ALC>Alacra ID</OPTION> <OPTION value=BOS>BOSS</OPTION> <OPTION value=BKA>Bankers Almanac ID</OPTION> ` ` ` ` </SELECT> ,并且g.linkedline的org.openqa.selenium.JavascriptException: JavaScript error (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:42:44.417Z' System info: host: 'VKRDAP0009714', ip: '30.206.79.17', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_202' Driver info: org.openqa.selenium.ie.InternetExplorerDriver Capabilities [{browserAttachTimeout=0, enablePersistentHover=false, ie.forceCreateProcessApi=false, pageLoadStrategy=normal, unhandledPromptBehavior=dismiss, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=true, browserName=internet explorer, initialBrowserUrl=http://localhost:45547/, takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=false, platformName=WINDOWS, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=dismiss}] Session ID: f0f347a8-b5c6-4bf1-bd89-576498a53872 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167) at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40) at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82) at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:646) at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:582) at com.bofa.ivv.application.driver.tandem.cucumber.step_definitions.CreateParty.selectFromDropDown(CreateParty.java:434) 必须忽略,并且不显示在结果中。

这是我的查询

我尝试在我的group by上做一个group by。我也尝试过select,但它给了我1个结果。

select

4 个答案:

答案 0 :(得分:1)

我会这样尝试,您可以使用窗口函数row_number强制排名。如果您对每个唯一的物品代码或房屋只感兴趣1件物品,则将您感兴趣的物品替换为Partition by子句,然后在order by子句中指定要订购的物品。例如,在下面的查询中,感兴趣的项目是ItemCode,我们正在仓库和aantal desc上订购(我假设您要查看的是aantal而不是更少的项目,但是如果您适合,则可以删除desc需求。

select * from
(
SELECT row_number()over(partition by i.Itemcode order by g.warehouse, g.aantal desc) as rowno, i.ItemCode, g.warehouse, SUM(g.aantal) AS Voorraad, MAX(CASE WHEN g.transtype = 'N' THEN g.sysmodified ELSE NULL END) AS LastDate
FROM    dbo.gbkmut AS g INNER JOIN  dbo.Items AS i
        ON g.artcode = i.ItemCode AND g.reknr = i.GLAccountDistribution AND (
                                                g.transtype = 'N' OR
                                                (g.transtype = 'B' AND 
                                                g.transsubtype = 'B' AND 
                                                g.freefield1 NOT IN ('B', 'Q', 'W', 'K') AND 
                                                g.BlockItem = '0' AND 
                                                g.AllocationType = 'F') OR ( g.transtype = 'B' AND g.transsubtype = 'B' AND g.freefield1 = 'V' AND g.AllocationType = 'B'))
WHERE g.transtype = 'N' AND (i.ItemCode = 'TESTARTIKEL' OR i.ItemCode = 'IQ7-60-2-INT') GROUP BY i.ItemCode, g.warehouse, g.LinkedLine 
) x where rowno=1;

答案 1 :(得分:0)

SELECT i.ItemCode, g.warehouse, SUM(g.aantal) AS Voorraad, 
MAX(CASE WHEN g.transtype = 'N' THEN g.sysmodified ELSE NULL END) AS LastDate
FROM    dbo.gbkmut AS g INNER JOIN  dbo.Items AS i
    ON g.artcode = i.ItemCode AND g.reknr = i.GLAccountDistribution AND (
                                            g.transtype = 'N' OR
                                            (g.transtype = 'B' AND 
                                            g.transsubtype = 'B' AND 
                                            g.freefield1 NOT IN ('B', 'Q', 'W', 'K') AND 
                                            g.BlockItem = '0' AND 
                                            g.AllocationType = 'F') OR ( g.transtype = 'B' AND g.transsubtype = 'B' AND g.freefield1 = 'V' AND g.AllocationType = 'B'))
WHERE g.transtype = 'N' AND i.ItemCode = 'IQ7-60-2-INT' GROUP BY i.ItemCode, g.warehouse, g.LinkedLine

这将首先返回结果Voorraad 0,并且使用DwightReynoldson脚本返回1080

答案 2 :(得分:0)

我发现了问题。谢谢大家!

Voorraad有一个描述,那就是为什么结果不正确。

这是代码

select * from
(
SELECT 
    i.ItemCode, 
    g.warehouse, 
    SUM(g.aantal) AS Voorraad, 
    MAX(CASE 
        WHEN g.transtype = 'N' THEN g.sysmodified 
        ELSE NULL 
    END) AS LastDate,
    row_number()
        over
            (partition by 
                i.Itemcode 
                order by 
                    g.warehouse 
                    ) as rowno
FROM    dbo.gbkmut AS g 
    INNER JOIN  dbo.Items AS i
        ON g.artcode = i.ItemCode 
            AND g.reknr = i.GLAccountDistribution 
            AND ( g.transtype = 'N' 
                    OR ( g.transtype = 'B' 
                          AND g.transsubtype = 'B' 
                          AND g.freefield1 NOT IN ( 'B', 'Q', 'W', 'K' ) 
                          AND g.blockitem = '0' 
                          AND g.allocationtype = 'F' ) 
                     OR ( g.transtype = 'B' 
                          AND g.transsubtype = 'B' 
                          AND g.freefield1 = 'V' 
                          AND g.allocationtype = 'B' ) ) 
WHERE  g.transtype = 'N' 
       AND ( i.itemcode = 'TESTARTIKEL' 
              OR i.itemcode = 'IQ7-60-2-INT' ) 
GROUP  BY i.itemcode, 
          g.warehouse, 
          g.linkedline
) x where rowno=1 ;    

答案 3 :(得分:-1)

您可以使用:

select *****Here the columns that you want to use****** from (****Your query****) anyName

在您的情况下:

select *****Here the columns that you want to use****** from (
SELECT i.ItemCode, g.warehouse, SUM(g.aantal) AS Voorraad, MAX(CASE WHEN g.transtype = 'N' THEN g.sysmodified ELSE NULL END) AS LastDate
FROM    dbo.gbkmut AS g INNER JOIN  dbo.Items AS i
        ON g.artcode = i.ItemCode AND g.reknr = i.GLAccountDistribution AND (
                                                g.transtype = 'N' OR
                                                (g.transtype = 'B' AND 
                                                g.transsubtype = 'B' AND 
                                                g.freefield1 NOT IN ('B', 'Q', 'W', 'K') AND 
                                                g.BlockItem = '0' AND 
                                                g.AllocationType = 'F') OR ( g.transtype = 'B' AND g.transsubtype = 'B' AND g.freefield1 = 'V' AND g.AllocationType = 'B'))
WHERE g.transtype = 'N' AND (i.ItemCode = 'TESTARTIKEL' OR i.ItemCode = 'IQ7-60-2-INT') GROUP BY i.ItemCode, g.warehouse, g.LinkedLine 

) Name