我的表是:
tblProduct
male| female| Brand_ID
和
tblBrands
Brand_ID| Brand_Name
我的查询在这里:
Select distinct
a.Male, a.Female, b.Brand_Name
from
tblProduct as a
inner join
tblBrands as b on b.Brand_ID = a.Brand_ID
此查询返回此输出:
Male Female Brand_Name
----------------------------------
1 0 ABC
0 1 ABC
1 0 ABC
1 0 PQR
1 0 PQR
0 1 PQR
0 1 XYZ
0 1 XYZ
但是我想要下面显示的输出 - 我应该怎样做才能得到它?
Male Female Brand_Name
----------------------------------
1 1 ABC
1 1 PQR
0 1 XYZ
检查是否品牌XYZ是男性还是女性如果男性位是真,女性位是真是显示XYZ 1 1或者如果女性或男性存在根据显示XYZ 1 0或XYZ 0 1 < / p>
答案 0 :(得分:2)
一个简单的GROUP BY
子句将解决您的问题
Select
Max(cast(a.Male as int)) as Male ,
Max(cast(a.Female as int)) as Female,
b.Brand_Name
from tblProduct as a inner join
tblBrands as b on b.Brand_ID = a.Brand_ID
group by b.Brand_Name
答案 1 :(得分:1)
由于您提到Male
和Female
列的类型为bit
,因此您可以编写查询,不需要 {{1} }和MAX
函数:
CAST