Sql r'我建议只需添加一个计数列,您就可以知道它是否存在。@Tony\u Henrich。我不知道你想要什么样的结果集。如前所述,您将为数据中的每个订单(或至少每个电视订单)获得一个单独的行。不过,描述表明您希望每个客户有一行。但是如果是这样,为什么要在选择中
Sql r'我建议只需添加一个计数列,您就可以知道它是否存在。@Tony\u Henrich。我不知道你想要什么样的结果集。如前所述,您将为数据中的每个订单(或至少每个电视订单)获得一个单独的行。不过,描述表明您希望每个客户有一行。但是如果是这样,为什么要在选择中,sql,sql-server,tsql,sql-server-2008-r2,Sql,Sql Server,Tsql,Sql Server 2008 R2,r'我建议只需添加一个计数列,您就可以知道它是否存在。@Tony\u Henrich。我不知道你想要什么样的结果集。如前所述,您将为数据中的每个订单(或至少每个电视订单)获得一个单独的行。不过,描述表明您希望每个客户有一行。但是如果是这样,为什么要在选择中包含订单信息呢?@GordonLinoff我添加了订单信息,所以显示了信息,但是是的,订单不在选择中。我希望返回一行如果一个客户有10个订单会发生什么?哦,它将为一个客户返回多行。修改了我要查询的问题,需要在单个案例声明中处理。不是多行。我已尝
r'我建议只需添加一个计数列,您就可以知道它是否存在。@Tony\u Henrich。我不知道你想要什么样的结果集。如前所述,您将为数据中的每个订单(或至少每个电视订单)获得一个单独的行。不过,描述表明您希望每个客户有一行。但是如果是这样,为什么要在
选择中包含订单信息呢?@GordonLinoff我添加了订单信息,所以显示了信息,但是是的,订单不在选择中。我希望返回一行如果一个客户有10个订单会发生什么?哦,它将为一个客户返回多行。修改了我要查询的问题,需要在单个案例声明中处理。不是多行。我已尝试在单个case语句中处理。让我知道这是否对你有帮助这并不能以任何形式回答我的问题。这个问题是关于使用case语句并根据当前数据类型显示特定文本的特定问题。@Tony\u Henrich我添加了case语句。这会产生一个错误:“Customer.CustId”列在选择列表中无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中。@Tony\u Henrich。我推荐了聚合,然后没有包括分组依据
。已经修好了。
CREATE TABLE [dbo].[Customer](
[CustId] [int] NOT NULL,
[CustomerName] [varchar](50) NOT NULL
)
CREATE TABLE [dbo].[Order](
[OrderId] [int] NOT NULL,
[CustId] [int] NOT NULL,
[Description] [varchar](50) NOT NULL
)
INSERT INTO customer (CustId, CustomerName) VALUES
(1, 'John'),
(2, 'Tom')
GO
INSERT INTO [order] (OrderId, CustId, Description) VALUES
(1, 2, 'TV')
go
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
DROP TABLE #temp;
SELECT
'TV' Description INTO #temp
SELECT c.CustId
,t.Description
,o.Description
,CASE
WHEN t.Description IS NULL THEN 'Did not find any orders'
WHEN t.Description IS NULL THEN 'Found order but not TV order'
ELSE 'Found TV Order'
END Status
FROM Customer c
LEFT JOIN [Order] o
ON o.CustId = c.CustId
LEFT JOIN #temp t
ON t.Description = o.Description
WHERE c.CustId = 1
select a.custid,a.CustomerName,orderid=isnull(b.orderId,0),
description=iif(b.description is null,'Did not Find Any Order',b.description)
from Customer a left join [Order] b on a.custid=b.Custid
,case when t.Description = o.Description then 'Found TV Order'
when isnull(t.Description,'No') = isnull(o.Description,'No') then 'Did not find any orders'
when isnull(t.Description,'No') <> isnull(o.Description,'No') then 'Found order but not TV order'
end Status
SELECT c.CustId
,t.Description tdesc
,o.Description odesc
,case when t.Description = o.Description then 'Found TV Order'
when isnull(t.Description,'No') = isnull(o.Description,'No') then 'Did not find any orders'
when isnull(t.Description,'No') <> isnull(o.Description,'No') then 'Found order but not TV order'
end Status
/* ,CASE
WHEN t.Description IS NULL THEN 'Did not find any orders'
WHEN t.Description IS NULL THEN 'Found order but not TV order'
ELSE 'Found TV Order'
END Status */
FROM Customer c
LEFT JOIN [Order] o ON o.CustId = c.CustId
LEFT JOIN #temp t ON t.Description = o.Description
select CustId,descpt
from
(
SELECT c.CustId
, case when o.Description is null then 'Did not find any orders' end descpt
FROM Customer c
LEFT JOIN [Order] o ON o.CustId = c.CustId ) no_orders
where no_orders.descpt is not null
union
select CustId,descpt
from
(
SELECT c.CustId
, case when o.Description ='TV' then 'Found order for TV ' end descpt
FROM Customer c
LEFT JOIN [Order] o ON o.CustId = c.CustId ) no_orders
where no_orders.descpt is not null
union
select CustId,descpt
from
(
SELECT c.CustId
, case when o.Description <> 'TV' then 'Found order but not TV order' end descpt
FROM Customer c
LEFT JOIN [Order] o ON o.CustId = c.CustId ) no_orders
where no_orders.descpt is not null
select
c.CustId,
t.Description,
case
when o.Description is null then
'Did not find any orders'
when o.Description <> t.Description then
concat('Found order but not ',t.Description,' order')
else
concat('Found ',t.Description,' order')
end as status
from dbo.Customer as c
cross join (
select 'TV' as Description
) as t
outer apply (
select top 1 tt.Description
from dbo.[Order] as tt
where
tt.CustId = c.CustId
order by
case when tt.Description = t.Description then 0 else 1 end
) as o
select
c.CustId,
t.Description,
case
when o1.Description is not null then
concat('Found ',t.Description,' order')
when o2.OrderId is not null then
concat('Found order but not ',t.Description,' order')
else
'Did not find any orders'
end as status
from dbo.Customer as c
cross join (
select 'TV' as Description
) as t
left join dbo.[Order] as o1 on
o1.CustId = c.CustId and
o1.Description = t.Description
outer apply (
select top 1 tt.OrderId
from dbo.[Order] as tt
where
tt.CustId = c.CustId and
tt.Description <> t.Description
) as o2
CREATE TABLE [dbo].[Customer](
[CustId] [int] NOT NULL,
[CustomerName] [varchar](50) NOT NULL
)
CREATE TABLE [dbo].[Order](
[OrderId] [int] NOT NULL,
[CustId] [int] NOT NULL,
[Description] [varchar](50) NOT NULL
)
CREATE TABLE [dbo].[FakeTemp](
[Description] [varchar](50) NOT NULL
)
INSERT INTO FakeTemp (Description) VALUES
('TV')
GO
INSERT INTO customer (CustId, CustomerName) VALUES
(1, 'John'),
(2, 'Tom'),
(3, 'Steve')
GO
INSERT INTO [order] (OrderId, CustId, Description) VALUES
(1, 1, 'TV')
go
INSERT INTO [order] (OrderId, CustId, Description) VALUES
(2, 2, 'VCR')
go
INSERT INTO [order] (OrderId, CustId, Description) VALUES
(3, 1, 'VCR')
go
select c.CustomerName, count(OrderId) as TV_Orders
, (select count(OrderID) from [order] where CustId = 1) as All_Orders
from customer c
left outer join [order] o on c.CustId = o.CustId
inner join FakeTemp t on o.Description = t.Description
where c.CustID = 1
group by c.CustomerName, t.Description
select
CASE
WHEN (select count(OrderID) from orders where CustId = 1) = 0 THEN 'Did not find any orders'
WHEN count(OrderID) = 0 THEN 'Found order but not TV order'
ELSE 'Found TV Order'
End as Status
from customer c
left outer join [orders] o on c.CustId = o.CustId
inner join FakeTemp t on o.Description = t.Description
where c.CustID = 1
group by c.CustomerName, t.Description
SELECT c.CustId,
(CASE WHEN COUNT(o.CustId) = 0
THEN 'Did not find any orders'
WHEN SUM(CASE WHEN o.Description = 'TV' THEN 1 ELSE 0 END) = 0
THEN 'Found order but not TV order'
ELSE 'Found TV Order'
END) as Status
FROM Customer c LEFT JOIN
[Order] o
ON o.CustId = c.CustId
WHERE c.CustId = 1
GROUP BY c.CustId;