0

您好,我有以下 SQL 查询

 Select catalogid , numitems, allitems - numitems ignoreditems
        from ( 
        select i.catalogid,
         sum(case when (ocardtype in ('PayPal','Sofort') OR
                        ocardtype in ('mastercard','visa') and
                        odate is not null) then numitems
                        else 0 end) numitems,
         sum(numitems) allitems 
        from orders o
        join oitems i on i.orderid=o.orderid
        join products T1 on T1.catalogid = i.catalogid
        group by i.catalogid
        ) X 

在最后一个连接语句表产品包含 8 列并且它们没有显示在结果查询中,我只能看到列 catalogid、numitems 和ignoreitems,所以我做错了什么,如果我必须选择这些列才能让他们出现我怎么能用这种语法来做呢?

4

2 回答 2

1
Select catalogid , numitems, allitems - numitems ignoreditems, X.c1,X.c2,X.c3,X.c4,X.c5,X.c6,X.c7,X.c8
    from ( 
    select i.catalogid,
     sum(case when (ocardtype in ('PayPal','Sofort') OR
                    ocardtype in ('mastercard','visa') and
                    odate is not null) then numitems
                    else 0 end) numitems,
     sum(numitems) allitems ,
     T1.c1, T1.c2, T1.c3, T1.c4, T1.c5, T1.c6, T1.c7, T1.c8
    from orders o
    join oitems i on i.orderid=o.orderid
    join products T1 on T1.catalogid = i.catalogid
    group by i.catalogid, T1.c1, T1.c2, T1.c3, T1.c4, T1.c5, T1.c6, T1.c7, T1.c8
    ) X 
于 2012-10-02T09:57:30.513 回答
1

您只是在查询这三列。如果您希望产品中的列显示在结果集中,只需将它们添加到 select 语句中。您可以一个一个地添加它们,或者只添加 T1.* 来查询所有它们:

Select catalogid , numitems, allitems - numitems ignoreditems, X.columnName1, X.ColumnName2, X.*
from ( 
select i.catalogid,
sum(case when (ocardtype in ('PayPal','Sofort') OR
ocardtype in ('mastercard','visa') and
odate is not null) then numitems
else 0 end) numitems,
sum(numitems) allitems,
-- This
T1.ColumnName1, T1.ColumName2, ...
--- or this way
T1.*
from orders o
join oitems i on i.orderid=o.orderid
join products T1 on T1.catalogid = i.catalogid
group by i.catalogid
) X 
于 2012-10-02T09:58:52.260 回答