Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize SQL view/query with self joins
    primarykey
    data
    text
    <p>I'm an extreme novice running SQL SERVER 2005. I have two tables that I'm putting together in a view. The first table just has 2 columns that go into the view. One column is an ID number and the other is name. The second table is where all the data is. The columns in that table are ID, Date, Hour, PriceType, MarketType, and Price. My goal is to have a view that filters PriceType to be a fixed value, and then takes the difference of Price when MarketType is DAM from the price with the MarketType is RTM. Right now that looks like the following....</p> <pre><code>SELECT dbo.nodes.commonname, dbo.nodes.node_id, da.pricedate, da.hour, rt.price AS rtm, da.price AS dam, da.price - rt.price AS dart FROM dbo.pnodes INNER JOIN dbo.pnode_prices AS da ON dbo.pnodes.node_id = da.pnode_id INNER JOIN dbo.pnode_prices AS rt ON dbo.pnodes.node_id = rt.pnode_id AND da.pricetype = rt.pricetype AND da.pricedate = rt.pricedate AND da.hour = rt.hour WHERE ( da.pricetype = 'LMP' ) AND ( da.markettype = 'DAM' ) AND ( rt.markettype = 'RTM' ) </code></pre> <p>This view doesn't take so long by itself. Sometimes I want to take it another step and have the difference between 2 views where I'll run a query that joins the above view and takes the difference of Price <code>where view1.CommonName=abc and view2.CommonName=xyz</code>. When I run that query it takes 10 times as long to run as it takes to run the simple view by itself. I assume that if I did things optimally that it wouldn't take 10x the time. Is there any low hanging fruit that I can do to improve this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload