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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:49:16+00:00 2026-06-19T01:49:16+00:00

Trying to translate the following sql query to map reduce mongodb: select 100.00 *

  • 0

Trying to translate the following sql query to map reduce mongodb:

select
    100.00 * sum(case 
            when p_type like 'PROMO%'
            then l_extendedprice*(1-l_discount) 
            else 0
            end) / sum(l_extendedprice * (1 - l_discount)) as promo_revenue
from 
    lineitem, 
    part
where 
    l_partkey = p_partkey
    and l_shipdate >= date '1995-09-01'
    and l_shipdate < date '1995-09-01' + interval '1' month;

I used in the collection above:

{
    "_id" : ObjectId("511b7d1b3daee1b1446ecdfe"),
    "l_linenumber" : 1,
    "l_quantity" : 17,
    "l_extendedprice" : 21168.23,
    "l_discount" : 0.04,
    "l_tax" : 0.02,
    "l_shipdate" : ISODate("1996-03-13T03:00:00Z"),
    "l_commitdate" : ISODate("1996-02-12T03:00:00Z"),
    "l_receiptdate" : ISODate("1996-03-22T03:00:00Z"),
    "l_comment" : "blithely regular ideas caj",
    "partsupp" : {
        "ps_availqty" : 6157,
        "ps_supplycost" : 719.17,
        "ps_comment" : "blithely ironic packages haggle quickly silent platelets",
        "ps_partkey" : {
            "p_partkey" : NumberLong(155190),
            "p_name" : "slate lavender tan lime lawn",
            "p_mfgr" : "Manufacturer#4",
            "p_brand" : "Brand#44",
            "p_type" : "PROMO BRUSHED NICKEL"
        }
    }
}

This mapreduce:

 db.runCommand({
    mapreduce: "lineitem",
    query: {
        l_shipdate: {'$gte': new Date("Sept 01, 1995"), '$lt': new Date("Oct 01, 1995")}
    },
    map: function() {
        var data = {promo_revenue:0, divisor:0, dividendo: 0 };
        var pattern = /PROMO$/;
        var discount = 1 - this.l_discount;
        var revenue = this.l_extendedprice * discount;

            if(this.partsupp.ps_partkey.p_type.match(pattern)!= null){
                data.divisor = revenue;
            }else{
                data.divisor = 0;
            }

            data.dividendo = revenue;
            emit("PROMO_REVENUE", data);
    },
    reduce: function(key, values) {
        var data = {promo_revenue:0, divisor:0, dividendo: 0 };
        for (var i = 0; i < values.length; i++) {
            data.divisor += values[i].divisor;
            data.dividendo += values[i].dividendo;
        }

        var resultado = data.divisor / data.dividendo;
        data.promo_revenue = 100.00 * resultado;

        return data;
    },
    out: 'query014'
 });

I retrieve this answer:

{ "_id" : "PROMO_REVENUE", "value" : { "promo_revenue" : 0, "divisor" : 0, "dividendo" : 2761949328.227077 } }

The problem:

The RegEx that i was applied, is it correct? It’s strange the divisor be ever zero. And only if the divisor be ever zero the answer is correct or not?

Thanks

  • 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-19T01:49:17+00:00Added an answer on June 19, 2026 at 1:49 am

    LIKE 'PROMO%' is not a regex, but % is a wildcard for “zero or more characters of anything.” The regex equivalent would probably be /PROMO.*/. /PROMO$/ means “ends with PROMO.”

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

Sidebar

Related Questions

I have the following SQL, which I am trying to translate to LINQ: SELECT
I am trying to translate the following query into entity framework: SELECT Subjects.Name FROM
I'm trying to select some data from another query. The SQL will look like
I'm trying to translate an SQL query into LINQ, but after numerous attempts, I'm
I am trying to write the following SQL query as a JPA query. The
I have the following MySQL query that I'm trying to translate into an equivalent
I'm trying to translate the following JS code to ObjC; It basically defines a
when trying to translate the confirmation message to Norwegian i get the following error:
In the following code below, I'm trying to translate, or more a graphics context
for some reasons I am trying to translate the following RoR view code to

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.