通过汇总查询中的子查询进行选择

时间:2018-10-03 15:27:37

标签: sql sql-server

我正在尝试获取商店列表。我希望结果中包含以下列:

  • 地址
  • paid_amount
  • jobs_count
  • items_count

以下查询为我提供了所有这些信息,除了@using (Ajax.BeginForm("Save", "PaymentLandingPage", new AjaxOptions { HttpMethod = "Post", InsertionMode = InsertionMode.Replace, })) { <table class="PEmailFields"> <tr> <td> <div style="float:left;"> <ul style="list-style-type: none;"> <li><span id="PEmaillbl">Payment Notification Email:</span></li> <li><br /></li> </ul> </div> <div style="float:right;"> <ul style="list-style-type: none;"> @Html.HiddenFor(m => m.PrimaryNamedInsured) <li>@Html.TextBoxFor(m => m.PaymentNotificationEmail, new { @id = "PEmailtxbx", placeholder = "Example: ppp@test.com", maxlength = "100", style = "margin-left:25px;min-width:530px;", Value = Model.PrimaryNamedInsured.PrimaryEmail })</li> <li><span id="PEmaildsclmr" data-PEmailValue="@Model.PaymentNotificationEmail"><i style="margin-left:190px;font-size:small;">* This email address is used for payment notifications.</i></span></li> </ul> </div> </td> </tr> <tr> <td></td> </tr> </table> <input style="margin-left:35%;" type="submit" value="Save Email" /> }

package sandbox.xmlworker;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import sandbox.WrapToTest;

@WrapToTest
public class D02_ParseHtml {

    public static final String HTML = "resources/xml/walden.html";
    public static final String DEST = "results/xmlworker/walden1.pdf";

    /**
     * Html to pdf conversion example.
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML));
        // step 5
        document.close();
    }

    /**
     * Main method
     */
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new D02_ParseHtml().createPdf(DEST);
    }
}

我试图通过添加子选择来通过作业获取属于商店的商品数量:

items_count

...这给了我以下错误:

  

选择列表中的'jobs.id'列无效,因为它既不包含在聚合函数中也不在GROUP BY子句中。

在此查询中提取SELECT s.address AS address, COALESCE(SUM(j.paid_amount), 0) AS paid_amount, COUNT(j.id) AS jobs_count FROM stores s LEFT OUTER JOIN jobs_coordinates jc ON jc.store_id = s.id FULL OUTER JOIN jobs j ON j.id = jc.job_id GROUP BY s.id, s.address 的正确方法是什么?

3 个答案:

答案 0 :(得分:0)

没有理由混合使用不同类型的外部联接。我建议您只使用子查询来预先汇总结果:

SELECT s.address AS address,
       COALESCE(SUM(j.paid_amount), 0) AS paid_amount,
       COUNT(j.id) AS jobs_count,
       SUM(j.item_count) as item_count
FROM stores s LEFT JOIN
     jobs_coordinates jc
     ON jc.store_id = s.id LEFT JOIN
     (SELECT j.id, COUNT(*) as item_count
      FROM jobs j LEFT JOIN
           job_items ji
           ON ji.job_id = j.id
      GROUP BY j.id
     ) j
     ON j.id = jc.job_id
GROUP BY s.id, s.address

答案 1 :(得分:0)

尝试一下。

    SELECT
       s.address AS address,
       COALESCE(SUM(j.paid_amount), 0) AS paid_amount,
       SUM(jobs.items_count) AS jobs_count
FROM stores s 
LEFT OUTER JOIN jobs_coordinates jc ON jc.store_id = s.id
FULL OUTER JOIN 
(SELECT COUNT(*) as items_count, id FROM job_items group by id) jobs ON jobs.id = jc.job_id
GROUP BY s.id, s.address

答案 2 :(得分:0)

我想要:

import sys
from PyQt4 import QtCore, QtGui


class ListWidget(QtGui.QListWidget):
    def __init__(self, parent=None):
        super(ListWidget, self).__init__(parent)
        self.setSelectionMode(QtGui.QListWidget.MultiSelection)

    @QtCore.pyqtSlot()
    def selectAll(self):
        for i in range(self.count()):
            it = self.item(i)
            if it is not None:
                it.setSelected(True)

    @QtCore.pyqtSlot()
    def clearSelection(self):
        for i in range(self.count()):
            it = self.item(i)
            if it is not None:
                it.setSelected(False)


class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        lay = QtGui.QVBoxLayout(self)

        button_sel = QtGui.QPushButton("Select All")
        button_unsel = QtGui.QPushButton("Clear Selection")

        self.list_widget = ListWidget()

        for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
            it = QtGui.QListWidgetItem(letter)
            self.list_widget.addItem(it)

        button_sel.clicked.connect(self.list_widget.selectAll)
        button_unsel.clicked.connect(self.list_widget.clearSelection)

        lay.addWidget(button_sel)
        lay.addWidget(button_unsel)
        lay.addWidget(self.list_widget)     


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())