根據輸入的月份生成累計工時

2021-04-02 21:50:50 字數 3150 閱讀 5262

資料庫:mssql

用儲存過程實現:

--測試資料:

create table work1

(worktime decimal,workmonth char(2),type varchar(20)) go

create table chanpin(type varchar(20),cpname varchar(60)) go

insert into work1 values(11, '1', 'c01')

insert into work1 values( 11, '1', 'c02')

insert into work1 values(11, '2', 'c02')

insert into work1 values(12, '1', 'c02')

insert into work1 values( 12, '2', 'c02')

insert into work1 values(13, '1', 'c02')

insert into work1 values(14, '1', 'c03')

insert into work1 values(15, '1', 'c03')

insert into chanpin values('c01', 'cc0011')

insert into chanpin values('c02', 'cc0022')

insert into chanpin values('c03', 'cc0033') go

--需要結果:

產品    產品工時  1月   2月

cc0011   11        11   0

cc0022   59        36   23

cc0033   29        29   0

------------------------------------

create proc my_p1 (@m1 char(2)='0',@m2 char(2)='0',@m3 char(2)='0',@m4 char(2)='0',@m5 char(2)='0',@m6 char(2)='0',

@m7 char(2)='0',@m8 char(2)='0',@m9 char(2)='0',@m10 char(2)='0',@m11 char(2)='0',@m12 char(2)='0') as

declare @sql varchar(4000)

select @sql='select cpname as ''產品'',sum(worktime) as  ''產品工時'','

if @m1<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m1+''' then worktime else 0 end) as

'''+@m1+'月'','

if @m2<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m2+''' then worktime else 0 end) as

'''+@m2+'月'','

if @m3<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m3+''' then worktime else 0 end) as

'''+@m3+'月'','

if @m4<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m4+''' then worktime else 0 end) as

'''+@m4+'月'','

if @m5<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m5+''' then worktime else 0 end) as

'''+@m5+'月'','

if @m6<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m6+''' then worktime else 0 end) as

'''+@m6+'月'','

if @m7<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m7+''' then worktime else 0 end) as

'''+@m7+'月'','

if @m8<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m8+''' then worktime else 0 end) as

'''+@m8+'月'','

if @m9<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m9+''' then worktime else 0 end) as

'''+@m9+'月'','

if @m10<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m10+''' then worktime else 0 end) as

'''+@m10+'月'','

if @m11<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m11+''' then worktime else 0 end) as

'''+@m11+'月'','

if @m12<>'0'

select @sql=@sql+'sum(case workmonth when

'''+@m12+''' then worktime else 0 end) as

'''+@m12+'月'','

select @sql=left(@sql,len(@sql)-1)

select @sql=@sql+' from work1 w,chanpin c where w.type=c.type group by cpname'

exec(@sql) go

--呼叫示例:

my_p1 '1','2'

根據輸入的日期展示當前月份的日曆

根據具體日期展示當前月份的日曆 public static void setcalendar catch parseexception e system.out.print 您輸入的日期是 dateformat.format date n 輸出日曆 system.out.print t日 t一 t二...

Java 根據輸入年月顯示對應月份的日曆

不是技術貼,只是記錄貼,主函式 輸入年 月,輸出月份日曆。public static void main string args 因為日曆上面具體月 年是另外的演算法,分開一個函式,中間虛線和星期是不變的,另外處理。日曆又分為兩部分,上面顯示具體年月日,下面顯示月份日期。類似下圖 先劃分兩個模組,一...

根據字串查詢數字對應的月份

要求根據數字1 12數字返回月份名稱 monthstr 一月二月三月四月五月六月七月八月九月十月十一月十二月 monthid eval input 請輸入月份數字 1 12 if monthid in range 1 10 pos monthid 1 2print monthstr pos pos ...

根據輸入值定義的陣列大小

指標表示,只針對內 include using namespace std int main for i 0 i相同點 都可用於申請動態記憶體和釋放記憶體 不同點 1 操作物件有所不同。malloc與free是c c 語言的標準庫函式,new delete 是c 的運算子。對於非內部資料類的物件而言...

MySQL根據輸入的查詢條件排序

現在一個需求是查詢某一列,用逗號分開,返回的結果要根據輸入的順序返回結果 比如 姓名的輸入框輸入的是 zhangsan,lisi 那麼返回的結果也要是按照 zhangsan,lisi 這樣的順序展示 有如下表classroom,內容如下 如果根據欄位名稱去查,那麼它會根據字典順序排序,如下所示 如果...