SQL問題

發(fā)現(xiàn)最終數(shù)據(jù)好像就是多個限制條件連在一起的凤跑,而且還是相同的限制條件柿冲,
所以先把問題拆分一下酬滤!

我在這個表里查詢了數(shù)據(jù)
select examid,userid,workstation, max(cast(score as int)) score,workstationid from (
select distinct(userid),workstation,examid,score,workstationid from EsopUserExam where userid='F1331881'
) a
group by examid,userid,workstation,workstationid

workstation和workstationid是一一對應(yīng)關(guān)系(但因為是不同BU的隧膏,所以這里看不出來贿堰,)

考試試卷信息

要用到的欄位都顯示出來了跑杭。

問題铆帽?:這個表里他有相同的workstation,例如印刷機有多個,我現(xiàn)在要做到是把相同的給加入限制條件(如果有相同的workstation,就取這個useID所在的bu 工站ID(workstationID))

select * from esoppost


esoppost表

select * from tb_emp


3.png
聯(lián)合查詢一下德谅,查找到userid 所在的BU的工站ID了爹橱,

select a.* from esoppost a,tb_emp b where a.buname=b.plant and b.emp_no='F1331881'


4.png

第一張圖的workstationID和esoppost表中的ID是一一對應(yīng)關(guān)系

已經(jīng)搞定了第一個小問題,雖然這語句寫的有點渣窄做,但
select * from (
select examid,userid,workstation, max(cast(score as int)) score,workstationid from (
select distinct(userid),workstation,examid,score,workstationid from EsopUserExam  where userid='F1331881'  ) a
group by examid,userid,workstation,workstationid )a where workstationid  in(
select a.id from esoppost a,tb_emp b where a.buname=b.plant and b.emp_no='F1331881')
union
select * from (
select examid,userid,workstation, max(cast(score as int)) score,workstationid from (
select distinct(userid),workstation,examid,score,workstationid from EsopUserExam  where userid='F1331881'  ) a
group by examid,userid,workstation,workstationid )a where workstationid not  in(
select a.id from esoppost a,tb_emp b where a.buname=b.plant and b.emp_no='F1331881') and workstation not in(
select workstation from (
select examid,userid,workstation, max(cast(score as int)) score,workstationid from (
select distinct(userid),workstation,examid,score,workstationid from EsopUserExam  where userid='F1331881'  ) a
group by examid,userid,workstation,workstationid )a where workstationid  in(
select a.id from esoppost a,tb_emp b where a.buname=b.plant and b.emp_no='F1331881'))
問題2愧驱?:這個語句如何改進
最終語句
select  examid,userid,workstation,workstationid, cast(score as varchar) score from (
select * from (
select examid,userid,workstation, max(cast(score as int)) score,workstationid from (
select distinct(userid),workstation,examid,score,workstationid from EsopUserExam  where userid='F1331881'  ) a
group by examid,userid,workstation,workstationid )a where workstationid  in(
select a.id from esoppost a,tb_emp b where a.buname=b.plant and b.emp_no='F1331881')
union
select * from (
select examid,userid,workstation, max(cast(score as int)) score,workstationid from (
select distinct(userid),workstation,examid,score,workstationid from EsopUserExam  where userid='F1331881'  ) a
group by examid,userid,workstation,workstationid )a where workstationid not  in(
select a.id from esoppost a,tb_emp b where a.buname=b.plant and b.emp_no='F1331881') and workstation not in(
select workstation from (
select examid,userid,workstation, max(cast(score as int)) score,workstationid from (
select distinct(userid),workstation,examid,score,workstationid from EsopUserExam  where userid='F1331881'  ) a
group by examid,userid,workstation,workstationid )a where workstationid  in(
select a.id from esoppost a,tb_emp b where a.buname=b.plant and b.emp_no='F1331881'))
)a
union 
select distinct examid,userid,station,workstationid,score from (
select userid,examid,station,classname,workstationid,score from Practical  
union all
select distinct userid,examid,station,classname,workstationid,score from PractrcalTemp
) b

最終效果


success.png

查詢結(jié)束,這個問題解決了M终怠组砚!現(xiàn)在是怎么把這個語句給優(yōu)化
雖然可以寫在存儲里面!但這語句看起來就很渣的樣子掏颊,完全不能裝逼嘛糟红!
所以要如何優(yōu)化呢!N谝丁E璩ァ!

  • 我優(yōu)化了一下准浴,但只是把語句縮短了一點點而已事扭,完全說不上優(yōu)化啊P至选>浜怠:
把 (
select distinct(userid),workstation,examid,score,workstationid from EsopUserExam  where userid='F1331881' 
 ) a
換成 
EsopUserExam  where userid='F1331881' 
這里 的group by 分組效果和distinct去重效果 一樣所以 這里去除了這個語句

改進了一下,越來越長了晰奖,這絕對是我寫過的單句最長的SQL
有用到的函數(shù):

  • isnull(str1,str2) 當str1為空時谈撒,用str2代替
  • isnumeric(str1) 查找當前欄位為數(shù)字的;
  • distinct() 去重
    -count()計數(shù)
select @gg=workstationid from #a where workstationid in (
            select workstationid from #a where workstation in (
            select workstation from #a group by workstation having  count(workstation)>1) group by workstationid having count(workstationid)<2) 
            and isnumeric(score)>0

            select * from #a where workstation not in (
            select workstation from #a where workstationid in (
            select workstationid from #a where workstation in (
            select workstation from #a group by workstation having  count(workstation)>1) group by workstationid having count(workstationid)<2)
            and workstationid in (  select a.id from esoppost a,tb_emp b where a.buname=b.plant and b.emp_no='F1331881' ))
            union
            select* from #a where workstationid in (
            select isnull(workstation,()) from #a where workstationid in (
            select workstationid from #a where workstation in (
            select workstation from #a group by workstation having  count(workstation)>1) group by workstationid having count(workstationid)<2)
            and workstationid in (  select a.id from esoppost a,tb_emp b where a.buname=b.plant and b.emp_no='F1331881' ))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末匾南,一起剝皮案震驚了整個濱河市啃匿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖溯乒,帶你破解...
    沈念sama閱讀 212,454評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件夹厌,死亡現(xiàn)場離奇詭異,居然都是意外死亡裆悄,警方通過查閱死者的電腦和手機矛纹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來光稼,“玉大人或南,你說我怎么就攤上這事“” “怎么了采够?”我有些...
    開封第一講書人閱讀 157,921評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長冰垄。 經(jīng)常有香客問我蹬癌,道長,這世上最難降的妖魔是什么虹茶? 我笑而不...
    開封第一講書人閱讀 56,648評論 1 284
  • 正文 為了忘掉前任逝薪,我火速辦了婚禮,結(jié)果婚禮上写烤,老公的妹妹穿的比我還像新娘翼闽。我一直安慰自己,他們只是感情好洲炊,可當我...
    茶點故事閱讀 65,770評論 6 386
  • 文/花漫 我一把揭開白布感局。 她就那樣靜靜地躺著,像睡著了一般暂衡。 火紅的嫁衣襯著肌膚如雪询微。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,950評論 1 291
  • 那天狂巢,我揣著相機與錄音撑毛,去河邊找鬼。 笑死唧领,一個胖子當著我的面吹牛藻雌,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播斩个,決...
    沈念sama閱讀 39,090評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼胯杭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了受啥?” 一聲冷哼從身側(cè)響起做个,我...
    開封第一講書人閱讀 37,817評論 0 268
  • 序言:老撾萬榮一對情侶失蹤鸽心,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后居暖,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體顽频,經(jīng)...
    沈念sama閱讀 44,275評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,592評論 2 327
  • 正文 我和宋清朗相戀三年太闺,在試婚紗的時候發(fā)現(xiàn)自己被綠了糯景。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,724評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡省骂,死狀恐怖莺奸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情冀宴,我是刑警寧澤,帶...
    沈念sama閱讀 34,409評論 4 333
  • 正文 年R本政府宣布温学,位于F島的核電站略贮,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏仗岖。R本人自食惡果不足惜逃延,卻給世界環(huán)境...
    茶點故事閱讀 40,052評論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望轧拄。 院中可真熱鬧揽祥,春花似錦、人聲如沸檩电。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,815評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽俐末。三九已至料按,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間卓箫,已是汗流浹背载矿。 一陣腳步聲響...
    開封第一講書人閱讀 32,043評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留烹卒,地道東北人闷盔。 一個月前我還...
    沈念sama閱讀 46,503評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像旅急,于是被迫代替她去往敵國和親逢勾。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,627評論 2 350

推薦閱讀更多精彩內(nèi)容