Showing posts with label output. Show all posts
Showing posts with label output. Show all posts

Monday, March 26, 2012

Duplicate index spool or not?

Hello. I have a question about the execution plan here:

The four branches are identical and the Index Spools take in the same record set and output the same expressions. Also, no rebinding will be necessary during the entire query (as far as I can gather). My question is (and I know it may sound silly, but bare with the newbie here): does that plan say that there will be four identical temporary indexes created, one for each branch? Or is it that there will be a single spooled index which will be used on all 4 branches (and that would make perfect sense) but there's no other way for the execution plan to show it (as in, draw it)?

Here's a reduced version of my query:
(and full version here)

-- CTE here

WITH yearlySales (SalesPersonID, SalesYear, TotalSales) AS
(SELECT SalesPersonID, YEAR(OrderDate) as SalesYear, SUM(TotalDue) as TotalSales
FROM Sales.SalesOrderHeader
WHERE YEAR(OrderDate) BETWEEN 2001 AND 2003
GROUP BY SalesPersonID, YEAR(OrderDate)
)

-- Main statement

SELECT sp.SalesPersonID
FROM Sales.SalesPerson sp INNER JOIN HumanResources.Employee
ON sp.SalesPersonID = Employee.EmployeeID
WHERE

((SELECT TotalSales FROM yearlySales
WHERE SalesYear = 2003 AND SalesPersonID = sp.SalesPersonID)

<=

(SELECT TotalSales FROM yearlySales
WHERE SalesYear = 2002 AND SalesPersonID = sp.SalesPersonID)

OR

(SELECT TotalSales FROM yearlySales
WHERE SalesYear = 2002 AND SalesPersonID = sp.SalesPersonID)

<=

(SELECT TotalSales FROM yearlySales
WHERE SalesYear = 2001 AND SalesPersonID = sp.SalesPersonID))

The plan indicates that four identical temporary indexes are being created. This is what happens. CTE in the standard is supposed to provide more than just syntax level substitution. So if you use a particular query expression multiple times then the database engine is supposed to optimize the access patterns. But SQL Server does not currently do this and we do only syntax level substitution. Hence your query above has the plan you are seeing. You can simplify the query by doing below instead which is more efficient:

WITH yearlySales (SalesPersonID, SalesYear, TotalSales) AS
(
SELECT SalesPersonID, YEAR(OrderDate) as SalesYear, SUM(TotalDue) as TotalSales
FROM Sales.SalesOrderHeader
WHERE YEAR(OrderDate) BETWEEN 2001 AND 2003
GROUP BY SalesPersonID, YEAR(OrderDate)
),
p_yearlysales (SalesPersonID, [2001], [2002], [2003]) as
(
select SalesPersonID, [2001], [2002], [2003]
from yearlysales
pivot (max(TotalSales) for SalesYear in ([2001], [2002], [2003])) as p
)
-- Main statement

SELECT sp.SalesPersonID
FROM Sales.SalesPerson sp INNER JOIN HumanResources.Employee
ON sp.SalesPersonID = Employee.EmployeeID
JOIN p_yearlysales as p
ON p.SalesPersonID = sp.SalesPersonID
WHERE [2003] <= [2002] or [2002] <= [2001];

Wednesday, February 15, 2012

DTS to output .csv

I am selecting the following fields from my table and drop them into my .csv, the problem is that if i select all the fields which I commented out, and click on define columns, populate from source, execute, then on the Destination Tab, none of my columns are selected, it is blank, please help, here is my table

select
trad_type,
reference,
principal,
book,
strategy,
cpty,
buy_sell,
Quantity,
ident_type,
ext_ident,
sec_name,
price,
price_divisor,
traded_net_ind,
trade_ccy,
trade_ldt,
value_date,
commission,
exchange_fee,
other_fees
gross_consid,
net_consid,
sett_ccy,
trad_sett_ccy_xrate,
/*trad_sett_ccy_xrate_mdv_ind
trad_inst_ccy_xrate
trad_inst_ccy_xrate_mdv_ind
*/
pb,
acct,
inst_class
/*cont_desc
pl_book_ccy_xrate
Id*/
from Table1Use the sql analyzer to get the list you want. Copy the list to a spreadsheet and change the file type. Or, create a temp_table and select into the temp_table.|||I still get the same problem.

DTS to export formated date to excel

Hello,

I am trying to output data from my sql table to an excel spreadsheet and send it by email which works fine, the problem is he wants the date to be in the format d-mmm-yy, which is easy to format in excel manually, but he do not want to do this manually. I tried to do this when I select the date from the table to spreadsheet, "select convert(char,value_date,106) from table", but this don't get transported to the excel spreadsheet, I get my results on the spread sheet as dd/mm/yy. Can you please help either to set the date on excel forever to be in this format "d-mmm-yy" or to force this output to excelOk, i manage to answer myself, you need to format the cells onto the excel file itself

DTS to Excel (replace existing rows)

Hi,

I am using a DTS package to output a view to a pre-determined Excel file. Currently it just adds the output to the bottom of the current table in excel but i would like it to delete the contents of the worksheet before adding the new rows.

Any help is much appreciated.

Thanks

Greg

So in other words, if a row already exists update it, otherwise insert. Is that correct?

If so, read this: http://www.sqlis.com/default.aspx?311

-Jamie