SQL:为另一列

时间:2017-08-17 13:07:40

标签: sql ms-access

我试图确定一列中的多少个唯一值可以按另一列中的值进行分组。

我的表格中有很多栏目,但我感兴趣的是:' department'和'用户'

这两个列的数据如何,重复用户具有相同的部门。

例如

Department    User

Insights      Mike

Insights      Mike

Insights      Chris

Market        Julie

Research      Will

Research      Sabrina

Research      Bryan

我想要的是以下内容:

Department    DistinctUsers

Insights       2

Market         1

Research       3

我的基本SQL知识告诉我这是代码的结构:

SELECT department, COUNT(DISTINCT user) 
FROM Sheet1
GROUP BY department, user

我看到人们会在 (SELECT DISTINCT User from Sheet1) 子句中实现 FROM ,但我无法将其集成到代码中。< / p>

非常感谢任何建议或提示!

谢谢!

3 个答案:

答案 0 :(得分:4)

正如评论中所提到的,您只需要从组中删除用户。

SELECT 
   department, 
   COUNT(DISTINCT [user]) as CT
FROM Sheet1
GROUP BY department

使用

SELECT
   department,
   count([user])
FROM
   (SELECT DISTINCT department, [user] from Sheet1) as x
GROUP BY
   department

答案 1 :(得分:1)

您的代码将完成这项工作,但不需要由用户分组

答案 2 :(得分:0)

import webbrowser
from Tkinter import *
from PIL import ImageTk,Image

Url1 = 'https://www.nedbank.co.za'
Url2 = 'https://www.facebook.com'

def openUrl1():
        webbrowser.open(Url1, 2)

def openUrl2():
        webbrowser.open(Url2, 2)

root = Tk()
root.title('App')
root.minsize(width = 400, height = 400)
root.maxsize(width = 400, height = 400)
image = Image.open("C:\\Users\\Damian\\Pictures\\Lores.png")
photo = ImageTk.PhotoImage(image)
label = Label(image = photo)
label.image = photo
label.place(x = 0, y = 0)

color1 = 'white'
button1 = Button(
            text = color1,
            bg = color1,
            relief = "raised",
            width = 220,
            height = 85,
            command = openUrl1
)
Original = Image.open("C:\\Users\\Damian\\Pictures\\NedbankLogoNew.png")
im_pm = ImageTk.PhotoImage(Original)
button1.config(image = im_pm)
button1.place(x = 0, y = 0)
root.mainloop()