Solved

How to loop through a dataset using t-sql

Posted on 2009-03-30
2
440 Views
Last Modified: 2012-05-06
I have a database that has different prices for different items from different stores.  I've given each item a standard number, and an indicator to tell where the item came from.  What I'd like to do is create a procedure to give me a percent price difference based on the average of all items.  So for the sample data below, I want the average price of all items from Wal-Mart, the average price of all items from Shaw's and the average price of all items from Meijer.  Then I want the percent difference between all three.  So if the avg for Wal-Mart is 2.82, and Shaw's is 2.76, then the pct diff between Shaw's and Walmart is 2.13%

The other thing is the data is grouped by department.  So I have line items for dairy, bread, paper products, etc.  The same calcuation would have to be done for all of the departments.

Thanks for help on this.

Ven_Ind  Vendor     Item                                        Price       Dept_Ind

1             Wal-Mart	Eggs, large, dozen	                3.13       12

1             Wal-Mart	Eggs, medium, dozen	3.05       12

1             Wal-Mart	Cheese, sliced, American	2.89       12

1             Wal-Mart	Cheese, sliced, Swiss	2.74       12

1             Wal-Mart	Sour cream	                2.59       12

1             Wal-Mart	Sour cream,light	                2.52       12

2             Shaw's	Eggs, large, dozen	                3.13       12

2             Shaw's	Eggs, medium, dozen	2.97       12

2             Shaw's	Cheese, sliced, American	2.82       12

2             Shaw's	Cheese, sliced, Swiss	2.67       12

2             Shaw's	Sour cream	                2.52       12

2             Shaw's	Sour cream,light	                2.45       12

3             Meijer	Eggs, large, dozen	                2.45       12

3             Meijer	Eggs, medium, dozen	3.20       12

3             Meijer	Cheese, sliced, American	3.13       12

3             Meijer	Cheese, sliced, Swiss	3.05       12

3             Meijer	Sour cream	                2.97       12

3             Meijer	Sour cream,light	                2.88       12

Open in new window

0
Comment
Question by:kenyayvette
2 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 24022580
Please try this:
select a.vendorid, a.vendor, a.avgprice, b.vendorid, b.vendor, b.avgprice, ((a.avgprice - b.avgprice) / a.avgprice) as ptcdifference from (

select vendorid, vendor, avg(price) as avgprice from yourtable

group by Vendor) a

cross join (select vendorid, vendor, avg(price) as avgprice from yourtable

group by Vendor) b

where a.vendor < b.vendorid

Open in new window

0
 
LVL 2

Author Comment

by:kenyayvette
ID: 24024086
Thank you so very much, that is exactly what I needed.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now