SQL - Distinct& amp;算上一张桌子

时间:2016-07-13 12:53:34

标签: sql

需要一个简单的查询来汇总来自存在3列的表的结果: 订单ID,类别&品牌。

摘要应包含订单ID,不同的类别数量以及属于订单ID的品牌的不同数量。

示例数据:

orderno product brand
1       A       Z
1       A       X
1       B       Y
2       C       X
2       B       X
3       C       X
3       B       Y

预期结果:

orderno product brand
1       2       3
2       2       1
3       2       2

Sample Data & Summary

2 个答案:

答案 0 :(得分:0)

使用DISTINCT的{​​{1}}来获得预期结果:

COUNT

使用给定数据执行示例

select orderno, count(distinct product) as product, count(distinct brand) as brand
from testtable
group by orderno

结果:

declare @test1 table (orderno int, product varchar(2), brand varchar(2))
insert into @test1 (orderno, product, brand) values 
(1, 'A', 'Z'),
(1, 'A', 'X'),
(1, 'B', 'Y'),
(2, 'C', 'X'),
(2, 'B', 'X'),
(3, 'C', 'X'),
(3, 'B', 'Y');

select orderno, count(distinct product) as product, count(distinct brand) as brand
from @test1
group by orderno 

答案 1 :(得分:0)

试试这个,。

#!/bin/sh
username=$1

if [ $(grep -c '^'$username':' /etc/passwd) = 1 ] 
then
    echo "Warning, please user your AD credentials"
    login
fi

输出。,

/etc/pam.d/sshd