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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:56:44+00:00 2026-06-14T03:56:44+00:00

I am not sure how to solve this problem: We import order information from

  • 0

I am not sure how to solve this problem:

We import order information from a variety of online vendors ( Amazon, Newegg etc ). Each vendor has their own specific terminology and structure for their orders that we have mirrored into a database. Our data imports into the database with no issues, however the problem I am faced with is to write a method that will extract required fields from the database, regardless of the schema.

For instance assume we have the following structures:

Newegg structure:

"OrderNumber" integer NOT NULL, -- The Order Number
"InvoiceNumber" integer, -- The invoice number
"OrderDate" timestamp without time zone, -- Create date.

Amazon structure:

"amazonOrderId" character varying(25) NOT NULL, -- Amazon's unique, displayable identifier for an order.
"merchant-order-id" integer DEFAULT 0, -- A unique identifier optionally supplied for the order by the Merchant.
"purchase-date" timestamp with time zone, -- The date the order was placed.

How can I select these items and place them into a temporary table for me to query against?

The temporary table could look like:

"OrderNumber" character varying(25) NOT NULL,
"TransactionId" integer,
"PurchaseDate" timestamp with time zone

I understand that some of the databases represent an order number with an integer and others a character varying; to handle that I plan on casting the datatypes to String values.

Does anyone have a suggestion for me to read about that will help me figure this out?

I don’t need an exact answer, just a nudge in the right direction.

The data will be consumed by Java, so if any particular Java classes will help, feel free to suggest them.

  • 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-14T03:56:45+00:00Added an answer on June 14, 2026 at 3:56 am

    First, you can create a VIEW to provide this functionality:

    CREATE VIEW orders AS
    SELECT '1'::int            AS source -- or any other tag to identify source
          ,"OrderNumber"::text AS order_nr
          ,"InvoiceNumber"     AS tansaction_id -- no cast .. is int already
          ,"OrderDate" AT TIME ZONE 'UTC' AS purchase_date -- !! see explanation
    FROM   tbl_newegg
    
    UNION  ALL  -- not UNION!
    SELECT 2
           "amazonOrderId"
          ,"merchant-order-id"
          ,"purchase-date"
    FROM   tbl_amazon;
    

    You can query this view like any other table:

    SELECT * FROM orders WHERE order_nr = 123 AND source = 2;
    
    • The source is necessary if the order_nr is not unique. How else would you guarantee unique order-numbers over different sources?

    • A timestamp without time zone is an ambiguous in a global context. It’s only good in connection with its time zone. If you mix timestamp and timestamptz, you need to place the timestamp at a certain time zone with the AT TIME ZONE construct to make this work. For more explanation read this related answer.

      I use UTC as time zone, you might want to provide a different one. A simple cast "OrderDate"::timestamptz would assume your current time zone. AT TIME ZONE applied to a timestamp results in timestamptz. That’s why I did not add another cast.

    • While you can, I advise not to use camel-case identifiers in PostgreSQL ever. Avoids many kinds of possible confusion. Note the lower case identifiers (without the now unnecessary double-quotes) I supplied.

    • Don’t use varchar(25) as type for the order_nr. Just use text without arbitrary length modifier if it has to be a string. If all order numbers consist of digits exclusively, integer or bigint would be faster.

    Performance

    One way to make this fast would be to materialize the view. I.e., write the result into a (temporary) table:

    CREATE TEMP TABLE tmp_orders AS
    SELECT * FROM orders;
    
    ANALYZE tmp_orders; -- temp tables are not auto-analyzed!
    
    ALTER TABLE tmp_orders
    ADD constraint orders_pk PRIMARY KEY (order_nr, source);
    

    You need an index. In my example, the primary key constraint provides the index automatically.

    If your tables are big, make sure you have enough temporary buffers to handle this in RAM before you create the temp table. Else it will actually slow you down.

    SET temp_buffers = 1000MB;
    

    Has to be the first call to temp objects in your session. Don’t set it high globally, just for your session. A temp table is dropped automatically at the end of your session anyway.

    To get an estimate how much RAM you need, create the table once and measure:

    SELECT pg_size_pretty(pg_total_relation_size('tmp_orders'));
    

    More on object sizes under this related question on dba.SE.

    All the overhead only pays if you have to process a number of queries within one session. For other use cases there are other solutions. If you know the source table at the time of the query, it would be much faster to direct your query to the source table instead. If you don’t, I would question the uniqueness of your order_nr once more. If it is, in fact, guaranteed to be unique you can drop the column source I introduced.

    For only one or a few queries, it might be faster to use the view instead of the materialized view.

    I would also consider a plpgsql function that queries one table after the other until the record is found. Might be cheaper for a couple of queries, considering the overhead. Indexes for every table needed of course.

    Also, if you stick to text or varchar for your order_nr, consider COLLATE "C" for it.

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

Sidebar

Related Questions

I have the following to solve and I'm not sure how to approach this:
i'm not sure how to solve the following problem: i have a triangle with
I have a simple problem but I am not sure how to solve it.
I've got a problem I'm not sure how best to solve. I have an
I am experiencing this problem for few hours now and really not sure how
I'm not sure if this is possible in ASP.NET, but here is the problem
I've run into a little difficulty and I'm not sure how best to solve
So I have an issue that I'm not 100% sure on how to solve.
Not sure whats going on here, or what could be the integer in this
Not sure how to ask this, but i'll give it a try: I have

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.