Solved

Learning SQL coding techniques

Posted on 2011-02-24
3
332 Views
Last Modified: 2012-05-11
I am also looking for another coding example for this. I learn best by code examples. Any help would be greatly appreciated. I have tried several different ways but I would love to see how an experienced Pro would do this.
Thanks.

Given the following tables and sample data:

create table  Company           ( CompanyID int, CompanyName varchar(30) )
          insert Company values (1,                         'Acme Construction')
          insert Company values (2,                         'AAA Buildings')

create table  Item            ( ItemID int, ItemName varchar(10), UnitCost money )
           insert Item values ( 1,                'Widget',                        1.00)   -- each unit $1
           insert Item values ( 2,                'Sprocket',                      2.00)   -- each unit $2

create table ItemOrder            ( ItemOrderID int, CompanyID int, ItemID int, OrderDate datetime, UnitsOrdered int )
-- Acme orders 3 widgets on 10/5
          insert ItemOrder values ( 1,                            1,                         1,             '10/5/05',                     3 )
-- AAA Buildings orders 1 sprocket on 11/1
          insert ItemOrder values ( 2,                             2,                         2,             '11/1/05',                    1 )

My manager says: ""I want to know, for every company and item combination, the following
information: total orders, total units ordered, total cost of units ordered, first order date,
and last order date.

The result set should be:
 CompanyName, ItemName, TotalOrders, TotalUnitsOrdered, TotalOrderCost, FirstOrderDate, LastOrderDate
Write the SQL for this.
0
Comment
Question by:tesla764
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Accepted Solution

by:
infolurk earned 250 total points
ID: 34976297
Select c.CompanyName, i.ItemName, Count(o.ItemOrderID) as TotalOrders, Sum(o.UnitsOrdered) as TotalUnitsOrdered,
Sum(o.UnitsOrdered) * i.UnitCost as TotalOrderCost
from Company c
join
ItemOrder o
on c.CompanyId = o.CompanyID
Join
Item i
on o.ItemID = i.ItemID
Group By CompanyName, ItemName, TotalOrders, TotalUnitsOrdered, TotalOrderCost
Order By CompanyName, Itemname

Will get you started. The date fields will need subquerys as they need to be filtered for min and max values within the unit types.
0
 
LVL 11

Assisted Solution

by:dougaug
dougaug earned 250 total points
ID: 34976314
select c.CompanyName,
       i.ItemName,
       COUNT(*) as TotalOrders,
       SUM(io.UnitsOrdered) as TotalUnitsOrdered,
       SUM(io.UnitsOrdered) * i.UnitCost as TotalOrderCost,
       MIN(io.OrderDate) as FirstOrderDate,
       MAX(io.OrderDate) as LastOrderDate
       
  from Company c join ItemOrder io on c.CompanyID = io.CompanyID
                 join Item i on io.ItemID = i.ItemID

group by  c.CompanyName, i.ItemName, i.UnitCost
0
 

Author Closing Comment

by:tesla764
ID: 34977551
Thanks again. This has been very interesting and Helpful.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql query help 15 55
point in time restore in SQL server 26 46
Sql case statement to calculate totals 5 37
how to make geography query faster?  SQL 7 46
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question