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 6333907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:33:24+00:00 2026-05-24T18:33:24+00:00

Before I set out to write a Python script, I want to see if

  • 0

Before I set out to write a Python script, I want to see if MySQL alone can produce the result.

I have a list of products :

PID Product 
-----------
1   AAA
2   ABC
3   BAC
4   CAB
5   CBA

I have a list of companies ordering these products several times :

CID PID
-------
1   1
2   3
1   5
3   2
1   1
2   3 

The desired result :

CID AAA ABC BAC CAB CAB CBA
---------------------------
1   Y                   Y
2           Y
3       Y  

How I would do this in Python?

  • Create a temp table with columns (CID AAA ABC BAC CAB CAB CBA)
  • Run 2 loops and update the desired table when the desired column matches.

Just curious to see if there exists a MySQL only solution.

p.s : This is just a sample and the actual problem has few 100 products and few 1000 companies. I created a temp table for 100 products by doing a transpose in Excel and converted it to a MySQL table.

Following is the approach I finally resorted to. Thanks for the feedback guys.

########### Python script to generate the MySQL query ##############

#MySQL Connection String Goes here#

#Generate MySQL 'CASE' logic

cursor = db.cursor()

if __name__ == '__main__':

    cursor.execute("select PID, Product from products")
    productlist = cursor.fetchall()

    for product in productlist:
        print ("max(case when PID = %s then 'Y' else '' end) as `%s`,") % (product[0], product[1])

db.close()

Use the generated query in the format as suggested by nick.

select cid,
max(case when pid = 1 then 'Y' else '' end) as AAA,
max(case when pid = 2 then 'Y' else '' end) as ABC,
max(case when pid = 3 then 'Y' else '' end) as BAC,
max(case when pid = 4 then 'Y' else '' end) as CAB,
max(case when pid = 5 then 'Y' else '' end) as CBA
from companies
group by cid
  • 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-24T18:33:25+00:00Added an answer on May 24, 2026 at 6:33 pm
    select cid,
    max(case when pid = 1 then 'Y' else '' end) as AAA,
    max(case when pid = 2 then 'Y' else '' end) as ABC,
    max(case when pid = 3 then 'Y' else '' end) as BAC,
    max(case when pid = 4 then 'Y' else '' end) as CAB,
    max(case when pid = 5 then 'Y' else '' end) as CBA
    from companies
    group by cid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some additional configuration needed before I can set thread priorities in a
I have set a property across threads before and I found this post Cross-thread
I want to set some attributes just before the object is serialized, but as
I want to set the Text of a TextBlock in my StatusBar before making
I have a field where the user can input text and I want them
I want to set the Edittext as like, whatever i write and enter in
The code is, set VAR=before if %VAR% == before ( set VAR=after; echo %VAR%
Basically, what I'm wondering is if I need to set debug=false before hitting the
I wish to set a usererror string before leaving a function, depending on the
Does anyone know how to set the working directory in JavaScript before? Code I

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.