佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1745|回复: 22

select 的问题

  [复制链接]
发表于 19-10-2010 05:30 PM | 显示全部楼层 |阅读模式
大家好,

我要找回两年前买过货的顾客. 这些"两年前顾客"在这半年没买货的

比方说:

ah ming
ah long
ah tan

这3个人在两年前买过货, 但是在这半年没买货有 ah long 而已.


sql 要怎样写呢?
各位大侠帮帮忙...

谢谢
回复

使用道具 举报


ADVERTISEMENT

发表于 19-10-2010 06:20 PM | 显示全部楼层
SELECT NAME FROM TABLE WHERE (Curdate() - ExpiredDate) >= 730

类似这样吧
回复

使用道具 举报

发表于 19-10-2010 06:35 PM | 显示全部楼层
回复 1# ocm1


For MSSQL Only

SELECT * FROM [Table Name] WHERE ([Date Colum] BETWEEN [2 Years Ago Date from] AND [2 Years Ago Date to]) AND [Customer Name] NOT IN (SELECT [Table Name].[Customer Name Colum]) FROM [Table Name] WHERE [Date Colum] BETWEEN [Half Years Ago Date] AND [Today's Date])
回复

使用道具 举报

 楼主| 发表于 19-10-2010 08:11 PM | 显示全部楼层
先说声谢谢,
谢谢你们的帮忙

SELECT * FROM [Table Name] WHERE ([Date Colum] BETWEEN [2 Years Ago Date from] AND [2 Years Ago Date to])
这段我明白,但是后面就不清楚了。

一下是我的table,请帮忙看看
回复

使用道具 举报

发表于 19-10-2010 10:48 PM | 显示全部楼层
本帖最后由 Reader 于 19-10-2010 10:51 PM 编辑

The TSQL below only work if your xyear and xmonth column Data type is  int

SELECT * FROM table_1
WHERE (xyear<=2008 AND xmonth<=10)
AND  xuser_id NOT IN
(SELECT table_1.xuser_id FROM table_1 WHERE (xyear>=2010 AND xmonth>=5))

please adjust the date and month according to what you need...
回复

使用道具 举报

发表于 19-10-2010 11:02 PM | 显示全部楼层
Explain:

你的要求是找出两年前/更久前的顾客,但是他们在从现在起的半年前没有重新光顾你的店的资料。

SELECT * FROM table_1 WHERE (xyear<=2008 AND xmonth<=10)
以上是用来找两年前/更久前的顾客资料....(condition 是 2008年10月前)

AND  xuser_id NOT IN
Extra Condition 是 两年前/更久前顾客资料里的xuser_id不存在/不出现在 Sub Query里

(SELECT table_1.xuser_id FROM table_1 WHERE (xyear>=2010 AND xmonth>=5))
Sub Query 半年前到本月为止来光顾你的顾客资料。

完成。。。。
回复

使用道具 举报

Follow Us
发表于 20-10-2010 08:52 AM | 显示全部楼层
WHERE (xyear<=2008 AND xmonth<=10)
...
Reader 发表于 19-10-2010 10:48 PM


你的有bugs
那2007年11月不是選不出!
回复

使用道具 举报

发表于 20-10-2010 09:06 AM | 显示全部楼层
大家好,

我要找回两年前买过货的顾客. 这些"两年前顾客"在这半年没买货的

比方说:

ah ming
ah l ...
ocm1 发表于 19-10-2010 05:30 PM



   
select distinct xuser_id
from t1 a
where  to_date(xyear||xmonth,'yyyymm') < sysdate - 365*2
and not exists (
    select * from t1 b
    where a.xuser_id=b.xuser_id
    and to_date(xyear||xmonth,'yyyymm') > sysdate - 365*0.5
)
回复

使用道具 举报


ADVERTISEMENT

发表于 20-10-2010 09:17 AM | 显示全部楼层
本帖最后由 Reader 于 20-10-2010 09:22 AM 编辑

哈哈。。。因为我偷懒!有想过应该Convert去DATEformat再compare的。不过太夜了。没去修正。
多谢指正!
回复

使用道具 举报

 楼主| 发表于 20-10-2010 12:54 PM | 显示全部楼层


select distinct xuser_id
from t1 a
where  to_date(xyear||xmonth,'yyyymm') < sysdate - 365*2
and not exists (
    select * from t1 b
    where a.xuser_id=b.xuser_id
    and to_date(xyear||xmonth,'yyyymm') > sysdate - 365*0.5
)

先谢谢,你们的SQL很难.不过还是慢慢学习。
我不明白什么是 to_date? 要怎样写?? 我跟着做有 error 出啦。
to_date(xyear||xmonth,'yyyymm') < sysdate

365*0.5 是 182.5 为什么要放 182.5呢??
sysdate 是什么??
回复

使用道具 举报

发表于 20-10-2010 01:12 PM | 显示全部楼层
我不明白什么是 to_date?
沒to_date的話就有bugs,那2007年11月不是選不出!

我跟着做有 error 出啦。
to_date 是oracle自帶的function, 如果你是其它數據庫,可以google 它的snytax 是什麼。

365*0.5 是 182.5 为什么要放 182.5呢??
因為你要半年,那365×0.5 不是半年咯。

sysdate 是什么??
sysdate 是現在的日子。
回复

使用道具 举报

 楼主| 发表于 20-10-2010 01:25 PM | 显示全部楼层
我是使用 MSSQL 2008, 那么你的 code 应该不能用了。。
回复

使用道具 举报

 楼主| 发表于 20-10-2010 02:39 PM | 显示全部楼层
这个是参考以上的
select distinct xuser_id from tinvoice
where (xyear between 2008 and 2009) and xuser_id not in (select tinvoice.xuser_id from tinvoice where xyear >=2010 and xmonth>=5)

各位看看,如果有什么bug 请告诉我。。。 小弟慢慢学

谢谢
回复

使用道具 举报

发表于 20-10-2010 02:40 PM | 显示全部楼层
ocm1,

winmxaa example is PL/SQL, but the logic is there, to_date is used to convert the data into Date format inorder to do the compare.

please let us know your Data type for xyear and xmonth
回复

使用道具 举报

 楼主| 发表于 20-10-2010 03:02 PM | 显示全部楼层
xyear and xmonth 的DataType 是 int
回复

使用道具 举报

发表于 20-10-2010 03:24 PM | 显示全部楼层
这个是参考以上的
select distinct xuser_id from tinvoice
where (xyear between 2008 and 2009) and xu ...
ocm1 发表于 20-10-2010 02:39 PM



如果您只要找2008年(不需要限定2008年那一个月份)和2009年,这半年内没再到回来的的顾客资料。是可以用以上方法完成。
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 20-10-2010 03:54 PM | 显示全部楼层


我发现一个问题
1.select distinct xuser_id
没问题,

2.select distinct xuser_id,xyear,xmonth,xrm
我加了 xyear,xmonth,xrm 之后就出现duplicate 的答案了。

要如何不会出现 duplicate,又可以出现xuser_id,xyear,xmonth,xrm
回复

使用道具 举报

发表于 20-10-2010 05:51 PM | 显示全部楼层
of course, distinct xuser_id,xyear,xmonth,xrm
是表示你要同时满足所有Column没有Duplicate的condition

我不知道你为何要所有column, 不过你的要求,我没试过。无法帮忙。
回复

使用道具 举报

 楼主| 发表于 20-10-2010 06:55 PM | 显示全部楼层
不是,我的意思是说:
如果我放distinct xuser_id 它只能display 出xuser_id 而已。(1)

如果我放 distinct xuser_id ,xname(从别table拿的name, 它就会出现bug了)
参考而已:
回复

使用道具 举报

发表于 21-10-2010 10:36 AM | 显示全部楼层
先SELECT INTO 另一个Table 再JOIN Query to get name field.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 9-5-2024 02:39 AM , Processed in 0.081957 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表