简体   繁体   English

从一个表中选择不同的值并连接到另一个表

[英]Select distinct values from one table and join with another table

I have a problem that I've spent way to much time trying to figure out, with close to no success at all.. I'll try to describe the problem as good as I can, and use an example, which is the solution I use right now.我有一个问题,我花了很多时间试图弄清楚,但几乎没有成功。我会尽量描述这个问题,并使用一个例子,这是解决方案我现在用。

I have two different MS SQL tables.我有两个不同的 MS SQL 表。

Table 1:表格1:

  • itemNumber - 192031, 533853 etc. itemNumber - 192031、533853 等。
  • date - the date the database post was added date - 添加数据库帖子的日期
  • quantity - the amount of items for each item number数量 - 每个项目编号的项目数量

Table 2:表 2:

  • MTITNO - also item number, contains many different item numbers (more than Table 1) MTITNO - 也是项目编号,包含许多不同的项目编号(比表 1 多)
  • MTTRDT - the date the database post was added MTTRDT - 添加数据库帖子的日期
  • MTTYP - transaction type. MTTYP - 交易类型。 I will be looking for MTTYP = 11我将寻找 MTTYP = 11
  • MTTRQT - transaction quantity. MTTRQT - 交易数量。 I will be looking for MTTRQT < 0我将寻找 MTTRQT < 0

So what I want to do is to get DISTINCT itemNumber between two dates from Table 1. Once I have those item numbers, I would like to join Table 2 on item number, and also between the same dates that I use in the query for Table 1. I also need to only get the values from Table 2 where MTTYP = 11 and MTTRQT < 0 and SUM MTTRQT.所以我想要做的是从表 1 中的两个日期之间获取 DISTINCT itemNumber。一旦我有了这些项目编号,我想加入表 2 的项目编号,以及我在查询表中使用的相同日期之间1. 我也只需要从表 2 中获取值,其中 MTTYP = 11 和 MTTRQT < 0 和 SUM MTTRQT。

I've sorted this by using loops in java code, which isn't that good to be honest.我已经通过在 Java 代码中使用循环对其进行了排序,老实说这不太好。 What I do is this:我做的是这样的:

  1. SELECT DISTINCT itemNumber "itemNumber" SELECT DISTINCT itemNumber "itemNumber"
    FROM Table 1从表 1
    WHERE date BETWEEN @fromDate AND @toDate;在@fromDate 和@toDate 之间的日期;

  2. Take the top value from this result (that is the first item number) and then:从此结果中取最高值(即第一个项目编号),然后:

  3. SELECT Sum(MTTRQT) "SUM_MTTRQT_"选择总和(MTTRQT)“SUM_MTTRQT_”
    FROM Table 2从表 2
    WHERE MTITNO = "the first item number from the result query from above" WHERE MTITNO = "上面查询结果的第一个项目编号"
    AND MTTTYP = 11和 MTTTYP = 11
    AND MTTRDT BETWEEN @fromDate AND @toDate和 MTTRDT 在 @fromDate 和 @toDate 之间
    AND MITTRA.MTTRQT < 0和 MITTRA.MTTRQT < 0

  4. Add the result to a new list.将结果添加到新列表中。 Remove the item number used删除使用的项目编号

  5. Loop through all the item numbers in the list and run step 3 and 4 for every single item number (this is the bad part).循环遍历列表中的所有项目编号,并对每个项目编号运行第 3 步和第 4 步(这是不好的部分)。

Surely there must be a SQL query that produces the same result!?当然必须有一个 SQL 查询产生相同的结果!?

Appreciate any help I can get!感谢我能得到的任何帮助!

Update: This is the data I have.更新:这是我拥有的数据。
Table 1表格1

|Item number | Quantity | date
 192031      |    1     | 20190521
 192031      |    1     | 20190522
 19192301    |    2     | 20190521
 19189507    |    1     | 20190523
 19189507    |    1     | 20190521
 19189507    |    1     | 20190524

Table 2表 2

|MTITNO    | MTTRDT   | MTTTYP | MTTRQT
 192031    | 20190520 | 11     | -1
 192031    | 20190520 | 11     | -1
 192031    | 20190520 | 11     | -1
 192031    | 20190520 | 11     | -1
 19189507  | 20190520 | 11     | -1
 19189507  | 20190520 | 11     | -1
 19189507  | 20190520 | 11     | -1
 19189507  | 20190520 | 11     | -1
 19189507  | 20190521 | 11     | -1
 19189507  | 20190521 | 11     | -1
 19189507  | 20190521 | 11     | -1

Table 2 contains all sorts of item numbers (that is item numbers that you can find in Table 2, but not in Table 1), and many more posts.表 2 包含各种项目编号(即您可以在表 2 中找到的项目编号,但在表 1 中找不到),以及更多帖子。 There can be posts in Table 1 and no posts in Table 2 for one or more item numbers.对于一个或多个项目编号,表 1 中可以有帖子,表 2 中没有帖子。

I want to summarise the MTTRQT for all items where the item number is in both Table 1 and Table 2 and within the date span I have set.我想总结一下项目编号在表 1 和表 2 中以及在我设置的日期范围内的所有项目的 MTTRQT。 The "amount used" in the desired result below is MTTRQT added up for every single item number.下面所需结果中的“使用量”是每个单个项目编号的 MTTRQT 相加。

Desired result想要的结果
So if I look for all the item numbers with date between 20190520 - 20190524, I should get the list below.因此,如果我查找日期在 20190520 - 20190524 之间的所有项目编号,我应该得到下面的列表。
"Item number" is supposed to be DISTINCT item numbers from Table 1. “项目编号”应该是表 1 中的 DISTINCT 项目编号。

"Amount used" is the SUM function, that sums MTTRQT where all the conditions are met. “使用量”是 SUM 函数,它对满足所有条件的 MTTRQT 求和。

|Item Number | Amount used
 192031      | -4     
 19189507    | -7 

Reading through the lines a bit, but is this not what you're after?稍微通读一下,但这不是你想要的吗?

SELECT SUM(T2.MTTRQT) AS [SUM_MTTRQT_]
FROM [Table 2] T2
     LEFT JOIN (SELECT TOP (1)
                       T1.ItemNumber
                FROM [Table 1] T1
                WHERE T1.[date] BETWEEN @fromDate AND @toDate --Note, if [date] has a time portion, this is unlikely to work as you expect
                ORDER BY T1.ItemNumber) T1 ON T2.MTITNO = T1.ItemNumber --Assumed ORDER BY clause
WHERE T2.MTTTYP = 11
  AND T2.MTTRDT BETWEEN @fromDate AND @toDate --Note, if MTTRDT has a time portion, this is unlikely to work as you expect
  AND T2.MITTRA.MTTRQT < 0;

If I am following your logic correctly:如果我正确地遵循您的逻辑:

select sum(mttrqt)
from table2 t2
where t2.mtitno in (select t1.itemno
                    from table1 t1
                    where t1.date >= @date1 and t1.date <= @date2
                   ) and
      t2.mttrdt >= @date1 and
      t2.mttrdt <= @date1 and
      t2.mttype = 11 and
      t2.mttrqt < 10;

Have you tried this:你有没有试过这个:

SELECT Sum(MTTRQT) "SUM_MTTRQT_"
FROM Table 2
WHERE MTITNO in (SELECT DISTINCT itemNumber "itemNumber"
FROM Table 1
WHERE date BETWEEN @fromDate AND @toDate;)
AND MTTTYP = 11
AND MTTRDT BETWEEN @fromDate AND @toDate
AND MITTRA.MTTRQT < 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM