當前位置:首頁 » 編程語言 » sql動態列查詢
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql動態列查詢

發布時間: 2022-10-03 12:40:49

『壹』 sql可以動態查詢嗎

沒看明白 怎麼個動態法
select QTY_on_WAY from prdt1 where prd_no=:prd_no 跟 TF_MO表 有啥關系??

『貳』 如何解決sql語句中的動態多列查詢問題

select sum(case when con1||con2='11' then count else 0 end) 三星筆記本,
sum(case when con1||con2='12' then count else 0 end) 三星台式機,
sum(case when con1||con2='22' then count else 0 end) 蘋果筆記本,
sum(case when con1||con2='22' then count else 0 end) 蘋果台式機
from table_name

另外用decode一樣可以替代case when,效率一樣

『叄』 請問sql中怎麼實現欄位的動態查詢

用動態sql即可實現。

如student表中有如下內容:

『肆』 SQL如何使用select動態列 from 進行查詢

這就是傳說中的交叉表
/*
普通行列轉換

假設有張學生成績表(tb)如下:
Name Subject Result
張三 語文 74
張三 數學 83
張三 物理 93
李四 語文 74
李四 數學 84
李四 物理 94
*/

-------------------------------------------------------------------------
/*
想變成
姓名 語文 數學 物理
---------- ----------- ----------- -----------
李四 74 84 94
張三 74 83 93
*/

create table tb
(
Name varchar(10) ,
Subject varchar(10) ,
Result int
)

insert into tb(Name , Subject , Result) values('張三' , '語文' , 74)
insert into tb(Name , Subject , Result) values('張三' , '數學' , 83)
insert into tb(Name , Subject , Result) values('張三' , '物理' , 93)
insert into tb(Name , Subject , Result) values('李四' , '語文' , 74)
insert into tb(Name , Subject , Result) values('李四' , '數學' , 84)
insert into tb(Name , Subject , Result) values('李四' , '物理' , 94)
go

--動態SQL,指subject不止語文、數學、物理這三門課程。
declare @sql varchar(8000)
set @sql = 'select Name as ' + '姓名'
select @sql = @sql + ' , max(case Subject when ''' + Subject + ''' then Result else 0 end) [' + Subject + ']'
from (select distinct Subject from tb) as a
set @sql = @sql + ' from tb group by name'
exec(@sql)
/*
姓名 數學 物理 語文
---------- ----------- ----------- -----------
李四 84 94 74
張三 83 93 74
*/

『伍』 如何執行 sql 動態分組 查詢

你說的前後一周內沒有明白范圍,是你查詢的日期,還是訂單日期
還是沒太明白前後一周內,我先這樣寫吧,日期范圍你自己套進去就行了

select id,商品名稱,訂單金額,數量,訂單日期
,(select sum(訂單金額) from 訂單表 t2 where t1.商品名稱=t2.商品名稱 and t2.訂單日期>=(t1.訂單日期-7) and t2.訂單日期<=(t1.訂單日期+7)) 合計值
from 訂單表 t1
where to_char(t1.訂單日期, 'YYYY-MM-DD') >= '2019-03-01' and to_char(t1.訂單日期, 'YYYY-MM-DD')<'2019-03-02';

『陸』 SQL查詢動態列的方法

首先,這張表本身就很不合理,如果不是硬要用這種結構,推薦您使用一列存儲數據,我有點明白您可能是要最後通過select生成一張月報表,不知我的猜想對嗎?(好像還是沒搞清楚)

就事論事吧
create proc xxx @day int
as
if @day=1 select * from 表 where D1=@day
if @day=2 select * from 表 where D2=@day
if @day=3 select * from 表 where D3=@day
if @day=4 select * from 表 where D4=@day
後面就不寫了,共要31行,恐怕也是最好想出的辦法了

『柒』 sql動態查詢

用動態sql即可實現。

如student表中有如下內容:

『捌』 SQL 動態查詢問題

是不是可以從T_SQL中取到@sql中用charindex的方式取得變數的位置,然後進行字元串截斷,自己拼接,在用execute(@sql)來執行?
因為T_SQL中取出來後,變數也就不是原有的變數了,而是都進入了一個字元串當中,所以再也不能當做變數使用了。你只能把這個變數找出來,然後通過字元串截斷的方式把前後截出來,然後把你真正的變數拼到上面。

『玖』 sql交叉動態列查詢

select b.id,b.title,
min(case when a.id=1 then '是' end) as 新聞,
min(case when a.id=2 then '是' end) as 公告,
min(case when a.id=3 then '是' end) as 帖子,
from a,b where a.id=b.a.id
group by b.id,b.title

『拾』 oralce怎麼用SQL查詢出動態列出來呢根據查詢條件得到相應的列。

select 商品名稱,SUM(訂單數量),
SUM(case TO_CHAR(訂單日期,'MM') when '04' then 1 else 0 end) "4月",
SUM(case TO_CHAR(訂單日期,'MM') when '05' then 1 else 0 end) "5月",
SUM(case TO_CHAR(訂單日期,'MM') when '06' then 1 else 0 end) "6月"
from 數據表
where to_char(訂單日期,'yyyyMM') >= '20105' AND to_char(訂單日期,'yyyyMM') <= '20106'
group by to_char(訂單日期,'MM')

這個是可以的,根據實際的資料庫修改一下相關欄位就行了