从查询创建的表中获取最大的“ dauer”条目

我知道这是SQL的常见问题,我已经找到了很多有关如何执行此操作的解释,但是我无法弄清楚当我想要获取最大条目的表时该怎么做( dauer)从查询已创建的表中获取。

我正在使用SQL Server数据库。我真的希望你们能给我一个提示。

SELECT 
    foo.vname,foo.nname,foo.sw_name,SUM(foo.dauer) AS dauer 
FROM
    (SELECT 
         benutzer.vname,benutzer.nname,verwendung.sw_name,verwendung.dauer
     FROM 
         benutzer 
     INNER JOIN 
         verwendung ON benutzer.login = verwendung.login 
                    AND benutzer.pw = verwendung.pw
     GROUP BY 
         benutzer.login,benutzer.pw,verwendung.dauer
     ORDER BY 
         benutzer.vname ASC,benutzer.nname ASC,verwendung.sw_name ASC) AS foo
GROUP BY 
    foo.vname,foo.sw_name

编辑:澄清我想知道的事情

我想获取表中dauer列中具有最大值的那些条目。

例如:

对于Ada,应该是Ada-Lovelace-Presmaker-910

从查询创建的表中获取最大的“ dauer”条目

wangzhenhua7 回答:从查询创建的表中获取最大的“ dauer”条目

我正在尝试了解您的需求,如果我是对的,那么可以做到: 如您所说:

  

我想从已经存在的表中获取最大条目(“ dauer”)   由查询创建

class PadDraw extends JComponent{
Image image;
//this is gonna be your image that you draw on
Graphics2D graphics2D;
//this is what we'll be using to draw on
int currentX,currentY,oldX,oldY;
//these are gonna hold our mouse coordinates
int firstX;
int firstY;

//Now for the constructors
//will draw from tail to head
public PadDraw(){
    setDoubleBuffered(false);
    addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent e){
            if (currentX == 0 && currentY == 0) {
                firstX= e.getX();
                firstY = e.getY();
                oldX = e.getX();
                oldY = e.getY();
            }
            currentX = e.getX();
            currentY = e.getY();
            graphics2D.drawLine(oldX,oldY,currentX,currentY);
            repaint();
            oldX = currentX;
            oldY = currentY;
        }
    });
}

public void paintComponent(Graphics g){
    if(image == null){
        image = createImage(getSize().width,getSize().height);
        graphics2D = (Graphics2D) image.getGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2D.setComposite(AlphaComposite.Clear);
        graphics2D.fillRect(0,getSize().width,getSize().height);
        graphics2D.setComposite(AlphaComposite.Src);
        clear();
    }
    g.drawImage(image,null);
}
//this is the painting bit
//if it has nothing on it then
//it creates an image the size of the window
//sets the value of Graphics as the image
//sets the rendering
//runs the clear() method
//then it draws the image

public void clear(){
    currentX = 0;
    currentY = 0;

    graphics2D.setPaint(Color.white);
    graphics2D.fillRect(0,getSize().height);
    graphics2D.setPaint(Color.black);
    repaint();
}
//this is the clear
//it sets the colors as white
//then it fills the window with white
//thin it sets the color back to black

public void save(){
    repaint();
    if (currentX != 0 && currentY != 0) {
        graphics2D.drawLine(oldX,firstX,firstY);
        currentX = 0;
        currentY = 0;
    }

    try {
        BufferedImage bfrdImage = new BufferedImage
    (image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_ARGB);
        // Draw the image on to the buffered image
        Graphics2D bGr = bfrdImage.createGraphics();

        bGr.setComposite(AlphaComposite.Clear);
        bGr.fillRect(0,getSize().height);
        bGr.setComposite(AlphaComposite.Src);
        bGr.drawImage(image,null);

        javax.imageio.ImageIO.write(bfrdImage,"PNG",new File("Drawing.PNG"));
        bGr.dispose();
    } catch (Exception ex) {
        Logger.getLogger(PadDraw.class.getName()).log(Level.SEVERE,null,ex);
    }
}
//saves and also comes back to the first point to finalize the shape
}

根据您的数据库是什么,查询可能会有所不同...

,

所以几个小时后,我想出了办法:

SELECT vname,nname,sw_name,dauer
FROM

(SELECT login,pw,dauer
  FROM 
    (SELECT sw_hersteller,benu.login,benu.pw,sum(dauer) dauer
    FROM benutzer benu,verwendung verw
    WHERE benu.pw = verw.pw AND benu.login=verw.login
    GROUP BY sw_hersteller,benu.pw
    ORDER BY vname asc,nname asc) AS foo
  GROUP BY foo.login,foo.pw,foo.sw_name,foo.dauer
  HAVING foo.dauer >= ALL(
  SELECT sum(verwendung.dauer)
  FROM verwendung
  WHERE foo.pw = verwendung.pw AND foo.login = verwendung.login
  GROUP BY login,sw_hersteller))as faa 
INNER JOIN benutzer b
ON b.login = faa.login AND b.pw = faa.pw

ORDER BY vname ASC,nname ASC,sw_name ASC
本文链接:https://www.f2er.com/3170055.html

大家都在问