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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:40:17+00:00 2026-06-15T04:40:17+00:00

i’ve really bad problem with this sql query. base on my tables i need

  • 0

i’ve really bad problem with this sql query.
base on my tables i need some thing like this result:

table1

Id | Type | Size |Count | OwnerId
___________________________________
1     A      1      12      1
2     A      2      12      1
3     B      1      14      1
4     B      1      20      1
5     A      1      12      2
6     A      1      17      2

table2

Id | name
_________
1     A
2     B

The Result

______________________
Name |  Size1Type1 Count |  Size2Type1 Count |  Size1Type2 Count

thanks indeeeeed .

  • 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-06-15T04:40:18+00:00Added an answer on June 15, 2026 at 4:40 am

    You did not specify what RDBMS you are using but you should be able to get the result by implementing an aggregate function with a CASE statement. This process is similar to a PIVOT:

    select t2.name,
      sum(case when t1.size = 1 and t1.type = 'a' then 1 else 0 end) Size1Type1Count,
      sum(case when t1.size = 2 and t1.type = 'a' then 1 else 0 end) Size2Type1Count,
      sum(case when t1.size = 1 and t1.type = 'b' then 1 else 0 end) Size1Type2Count,
      sum(case when t1.size = 2 and t1.type = 'b' then 1 else 0 end) Size2Type2Count
    from table1 t1
    inner join table2 t2
      on t1.ownerid = t2.id
    group by t2.name
    

    See SQL Fiddle with Demo

    Result:

    | NAME | SIZE1TYPE1COUNT | SIZE2TYPE1COUNT | SIZE1TYPE2COUNT | SIZE2TYPE2COUNT |
    --------------------------------------------------------------------------------
    |    A |               1 |               1 |               2 |               0 |
    |    B |               2 |               0 |               0 |               0 |
    

    If you want to include your count field, then you would use something like this:

    select t2.name,
      sum(case when t1.size = 1 and t1.type = 'a' then "Count" end) Size1Type1Count,
      sum(case when t1.size = 2 and t1.type = 'a' then "Count" end) Size2Type1Count,
      sum(case when t1.size = 1 and t1.type = 'b' then "Count" end) Size1Type2Count,
      sum(case when t1.size = 2 and t1.type = 'b' then "Count" end) Size2Type2Count
    from table1 t1
    inner join table2 t2
      on t1.ownerid = t2.id
    group by t2.name;
    

    See SQL Fiddle with Demo

    Result:

    | NAME | SIZE1TYPE1COUNT | SIZE2TYPE1COUNT | SIZE1TYPE2COUNT | SIZE2TYPE2COUNT |
    --------------------------------------------------------------------------------
    |    A |              12 |              12 |              34 |          (null) |
    |    B |              29 |          (null) |          (null) |          (null) |
    

    Or you could even perform multiple joins on the tables to get the result that you want:

    select t2.name, 
      sum(t1_a1."count") Size1Type1Count,
      sum(t1_a2."count") Size2Type1Count,
      sum(t1_b1."count") Size1Type2Count,
      sum(t1_b2."count") Size2Type2Count
    from table2 t2
    left join table1 t1_a1
      on t1_a1.ownerid = t2.id
      and t1_a1.size = 1 
      and t1_a1.type = 'a'
    left join table1 t1_a2
      on t1_a2.ownerid = t2.id
      and t1_a2.size = 2
      and t1_a2.type = 'a'
    left join table1 t1_b1
      on t1_b1.ownerid = t2.id
      and t1_b1.size = 1
      and t1_b1.type = 'b'
    left join table1 t1_b2
      on t1_b2.ownerid = t2.id
      and t1_b2.size = 2
      and t1_b2.type = 'b'
    group by t2.name
    

    See SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters

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.