我想在sql中明确记录记录。
+----------+-------+----------------------+----------+----------+----------+----------+ | Products | Totel | sellers=COUNT(C2:F2) | seller a | seller b | seller c | seller d | +----------+-------+----------------------+----------+----------+----------+----------+ | a | 3 | 23.4 | 34.2 | null | 34.2 | | | b | 2 | 23.4 | null | 34.2 | null | | | c | 1 | 23.4 | null | null | null | | | d | 4 | 23.4 | 34.2 | 34.2 | 34.2 | | +----------+-------+----------------------+----------+----------+----------+----------+
我可以使用this = COUNT(C2:F2)在Excel中执行此操作。 在sql中怎么办?
答案 0 :(得分:1)
您可以使用:
select sum(
case when [seller a] is null then 0 else 1 end
+ case when [seller b] is null then 0 else 1 end
+ case when [seller c] is null then 0 else 1 end
+ case when [seller d] is null then 0 else 1 end
)
from your_table
这将给出所有行的总数,因为它似乎不是很明显如何对它进行分组。
答案 1 :(得分:0)
这会有效,但需要GROUP BY
Select
Products,
sum(
case when SellerA is null then 0 else 1 end
+ case when SellerB is null then 0 else 1 end
+ case when SellerC is null then 0 else 1 end
+ case when SellerD is null then 0 else 1 end
) AS Total_Seller,
SellerA,
SellerB,
SellerC,
SellerD
from MyTable
GROUP BY
Products,
SellerA,
SellerB,
SellerC,
SellerD
或者,你也可以使用它。
Select
Products,
ISNULL((SellerA/SellerA),0)
+ISNULL((SellerB/SellerB),0)
+ISNULL((SellerC/SellerC),0)
+ISNULL((SellerD/SellerD),0) AS Total_Seller,
SellerA,
SellerB,
SellerC,
SellerD
from MyTable