Doubt Beginner SQL

Asked

Viewed 62 times

0

I need to know what the following queries accomplish, but I am doubtful, because I am starting to study SQL now. You can help me?

"SELECT user_id, CAST(SUM(points_per_badge) AS INT) "
        + "FROM (SELECT p.user_id AS user_id, (COUNT(1) * b.quantity) AS points_per_badge "
        + "FROM CX_PUNCTUATION p JOIN CX_BADGE b ON b.id = p.badge_id WHERE p.user_id NOT IN :ignoredUsers "
        + "GROUP BY user_id, b.quantity) AS points_per_user GROUP BY user_id ORDER BY 2 DESC, 1"

and

"SELECT p.badge_id, CAST((COUNT(1) * b.quantity) AS INT) "
            + "FROM CX_PUNCTUATION p JOIN CX_BADGE b ON b.id = p.badge_id WHERE p.user_id = :userId "
            + "GROUP BY p.badge_id, b.quantity"
  • Explain better what you mean by traduzir. You want to know what they do?

  • 2

    If you are starting now, it is a suggestion to forget these queries and learn the basics first. It will be much easier.

1 answer

0

The first query is returning the ID, is performing a sum and putting the result of this sum with integer, then these quotes and the + sign is concatenating (joining) joining the result with the result of other SELECT’s being performed below to bring everything in the same row, then grouping(Group By) and ordering(Order By). The second SELECT is returning the ID and the result of a multiplication of the count with the quantity and again putting this value as integer, in the end it is grouping this result by the ID and quantity. It is relatively simple this query, but I think I should start with more basic things to better understand and progress in SQL language.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.