【问题标题】:SQL Query to Retrieve data by comparing column name to a particular string通过将列名与特定字符串进行比较来检索数据的 SQL 查询
【发布时间】:2013-03-06 19:28:44
【问题描述】:

我有一个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 开发人员中使用的实际代码。我复制粘贴了
  • 我想知道您如何将 stores 保存在表格中的一列中?如果是这样,当它有多个商店时,实际价值是什么样的。在这种情况下,您可能希望使用 where store like '%X%'
  • ok 将字符串存储在varchar 中并使用store=varcharVariable 进行检查
  • @spiritwalker 这是示例数据 STORE CUSTOMER TRANSACTION X 4567 66666 X 4567 55555 X 2233 43216 Y 2345 67890 Y 4567 23456`

标签: sql oracle


【解决方案1】:

尝试以下操作:

   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)

【讨论】:

  • 谢谢,我认为它有效。我有在 2 家或更多商店购物的顾客。但我想分别知道这些值,例如,在 (Y,Z) 和 p 在 (X,Y) m 购物的顾客 n 和 p 在所有三个。有什么办法吗?
  • 我已经编辑了上面的答案,并在您的问题中包含了示例代码。检查这是否适合您。
  • 您按客户分组并计数?
  • 查询将返回在不同商店购物并在多个商店购物的客户数量。
【解决方案2】:

按客户从表名组中选择不同的客户,商店有count(stores)>2

sorry....你可以这样试试

"select customer,store from table_name group by customer ,store have count(customer)>=1 order by customer asc"

我希望这是正确的。

【讨论】:

  • 不,它给出了所有行,包括那些在单一商店购物的行
  • 现在我已经编辑了查询,就这样试试吧,。 select customer,store from table_name group by customer ,store有count(customer)>=1 order by customer asc
  • 它仍然返回所有的行。
  • 我正在使用 sqlserver 2008....只需检查一下..select customer,trans,store from table_name group by customer ,trans,store with count(customer)>=1 order by customer asc
【解决方案3】:

我认为你应该在 MySQL 中使用类似 GROUP_CONCAT 的东西。

Here you can find how you can emulate it in Oracle

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

SQLFiddle demo

SELECT customer, 
       LISTAGG(store, ',') WITHIN GROUP (ORDER BY store) 
       AS STORES
FROM   
(select distinct customer,store from t) t1
GROUP BY customer;

【讨论】:

    猜你喜欢
    • 2018-11-04
    • 2016-02-12
    • 2016-04-04
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多