Using Multiple WHERE Conditions In Single SQL Query

Kaustubh Katdare

Kaustubh Katdare

@thebigk Oct 26, 2024
I'm wondering if there's a way to use WHERE condition in SQL more than once. Here's what I'm trying to achieve:-

Get the output in following format:-
[Username] [Article Contributed Last Month] [Total Articles Contributed]

Those are the columns, with the last column trying to pull the count of all articles contributed irrespective of the time. My query looks like this: -

SELECT xyz AS username, COUNT(abc) As count_last_month FROM pqr
INNER JOIN ...
ON ....
WHERE category = 2 AND date = ...condition to select dates in the last month
This query totally ignores the third column, where I need to fetch the total count of articles. For this, I should have to add another WHERE condition where I tweak the date = to get data for all the dates; not just the last month.

Can someone tell me how to do that?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Nov 6, 2015

    Try following:
    select username, count(posts), (Subquery to get all post by username with join to main query)
    where daterange group by user name
  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Nov 7, 2015

    Will definitely try that; but the query looks obnoxiously large ☕
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Nov 7, 2015

    yes, it will. You are trying to group to different counts in query.
    that Subquery will not work but there could be some workaround like HAVING clause.
  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Nov 7, 2015

    #-Link-Snipped-#

    May be this discussion will help you?