我有3个表,但我无法正确查看查询结果。
以下是我正在使用的表格。
Project
Product
Users
我需要来自项目的名称和来自产品的Ar_Description才能为所记录的用户显示。但查询结果只显示一个结果。
我希望按分配给用户的组进行分组。
获取正确的用户和组,但不是该组中的所有项目。
以下是我正在使用的代码:
userLab =
Select USERS.C_USER_LAB
Where user_name = USER
Order By user_name
usergroup =
Select USERS.GROUP_NAME
Where user_name = USER
Order By user_name
收到=“F”
cleararray(UnitsArray1)
Query ="Select DISTINCT PROJECT.NAME, PRODUCT.AR_DESCRIPTION, Project.GROUP_NAME from Project INNER JOIN PRODUCT ON PROJECT.C_SAMPLE_TYPE = PRODUCT.C_PRODUCT_TYPE where PROJECT.C_RECEIVED = '" + Receive + "' and PROJECT.GROUP_NAME = '" + usergroup + "' and PROJECT.C_LAB = '" + userLab + "'"
预期数据:
Data Expected as different projects are there
收到的数据:
Same Project for different Description (All are not received or in project table)
答案 0 :(得分:0)
此查询将返回与第二个附加屏幕截图相同的结果。
Select Project.ProjectName, Product.Ar_Description, Project.GROUP_NAME
From Project
inner join Product on Product.ProjectId = Project.ProjectId
inner join Users U on U.Userid = Project.Userid
Where U.Userid = 123
and Project.GROUP_NAME = 'GDL_RY'
group by Project.ProjectName,Product.Ar_Description,Project.GROUP_NAME
让我为你创造一些合乎逻辑的理解。看,您在第一个屏幕截图中附加的预期输出是可获取的,但是如您所见,要获得它,您必须删除Ar_Description
列。但是每当你添加Product.Ar_Description
项目重复时,为什么?
因为现在查询也将遍历Ar_Description列,并为每个唯一数据添加一行..例如
1) Project1 has Careem in AR_Description 2) Project1 also has powder in AR_Description hence 2 rows for Project1.
还有其他方法,其中一种方法是获取count
Ar_Description
或将其旋转。
为计数:
Select Project.ProjectName, Count(Product.Ar_Description), Project.GROUP_NAME
From Project
inner join Product on Product.ProjectId = Project.ProjectId
inner join Users U on U.Userid = Project.Userid
Where U.Userid = 123
and Project.GROUP_NAME = 'GDL_RY'
group by Project.ProjectName,Count(Product.Ar_Description)
结果将是: Project1有100个AR_Descriptions Project2有1个AR_Descriptions
对于数据透视: 看到这个答案:turn rows into columns