【问题标题】:How do I get distinct items from one table into another?如何将不同的项目从一张桌子转移到另一张桌子?
【发布时间】:2017-11-10 18:45:33
【问题描述】:

EDIT:因为我使用的是 ExactTarget (Salesforce Marketing Cloud),所以我只能访问 SELECT 语句。没有任何好的 SQL 可以使这变得简单。

我有一张客户表和一张优惠券表。我想为符合条件的客户分配唯一代码,但我有点卡在查询 #1 上。 (查询 #2 是一个简单的连接。)

表 1

+-------------+-------------+------------+ |客户 |优惠券类型 |优惠码 | +-------------+-------------+------------+ |客户1 | 20pctoff | | |客户2 | 20pctoff | | |客户3 | 10pctoff | | |客户4 |空 | | +-------------+-------------+------------+

表 2

+-------------+-------------+------------+ |优惠券类型 |优惠码 |分配给 | +-------------+-------------+------------+ | 10pctoff |优惠券1 | | | 20pctoff |优惠券2 | | | 30pctoff |优惠券3 | | +-------------+-------------+------------+

查询 #1 将导致以下结果:

客户1 = 优惠券2
Customer2 = none(没有更多 20pctoff 可用)
客户 3 = 优惠券 1
Customer4 = none(不符合条件)

更新表 2 的第二个 SQL 是一个简单的连接(我不需要帮助)。

【问题讨论】:

  • Customer2 = none (没有更多 20pctoff 可用) 优惠券数量存储在哪里?还是每种类型只有一种??
  • Customer2 = none -- 因为给我代码的人没有给我足够的代码。我有一个单独的例程来订购更多优惠券。 Customer2 稍后会得到他们的。
  • 我觉得我将不得不使用 2 个临时表... SELECT row_number() as id, customer, coupontype where coupontype = 20pctoff // 选择 row_number() 作为 id, couponcode , coupontype where coupontype = 20pctoff // select t1.customer, t1.coupontype, t2.couponcode from table1 t1 left join on table2 t2 on t1.id = t2.id
  • 如果您有两张 20pctoff 优惠券,表 2 中的第四个条目是什么??
  • 这将是另一个优惠券类型/优惠券代码对。 (可能是“20pctoff”/“Coupon4”)。抱歉,我的 table2 示例应该更好。

标签: sql


【解决方案1】:

用第三个表解决。太不优雅了,但这就是有时使用 ExactTarget 所要付出的代价。警告,需要一次遍历已知的 CouponCodes 1。

新表:

+-----+------+--------------+ |身份证 |客户 |优惠码 | +-----+------+--------------+

目标表3,覆盖

SELECT row_number() OVER (order by Customer) 作为 ID, Customer from Table1 where CouponType = '20pctoff'

目标表3,更新

SELECT row_number() OVER (order by CouponCode) as ID, CouponCode from Table2 where CouponType = '20pctoff' and AssignedTo is NULL

目标表1,更新

SELECT t3.Customer, t3.CouponCode FROM table3 t3 inner join table1 t1 on t1.Customer = t3.Customer 其中 t3.Customer 不为 NULL 且 t3.CouponCode 不为 NULL

目标表2,更新

SELECT t3.Customer as AssignedTo, t3.CouponCode FROM table3 t3 inner join table2 t2 on t2.CouponCode = t3.CouponCode where t3.Customer is not null and t3.CouponCode is not null

【讨论】:

    猜你喜欢
    • 2020-07-29
    • 2016-01-23
    • 2011-10-09
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多