CrazyEngineers
  • Using Multiple WHERE Conditions In Single SQL Query

    Kaustubh Katdare

    Kaustubh Katdare

    @thebigk
    Updated: Oct 26, 2024
    Views: 1.2K
    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?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Anoop Kumar

    MemberNov 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
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorNov 7, 2015

    Will definitely try that; but the query looks obnoxiously large ☕
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberNov 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberNov 7, 2015

    #-Link-Snipped-#

    May be this discussion will help you?
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register