细化SQL查询

细化SQL查询,sql,Sql,我的查询返回的数据太多。这是查询、一些示例数据和我真正想要的示例 select sd.ident,sd.suniq, td.testc, td.testuniq, td.subtestc, ts.testscore, ud.metadept, max(ts.takendt)AS testdate from studemo sd, stutests ts, testdef td, udefstu ud where ts.suniq =sd.suniq and td.testuniq = ts.te

我的查询返回的数据太多。这是查询、一些示例数据和我真正想要的示例

select sd.ident,sd.suniq, td.testc, td.testuniq, td.subtestc, ts.testscore, ud.metadept, max(ts.takendt)AS testdate
from studemo sd, stutests ts, testdef td, udefstu ud
where ts.suniq =sd.suniq
and td.testuniq = ts.testuniq
and ts.suniq = ud.suniq
and td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
group by sd.suniq, sd.ident, td.testc, td.subtestc, ts.testscore, ud.metadept, ts.takendt, td.testuniq
order by suniq
ident   suniq   testc   testuniq    subtestc    testscore   metadept    testdate
102201  2001444 ADEPT   2000091 L3  23  NULL    09/01/2006 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  5   NULL    09/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000090 L2  32  NULL    09/01/2006 00:00
141058  2004980 ADEPT   2000089 L1  27  NULL    05/01/2006 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00
141058  2004980 ADEPT   2000090 L2  27  NULL    06/01/2007 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00
样本输出

ident   suniq   testc   testuniq    subtestc    testscore   metadept    testdate
102201  2001444 ADEPT   2000091 L3  23  NULL    09/01/2006 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  5   NULL    09/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000090 L2  32  NULL    09/01/2006 00:00
141058  2004980 ADEPT   2000089 L1  27  NULL    05/01/2006 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00
141058  2004980 ADEPT   2000090 L2  27  NULL    06/01/2007 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00
想要的产出

ident   suniq   testc   testuniq    subtestc    testscore   metadept    testdate
102201  2001444 ADEPT   2000091 L3  23  NULL    09/01/2006 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  5   NULL    09/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000090 L2  32  NULL    09/01/2006 00:00
141058  2004980 ADEPT   2000089 L1  27  NULL    05/01/2006 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00
141058  2004980 ADEPT   2000090 L2  27  NULL    06/01/2007 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00

试试这个。您的分组条件太多,需要在其余字段上进行聚合。从您期望的输出来看,它们在我看来都像最大值:

ident   suniq   testc   testuniq    subtestc    testscore   metadept    testdate
102201  2001444 ADEPT   2000091 L3  23  NULL    09/01/2006 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  5   NULL    09/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000090 L2  32  NULL    09/01/2006 00:00
141058  2004980 ADEPT   2000089 L1  27  NULL    05/01/2006 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00
141058  2004980 ADEPT   2000090 L2  27  NULL    06/01/2007 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00
select  sd.ident,
        sd.suniq, 
        td.testc, 
        MAX(td.testuniq) as TestUniq, 
        MAX(td.subtestc) as Subtestc, 
        MAX(ts.testscore) as TestScore, 
        MAX(ud.metadept) as metadept, 
        max(ts.takendt)AS testdate
from studemo sd, stutests ts, testdef td, udefstu ud
where ts.suniq =sd.suniq
and td.testuniq = ts.testuniq
and ts.suniq = ud.suniq
and td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
group by sd.suniq, sd.ident, td.testc,
order by suniq

我想我知道你在追求什么,你是想让最新的或最高价值的项目显示独家。。。您是否尝试过“distinct”关键字?如果我有一个测试数据集来尝试一些查询,我将能够很快给你正确的答案,但从我的头脑中提取它是另一回事

ident   suniq   testc   testuniq    subtestc    testscore   metadept    testdate
102201  2001444 ADEPT   2000091 L3  23  NULL    09/01/2006 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  5   NULL    09/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000090 L2  32  NULL    09/01/2006 00:00
141058  2004980 ADEPT   2000089 L1  27  NULL    05/01/2006 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00
141058  2004980 ADEPT   2000090 L2  27  NULL    06/01/2007 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00
选择distinct sd.ident、sd.suniq、td.testc、td.testuniq、td.subtestc、ts.testscore、ud.metadept、maxts.takendtAS testdate 来自studemo sd、Studs、testdef td、udefstu ud 其中ts.suniq=sd.suniq 和td.testuniq=ts.testuniq 和ts.suniq=ud.suniq 和td.testuniq在'2000089','2000090','2000091','2000092' 按sd.suniq、sd.ident、td.testc、td.subtestc、ts.testscore、ud.metadept、ts.takent、td.testuniq分组
按suniq desc下单

您能解释一下样本输出背后的逻辑吗?为什么即使in子句中列出了testuniq='2000089'的值,示例输出中也不包含该值?示例与所需的有什么区别?看起来你在寻找最新测试日期的那一行,但可能会有所不同,因为我似乎不明白你想要什么……但是……你想要什么样的精炼?你只是想要更少的结果吗?或者你想要的4个结果有什么特别的地方需要寻找吗?-1没有解释你想要的是什么。我相信OP每个标识只需要一行。当出现多个字段时,请选择要显示的最新testdate的字段,而不应输出具有相同标识的所有其他字段。DISTINCT只会为他提供您指定的字段的唯一组合,即所有字段。
ident   suniq   testc   testuniq    subtestc    testscore   metadept    testdate
102201  2001444 ADEPT   2000091 L3  23  NULL    09/01/2006 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  5   NULL    09/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2006 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000090 L2  32  NULL    09/01/2006 00:00
141058  2004980 ADEPT   2000089 L1  27  NULL    05/01/2006 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00
141058  2004980 ADEPT   2000090 L2  27  NULL    06/01/2007 00:00
102201  2001444 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
101184  2001532 ADEPT   2000092 L4  7   NULL    06/01/2007 00:00
590122  2001950 ADEPT   2000091 L3  22  NULL    06/01/2007 00:00
141058  2004980 ADEPT   2000090 L2  28  NULL    01/25/2008 00:00