Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6611439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:58:11+00:00 2026-05-25T19:58:11+00:00

I am trying to construct a query that is a bit more complicated than

  • 0

I am trying to construct a query that is a bit more complicated than anything I’ve done in my limited experience with databases.

TABLE:

id - data - type - data3
1 - hello - 1 - 1
2 - goodbye - 1 - 1
3 - goodbye - 1 - 2
4 - goodbye - 2 - 1
5 - hello - 2 - 1

The goal is to do 4 things:

  1. GROUP the results by “data”, but only return one result/row of each
    data type.
  2. COUNT the total number of each “data GROUP and return this number.
  3. Do this for both “type”=1 and “type”=2, though I only need each
    “data” GROUP item once.
  4. the ability to sort results based on each SELECT item.

So the final result returned should be (sorry to be confusing!):

data, COUNT(data["type"]=1), COUNT(data["type"]=2 AND data["data"] = data)

So, for the sample table above, desired results would be:

loop 1 - hello, 2, 1
loop 2 - goodbye, 3, 1

Then, ideally, I could sort results by any of these.

This is the query I was trying to construct before resorting to posting this, I don’t think it’s even close to being correct, but it may help illustrate what I’m trying to achieve a bit better:

SELECT 
  (
   SELECT `clicks_network_subid_data`, COUNT(*) 
   FROM track_clicks 
   WHERE `clicks_campaign_id`='$id' AND `clicks_click_type` = '1'
  ) AS keywords, 
  (
   SELECT COUNT(*) 
   FROM track_clicks 
   WHERE `clicks_campaign_id`='$id' AND `clicks_click_type` = '2' AND `clicks_network_subid_data` = keywords.clicks_network_subid_data 
  ) AS offer_clicks 
 GROUP BY keywords.clicks_network_subid_data 
 ORDER BY keywords.COUNT(*) DESC

I also need to do a JOIN on another table to grab one more piece of data, but I think I can handle that once I get this part figured out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:58:12+00:00Added an answer on May 25, 2026 at 7:58 pm

    You can use an IF-function for this

    SELECT `clicks_network_subid_data`, 
    SUM(IF(clicks_click_type` == '1',1,0)) as keywords,
    SUM(IF(clicks_click_type` == '2',1,0)) as offer_clicks,
    FROM track_clicks 
    GROUP BY clicks_network_subid_data
    ORDER BY clicks_network_subid_data DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to construct a query that will include a column indicating whether or
Very complex query been trying to construct it for few days with more real
I'm trying construct a PostgreSQL query that does the following but so far my
Hey All! I'm trying to construct a query that is something like this: Where
I'm trying to construct a JDOQL query (using datanucleus) that will search for matches
I'm trying to construct a query that gives me a list of distinct records
I'm trying to construct a linq query that pulls all nodes that have a
I'm trying to construct a SQL query that I think requires multiple JOIN's, but
I am trying to construct a LINQ query that will take a list of
Trying to construct a query such that I have multiple statement specifying joins, each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.