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

  • Home
  • SEARCH
  • 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 7051529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:16:28+00:00 2026-05-28T03:16:28+00:00

To do a crosstab/pivot in sqlite one can use the case statement as described,

  • 0

To do a crosstab/pivot in sqlite one can use the case statement as described, eg, here. However, one cannot do calculations on such ‘generated’ columns, ie, divide the content of the ‘Puma’ column by the ‘Fiesta’ column.

My workaround has been to create an auxiliary view containing the ‘Puma’ and ‘Fiesta’ columns and join this view to the main ‘crosstab’ output and divide the columns from the joined auxiliary table.

CREATE VIEW aux_table AS SELECT
    shop_id,
    sum(CASE WHEN product = 'Fiesta' THEN units END) as Fiesta,
    sum(CASE WHEN product = 'Focus' THEN units END) as Focus`
FROM sales
GROUP BY shop_id;

SELECT 
    shop_id,
    sum(CASE WHEN product = 'Fiesta' THEN units END) as Fiesta,
    sum(CASE WHEN product = 'Focus' THEN units END) as Focus,
    sum(CASE WHEN product = 'Puma' THEN units END) as Puma,
    at.Fiesta/at.Focus 'ratio between Fiesta and Focus'
FROM sales
JOIN aux_table at ON
     sales.shop_id = at.shop_id
GROUP BY shop_id;

But I wonder whether there is a simpler way to do this.

  • 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-28T03:16:28+00:00Added an answer on May 28, 2026 at 3:16 am

    You can always just do the sums again, like so:

    SELECT 
        shop_id,
        sum(CASE WHEN product = 'Fiesta' THEN units END) as Fiesta,
        sum(CASE WHEN product = 'Focus' THEN units END) as Focus,
        sum(CASE WHEN product = 'Puma' THEN units END) as Puma,
        sum(CASE WHEN product = 'Fiesta' THEN units END) / sum(CASE WHEN product = 'Focus' THEN units END) as Ratio
    FROM sales
    GROUP BY shop_id
    

    Or, faster, you can wrap it up in a subquery, like this:

    select
        shop_id,
        Fiesta,
        Focus,
        Puma,
        Fiesta/Focus as Ratio
    from
        (
        SELECT 
            shop_id,
            sum(CASE WHEN product = 'Fiesta' THEN units END) as Fiesta,
            sum(CASE WHEN product = 'Focus' THEN units END) as Focus,
            sum(CASE WHEN product = 'Puma' THEN units END) as Puma
        FROM sales
        GROUP BY shop_id
        ) x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to create a pivot and/or cross-tab query using for SQLite
What is the Vertica equivalent of crosstab or pivot table found in PostgreSQL and
I think I need a way to perform a pivot or crosstab using C#
I want to develop some crosstab also know as pivot reports in Asp.net with
Using Django 1.1, how could I create a crosstab (pivot table) SQL query using
I have a problem with creating a pivot table in PostgreSQL using the crosstab()
I have a switch statement in a crosstab query: Switch([Age]<20, Under 20, [Age]>=20 and
What is the best option of rendering crosstab report/pivot table in Java EE Web
I want to pull data with an MS Access crosstab query so I can
Ok here is my setup. Main report containing a subreport. Subreport is a crosstab

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.