SQL - 创建零值数据的附加行(交叉连接类型?)

时间:2014-10-22 12:43:58

标签: sql-server join cross-join unique-values

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

我有表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

使用此表,我有多个CostTypes。我想采用所有独特的costtypes并将它们加入到数据中以创建一个将导致的视图(在这种情况下为学校)并将为成本投入零。 (用于稍后在报告上返回零列)

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,然后使用原始查询进行左连接,我认为这是最不理想的,所以我想把它扔到你们。

感谢您的投入。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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