473,326 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Views / Performance - 2 Rows to 2 Columns

gateshosting
Good day,

I am programming my Stored Procedures and Views for performance, since we are dealing with millions of records. I know this question is going to sound like I do NOT want to do it this way, but in this case, it may work...


I have an Invoice LineItems table which will only ever contain up to 3 items. It will NEVER contain any more than that. We only deal with 3 items/services, and it will never change. So let's call them A, B, and C.

I have two options and I need to know if number (1) is doable, and/or if it will be faster, and more efficient to do (2).

(1)
Create a view that has 4 columns: Invoice Number, Product A Price, Product B Price, and Product C Price. I want to do it this way because it will work perfectly for a Stored Procedure I wrote which is running very quickly and does exactly what I need...

(2)
Create 3 views that get Product A, Product B Price, and Product C Price, and then just join them in another view. Is this going to be too much overhead? I mean, it will be running 4 views essentially... What do you think?

Here is something I wrote hoping it would work... but no go. The result was not what was expected:


Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     [invoice number],
  3.  
  4.     CASE WHEN [Item Number] = 'A' THEN [Price]) END
  5.     AS APRICE,
  6.  
  7.     CASE WHEN [Item Number] = 'B' THEN [Price]) END
  8.     AS BPRICE
  9. FROM
  10.     [Invoice Lineitems]
  11.  
  12. GROUP BY
  13.     [invoice number]
  14.  
  15.  
Dec 6 '06 #1
4 1537
almaz
168 Expert 100+
...
(1)
Create a view that has 4 columns: Invoice Number, Product A Price, Product B Price, and Product C Price. I want to do it this way because it will work perfectly for a Stored Procedure I wrote which is running very quickly and does exactly what I need...

(2)
Create 3 views that get Product A, Product B Price, and Product C Price, and then just join them in another view. Is this going to be too much overhead? I mean, it will be running 4 views essentially... What do you think?
...
Looking at your code: you've almost done your task, just need to make a final step. You know that there will be only one record per Item Number for each invoice number, but SQL Server doesn't. So you have to specify what it should do with potentially multiple values:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     [invoice number],
  3.  
  4.     AVG(CASE WHEN [Item Number] = 'A' THEN [Price]) END)
  5.     AS APRICE,
  6.  
  7.     AVG(CASE WHEN [Item Number] = 'B' THEN [Price]) END)
  8.     AS BPRICE,
  9.  
  10.     AVG(CASE WHEN [Item Number] = 'C' THEN [Price]) END)
  11.     AS CPRICE
  12. FROM
  13.     [Invoice Lineitems]
  14.  
  15. GROUP BY
  16.     [invoice number]
Dec 6 '06 #2
Awesome... I will give it a try.

Do you think that script will perform better than joining the line items table three times to create the values? I did a script that works, but think (logically) that it will be too much overhead... but maybe not. I don't know much about specifics of performance in SQL Server.
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     i.[invoice number] AS [Invoice Number],
  3.     [i].[Paid] as [Paid],
  4.  
  5.     SUM([li1].[quantity] * [li1].[Price]) AS TransportPrice,
  6.     SUM([li2].[quantity] * [li2].[Price]) AS InopPrice,
  7.     SUM([li3].[quantity] * [li3].[Price]) AS FeeAdvancePrice
  8.  
  9. FROM
  10.     [Invoice] i LEFT OUTER JOIN
  11.     [Invoice Lineitems] li1 ON [i].[invoice number] = [li1].[invoice number] and [li1].[Item Number] = 'A' LEFT OUTER JOIN
  12.     [Invoice Lineitems] li2 ON [i].[invoice number] = [li2].[invoice number] and [li2].[Item Number] = 'B' LEFT OUTER JOIN
  13.     [Invoice Lineitems] li3 ON [i].[invoice number] = [li3].[invoice number] and [li3].[Item Number] = 'C'
  14.  
  15. GROUP BY
  16.     [i].[invoice number], [i].[Paid]
  17.  
Thanks,

Michael C. Gates
Dec 6 '06 #3
iburyak
1,017 Expert 512MB
Just a note:
When you do case in select statement search goes once through the table using indexes if possible.

If you have 3 separate views it goes 3 times through the same table and then you join all of them which is work on the background where each view doesn't have an index I assume.

You have to check a showplan to be sure how it works.
Dec 6 '06 #4
Just FYI for anyone reading... I ran tests:

1. Joining the same table three times, to get the 3 different products was a little slower than the script almaz gave me.
Expand|Select|Wrap|Line Numbers
  1. AVG(CASE WHEN [Item Number] = 'A' THEN [Price] END) AS A,
  2. AVG(CASE WHEN [Item Number] = 'B' THEN [Price] END) AS B,
  3. AVG(CASE WHEN [Item Number] = 'C' THEN [Price] END) AS C
  4.  
Best regards,

Michael C. Gates
Dec 7 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be shown in the language the user is using (Finnish,...
3
by: teddysnips | last post by:
This from a SQL Server manual: "Complex queries, however, such as those in decision support systems, can reference large numbers of rows in base tables and aggregate large amounts of information...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
15
by: rod.weir | last post by:
Fellow database developers, I would like to draw on your experience with views. I have a database that includes many views. Sometimes, views contains other views, and those views in turn may...
33
by: Peter | last post by:
People are telling me it is bad to put select * from <atable> in a view. I better should list all fields of the table inside the definition of the view. I dont know exactly why but some...
6
by: Jim Devenish | last post by:
I have 3 views, two of which depend on the other: CREATE VIEW dbo.CustomerListQueryAccounts AS SELECT dbo.CustomerListQuery.* FROM dbo.CustomerListQuery WHERE (isProspect = 0)...
1
by: sp | last post by:
Hello I have a problem with the refresh performance in datagrid – when datagrid is being shown it is so slow that I can see one by one cells is drawn -datagrid contains about 35x40 of...
10
by: shsandeep | last post by:
The ETL application loaded around 3000 rows in 14 seconds in a Development database while it took 2 hours to load in a UAT database. UAT db is partitioned. Dev db is not partitioned. the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.