TSQL - 寻找代码澄清

TSQL - Looking for code clarification(TSQL - 寻找代码澄清)
本文介绍了TSQL - 寻找代码澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用的代码,结果贴在下面:

This is the code I used and the result is posted below:

SELECT * 
FROM
    (SELECT 
         P.ProductID, PC.Name, ISNULL (P.Color, 'uncolored') AS color
     FROM 
         SalesLT.ProductCategory AS PC
     JOIN 
         SalesLT.Product AS P ON PC.ProductCategoryID = P.ProductCategoryID
) AS PPC
PIVOT 
    (COUNT (ProductID) FOR Color IN ([Red],[Blue],[Silver],[Black],[Yellow],[Grey],[Multi],[Uncolored])) AS pvt
ORDER BY 
    Name;

我没有要求查询提供我的名字,有人能解释一下这个名字"是如何出现在结果中的吗?如果我希望 ProductID 出现而不是名称,我该如何修改代码?

I didn't request the query to provide me the name, could someone explain me how this 'name' thing popped up in the result? How can I modify the code if I want ProductID to appear instead of name?

非常感谢任何帮助或反馈.

Any help or feedback would be greatly appreciated.

推荐答案

ProductID 不会显示,因为您在透视表时将其用作聚合列.只会显示每种颜色的产品 ID 计数.为了查看 ProductID 删除查询的数据透视部分.Name 显示在您的最终结果中,因为它存在于您的内部查询中.

ProductID won't show because you have used it as an aggregation column while pivoting the table. Only the counts of product Id for each colour will be shown. For seeing ProductID remove the pivot part of query. Name shows up in your final result because it is there in your inner query.

旋转表格将行更改为列.在您的情况下,颜色 Red、Blue、Silver、Black、Yellow、Grey、Multi、Uncolored(如在 pivot 子句中给出)的行值更改为列.在这些列的每一列下,显示了该颜色的表中的 ProductID 计数以及名称"列中的名称.因此在查询中没有任何地方显示单个 productId.内部查询的 ProductID 列仅用于计算计数,不显示在最终结果中.

Pivoting the table changes rows to columns. In your case the row values for colours Red,Blue,Silver,Black,Yellow,Grey,Multi,Uncolored ( as given inside pivot clause) are changed to columns. Under each of these columns, the counts of ProductIDs present in the table for that colour and the name in 'Name' column are shown. So nowhere in the query individual productIds are shown. The ProductID column the inner query is only used for calculating counts and not shown in final result.

这篇关于TSQL - 寻找代码澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)