简体   繁体   English

SQL 聚合和分组

[英]SQL Aggregation and Grouping

Just diving into SQL and had seen a report format that I like and was hope this is the right forum to ask.刚刚深入 SQL 并看到了我喜欢的报告格式,并希望这是正确的提问论坛。 i am familiar with basic select statements and grouping but I am stumped on how to do this or if it is possible with a single data table.我熟悉基本的选择语句和分组,但我很难做到这一点,或者是否可以使用单个数据表。

Anyways below is a sample of a table I have.无论如何,下面是我拥有的表格样本。 Table:桌子:

Item      Loc   Frozen     Scanned    Adj
1457481   85      0         1          1
1457481   85      0         1          1
1457481   25      1         1          0
1457481   25      1         1          0
1457481   35      1         1          0
1457481   45      1         1          0

Here is the format I would like to show:

Item       GoodLoc     Adj
1457481     2@25       2@85
            1@35
            1@45    


Is this even possible without joining additional tables?这甚至可能不加入额外的表吗? Any assistance is greatly appreciated.非常感谢任何帮助。

Without more details of the problem this is just guesswork but here is a possible solution:没有关于问题的更多细节,这只是猜测,但这里有一个可能的解决方案:

Assuming table:假设表:

CREATE TABLE Items
(
    item    INT,
    Loc     INT,
    Frozen  BIT,
    Scanner BIT,
    Adj     BIT
)

try尝试

SELECT  DISTINCT
        Item,
        CASE
            WHEN Frozen = 0
            THEN NULL
            ELSE CONVERT(VARCHAR(5),COUNT(Loc) OVER (PARTITION BY Loc)) + '@' + CONVERT(VARCHAR(5),Loc)
        END As GoodLoc,
        CASE
            WHEN Adj = 0
            THEN NULL
            ELSE CONVERT(VARCHAR(5),COUNT(Adj) OVER (PARTITION BY Adj)) + '@' + CONVERT(VARCHAR(5),Loc)
        END As Adj
FROM    Items
ORDER BY GoodLoc DESC

As others have said, it seems like you are trying to present data which you should do in the presentation layer (SSRS) rather than Management Studio正如其他人所说,您似乎正在尝试呈现您应该在表示层 (SSRS) 而不是 Management Studio 中执行的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM