在 SQL 或 SSRS 中查找一组记录中的字母

发布于 2024-09-12 03:43:13 字数 698 浏览 6 评论 0原文

我有以下数据(我有更多行,这只是一个示例):

VALUE   Location             FLEET    REGION
P       Pocatello, ID        104       232
B       Pocatello, ID        104       232
B       Spokane, WA          107        232
B       Spokane, WA          107       232

在报告服务或 MSSQL 中,我需要按队列进行分组。将其分组后,我希望它查看每个组包含哪些值。

如果该组包含值 P 和 B,则其生产 如果该组仅包含 B 则为 Reporting 如果该组包含 P,则其生产率

我希望从这个示例中得到的是最终返回的以下行。

VALUE         LOCATION           FLEET    REGION
Production     Pocatello,ID       104       232
Reporting      Spokane, WAS       107       232

现在,SQL 查询报告了我的问题顶部的数据。我需要使用 MSSQL 或 SSRS 进行分组和计算,其中任何一个都可以工作,但是我该如何去做才能获得上面列出的数据。

谢谢!

I have the following data (I have a lot more rows this is just an example):

VALUE   Location             FLEET    REGION
P       Pocatello, ID        104       232
B       Pocatello, ID        104       232
B       Spokane, WA          107        232
B       Spokane, WA          107       232

In either reporting services or MSSQL I need to group by fleet. Afer it is grouped I want it to see what values each group contains.

If the group contains values P and B then its Production
If the group contains B only then it is Reporting
If the group contains P then its Productivity

What I want from this example is the following rows to be returned in the end.

VALUE         LOCATION           FLEET    REGION
Production     Pocatello,ID       104       232
Reporting      Spokane, WAS       107       232

Right now the SQL query reports the data at the top of my quesiton. I need to either do the grouping and calculation is MSSQL or SSRS either one will work but how do I go about doing it to get the data listed like I have right above.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪异←思 2024-09-19 03:43:13

您可以对其他列进行分组,并检查 case 语句中是否存在 if PB

declare @t table (value char(1), Location varchar(50), fleet int, region int)

insert @t values
    ('P',       'Pocatello, ID',        104,       232),
    ('B',       'Pocatello, ID',        104,       232),
    ('B',       'Spokane, WA',          107,       232),
    ('B',       'Spokane, WA',          107,       232);

select  case 
            when max(case when value = 'P' then 1 end) = 1 
                 and max(case when value = 'B' then 1 end) = 1 then 'Production' 
            when max(case when value = 'P' then 1 end) = 1 then 'Productivity' 
            when max(case when value = 'B' then 1 end) = 1 then 'Reporting' 
        end
,       location   
,       fleet
,       region
from    @t
group by
        location   
,       fleet
,       region

这将打印:

(No column name)    location        fleet   region
Production          Pocatello, ID   104     232
Reporting           Spokane, WA     107     232

You could group on the other columns, and check for the presence if P or B in a case statement:

declare @t table (value char(1), Location varchar(50), fleet int, region int)

insert @t values
    ('P',       'Pocatello, ID',        104,       232),
    ('B',       'Pocatello, ID',        104,       232),
    ('B',       'Spokane, WA',          107,       232),
    ('B',       'Spokane, WA',          107,       232);

select  case 
            when max(case when value = 'P' then 1 end) = 1 
                 and max(case when value = 'B' then 1 end) = 1 then 'Production' 
            when max(case when value = 'P' then 1 end) = 1 then 'Productivity' 
            when max(case when value = 'B' then 1 end) = 1 then 'Reporting' 
        end
,       location   
,       fleet
,       region
from    @t
group by
        location   
,       fleet
,       region

This prints:

(No column name)    location        fleet   region
Production          Pocatello, ID   104     232
Reporting           Spokane, WA     107     232
半窗疏影 2024-09-19 03:43:13

如果没有表定义,就很难说。您的示例数据中已经有完整的重复项,这表明表上没有主键。每个车队的位置总是相同吗?那地区呢?如果这实际上是另一个连接表的查询的结果集,那么您应该提及这一点并包含各个表的表定义。

这是我对可能为您指明正确方向的最佳猜测:

SELECT
    CASE
        WHEN MIN(value) = 'B' THEN
            CASE
                WHEN MAX(value) = 'P' THEN 'Production'
                ELSE 'Reporting'
            END
        WHEN MAX(value) = 'P' THEN 'Productivity'
        ELSE NULL
    END,
    location,
    fleet,
    region
FROM
    Some_Table
GROUP BY
    location,
    fleet,
    region

这依赖于它们只是“价值”的两个可能值,否则不容易适应。

Without a table definition(s), it's hard to say. You already have a complete duplicate in your sample data, which indicates that there is no Primary Key on the table. Is the location for each fleet always the same? What about the region? If this is actually a result set from another query that is joining tables then you should mention that and include the table definitions for the individual tables.

Here's my best guess as to something that might point you in the right direction:

SELECT
    CASE
        WHEN MIN(value) = 'B' THEN
            CASE
                WHEN MAX(value) = 'P' THEN 'Production'
                ELSE 'Reporting'
            END
        WHEN MAX(value) = 'P' THEN 'Productivity'
        ELSE NULL
    END,
    location,
    fleet,
    region
FROM
    Some_Table
GROUP BY
    location,
    fleet,
    region

This relies on their only being two possible values for "value" and is not easily adaptable otherwise.

心是晴朗的。 2024-09-19 03:43:13
Select Fleet
    , Min(Case
        When ValueCount.PCount = 1 And ValueCount.BCount = 1 Then 'Production'
        When ValueCount.PCount = 1 And ValueCount.BCount = 0 Then 'Productivity'
        Else 'Reporting'
        End) As Value
    , Min(Location) As Location
    , Min(Region) As Region
From Table
    Join    (
            Select Fleet
                , Sum( Case When T2.Value = 'P' Then 1 Else 0 End ) As PCount
                , Sum( Case When T2.Value = 'B' Then 1 Else 0 End ) As BCount
            From Table As T2
            Where T2.Value In('P','B')
            Group By T2.Fleet
            ) As ValueCount
        On ValueCount.Fleet = Table.Fleet
Group By Fleet
Select Fleet
    , Min(Case
        When ValueCount.PCount = 1 And ValueCount.BCount = 1 Then 'Production'
        When ValueCount.PCount = 1 And ValueCount.BCount = 0 Then 'Productivity'
        Else 'Reporting'
        End) As Value
    , Min(Location) As Location
    , Min(Region) As Region
From Table
    Join    (
            Select Fleet
                , Sum( Case When T2.Value = 'P' Then 1 Else 0 End ) As PCount
                , Sum( Case When T2.Value = 'B' Then 1 Else 0 End ) As BCount
            From Table As T2
            Where T2.Value In('P','B')
            Group By T2.Fleet
            ) As ValueCount
        On ValueCount.Fleet = Table.Fleet
Group By Fleet
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文