通过将列名与特定字符串进行比较来检索数据的SQL查询

通过将列名与特定字符串进行比较来检索数据的SQL查询,sql,oracle,Sql,Oracle,我有一个SQL表,由客户、事务和存储组成。存储有3个值(X、Y、Z) 我想检索在特定商店购物的客户,所以我使用了这个查询 select distinct customer from TABLE group by store. 但是,现在我需要在2家商店(X,Y)(Y,Z)(Z,X)和(X,Y,Z)购物的客户详细信息 当我使用 select distinct customer from TABLE where store='X' 它在oracle SQL Developer中给出了0个结果

我有一个
SQL
表,由客户、事务和存储组成。存储有3个值(X、Y、Z)

我想检索在特定商店购物的客户,所以我使用了这个查询

select distinct customer from TABLE group by store.
但是,现在我需要在2家商店(X,Y)(Y,Z)(Z,X)和(X,Y,Z)购物的客户详细信息

当我使用

select distinct customer from TABLE where store='X' 
它在oracle SQL Developer中给出了0个结果

如何从这里开始?

尝试以下操作:

   Select Customer From
    (
         Select Customer,Store from TableName group by Store,Customer
    )tbl
    Group By Customer Having COUNT(Customer)>=2 Order By Customer
编辑:

Declare @MainTable table
(
  Customer varchar(222),Store varchar(2222)
)
Insert Into @MainTable
Select 'C1','X'
Union All
Select 'C1','Y'
Union All
Select 'C1','X'
Union All
Select 'C2','X'
Union All
Select 'C2','Y'
Union All
Select 'C2','Z'
Union All
Select 'C3','X'
Union All
Select 'C3','Z'
Union All
Select 'C4','X'
Union All
Select 'C4','Y'


Declare @temp table 
(
  Customer varchar(200)
)
Insert Into @temp 
Select Customer From
    (
         Select Customer,Store from @MainTable group by Store,Customer
    )tbl
Group By Customer Having COUNT(Customer)>=2 Order By Customer


Declare @Customer_Store table 
(
  Customer varchar(200),
  Stores varchar(200)
)

DECLARE @Stores varchar(10)
Declare @Customer varchar(256)
While((Select COUNT(*) From @temp)>0)
Begin
        Set @Customer=(Select Top 1 Customer From @temp)
        Select @Stores=coalesce(@Stores + ',','') + Store From 
        @MainTable Where Customer=@Customer
        Group By Store
        Order By Store

        Insert Into @Customer_Store Select @Customer,@Stores

        Delete From @temp Where Customer=@Customer
        Set @Stores=null
End


Select Cast(COUNT(Customer) as Varchar(5))+' Customers shopped at Store ('+Stores+')'          CustomerDetail From @Customer_Store
Group By Stores
输出:

2 Customers shopped at Store (X,Y)
1 Customers shopped at Store (X,Y,Z)
1 Customers shopped at Store (X,Z)

按客户从tablename组中选择不同的客户,门店数量(门店数)>2

对不起……你可以这样试试

“从表中选择客户,门店名称按客户分组,门店计数(客户)>=1订单按客户asc”


我希望这是正确的。

我认为您应该在MySQL中使用类似的东西

例如,对于Oracle 11g R2,您可以使用Listag:


它说缺少标识这是我在SQL developer中使用的实际代码。我复制粘贴的itI我想知道如何将存储保存在表中的一列中?如果是,当它有多个存储时,实际值是什么样子的。在这种情况下,您可能希望使用像“%X%”这样的where存储,确定将字符串存储在
varchar
中,并使用
store=varcharVariable
@spirtwalker检查它。这是示例数据存储客户事务X 4567666 X 4567555555 X 223343216 Y234567890 Y 456723456`谢谢,我想它奏效了。我有顾客在两家或两家以上的商店购物。但是我想分别知道这些值,比如,n在(X,Y)购物的顾客数量,m在(Y,Z)购物的顾客数量,以及p在这三个地点购物的顾客数量。有什么办法吗?我已经编辑了上面的答案,并为您的问题提供了示例代码。检查这是否对您有效。您正在按客户分组并计数?查询将返回在不同商店购物并在多个商店购物的客户数。不,它将给出所有行,包括在单个商店购物的客户。现在我已编辑查询,请像这样尝试,。选择customer,store from table_name group by customer,store have count(customer)>=1 order by customer ascIts仍返回所有行。我正在使用sqlserver 2008…只需选中此项..选择customer,trans,store from table_name group by customer,trans,store have count(customer)>=1 order by customer asc
SELECT customer, 
       LISTAGG(store, ',') WITHIN GROUP (ORDER BY store) 
       AS STORES
FROM   
(select distinct customer,store from t) t1
GROUP BY customer;