1

2つのテーブルの数が同じかどうかを確認しようとしています

クエリが1つあります:

SELECT Count(1) AS Items,
       [Basket Id],
       [Customer Id]
FROM   Order_Lookup
GROUP  BY [Basket Id],
          [Customer Id]

どちらが返されますか:

3   2   135674
5   4   115576
3   5   115576
2   3   118342
4   6   182368

そして私も持っています:

SELECT Count(1) AS Items,
                 a.[Basket Id],
                 a.[Customer Id]
          FROM   C1059204.Order_Lookup a
          WHERE  a.[Product Id] NOT IN (SELECT [Product Id]
                                        FROM   Orders
                                        WHERE  [Customer_Id] = a.[Customer Id]
                                               AND OrderDate = Dateadd(DAY, Datediff(DAY, 0, Getutcdate()), -2)
                                               AND OrderStatus IN ('POSTED' ))
          GROUP  BY a.[Basket Id],
                    a.[Customer Id] 

どちらが返されますか:

3   2   135674
3   4   115576
3   5   115576
2   3   118342
4   6   182368

ご覧のとおり、115576バスケットID4の数値は低くなっています。

次のようなクエリが必要です

SELECT * FROM TABLE
WHERE (COUNT OF FIRST SUBQUERY) = (COUNT OF SECOND SUBQUERY)

どうすればこれを行うことができますか、私は長い間試みてきましたが、それを理解することはできません。

ありがとう

4

2 に答える 2

1

両方のサブクエリをラップして、エイリアス名で結合できます。

select *
from (
    SELECT Count(1) AS Items,
           [Basket Id],
           [Customer Id]
    FROM   Order_Lookup
    GROUP  BY [Basket Id],
              [Customer Id]
) as query1
join (
    SELECT Count(1) AS Items,
                     a.[Basket Id],
                     a.[Customer Id]
              FROM   C1059204.Order_Lookup a
              WHERE  a.[Product Id] NOT IN (SELECT [Product Id]
                                            FROM   Orders
                                            WHERE  [Customer_Id] = a.[Customer Id]
                                                   AND OrderDate = Dateadd(DAY, Datediff(DAY, 0, Getutcdate()), -2)
                                                   AND OrderStatus IN ('POSTED' ))
              GROUP  BY a.[Basket Id],
                        a.[Customer Id] 
) as query2 on query1.Items = query2.Items
于 2012-04-04T11:22:52.370 に答える
0

このようにしてください:

SELECT count(*),
       subquery.[Basket Id],
       subquery.[Customer Id] 
FROM (
  -- Put your subselect in a case expression returning 1 or 0 to indicate
  -- whether it would evaluate to true or false for any given record
  SELECT CASE WHEN a.[Product Id] IN (
                SELECT [Product Id]
                  FROM Orders
                 WHERE [Customer_Id] = a.[Customer Id]
                   AND OrderDate = Dateadd(DAY, Datediff(DAY, 0, Getutcdate()), -2)
                   AND OrderStatus IN ('POSTED' ))
         THEN 1
         ELSE 0 END [isPosted],
       a.[Basket Id],
       a.[Customer Id]
   FROM   C1059204.Order_Lookup a
 ) subquery
 GROUP  BY subquery.[Basket Id],
           subquery.[Customer Id] 
 -- Those groups that contain at least one record which would figure in your 
 -- "second subquery" should be omitted altogether
 HAVING SUM([isPosted]) = 0
于 2012-04-04T11:19:47.860 に答える