项目查询结果列
Project query results in column
我有一个相对简单的连接,它从各种表中提取数据
SELECT P.StoreName AS Store, CR.Model AS Terminal, T.TxType, COUNT(*) as Total
FROM [SVTx] SVCT
INNER JOIN [Txs] T ON T.TransactionID = SVCT.transactionID
INNER JOIN [TxIss] TI ON TI.TransactionID = T.transactionID
INNER JOIN [Stores] P ON P.[ExtStoreID] = TI.[ExtStoreID]
INNER JOIN [Terms] CR ON CR.[StoreID] = P.StoreID AND CR.[ExtTermCode] = TI.[ExtTermCode]
INNER JOIN [SVCrd] SVC ON SVC.SVCrdID = SVCT.SVCrdID
GROUP BY P.StoreName, CR.Model, TxType
ORDER BY P.StoreName, CR.Model, TxType
并产生如下结果
Store Terminal TransactionType Total
------- ---------- ----------------- -----------
Store 1 Terminal 1 1 23
Store 1 Terminal 1 2 17
Store 1 Terminal 2 1 28
Store 1 Terminal 2 2 24
... ... ... ..
这为我提供了每个商店和终端的任何给定类型的交易数量。
我想修改我的查询以添加任何给定 store/terminal 进行的总交易并将结果投射到列而不是行中。
要有想法,请看下面的例子:
Store Terminal 1 2 Total
------- ---------- ----- ----- -------
Store 1 Terminal 1 23 17 40
Store 1 Terminal 2 28 24 52
我已阅读有关 PIVOT 功能的信息,但在我使用 Azure SQL 数据库(SQL Server 2014 兼容性时,我只能找到 SQL Server 2008 的帮助水平)。
有什么帮助吗?
改用conditional Aggregate
;WITH cte
AS (SELECT p.storename AS Store,
cr.model AS Terminal,
t.txtype,
Count(*) AS Total
FROM [svtx] SVCT
INNER JOIN [Txs] ...),
intr
AS (SELECT store,
terminal,
Coalesce(Max(CASE WHEN transactiontype = 1 THEN total END),0) AS [1],
Coalesce(Max(CASE WHEN transactiontype = 2 THEN total END),0) AS [2],
Sum(total) AS total
FROM cte
GROUP BY store,
terminal)
SELECT store,
terminal,
[1],
[2],
total,
( [1] / Cast(total AS NUMERIC(22, 6)) ) * 100 AS percent1,
( [2] / Cast(total AS NUMERIC(22, 6)) ) * 100 AS percent2
FROM intr
我有一个相对简单的连接,它从各种表中提取数据
SELECT P.StoreName AS Store, CR.Model AS Terminal, T.TxType, COUNT(*) as Total
FROM [SVTx] SVCT
INNER JOIN [Txs] T ON T.TransactionID = SVCT.transactionID
INNER JOIN [TxIss] TI ON TI.TransactionID = T.transactionID
INNER JOIN [Stores] P ON P.[ExtStoreID] = TI.[ExtStoreID]
INNER JOIN [Terms] CR ON CR.[StoreID] = P.StoreID AND CR.[ExtTermCode] = TI.[ExtTermCode]
INNER JOIN [SVCrd] SVC ON SVC.SVCrdID = SVCT.SVCrdID
GROUP BY P.StoreName, CR.Model, TxType
ORDER BY P.StoreName, CR.Model, TxType
并产生如下结果
Store Terminal TransactionType Total
------- ---------- ----------------- -----------
Store 1 Terminal 1 1 23
Store 1 Terminal 1 2 17
Store 1 Terminal 2 1 28
Store 1 Terminal 2 2 24
... ... ... ..
这为我提供了每个商店和终端的任何给定类型的交易数量。
我想修改我的查询以添加任何给定 store/terminal 进行的总交易并将结果投射到列而不是行中。 要有想法,请看下面的例子:
Store Terminal 1 2 Total
------- ---------- ----- ----- -------
Store 1 Terminal 1 23 17 40
Store 1 Terminal 2 28 24 52
我已阅读有关 PIVOT 功能的信息,但在我使用 Azure SQL 数据库(SQL Server 2014 兼容性时,我只能找到 SQL Server 2008 的帮助水平)。
有什么帮助吗?
改用conditional Aggregate
;WITH cte
AS (SELECT p.storename AS Store,
cr.model AS Terminal,
t.txtype,
Count(*) AS Total
FROM [svtx] SVCT
INNER JOIN [Txs] ...),
intr
AS (SELECT store,
terminal,
Coalesce(Max(CASE WHEN transactiontype = 1 THEN total END),0) AS [1],
Coalesce(Max(CASE WHEN transactiontype = 2 THEN total END),0) AS [2],
Sum(total) AS total
FROM cte
GROUP BY store,
terminal)
SELECT store,
terminal,
[1],
[2],
total,
( [1] / Cast(total AS NUMERIC(22, 6)) ) * 100 AS percent1,
( [2] / Cast(total AS NUMERIC(22, 6)) ) * 100 AS percent2
FROM intr