3

我是论坛和 SQL 的新手,非常感谢所有信息。我找不到适合我的解决方案,所以我想在这里试一试。在雪花中经营。我的数据集看起来像这样(为麻烦的格式道歉):

PO  | DIV | PROD |  QTY |   CUST
123 | 1  | x |  10 |    Sonic
234 |   1 | x   | 9 |   Sonic
345 | 1 |   x | 8   | McD
456 | 1 | x | 10    | Wendy's

我想按 DIV、PROD 和 CUST 对 QTY 求和。一旦我有了这些总和的数量,我想按 DIV 和 PROD 取最大的 SUM(QTY),但保留 CUST 字段。因此,上面的答案如下所示:

1 | x | 19| Sonic

请注意,我显然有一个更大的数据库,因此我将列出所有独特的产品/部门组合以及 max(sum()),大约 600K 行。

我在这里的代码使我达到了 sum(QTY) 但现在我需要在保留 CUST 字段的同时拉出具有最大 sum(QTY) 的行。你能帮我吗?我发现了一些关于 best-n-per-group 的信息,但不确定这是否是我应该使用的以及如何使用

SELECT DIV, PROD, CUST, SUM(QTY) as QTY
from table
GROUP BY 
    DIV,
    PROD,
    CUST
Order by 
    DIV,
    PROD

编辑:Jay 的 CTE 方法对我有用,但我忘了提到我想用它创建一个表。CREATE OR REPLACE TABLE 不适用于 CTE。使用 CTE 方法或其他方法创建表的任何方法?

谢谢!

4

4 回答 4

3

MYSQL

SELECT `DIV`, PROD, CUST,QTY from 
(
SELECT `DIV`, PROD, CUST, SUM(QTY) as QTY from 
table1 
GROUP BY `DIV`, PROD, CUST Order by `DIV`, PROD
) AS T
WHERE QTY=(SELECT MAX(QTY) FROM (SELECT `DIV`, PROD, CUST, SUM(QTY) as QTY from 
table1 
GROUP BY `DIV`, PROD, CUST Order by `DIV`, PROD) AS T)

或者

SELECT `DIV`, PROD, CUST,QTY from 
(
SELECT `DIV`, PROD, CUST, SUM(QTY) as QTY from 
table1 
GROUP BY `DIV`, PROD, CUST Order by `DIV`, PROD
) AS T ORDER BY QTY DESC LIMIT 1;

现场演示

http://sqlfiddle.com/#!9/f945c2b/19

SQL 服务器

使用 CTE

WITH CTE AS
(
 SELECT DIV, PROD, CUST, SUM(QTY) as QTY,
 DENSE_RANK() OVER (PARTITION BY DIV, PROD ORDER BY SUM(QTY) DESC) AS Rank
 FROM 
 table1 
 GROUP BY DIV, PROD, CUST
)
SELECT DIV, PROD, CUST,QTY FROM CTE
WHERE Rank=1
ORDER BY DIV, PROD

使用嵌套查询

SELECT DIV, 
       PROD, 
       CUST,
       QTY 
FROM ( 
      SELECT DIV, PROD, CUST, SUM(QTY) as QTY, 
      DENSE_RANK() OVER (PARTITION BY DIV, PROD ORDER BY SUM(QTY) DESC) AS Rank
      FROM table1 
      GROUP BY DIV, PROD, CUST )AS T1 
 WHERE Rank=1 ORDER BY DIV, PROD;

现场演示

http://sqlfiddle.com/#!18/22001/11

于 2018-03-27T05:05:34.733 回答
0

在 MySQL 中试试这个:

SELECT A.`DIV`, A.PROD, IF(A.`DIV`=0,'NAN',A.AGG_QTY/A.`DIV`) QTY, A.CUST
FROM (SELECT `DIV`, PROD, CUST, SUM(QTY) AGG_QTY
      FROM `TABLE` 
      GROUP BY `DIV`, PROD, CUST
      ORDER BY SUM(QTY) DESC
      LIMIT 1) A;

查看它在SQL Fiddle上运行

于 2018-03-27T05:55:31.340 回答
0

sum(qty)您可以通过having子句过滤您的聚合

select 
       DIV, PROD, CUST, SUM(QTY) as QTY 
from table
group by DIV, PROD, CUST
having sum(QTY) = ( select max(QTY) from (
                    select sum(QTY) as QTY from table
                    group by DIV, PROD, CUST)a)
于 2018-03-27T05:48:50.133 回答
-1
private void button2_Click(object sender, EventArgs e)
{
    string selectQuery = "SELECT `username`, `password` FROM `users` WHERE `username` = '"+ username.Text + "' AND `password` = '" + password.Text +"';";
    MySqlDataAdapter adapter = new MySqlDataAdapter(selectQuery, DBConnect);
    DataTable table = new DataTable();
    adapter.Fill(table);
    if (table.Rows.Count <= 0)
    {
        // label_Message.ForeColor = Color.Red;
        // label_Message.Text = "Username Or Password Are Invalid";
        //timer1.Start();
        MessageBox.Show("Username Or Password Are Invalid");
    }
    else
    {
        //  panel1.Height = 0;
        //  label_Message.ForeColor = Color.Green;
        //  label_Message.Text = "Login Successfully";
        //   timer1.Start();
        MessageBox.Show("Login Successfully");
        admin1 admin1 = new admin1();
        admin1.Show();
        this.Hide();
    }
    table.Clear();
}
于 2018-03-27T06:04:23.233 回答