简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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