Ⅰ 如何用sql語句將下圖1~7列的數據在month相同的情況下顯示在同一行而不是隔行顯示
select year(test1.order_head.ship_date) as year,month(test1.order_head.ship_date) as month,
sum(case when DATEPART ( weekday ,test1.order_head.ship_date)=2 then test1.order_body.totalmoney else 0 end) as [1],
sum(case when DATEPART ( weekday ,test1.order_head.ship_date)=3 then test1.order_body.totalmoney else 0 end) as [2],
sum(case when DATEPART ( weekday ,test1.order_head.ship_date)=4 then test1.order_body.totalmoney else 0 end) as [3],
sum(case when DATEPART ( weekday ,test1.order_head.ship_date)=5 then test1.order_body.totalmoney else 0 end) as [4],
sum(case when DATEPART ( weekday ,test1.order_head.ship_date)=6 then test1.order_body.totalmoney else 0 end) as [5],
sum(case when DATEPART ( weekday ,test1.order_head.ship_date)=7 then test1.order_body.totalmoney else 0 end) as [6],
sum(case when DATEPART ( weekday ,test1.order_head.ship_date)=1 then test1.order_body.totalmoney else 0 end) as [7]
from test1.order_body inner join
test1.order_head on test1.order_body.ord_id = test1.order_head.ord_id
where year(test1.order_head.ship_date)='2013' and test1.order_body.totalquan>0
group by year(test1.order_head.ship_date),month(test1.order_head.ship_date)
order by year,month
Ⅱ sql 查出相同的記錄 並把相同記錄 顯示在一起
貌似你這個語句就已經可以查出所有的相同記錄來了吧?
相同的記錄顯示在一起
可以加個排序
select * from 表1 where id in(select id from 表1 group by id having count(id)>1)
order by 相同記錄的欄位名 asc
如果不行可以HI我或追問。
Ⅲ SQL 兩個表順序顯示
在select里分開查詢就行了
SELECTA.*, B.*FROM A,B WHERE <condition>
Ⅳ SQL 語法,select 的時候如何指定 行的順序
你可以在表中另加一列,比如叫 sequence,用int 類型,然後你就可以定義你需要的次序了。
只有 select * from table order by sequence, 就直接得到你需要的順序。
Ⅳ SQL中數據行相鄰且某一列的值相同則合並
Oracle
select distinct a,FIRST_VALUE (b) over(order by rownum) from t
MSSQL 也有FIRST_VALUE函數
Ⅵ 請教:SQL語句如何實現同一列里如果與上一行數據相同,則這一行該列數據不顯示,但是其他列要照樣顯示。
declare@idint,@countchar(20)
print'id'+''+'count'
declareC_qcursorfor
selectdistinctidfromqqq
openC_q
fetchnextfromC_qinto@id
while@@fetch_status=0
begin
printcast(@idasvarchar(10))+':'
declareC_cursorfor
select[count]fromqqq
whereid=@id
openC_
fetchnextfromC_into@count
while@@fetch_status=0
begin
print''+@count
fetchnextfromC_into@count
end
closeC_
deallocateC_
print'======================'
fetchnextfromC_qinto@id
end
closeC_q
deallocateC_q
其實你要做報表使用游標是一個很好的選擇。我採用的就是這個方法給你做的,效果一樣。不會你再問。望採納,謝謝!
Ⅶ sql 如何將欄位相同的排列在一起
select欄位1from表名orderby欄位1
Ⅷ sql 表欄位有相同數據怎麼排列順序
例如,按學生學號升序排列,學生成績按降序排列
sql是這樣寫的:select
*
from
tab
order
by
id,scroe
desc
sql
server會根據order
by跟id
scroe
先後進行排序,
先根據id升序排序,再根據scroe降序排序,也許你會發現scroe列的數據不是按照降序排列
這就是優先排序的原則,order
by
後面誰在前,誰就優先排序
你可以仔細看看相同的id(你可以插入幾行相同的id,不同scroe),score就是按照降序排列的
Ⅸ sql中如何實現相鄰兩行數據合並轉換
select sum(case when wgrp_id='2' then quota end) w2, sum(case when wgrp_id='3' ;then quota end) w3, mm;from table;group by mm。
SQL語言,是結構化查詢語言(Structured Query Language)的簡稱。SQL語言是一種資料庫查詢和程序設計語言,用於存取數據以及查詢、更新和管理關系資料庫系統;同時也是資料庫腳本文件的擴展名。
SQL語言是高級的非過程化編程語言,允許用戶在高層數據結構上工作。它不要求用戶指定對數據的存放方法,也不需要用戶了解具體的數據存放方式,所以具有完全不同底層結構的不同資料庫系統可以使用相同的結構化查詢語言作為數據輸入與管理的介面。SQL語言語句可以嵌套,這使他具有極大的靈活性和強大的功能。
Ⅹ SQL如何同一行對於相同值,如何顯示到我的第一行,其餘行不顯示
select t1.lot,isnull(t2.t_pur_qty,'') as pur_qty,isnull(t2.t_ruku,'') as ruku,t1.item_no,t1.part_type,t1.pri
from 表名 t1 left join (select top 1 lot as t_lot,pur_qty as t_pur_qty,ruku as t_ruku,item_no as t_item_no from 表名 group by lot,pur_qty,ruku,item_no) t2
on t1.lot=t2.t_lot and t1.item_no=t2.t_item_no
測試過,莫問題了