當前位置:首頁 » 編程語言 » sql子查詢

sql子查詢

發布時間: 2022-01-14 20:39:55

sql子查詢統計

select
count(distinct(b.visit_id))'visit_id次數',

count(case when typeid=14 then 1 else 0)'14出現次數',
count(case when typeid=65 then 1 else 0)'65出現次數' from a
inner join b on a.visit_id=b.visitId

Ⅱ 求SQL子查詢語句

不用子查詢就能出結果啊
select a.userid,a.name,sum(money) as money from a, b where a.userid=b.userid and freeze='true' group by a.userid,a.name
------------------
排序就在語句後加
order by a.userid asc

你查不到結果?
這個a.DeptID=1 是什麼?不會是寫錯了吧
如果這個代表freeze的話。。。。一定要放到表b內!

==========================
Select a.UserID, a.Name, Sum(b.Money) As Money
From a, b
Where a.DeptID=1 And b.UserID=a.UserID And b.freeze='true'
Group By a.UserID, a.Name
order by a.userid asc

Ⅲ sql子查詢

額,哥們,首先,你的表有沒有主外鍵關系?其次,戶表與人表中有一些什麼欄位,你得說清楚。
建表如下:
戶口表(h) 人表(p)
nub 戶號 pid 身份證號
address 住址 name 姓名
state 說明狀態 relationship 家庭關系
nub 戶號
introction 說明
state 狀態
查詢姓名、身份證號、家庭關系、戶主、住址。
select p.name,p.pid,p.relationship,nvl((select p1.name from p p1 where p1.relationship like "是戶主"),null) huzhuName,h.address
from p,h
where h.nub=p.nub;

nvl(old,new)如果有old值,返回old;否則返回new
上面那條子查詢語句
select p1.name from p p1 where p1.relationship like "是戶主"
是先查出關系是戶主的人的名字,條件具體情況自己定(——)。

Ⅳ SQL子查詢

select 用戶名 ,所處大區, 所處省區,
sum(onty_cnt) as onty_cnt,sum(offty_cnt) as offty_cnt,
sum(overtime) as overtime
from
(select 用戶名 ,所處大區, 所處省區, onty_time ,offty_time, 打卡日期,overtime,
case when to_char(onty_time,'hh24:mi') > '09:00' then 1 else 0 end as onty_cnt,
case when to_char(offty_time,'hh24:mi') < '17:20' then 1 else 0 end as offty_cnt
where 省區 = 參數
and 打卡日期 between '2009-01-01' and '2009-01-31' )
group by 用戶名 ,所處大區, 所處省區

-------------------------------------
這是按人統計, 如果不要每個人的, 只要全部, 那就:

select 所處大區, 所處省區,
sum(onty_cnt) as onty_cnt,sum(offty_cnt) as offty_cnt,
sum(overtime) as overtime
from
(select 用戶名 ,所處大區, 所處省區, onty_time ,offty_time, 打卡日期,overtime,
case when to_char(onty_time,'hh24:mi') > '09:00' then 1 else 0 end as onty_cnt,
case when to_char(offty_time,'hh24:mi') < '17:20' then 1 else 0 end as offty_cnt
where 省區 = 參數
and 打卡日期 between '2009-01-01' and '2009-01-31' )
group by 所處大區, 所處省區

-----------------------------
就是從外面的查詢中把用戶名去掉!
不明白HI我

Ⅳ sql子查詢!

select psnID,psnName,(select SUM(plan_UITime) from WorkPlan w where w.Plan_addPsnIDs=psnID)+
((select SUM(plan_CoderTime) from WorkPlan w where w.Plan_addPsnIDs=psnID)+
(select SUM(plan_TestTime) from WorkPlan w where w.Plan_addPsnIDs=psnID))
from Person
where psnID in(select psnID from ClientFeedbackContent where ClientFeedback_Admin='true')

Ⅵ SQL子查詢什麼時候用

嵌套SELECT語句也叫子查詢,一個 SELECT 語句的查詢結果能夠作為另一個語句的輸入值。子查詢不但能夠出現在Where子句中,也能夠出現在from子句中,作為一個臨時表使用,也能夠出現在select list中,作為一個欄位值來返回。
1、單行子查詢 :單行子查詢是指子查詢的返回結果只有一行數據。當主查詢語句的條件語句中引用子查詢結果時可用單行比較符號(=, >, <, >=, <=, <>)來進行比較。
例:
select ename,deptno,sal
from emp
where deptno=(select deptno from dept where loc='NEW YORK');

2、多行子查詢:多行子查詢即是子查詢的返回結果是多行數據。當主查詢語句的條件語句中引用子查詢結果時必須用多行比較符號(IN,ALL,ANY)來進行比較。其中,IN的含義是匹配子查詢結果中的任一個值即可("IN"
操作符,能夠測試某個值是否在一個列表中),ALL則必須要符合子查詢的所有值才可,ANY要符合子查詢結果的任何一個值即可。而且須注意ALL
和ANY 操作符不能單獨使用,而只能與單行比較符(=、>、< 、>= 、<= 、<>)結合使用。
例:
1).多行子查詢使用IN操作符號例子:查詢選修了老師名叫Rona(假設唯一)的學生名字
sql> select stName

Ⅶ 請教sql子查詢

1.子查詢是多表查詢的一種形式。子查詢就是次要查詢,它返回的結果數據被用

於主查詢的條件中。它返回的結果可以是一行或多行。比如:
例1//返回一條記錄
select *
from emp
where deptno=(select deptno
from dept
where dept_name='SALES')
例2//返回多條記錄
select *
from emp
where deptno in (select deptno
from dept
where dept_addr='北京')
由上例可以看出,子查詢並不要求兩個表有相關欄位,只要得到子查詢的結果集

就行,用於主查詢,而連接查詢,則必須要求兩個表有相關欄位。

2.連接查詢是對兩個表逐記錄(一行一行地)進行相關查詢,例1可寫為連接查詢

,例2不可以寫為連接查詢,因為子查詢返回多行記錄。
select *
from emp a, dept b
where a.deptno= b.deptno and b.dept_name='SALES'

上述形式為內連接,還有外連接

oracle中的欄位後面有加號是什麼意思,多說一點詳細一點
a.emp_no = b.emp_no (+)

不論把(+)寫在等號左邊項還是右邊項,統稱為外連接

在內連接中,只返回那些在連接表中有匹配數據的數據行

外連接即在被連接的表中加入一個空行來與沒有匹配行的數據進行匹配

比如查詢員工所屬的部門,總裁不屬於任何部門,(老闆不屬於任何部門,dname

項是空的)

select e.emp_name ename,d.dept_name dname
from emp e, dept d
where e.dept_no=d.dept_no(+)

ename dname
——————————
sam SALES
tom ACCOUNTING
king

上述外連接請參看:
http://..com/question/44054333.html

3.遞歸查詢可以用來表示相同實體內相互聯系的各個數據集間的關系;外連接可以用來連接那些本來沒有匹配數據的表;相關子查詢顯示如何在子查詢中使用主查詢中的數據來加強查詢條件,就是帶exists謂詞的查詢。

建議你看看薩師宣老師的《資料庫原理》(高教出版社)

Ⅷ sql的相關子查詢

確實不好理解,你們老師真厲害.
以下是我寫的代碼:
select * from s where sno in
(
select sno from sc a
join
(select cno from sc where sno='s2') b
on a.cno=b.cno
group by sno
having count(*)=(select count(*) from sc where sno='s2')
)
我的思路是這樣的,如果學生s1選的課程包含學生s2選的所有課程,那麼這兩個學生的課程inner join之後,結果集的記錄數應該等於s2的選課數.根據這個規則,就可以通過以下代碼求出學號:
select sno from sc a
join
(select cno from sc where sno='s2') b
on a.cno=b.cno
group by sno
having count(*)=(select count(*) from sc where sno='s2')
學號出來了,對應的名字也就不難求了,如第一段代碼.

Ⅸ sql where嵌套select子查詢

sql where嵌套select子查詢
判定A表的數據是否存在B表,如果存在則顯示存在,不存在則顯示不存在
例如S#存在於SC表和student表中,判定s#是否都在student表中存在存在則顯示存在,不存在則顯示不存在,具體如下:
from student

select s#,
case when s# in(select s# from sc) then '存在'
when s# not in( select s# from sc) then '不存在'
end
from student

熱點內容
單片機android 發布:2024-09-20 09:07:24 瀏覽:762
如何提高三星a7安卓版本 發布:2024-09-20 08:42:35 瀏覽:661
如何更換伺服器網站 發布:2024-09-20 08:42:34 瀏覽:309
子彈演算法 發布:2024-09-20 08:41:55 瀏覽:286
手機版網易我的世界伺服器推薦 發布:2024-09-20 08:41:52 瀏覽:815
安卓x7怎麼邊打游戲邊看視頻 發布:2024-09-20 08:41:52 瀏覽:160
sql資料庫安全 發布:2024-09-20 08:31:32 瀏覽:91
蘋果連接id伺服器出錯是怎麼回事 發布:2024-09-20 08:01:07 瀏覽:505
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:655
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:479