0

我试图弄清楚如何让查询工作。我有点想做交叉连接,但不是真正的交叉连接类型查询..也不是完全连接(我不认为)所以我把它扔给社区供输入。

我有表1:

Acct  AcctDesc   CostType   Value1  Value2
12    School     Tax        12.42   3.20
12    School     Supplies   34.22   9.88
12    School     Payroll    122.00  8.88
12    School     Milk       8.88    7.77
13    Work       Tax        28.88   9.70
13    Work       Supplies   15.15   8.80
13    Work       Cookies    5.90    7.00
13    Work       Payroll    79.00   8.88
13    Work       Misc       4.33    3.33
13    Work       Vehicle    8.33    0.33

有了这张表,我就有了多个 CostType。我想获取所有独特的成本类型并将它们加入数据以创建一个视图,该视图将产生(在本例中为学校)并将成本输入为零。(用于稍后在报告中返回零列)

Acct  AcctDesc   CostType   Value1  Value2
12    School     Tax        12.42   3.20
12    School     Supplies   34.22   9.88
12    School     Payroll    122.00  8.88
12    School     Milk       8.88    7.77
12    School     Cookies    0.00    0.00
12    School     Misc       0.00    0.00
12    School     Vehicle    0.00    0.00

我以为我可以做类似的事情

Select Acct, AcctDesc, CostType, Value1, Value2 
from Table1
Cross Join (Select Distinct CostType from Table1) t2

但我很快意识到它不会像那样工作。我也尝试过自然连接,但这也行不通。

我想我可能需要对表中的所有唯一值进行 CTE,然后对原始查询进行左连接,我认为这是最不理想的,所以我想把它扔给你们。

感谢您的输入。

4

2 回答 2

0

这应该有效(尽管可能有更好的方法来做到这一点):

SELECT 
    subq.Acct, 
    subq.AcctDesc, 
    subq.CostType, 
    Value1 = ISNULL(Value1,0), 
    Value2 = ISNULL(Value2,0) 
FROM (
    SELECT
       t1.CostType, 
       t2.Acct, 
       t2.AcctDesc 
    FROM Table1 t1 
    CROSS JOIN Table1 t2
    GROUP BY t1.CostType, t2.Acct, t2.AcctDesc
    ) subq
LEFT JOIN Table1 t ON subq.CostType = t.CostType 
                  AND subq.Acct = t.Acct
                  AND subq.AcctDesc = t.AcctDesc
--WHERE t.AcctDesc = 'School'
ORDER BY subq.Acct, subq.AcctDesc

样本输出:

Acct    AcctDesc         CostType         Value1             Value2
------- ---------------- ---------------- ------------------ ------------------
12      School           Cookies          0                  0
12      School           Milk             8,88               7,77
12      School           Misc             0                  0
12      School           Payroll          122                8,88
12      School           Supplies         34,22              9,88
12      School           Tax              12,42              3,2
12      School           Vehicle          0                  0
13      Work             Cookies          5,9                7
13      Work             Milk             0                  0
13      Work             Misc             4,33               3,33
13      Work             Payroll          79                 8,88
13      Work             Supplies         15,15              8,8
13      Work             Tax              28,88              9,7
13      Work             Vehicle          8,33               0,33
于 2014-10-22T13:27:10.087 回答
0

这似乎工作

SELECT 
    subq.Acct, 
    subq.AcctDesc, 
    subq.CostType, 
    Value1 = ISNULL(Value1,0), 
    Value2 = ISNULL(Value2,0) 
FROM (
    SELECT
       t2.CostType, 
       t1.Acct, 
       t1.AcctDesc 
    FROM Table1 t1 
    CROSS JOIN (select distinct CostType from Table1) t2
    GROUP BY t2.CostType, t1.Acct, t1.AcctDesc
    ) subq
LEFT JOIN Table1 t ON subq.CostType = t.CostType 
                  AND subq.Acct = t.Acct
                  and subq.AcctDesc = t.AcctDesc
--WHERE t.AcctDesc = 'School'
ORDER BY subq.Acct, subq.AcctDesc
于 2014-10-22T17:44:18.790 回答