周資料轉換為天資料的乙個Sql資料查詢

2022-04-17 15:10:00 字數 2785 閱讀 2069

以前碰到的乙個表,儲存時間按照週為單位儲存,而卻需要按照天為單位使用。為了減少程式複雜度,便增加了多層查詢,提高sql查詢結果可用性。

select 

(week_no-week_start)*7+week_day-week_day_start pp,

dateadd(day,(week_no-week_start)*7+week_day-week_day_start,month) the_day,

* from   

(select

case datepart(w,month) when 7 then 1 else datepart(w,month)+1 end as week_day_start,

1 week_start,

*  from

(select

[companyid]

,[employeeno]

,[year]

,[month]

,[week_no]

,[order_no]

,[sell_no]

,[workcode]

,1 as week_day

,[workhours_mon]

,[h_mon] as h

,[m_mon] as m

,[groupcode]

from

[cardtable]

union

select

[companyid]

,[employeeno]

,[year]

,[month]

,[week_no]

,[order_no]

,[sell_no]

,[workcode]

,2 as week_day

,[workhours_tue] as workhours

,[h_tue] as h

,[m_tue] as m

,[groupcode]

from

[cardtable]

union

select

[companyid]

,[employeeno]

,[year]

,[month]

,[week_no]

,[order_no]

,[sell_no]

,[workcode]

,3 as week_day

,[workhours_wed] as workhours

,[h_wed] as h

,[m_wed] as m

,[groupcode]

from

[cardtable]

union

select

[companyid]

,[employeeno]

,[year]

,[month]

,[week_no]

,[order_no]

,[sell_no]

,[workcode]

,4 as week_day

,[workhours_thu] as workhours

,[h_thu] as h

,[m_thu] as m

,[groupcode]

from

[cardtable]

union

select

[companyid]

,[employeeno]

,[year]

,[month]

,[week_no]

,[order_no]

,[sell_no]

,[workcode]

,5 as week_day

,[workhours_fri] as workhours

,[h_fri] as h

,[m_fri] as m

,[groupcode]

from

[cardtable]

union

select

[companyid]

,[employeeno]

,[year]

,[month]

,[week_no]

,[order_no]

,[sell_no]

,[workcode]

,6 as week_day

,[workhours_sat] as workhours

,[h_sat] as h

,[m_sat] as m

,[groupcode]

from

[cardtable]

union

select

[companyid]

,[employeeno]

,[year]

,[month]

,[week_no]

,[order_no]

,[sell_no]

,[workcode]

,7 as week_day

,[workhours_sun] as workhours

,[h_sun] as h

,[m_sun] as m

,[groupcode]

from

[cardtable]

) temp1

--    where

--    week_no=6

)temp2

--where not( week_no=week_start and week_dayorder by pp

Chr 將乙個有序資料轉換為乙個ANSI字元

try 嘗試執行 except 出錯的時候執行,except有特定的錯誤型別 end try 嘗試執行 finally 無論如何都強制執行 end 例 tryage strtoint edit1.text showmessage format 生於 d年 yearof now age except ...

sql 轉換時間戳查詢每天資料

最近開發過程中,用到的關於幾個sql查詢的語句,記錄一下 資料表的建立時間是時間戳格式 1 查詢資料表中,每天的資料總量 select count as countnumber,獲取總數 from unixtime 建立時間,y m d as datetime 將時間戳轉換為日期輸出 from 表名...

乙個Excel轉換為Json格式的Python指令碼

來自 專案路徑 乙個python指令碼,自動轉換excel裡面的配置到json格式。1 這個是為unity準備的,不過稍作修改可執行於任何環境 2 excel內容要求為鍵值對的形式,具體可以參考指令碼內的說明。3 讀取 裡面限制了讀取配置的路徑為resources config,即所有的配置都要放在...