2012-02-28 45 views
2

的所有值我有一個查找表,基本上是與信用卡類型連接表,但顯示一個查找表

ID | CCType   | 
----------------------- 
1 | AMEX   | 
2 | VISA   | 
3 | MASTER CARD  | 
4 | OTHER   | 

然後我有交易的另一個表,其中CCTypeID是在仰望ID表

TransID | CCTypeID | TransactionName | OrderID | 
----------------------------------------------------- 
1   | 2   | BILLPAY   |10 
2   | 4   | SECUREPAYMENT |13 
3   | 1   | BILLPAY   |15 

於是最後我有訂單另一個表

OrderId | OrderDate | Product | TransID | 
    ------------------------------------------------ 
    10  | 2012-02-27 |Grapes  | 1 
    13  | 2012-02-26 |Wine  | 2 
    14  | 2012-01-26 |Pepper  | 6 
    15  | 2012-01-26 |Apple  | 1 

I want to create a view that looks like this 

OrderDate | CCType  | Processed 
----------- --------------------------- 
2012-02-27 | AMEX  | 0 
2012-02-27 | VISA  | 1 
2012-02-27 | MASTER CARD | 0 
2012-02-27 | OTHER  | 0 
2012-02-26 | AMEX  | 0 
2012-02-26 | VISA  | 0 
2012-02-26 | MASTER CARD | 0 
2012-02-26 | OTHER  | 1 

我有嘗試RIGHT OUTER將信用卡類型與CCTypeID和ID上的事務分別加入,然後使用ORDERID上的訂單進行INNER JOIN,按ORDER.DATE和CardType分組,但它只顯示有無值,是否有辦法使它是否仍然列出了信用卡類型,無論是否有交易或訂單日期?只顯示零卡的類型?

會真正感謝您的幫助。

+2

您要從哪裏拉Processed字段?它沒有出現在你顯示的任何表格中 – Taryn 2012-02-28 15:58:39

+0

@bluefeet - 它似乎是該日期和卡片類型的記錄數。 – MatBailie 2012-02-28 16:06:52

回答

3

你的基本的查詢應該是這樣的......

SELECT 
    Orders.OrderDate, 
    CardType.CCType, 
    COUNT(*) 
FROM 
    Orders 
LEFT JOIN 
    Transactions 
    ON Transactions.TransID = Orders.TransID 
    AND Transactions.OrderID = Orders.OrderID 
LEFT JOIN 
    CardType 
    ON CardType.ID = Transaction.CCTypeID 
GROUP BY 
    Orders.OrderDate, 
    CardType.CCType 

如果你需要「所有日期」有值,那麼你需要額外的表加入上。例如,製作一個名爲calendar的表格,並預先填寫您需要的所有日期,然後在日期字段中創建一個索引。然後,您可以重新排序查詢強制要用於記錄每個組合...

SELECT 
    Calendar.Date, 
    CardType.CCType, 
    COUNT(Transaction.CCTypeID) 
FROM 
    Calendar 
CROSS JOIN 
    CardType 
LEFT JOIN 
    (
    Transactions 
    INNER JOIN 
    Orders 
     ON Transactions.TransID = Orders.TransID 
     AND Transactions.OrderID = Orders.OrderID 
) 
    ON CardType.ID = Transactions.CCTypeID 
    AND Calendar.Date = Orders.OrderDate 
WHERE 
    Calendar.Date BETWEEN ??? AND ??? 
GROUP BY 
    Calendar.Date, 
    CardType.CCType 
1

我真的只需要添加到現有的答案在這裏,因爲答案的第二部分是對的,但第一部分不按要求交叉連接,因此省略了OP可能要求所有有訂單日期的所有卡類型的場景。我不確定這是否合適禮儀,因爲我仍然想利用隨答案附帶的代碼格式...

-- CREATE SAMPLE DATA 
CREATE TABLE #CCType (ID INT NOT NULL PRIMARY KEY, CCType VARCHAR(15) NOT NULL) 
INSERT #CCType VALUES (1, 'AMEX'), (2, 'VISA'), (3, 'MASTER CARD'), (4, 'OTHER') 
CREATE TABLE #Trans (TransID INT NOT NULL PRIMARY KEY, CCTypeID INT NOT NULL, Transactionname VARCHAR(15), OrderID INT NOT NULL) 
INSERT #Trans VALUES (1, 2, 'BIILPAY', 10), (2, 4, 'SECUREPAYMENT', 13), (3, 1, 'BIILPAY', 15) 
CREATE TABLE #Orders (OrderID INT NOT NULL PRIMARY KEY, OrderDate DATETIME NOT NULL, Product VARCHAR(10), TransID INT) 
INSERT #Orders VALUES (10, '20120227', 'Grapes', 1), (13, '20120226', 'Wine', 2), (14, '20120226', 'Pepper', 6), (15, '20120226', 'Apples', 1) 
-- END CREATE SAMPLE DATA 

SELECT OrderDate, 
     CCType, 
     COUNT(CASE WHEN ID = CCTypeID THEN 1 END) [Processed] 
FROM #CCType, #Orders, #trans 
WHERE #Orders.TransID = #Trans.TransID 
AND  #Orders.OrderID = #Trans.OrderID 
GROUP BY OrderDate, CCType, ID 
ORDER BY OrderDate DESC, ID 

-- DISPOSE OF SAMPLE DATA 
DROP TABLE #Orders, #CCType, #Trans 
+0

不確定你想在'GROUP BY'中使用'ID'嗎? – MatBailie 2012-02-28 17:13:53

+0

@Dems只有這樣我纔可以將它包含在'ORDER BY'中因爲卡片在問題的例子中以ID順序出現。 – GarethD 2012-02-28 17:28:09

相關問題