我尝试过阅读过很多帖子,但我仍然无法弄清楚如何处理此请求:
我有一张如下表格
+-------+---------------+------------+
| ID | Comp. | Role |
+-------+---------------+------------+
| 1 | abc | All |
+-------+---------------+------------+
| 1 | abc | Sales |
+-------+---------------+------------+
| 2 | def | All |
+-------+---------------+------------+
| 3 | zeh | All |
+-------+---------------+------------+
| 3 | zeh | TI |
+-------+---------------+------------+
我想基于结果角色,如果一个特定ID有两个角色,一个是全部,另一个是销售那么我需要考虑销售行
输出应该是这样的
+-------+---------------+------------+
| ID | Comp. | Role |
+-------+---------------+------------+
| 1 | abc | Sales |
+-------+---------------+------------+
| 2 | def | All |
+-------+---------------+------------+
| 3 | zeh | TI |
+-------+---------------+------------+
答案 0 :(得分:0)
select t.*
from t
where t.role = 'Sales'
union all
select t.*
from t
where t.role <> 'Sales' and
not exists (select 1 from t t2 where t2.id = t.id);
答案 1 :(得分:0)
如果您只有2个角色值('Sales'和'All')
,这将有效 create table #tmp(ID INT,Comp VARCHAR(10),Role VARCHAR(10))
insert into #tmp
SELECT 1,'abc','All'
union ALL
SELECT 1,'abc','Sales'
union ALL
SELECT 2,'def','All'
select ID,Comp,MAX(Role) As Role from #tmp
Group by Id,comp
drop table #tmp
答案 2 :(得分:0)
根据我的理解
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:27.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
//Recycleview
implementation 'com.android.support:recyclerview-v7:27.0.2'
//Butterknife
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//SDP
implementation 'com.intuit.sdp:sdp-android:1.0.5'
//OkHttp
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
//RxJava and RxAndroid
implementation 'io.reactivex.rxjava2:rxjava:2.0.6'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.ashdavies.rx:rx-firebase:1.3.3'
//RxBinding
implementation 'com.jakewharton.rxbinding:rxbinding-design:0.4.0'
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
//Glide
implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
//GSON
implementation 'com.google.code.gson:gson:2.2.4'
//Image Crop Library
implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.+'
//Dagger Android
implementation 'com.google.dagger:dagger:2.13'
annotationProcessor 'com.google.dagger:dagger-compiler:2.13'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.13'
implementation 'com.google.dagger:dagger-android-support:2.13'
//Only applying android dagger.
implementation 'com.android.support:cardview-v7:27.0.2'
implementation 'com.android.support:multidex:1.0.2'
}
这可以正常查看此demo
答案 3 :(得分:0)
如果您有其他功能,则应添加到CASE WHEN
SELECT
ID
, Comp
, CASE WHEN role_id = 0 THEN 'All'
WHEN role_id = 1 THEN 'Sales'
WHEN role_id = 2 THEN 'TI'
ELSE NULL END AS Role
FROM
(
SELECT
ID
, Comp
, MAX(CASE WHEN Role = 'All' THEN 0
WHEN Role = 'Sales' THEN 1
WHEN Role = 'TI' THEN 2 END
ELSE -1 END) AS role_id
FROM
t
GROUP BY
ID
, Comp
)tmp
答案 4 :(得分:0)
UNION ALL 将取代
select * from table
where role <> 'All'
union all
select * from table t
where not exists (
select 1 from table
where id = t.id and [comp.] = t.[comp.] and role <> 'All'
)
order by 1
结果:
ID Comp. Role
1 abc Sales
2 def All
3 zeh IT