sql按条件
① sql按条件取值,根据不同的条件来取不同的结果
select case when DATEPART(hour,采样时间)<9 then 10
when DATEPART(hour,采样时间)>=9 and DATEPART(hour,采样时间)<10 then 11
else 11:30 end as time from table
② SQL按条件汇总求和
如下:
1、第一步,将示例表导入SQL语句中,见下图,转到下面的步骤。
③ mysql根据条件执行sql
在项目开发中,我们常常会用到根据不同条件,执行不同的sql,在mysql中可以用 IF(expr1,expr2,expr3) 来满足这个需求。下面我给大家一个示例:
示例1:sql比较复杂
SELECT
if(dom.PLAN_NO is null,
(ifnull( ( SELECT sum( DELIVERY_QTY ) FROM delivery_order_materiel WHERE 1=1 and PO_NO=pom.PO_NO and MATERIEL_CODE = pom.MATERIEL_CODE ), 0 )),
ifnull( ( SELECT sum( DELIVERY_QTY ) FROM delivery_order_materiel WHERE POM_PM_CODE = pom.PM_CODE AND PLAN_NO = dpm.PLAN_NO ), 0 )
)AS tempFiled
from tableName
where 1=1
示例2:
SELECT
if(dom.PLAN_NO=1,
( SELECT sum( DELIVERY_QTY ) FROM delivery_order_materiel WHERE 1=1 and PO_NO=pom.PO_NO and MATERIEL_CODE = pom.MATERIEL_CODE ),
( SELECT sum( DELIVERY_QTY ) FROM delivery_order WHERE 1=1 AND PLAN_NO = dpm.PLAN_NO )
)AS tempFiled
from tableName
where 1=1
④ SQL按条件汇总求和
1、首先在SQL语句中中导入样表。
⑤ sql怎么用查询结果作为条件进行查询
1、查询数据库表的所有字段并直接使用select语句。从数据库表中选择*。
⑥ SQL按条件汇总求和
select
sum(case
when
mark
=
0
then
point
else
0
end)
as
Point_0,
sum(case
when
mark
=
1
then
point
else
0
end)
as
Point_1,
Account
from
表A
group
by
Account;
***************
我从不辩解,测试log自己看:
[TEST@ORA1]
SQL>select
*
from
表A;
POINT
MARK
ACCOUNT
----------
----------
----------
20
1
John
30
0
Allen
10
1
Allen
15
1
John
20
0
Michael
25
0
John
6
rows
selected.
[TEST@ORA1]
SQL>select
sum(case
when
mark
=
0
then
point
else
0
end)
as
Point_0,
2
sum(case
when
mark
=
1
then
point
else
0
end)
as
Point_1,
3
Account
4
from
表A
5
group
by
Account;
POINT_0
POINT_1
ACCOUNT
----------
----------
----------
30
10
Allen
25
35
John
20
0
Michael
[TEST@ORA1]
SQL>
---
以上,希望对你有所帮助。
⑦ SQL 按条件 随机 取记录
1.select count(*),商家 from table group by 商家
2.select count(*) from table
3.select top 3 * from table order by rnd(id)
4.select top 66 * from table order by rnd(id)
3.4. access对随机函数的支持不好,建议在程序中实现,具体思路是先取出表中所有的id值,然后存到一个数组中,然后用程序取随机数组
5.到底是要消费笔数最多还是金额最大。两个条件会冲突的,消费只有1笔,但也有可以金额是最大的。
6.select id,列名1,列名2,.. into newtable(列名1,列名2,...) where id in(分别写3,4,5的条件)
7.delete table where id not in ( select min(id) from table group by id)