SQL聚合总和仅净出负行

时间:2016-04-23 00:53:43

标签: sql sql-server

我尝试根据日期汇总产品值。以下示例从20,000开始,增加5,000,然后减去7,000。结果应该是通过整个5,000吃,然后进入先前的积极行。这将删除5,000行。

我认为这就像执行按日期降序排序的和窗函数一样简单。但是,正如你在下面看到的那样,我想在任何保持正数的行上停止求和然后移动到下一行。

我无法弄清楚SQL中的逻辑使其工作。在我看来,它应该是:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;

        public TestPane() {
            setLayout(new BorderLayout());
            try {
                label = new JLabel(new ImageIcon(ImageIO.read(...)));
                label.setAutoscrolls(true);
                JScrollPane scrollPane = new JScrollPane(label, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                scrollPane.setBorder(null);
                add(scrollPane);

                MouseAdapter ma = new MouseAdapter() {

                    private Point origin;

                    @Override
                    public void mousePressed(MouseEvent e) {
                        origin = new Point(e.getPoint());
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }

                    @Override
                    public void mouseDragged(MouseEvent e) {
                        if (origin != null) {
                            JViewport viewPort = (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, label);
                            if (viewPort != null) {
                                int deltaX = origin.x - e.getX();
                                int deltaY = origin.y - e.getY();

                                Rectangle view = viewPort.getViewRect();
                                view.x += deltaX;
                                view.y += deltaY;

                                label.scrollRectToVisible(view);
                            }
                        }
                    }

                };

                label.addMouseListener(ma);
                label.addMouseMotionListener(ma);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

但是在一行中可能存在多个正值行,其中负值行可以在所有行中吃掉,或者可能存在多个负值连续。

This post看起来很有希望,但我不认为如果负值大于正值,逻辑就会起作用。

有:

public class ClassToTest {

private static final boolean CONF_FLAG = Configuration.getConfig()
.get(Status.Initialization).getConfFlag(); // throws an NPE

public methodToTest(TestObject a){
...
 }
}

想要:

public class TestClassToTest{

 TestObject a; 
 ClassToTest t;
    @Before
public void setUp() throws Exception {
     setFinalStatic(ClassToTest.class.getDeclaredField("CONF_FLAG"), true);// this fails!
    a = mock(TestObject.class);
    t =  new ClassToTest();
}
static void setFinalStatic(Field field, Object newValue) throws Exception {
    field.setAccessible(true);        
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(null, newValue);
}

2 个答案:

答案 0 :(得分:2)

我只能想到一个递归的cte解决方案

UPDATE Decks 
JOIN Amount ON amount.DeckName = decks.DeckName 
SET decks.DeckTotal = Decks.DeckTotal - Decks.DeckTotal
WHERE Amount.AmountName = @aName;

UPDATE Types t1
JOIN Cards ON cards.TypeName = t1.TypeName 
JOIN Amount ON amount.CardName = Cards.CardName
SET t1.TypeTotal = t1.TypeTotal - Amount.Amount
WHERE Amount.CardName = @aName;

DELETE * 
FROM Amount 
WHERE CardName = @aName;

答案 1 :(得分:-1)

我认为你想要这个:

select Date,
       Product,
       Sum(Value) As Value
  From TABLE_NAME
 Group By Date, Product
 Order by Date, Product;
那是正确的吗?