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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:37:08+00:00 2026-06-09T07:37:08+00:00

TABLE 1 ID | DATE | a | 10-07-2012 | b | 10-07-2012 |

  • 0

TABLE 1

ID    |      DATE       |   
 a    |   10-07-2012    |     
 b    |   10-07-2012    |     
 c    |   10-07-2012    | 

TABLE2

ID    |    OrdersID  |
 a    |     001      |
 b    |     002      |
 c    |     003      |

TABLE3

ItemID    |   OrdersID    |  Items |
    1     |     001       |   5    |  
    2     |     002       |   3    |  
    7     |     003       |   2    | 
    3     |     003       |   7    | 
    4     |     001       |   8    |
    2     |     003       |   7    |
    5     |     003       |   6    |   
    6     |     003       |   1    |

TABLE4

 ItemID   | ItemName   |  
    1     |  ABC       |
    2     |  EFG       |
    3     |  HIJ       |
    4     |  KLM       |
    5     |  NOP       |
    6     |  QRS       |
    7     |  TUV       |

I would like to select the ID’s from TABLE1 t where Date = 10-07-2012

In Table2 with the ID’s I would like to select the OrdersID‘s

With the OrdersID‘s I would like to add together how many Items of a ItemID exist e.g. IteamID 2 has a total of 10 Items

From TABLE3 I would like to know the top 5 Items and the quantity of Items in Order and get the names of the the ItemID from TABLLE4 ItemName.

Expected results 5 results

ItemID  |  ItemName  |  Quantity  | 
   2    |  EFG       |     10     | 
   3    |  HIJ       |      7     | 
      .....
  .....
      .....
      .....

SQL used at the start …

 SELECT SUM (t3.Items) , t4.ItemName
  FROM Table3 t3
  JOIN Table2 t2 ON t3.OrdersID = t2.OrdersID
  JOIN Table1 t1 ON t2.ID = t1.ID
  WHERE t1.[Date] = '10072012 00:00:00'
  • 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-09T07:37:09+00:00Added an answer on June 9, 2026 at 7:37 am

    Depending on how you want to break ties in your top five, this is a working answer.

    select top 5
        t3.ItemID,
        (select ItemName from TABLE4 as t4 where t4.ItemID = t3.ItemID) as ItemName,
        sum(t3.Items) as Quantity
    from TABLE3 as t3
    where t3.OrdersID in (
        select OrdersID
        from TABLE1 as t1 inner join TABLE2 as t2 on t2.ID = t1.ID
        where DATE = cast('20121007' as datetime)
    )
    group by t3.ItemID
    order by Quantity desc
    

    Here’s the output I get. Notice that Item 4 ranks higher than Item 3 which does not match with the sample output specified.

    ItemID | ItemName | Quantity  
       2   |    EFG   |    10  
       4   |    KLM   |     8  
       3   |    HIJ   |     7  
       5   |    NOP   |     6  
       1   |    ABC   |     5  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
got a table with dates and prices. Date Price 2012-01-01 25 2012-01-05 12 2012-01-10
I have the following table (simplified): user_id date hours 1 2012-03-01 5 2 2012-03-01
I have a table like this Date|OrderID 2012-03-09|123 2012-03-09|122 2012-03-07|121 And I am doing
How do you create a moving average in SQL? Current table: Date Clicks 2012-05-01
I have a table: Date | Code 2012-04-01 | 1 2012-04-01 | 0 2012-04-01
there are two tables: TABLE cars id | date ---------- 1 | 2012-01-04 2
Consider the following messages table: _date message ------------------------- 2012-02-22 hello 2012-02-22 another msg! 2012-03-05
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
Given the following table PAYMENT_Date TRANSACTION_TYPE PAYMENT_AMT 1/1/2012 P 184366 1/1/2012 R -5841 1/2/2012

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.