5

I've been researching most of the morning on how to do a computed case statement and apply a where clause to the result.

I'm attempting a SEDE query that eliminates as many lines from the select with where, then apply a computation on the remaining. The computation is expensive, as it is several like matches with wildcards.

Simple example, let me call this column Pcount:

CASE WHEN Posts.Title LIKE '%?' THEN 1 ELSE 0 END
+
CASE WHEN Posts.Title LIKE 'H%' THEN 1 ELSE 0 END

For "How do I SQL?", PCount should = 2 (both matched).
For "Hello my name is Alice.", PCount should = 1 (only "H%" matched).
For "Am I wrong?", PCount should = 1 (only "%?" matched).
For anything else, PCount = 0.

My first attempt used WITH:

WITH MyPosts (Id, Score, OwnerUserId, LastActivityDate, Tags, PCount)  
AS  
(  
Select
    Posts.Id,
    Posts.Score,
    Posts.OwnerUserId,
    Posts.LastActivityDate,
    Posts.Tags,
    (
           CASE WHEN Posts.Title LIKE '%?' THEN 1 ELSE 0 END
           +
           CASE WHEN Posts.Title LIKE 'H%' THEN 1 ELSE 0 END
    ) as PCount
    FROM Posts
    WHERE
      Posts.Score >= ##Score##
      AND Posts.ClosedDate IS NULL

)

SELECT DISTINCT
      MyPosts.Id AS [Post Link],
      MyPosts.PCount,
      MyPosts.Score AS [Post Score],
      MyPosts.OwnerUserId AS [User Link],
      MyPosts.LastActivityDate AS [Last Activity],
      MyPosts.Tags
FROM Posts
WHERE
    PCount > 1;

Line 34: Invalid column name 'PCount'.
The multi-part identifier "MyPosts.Id" could not be bound.
The multi-part identifier "MyPosts.PCount" could not be bound.
The multi-part identifier "MyPosts.Score" could not be bound.
The multi-part identifier "MyPosts.OwnerUserId" could not be bound.
The multi-part identifier "MyPosts.LastActivityDate" could not be bound.
The multi-part identifier "MyPosts.Tags" could not be bound.

But I've tried many ways, including without using WITH:

SELECT Pcount FROM Posts WHERE
    (
         CASE WHEN Posts.Title LIKE '%?' THEN 1 ELSE 0 END
         +
         CASE WHEN Posts.Title LIKE 'H%' THEN 1 ELSE 0 END
    ) AS Pcount

SELECT Pcount FROM Posts WHERE
    (SELECT (
           CASE WHEN Posts.Title LIKE '%?' THEN 1 ELSE 0 END
           +
           CASE WHEN Posts.Title LIKE 'H%' THEN 1 ELSE 0 END
    )) as Pcount > 1

SELECT Pcount FROM Posts WHERE
    (SELECT (
           CASE WHEN Posts.Title LIKE '%?' THEN 1 ELSE 0 END
           +
           CASE WHEN Posts.Title LIKE 'H%' THEN 1 ELSE 0 END
    ) as Pcount) > 1;

I've read this as well as googled about adding case, but 99% of those links are not about adding. They tend to be "case blah=1 then yep1" string results.

Anyone have an example for me?

6
  • 2
    If it helps, SEDE uses tsql.
    – Andrew T.
    Commented Sep 25, 2022 at 17:01
  • Yep, that helped me a little. Eliminated MySQL results in google. Commented Sep 25, 2022 at 17:03
  • 1
    For your attempt using WITH - you name your CTE MyPosts but then use Posts instead in your following FROM clause. Commented Sep 25, 2022 at 17:14
  • Well, that worked. Sadly. I guess I can blame it on that being the 20th or more attempt today and a mistake when pivoted to there. Commented Sep 25, 2022 at 17:18
  • This is almost a full Stack Overflow question, not a meta question. Is there anything that makes it specific to SEDE, e.g., some database schema subtleties or performance considerations? Commented Sep 26, 2022 at 11:10
  • I put it here due to the performance restrictions on data.stackexchange.com. But you are right, it is mostly asking the most efficient way to make this query. Commented Sep 26, 2022 at 11:13

1 Answer 1

4

It looks like Bill Tür stands with Ukraine already solved the majority of the problem you faced.

I went ahead and made some extra modifications. I've added an extra condition to only select Questions (PostTypeid = 1) as those only have titles. And I introduced an extra table to replace the CASE statement as I reckoned that would be easier to maintain. It is similar to query I use to calculate the Profile Spam score. Bringing all this together results in this final version:

WITH TitleLikes as (
   Select '%?' Value
   union
   Select 'H%'
),
MyPosts (Id, Score, OwnerUserId, LastActivityDate, Tags, PCount)  
AS  
(  
Select 
    Posts.Id,
    Posts.Score,
    Posts.OwnerUserId,
    Posts.LastActivityDate,
    Posts.Tags,
    (
          Select count(*)
          From TitleLikes
          Where Posts.Title Like TitleLikes.Value
    ) as PCount
    FROM Posts
    WHERE
      Posts.Score >= ##Score:int##
      AND Posts.ClosedDate IS NULL
      And Posttypeid = 1 -- Q
)

SELECT top 1000
      MyPosts.Id AS [Post Link],
      MyPosts.PCount,
      MyPosts.Score AS [Post Score],
      MyPosts.OwnerUserId AS [User Link],
      MyPosts.LastActivityDate AS [Last Activity],
      MyPosts.Tags
FROM  MyPosts
WHERE
    PCount > 1;

Be careful when you use magic link columns. When the result set is huge (think > 10,000 rows) the Query Processor of SQL Server gives up and throws an error. Reducing the set with a TOP [some number] prevent these mishaps but in that case you might want to add an Order By that suits your use case.

Keep in mind SEDE is updated once a week on Sunday.
Use the fine SEDE Tutorial written by the admirable Monica Cellio.
Say "Hi" in SEDE chat.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .