Question for the SQL Wizards & Masters out there. I've got to get the execution time down on this beast. It's taking 5+ minutes to execute and occasionally times out. I need help understanding how to make it more efficient. I'm returning about 100,000 rows.
The scenario is: I’m trying to identify “valid orders” that should be flagged for something called the "Dating season". These orders will have items associated with valid ‘category codes’ (dbo.DTITEMS ON od.CATEGORY = dbo.DTITEMS.CATEGORY).
Then I use the results of this query to basically update an order field “DTGSEASON” with the value of the current "dating season."
I reference a table called DATING (separate from CRM.Dbo.Dating) that stores parameters like Promotion Start & End Dates, current dating season, etc.
Here’s the query I’m running to find ‘Valid Orders’, called vDatingValidOrdersReg:
SELECT
h.CUSTOMER
, h.ORDNUMBER
, h.INVNETWTX
, dtgseason.VALUE AS dtgseason
, c.comp_dqdatingmin
, dbo.DTITEMS.ALTMINIMUM
, d.dat_datingapprovedon
, d.dat_ordsincepromostart
, h.EXPDATE
, dbo.DATING.PROMOSTART
, d.dat_season
, d.dat_year
, od.ITEM
, od.CATEGORY
, d.dat_DatingID
, c.Comp_Name
, d.dat_state
, h.ORDUNIQ
, c.comp_dqdatingmax
FROM CRM.dbo.Dating AS d
INNER JOIN CRM.dbo.Company AS c
ON d.dat_CompanyId = c.Comp_CompanyId
LEFT OUTER JOIN dbo.OEORDH AS h
ON c.Comp_IdCust = h.CUSTOMER
LEFT OUTER JOIN dbo.OEORDHO AS dtgseason
ON h.ORDUNIQ = dtgseason.ORDUNIQ AND dtgseason.OPTFIELD = 'dtgseason'
INNER JOIN dbo.OEORDD AS od
ON h.ORDUNIQ = od.ORDUNIQ
INNER JOIN dbo.DTITEMS
ON od.CATEGORY = dbo.DTITEMS.CATEGORY
INNER JOIN dbo.DATING
ON d.dat_season = dbo.DATING.SEASON
AND d.dat_year = dbo.DATING.YEAR
AND dbo.DTITEMS.SEASON = dbo.DATING.SEASON
AND dbo.DTITEMS.YEAR = dbo.DATING.YEAR
WHERE (h.ORDDATE BETWEEN dbo.DATING.PROMOSTART AND dbo.DATING.PROMOEND)
AND (h.EXPDATE BETWEEN dbo.DATING.EXPSHIPST AND dbo.DATING.EXPSHIPEND)
AND (d.dat_state = 'Approve')
AND (d.dat_Deleted IS NULL)
AND (dbo.DATING.SEASCLOSED = 0)
AND (dbo.DTITEMS.ALTMINIMUM = 0)
AND (h.ORDNUMBER NOT IN (
SELECT ORDNUMBER
FROM dbo.vDatingValidOrdersAlt))
AND (dbo.DATING.ORDERON = 1)
Here’s the query it references, which is basically the same thing, but looks for orders with different item categories: vDatingValidOrdersAlt. Is this my problem?
SELECT
h.CUSTOMER
, h.ORDNUMBER
, h.INVNETWTX
, dtgseason.VALUE AS dtgseason
, c.comp_dqdatingmin
, dbo.DTITEMS.ALTMINIMUM
, d.dat_datingapprovedon
, d.dat_ordsincepromostart
, h.EXPDATE
, dbo.DATING.PROMOSTART
, d.dat_season
, d.dat_year
, od.ITEM
, od.CATEGORY
, d.dat_DatingID
, c.Comp_Name
, d.dat_state
, h.ORDUNIQ, c.comp_dqdatingmax
FROM CRM.dbo.Dating AS d
INNER JOIN CRM.dbo.Company AS c
ON d.dat_CompanyId = c.Comp_CompanyId
LEFT OUTER JOIN dbo.OEORDH AS h
ON c.Comp_IdCust = h.CUSTOMER
LEFT OUTER JOIN dbo.OEORDHO AS dtgseason
ON h.ORDUNIQ = dtgseason.ORDUNIQ AND dtgseason.OPTFIELD = 'dtgseason'
INNER JOIN dbo.OEORDD AS od
ON h.ORDUNIQ = od.ORDUNIQ
INNER JOIN dbo.DTITEMS
ON od.CATEGORY = dbo.DTITEMS.CATEGORY
INNER JOIN dbo.DATING
ON d.dat_season = dbo.DATING.SEASON
AND d.dat_year = dbo.DATING.YEAR
AND dbo.DTITEMS.SEASON = dbo.DATING.SEASON
AND dbo.DTITEMS.YEAR = dbo.DATING.YEAR
WHERE (h.ORDDATE BETWEEN dbo.DATING.PROMOSTART AND dbo.DATING.PROMOEND)
AND (h.EXPDATE BETWEEN dbo.DATING.EXPSHIPST AND dbo.DATING.EXPSHIPEND)
AND (d.dat_state = 'Approve')
AND (d.dat_Deleted IS NULL)
AND (dbo.DATING.SEASCLOSED = 0)
AND (dbo.DTITEMS.ALTMINIMUM > 0)
AND (dbo.DATING.ORDERON = 1)
There must be a way to make this query less resource-intensive, but I'm not sure how to go about it. Thoughts and suggestions?
You have a logical issue in both of these queries. You have a left join to dbo.OEORDH but then have a where clause on that table. That is logically changing your left join to an inner join. As for performance I don't see anything glaring that you can change in your sql. I would however be surprised if you have indexes situated to cover these queries. We would need to see the table definition and indexes. Also, posting the execution plans would be crucial in figuring this out.
Here is what those two queries might look like with some formatting.
SELECT h.CUSTOMER
, h.ORDNUMBER
, h.INVNETWTX
, dtgseason.VALUE AS dtgseason
, c.comp_dqdatingmin
, di.ALTMINIMUM
, d.dat_datingapprovedon
, d.dat_ordsincepromostart
, h.EXPDATE
, dd.PROMOSTART
, d.dat_season
, d.dat_year
, od.ITEM
, od.CATEGORY
, d.dat_DatingID
, c.Comp_Name
, d.dat_state
, h.ORDUNIQ
, c.comp_dqdatingmax
FROM CRM.dbo.Dating AS d
INNER JOIN CRM.dbo.Company AS c ON d.dat_CompanyId = c.Comp_CompanyId
LEFT OUTER JOIN dbo.OEORDH AS h ON c.Comp_IdCust = h.CUSTOMER
LEFT OUTER JOIN dbo.OEORDHO AS dtgseason ON h.ORDUNIQ = dtgseason.ORDUNIQ
AND dtgseason.OPTFIELD = 'dtgseason'
INNER JOIN dbo.OEORDD AS od ON h.ORDUNIQ = od.ORDUNIQ
INNER JOIN dbo.DTITEMS di ON od.CATEGORY = di.CATEGORY
INNER JOIN dbo.DATING dd ON d.dat_season = dd.SEASON
AND d.dat_year = dd.YEAR
AND di.SEASON = dd.SEASON
AND di.YEAR = dd.YEAR
WHERE h.ORDDATE BETWEEN dd.PROMOSTART AND dd.PROMOEND
AND h.EXPDATE BETWEEN dd.EXPSHIPST AND dd.EXPSHIPEND
AND d.dat_state = 'Approve'
AND d.dat_Deleted IS NULL
AND dd.SEASCLOSED = 0
AND di.ALTMINIMUM = 0
AND h.ORDNUMBER NOT IN (SELECT ORDNUMBER FROM dbo.vDatingValidOrdersAlt)
AND dd.ORDERON = 1
And the second one.
SELECT h.CUSTOMER
, h.ORDNUMBER
, h.INVNETWTX
, dtgseason.VALUE AS dtgseason
, c.comp_dqdatingmin
, di.ALTMINIMUM
, d.dat_datingapprovedon
, d.dat_ordsincepromostart
, h.EXPDATE
, dd.PROMOSTART
, d.dat_season
, d.dat_year
, od.ITEM
, od.CATEGORY
, d.dat_DatingID
, c.Comp_Name
, d.dat_state
, h.ORDUNIQ
, c.comp_dqdatingmax
FROM CRM.dbo.Dating AS d
INNER JOIN CRM.dbo.Company AS c ON d.dat_CompanyId = c.Comp_CompanyId
LEFT OUTER JOIN dbo.OEORDH AS h ON c.Comp_IdCust = h.CUSTOMER
LEFT OUTER JOIN dbo.OEORDHO AS dtgseason ON h.ORDUNIQ = dtgseason.ORDUNIQ
AND dtgseason.OPTFIELD = 'dtgseason'
INNER JOIN dbo.OEORDD AS od ON h.ORDUNIQ = od.ORDUNIQ
INNER JOIN dbo.DTITEMS as di ON od.CATEGORY = di.CATEGORY
INNER JOIN dbo.DATING as dd ON d.dat_season = dd.SEASON
AND d.dat_year = dd.YEAR
AND di.SEASON = dd.SEASON
AND di.YEAR = dd.YEAR
WHERE h.ORDDATE BETWEEN dd.PROMOSTART AND dd.PROMOEND
AND h.EXPDATE BETWEEN dd.EXPSHIPST AND dd.EXPSHIPEND
AND d.dat_state = 'Approve'
AND d.dat_Deleted IS NULL
AND dd.SEASCLOSED = 0
AND di.ALTMINIMUM > 0
AND dd.ORDERON = 1
Related
I have created an SQL query which runs but it takes about 17 seconds to complete. I have narrowed down the problem to the IN clause within the Where section of the query. This section is responsible for also finding all records within the same table where the kitref of one record matches the partofkit field of multiple other records.
Could anyone help with suggestions on how I may be able to make this more efficient?
The full SQL is below:
SELECT
tblProductions.ProductionName, tblEquipment.KitRef, tblEquipment.PartOfKit, tblEquipment.Description,
tblCollection.HireID, tblCollection.CollectedBy, Format(tblCollection.DueBack,'dd/MM/yyyy') AS DueBack, Format(tblCollection.CollectionDate,'dd/MM/yyyy') AS CollectionDate, Format(tblCollection.CollectionTime,'HH:mm') AS CollectionTime, tblCollection.DiscountPC,
tblCollectionItemized.HireLine, tblCollectionItemized.Notes, tblCollectionItemized.BookingActive, tblCollectionItemized.DepositReturned, tblTariff.Tariff
FROM tblTariff
INNER JOIN (
tblProductions INNER JOIN (
tblCollection INNER JOIN (
tblEquipment
INNER JOIN tblCollectionItemized ON tblEquipment.KitKey = tblCollectionItemized.KitKey
) ON tblCollection.HireID = tblCollectionItemized.HireID)
ON tblProductions.ProductionIDKey = tblCollection.ProductionName
) ON tblTariff.TariffKey = tblCollection.Tarriff
WHERE (
tblCollectionItemized.BookingActive='TRUE'
AND tblEquipment.PartOfKit IN (
SELECT tblEquipment.KitRef
FROM tblEquipment
INNER JOIN tblCollectionItemized ON tblEquipment.KitKey = tblCollectionItemized.KitKey
WHERE tblCollectionItemized.ReturnsNumber =43
)
)
OR (
tblCollectionItemized.BookingActive='TRUE'
AND tblCollectionItemized.ReturnsNumber =43
)
Not a complete answer here but using some aliases and joins in a logical order make this nightmarish query into something a lot easier to see what is going on.
SELECT
p.ProductionName
, e.KitRef
, e.PartOfKit
, e.Description
, c.HireID
, c.CollectedBy
, Format(c.DueBack,'dd/MM/yyyy') AS DueBack
, Format(c.CollectionDate,'dd/MM/yyyy') AS CollectionDate
, Format(c.CollectionTime,'HH:mm') AS CollectionTime
, c.DiscountPC
, ci.HireLine
, ci.Notes
, ci.BookingActive
, ci.DepositReturned
, t.Tariff
FROM tblTariff t
INNER JOIN tblCollection c ON t.TariffKey = c.Tarriff
INNER JOIN tblProductions p ON p.ProductionIDKey = c.ProductionName
INNER JOIN tblCollectionItemized ci ON c.HireID = ci.HireID
INNER JOIN tblEquipment e ON e.KitKey = ci.KitKey
WHERE ci.BookingActive = 'TRUE'
AND e.PartOfKit IN
(
SELECT e2.KitRef
FROM tblEquipment e2
INNER JOIN tblCollectionItemized ci2 ON e2.KitKey = ci2.KitKey
WHERE ci2.ReturnsNumber = 43
)
OR
(
ci.ReturnsNumber = 43
)
you can try EXISTS instead of IN and add (nolock) hint to tables
SELECT
P.ProductionName,
E.KitRef,
E.PartOfKit,
E.Description,
C.HireID,
C.CollectedBy,
Format(C.DueBack,'dd/MM/yyyy') AS DueBack,
Format(C.CollectionDate,'dd/MM/yyyy') AS CollectionDate,
Format(C.CollectionTime,'HH:mm') AS CollectionTime,
C.DiscountPC,
CI.HireLine,
CI.Notes,
CI.BookingActive,
CI.DepositReturned,
T.Tariff
FROM tblTariff T
INNER JOIN tblCollection C (nolock) ON T.TariffKey = C.Tarriff
INNER JOIN tblProductions P (nolock) ON P.ProductionIDKey = C.ProductionName
INNER JOIN tblCollectionItemized CI (nolock) ON C.HireID = CI.HireID
INNER JOIN tblEquipment E (nolock) ON E.KitKey = CI.KitKey
WHERE (
tblCollectionItemized.BookingActive='TRUE'
AND EXISTS (
SELECT *
FROM tblEquipment E2 (nolock)
INNER JOIN tblCollectionItemized CI2 (nolock) ON E2.KitKey = CI2.KitKey
WHERE CI2.ReturnsNumber =43 AND E.PartOfKit = E2.KitRef )
)
OR (
CI.BookingActive='TRUE'
AND CI.ReturnsNumber =43
)
select distinct sotr_sys_no
, SODETS_VINYL_COLOUR
, SODETS_MDF_COLOUR
, SOTR_PROMISED_DATE
, DATEDIFF(dd,getdate(),sotr_promised_date) as DueDays
, AEXTRA_5_SHORT_NAME
, AEXTRA_5_VINYL_PARTCODE
, CASE WHEN SODETS_MDF_COLOUR > '0' THEN AltMDFCode ELSE AEXTRA_5_MDF_PARTCODE END AS AEXTRA_5_MDF_PARTCODE
, ISNULL(Vinylqty,0) As VinylQty
, ISNULL(MDFqty,0) as MDFQty
, Vinyldue
, MDFdue
, WO.WOOutstanding
from Defactouser.F_SO_Transaction WITH (NOLOCK)
inner join defactouser.F_SO_Transaction_Details WITH (NOLOCK)
on sotr_sys_no = sotd_head_no
inner join defactouser.F_SO_Transaction_Details_Extra WITH (NOLOCK)
on SOTD_SYS_NO = SODETS_LINK
left outer join (
select distinct AEXTRA_5_CODE as AltMDFKey
, AEXTRA_5_MDF_PARTCODE AS AltMDFCode
from DeFactoUser.F_AD_Extra_5 WITH (NOLOCK)
) as AltMDF
on SODETS_MDF_COLOUR = AltMDF.AltMDFKey
left outer join defactouser.F_AD_Extra_5 WITH (NOLOCK)
on SODETS_VINYL_COLOUR = [AEXTRA_5_CODE]
inner join defactouser.F_ST_Products WITH (NOLOCK)
on sotd_strc_code = strc_code
left Outer join (
SELECT Product_Code As VinylStockCode, sum(Physical_Qty_Units) as Vinylqty FROM DBO.DFBI_Stock_Physical WITH (NOLOCK)
WHERE Warehouse = 'DOORS' and LEFT(product_code ,3) = 'vfl'
Group By Product_Code
HAVING SUM(Physical_Qty_Units) >0
) VinylStock
on AEXTRA_5_VINYL_PARTCODE = VinylStock.VinylStockCode
left outer join (
SELECT Product_Code As MDFStockCode, sum(Physical_Qty_Units) as MDFqty FROM DBO.DFBI_Stock_Physical WITH (NOLOCK)
WHERE Warehouse = 'PANELS' and LEFT(product_code ,3) = 'MDF'
Group By Product_Code
HAVING SUM(Physical_Qty_Units) >0
) MDFStock
on CASE WHEN SODETS_MDF_COLOUR > '0' THEN AltMDF.AltMDFCode ELSE AEXTRA_5_MDF_PARTCODE END = MDFStock.MDFStockCode
left Outer JOin (select stex_strc_code as VinylStex , sum(STEX_QTY_UNITS) as Qty, MIN(stex_promised_date) as Vinyldue
from defactouser.F_ST_Transaction_Expediting
where left(stex_strc_code ,3) = 'vfl'
and stex_type = 'pop+'
group By STEX_STRC_CODE
) VinylStockIn
on AEXTRA_5_VINYL_PARTCODE = VinylStex
left Outer Join (
select stex_strc_code as MDFStex , sum(STEX_QTY_UNITS) as Qty, MIN(stex_promised_date) as MDFdue
from defactouser.F_ST_Transaction_Expediting
where left(stex_strc_code ,3) = 'mdf'
and stex_type = 'pop+'
group By STEX_STRC_CODE
) MDFStockIn on CASE WHEN SODETS_MDF_COLOUR > '0' THEN AltMDF.AltMDFCode ELSE AEXTRA_5_MDF_PARTCODE END = MDFStex
LEFT OUTER JOIN (
select SOTD_HEAD_NO, SODETS_VINYL_COLOUR as WOVinyl, SUM(BMTD_QTY_OUTSTANDING) as WOOutstanding from defactouser.f_bm_transactions_details
inner join defactouser.F_SO_Transaction_Details on BMTD_ORDER_LINK_NUMBER = SOTD_SYS_NO
inner join defactouser.F_SO_Transaction_Details_Extra on BMTD_ORDER_LINK_NUMBER = SODETS_LINK
where bmtd_type = 1 and bmtd_bmtr_type = 0 and bmtd_stwh_code in ('doors', 'shef trans') and SOTD_STATUS <99
Group by SOTD_HEAD_NO, SODETS_VINYL_COLOUR
) WO
on sotr_sys_no = WO.SOTD_HEAD_NO AND SODETS_VINYL_COLOUR = WO.WOVinyl
where (SOTD_QTY_UNITS_OUTSTANDING > 0
and SOTR_TYPE = 10
and SOTD_STWH_CODE IN ('doors' , 'hpp shef')
and left(sotd_strc_code ,5) <> 'drill'
and SOTR_CUST_CODE <>'hpp'
and STRC_ANAL1 = '1027'
and ISNULL(VinylQty,0) <10
and DATEDIFF(dd,getdate(),sotr_promised_date) <5
)
or
(SOTD_QTY_UNITS_OUTSTANDING > 0
and SOTR_TYPE = 10
and SOTD_STWH_CODE IN ('doors' , 'hpp shef')
and left(sotd_strc_code ,5) <> 'drill'
and SOTR_CUST_CODE <>'hpp'
and STRC_ANAL1 = '1027'
and ISNULL(MDFQty,0) <4
and DATEDIFF(dd,getdate(),sotr_promised_date) <5
)
Order By MDFQty, AEXTRA_5_MDF_PARTCODE
Currently this query produces a report that returns a table with products due to arrive in the next 5 days. How do I add a parameter to the report that will show me the results as it is, and then also to show a report for products due in whenever. I am using Report Builder 3.0, and have tried to add a parameter but cannot get the desired result.
Can this be done without editing the query and just in report builder?
Change you WHERE clause and replace < 5 with < #Days. Assuming the query is not a stored proc and is directly in the dataset query then SSRS will automatically add the #Days parameter to your report.
This question already has answers here:
How to find LEFT OUTER JOIN or RIGHT OUTER JOIN with ORACLE JOIN (+)
(2 answers)
Closed 7 years ago.
I am currently a Trainee trying to develop my SQL. Could someone help me with changing the old language to the new(orale 11g or sql 2014) Below is an example that I am current changing;
select apa.invoicE_num ap_invoice_num
, apa.creation_date
, rat.TRX_NUMBER ar_trx_number
, rat.creation_date loaded_into_ar
,asw.inserted_date
, decode(pvs.org_id,86,'SP',87,'SV')
, pv.vendor_nam
,pv.segment1,
pvs.vendor_site_code
, asw.attribute6
, asw.invoice_amount
, asw.INVOICE_NUM
from asw.asw_ap_invoices_interface_aud asw
, ra_customer_trx_all rat
, po_vendor_sites_all pvs
, po_vendors pv
, ap_invoices_all apa
where asw.invoice_num = rat.trx_number (+)
and asw.VENDOR_SITE_ID = pvs.vendor_site_id
and pvs.vendor_id = pv.vendor_id
and asw.invoice_num = apa.invoicE_num (+)
and asw.invoice_amount = apa.invoicE_amount (+)
and asw.vendor_site_id = apa.vendor_site_id (+)
and asw.attribute_category = 'Retek Import'
and asw.attribute6 in ('MB','DA','RM','SB')
and trunc(inserted_date) > '29-OCT-2015'
Thank you for reading this and I hope you can help!
SELECT *
FROM a, b
WHERE a.id=b.id(+)
the same as
SELECT *
FROM a LEFT OUTER JOIN b ON a.id=b.id
So in your case:
select apa.invoicE_num ap_invoice_num
, apa.creation_date
, rat.TRX_NUMBER ar_trx_number
, rat.creation_date loaded_into_ar
,asw.inserted_date
, decode(pvs.org_id,86,'SP',87,'SV')
, pv.vendor_nam
,pv.segment1,
pvs.vendor_site_code
, asw.attribute6
, asw.invoice_amount
, asw.INVOICE_NUM
from asw.asw_ap_invoices_interface_aud asw
left outer join ra_customer_trx_all rat on asw.invoice_num = rat.trx_number
inner join po_vendor_sites_all on pvs asw.VENDOR_SITE_ID = pvs.vendor_site_id
inner join po_vendors pv on pvs.vendor_id = pv.vendor_id
left outer join ap_invoices_all apa on asw.invoice_amount = apa.invoicE_amount and asw.vendor_site_id = apa.vendor_site_id
where asw.attribute_category = 'Retek Import'
and asw.attribute6 in ('MB','DA','RM','SB')
and trunc(inserted_date) > '29-OCT-2015'
I have a simple query that I would like some specific results but not sure of the right way to go about it. I'm using a SQL Server database and my query is as follows:
SELECT dt.year ,
db.dist_code ,
db.dist_name ,
db.s_code ,
db.s_name ,
fl.zip ,
fl.num_births ,
total_enrollment
FROM dbo.fact_enrollment_school AS fs
INNER JOIN dbo.dim_building AS db ON fs.building_key = db.building_key
INNER JOIN dbo.dim_time AS dt ON fs.time_key = dt.time_key
LEFT OUTER JOIN dbo.fact_live_birth AS fl ON dt.time_key = fl.time_key
AND db.building_key = fl.building_key
GROUP BY dt.year ,
db.dist_code ,
db.dist_name ,
db.school_code ,
db.school_name ,
fl.zip ,
total_enrollment ,
fl.num_births
What I need is to output the num_births total for every district_code that is the same.
Desired output:
I think you can achieve this by window function:
SELECT dt.year ,
db.dist_code ,
db.dist_name ,
db.s_code ,
db.s_name ,
fl.zip ,
sum(fl.num_births) over (partition by dt.year, db.dist_code),
total_enrollment
FROM dbo.fact_enrollment_school AS fs
INNER JOIN dbo.dim_building AS db ON fs.building_key = db.building_key
INNER JOIN dbo.dim_time AS dt ON fs.time_key = dt.time_key
LEFT OUTER JOIN dbo.fact_live_birth AS fl ON dt.time_key = fl.time_key
AND db.building_key = fl.building_key;
I have 2 queries.
Query # 1: inserts some data into a temp table
Query # 2: inserts the data from the temp table (with some joins) into a new table.
Running Query #1 alone takes 3 seconds. Running Query #2 alone takes 12 seconds.
When I run them both together as one execution, it runs forever (over 4 minutes).
What could possibly be the reason for this?
(I would post my code but it's very long and not much understandable to the outsider.)
Here's my SQL (at your own risk): Remember they run fine when ran alone.
-- START QUERY #1
SELECT * INTO #TempProdForSiteGoingTrim
FROM
(
SELECT
p.idProduct
, p.active
, p.sku
, p.description
, p.listprice
, p.price
, p.imageurl
, p.smallimageurl
, p.idManufacturer
, sortOrder
, CASE WHEN p.pgroup = '' OR p.pgroup IS NULL THEN CAST(p.idProduct AS VARCHAR) ELSE pgroup END [pgroup]
, CASE WHEN pa2.attr IS NULL THEN CAST(p.idProduct AS VARCHAR) ELSE CASE WHEN p.pgroup = '' OR p.pgroup IS NULL THEN CAST(p.idProduct AS VARCHAR) ELSE pgroup END END [RugGroup]
, pa1.attr [Color]
, pa3.attr [Collection]
, pa2.attr [RugSize]
, pa4.attr[RugShape]
FROM
(SELECT DISTINCT idProduct FROM ProdSite WHERE idSite = 39 ) s
INNER JOIN (SELECT * FROM products p WHERE active = -1 ) p ON s.idproduct = p.idproduct
LEFT OUTER JOIN (SELECT t.idproduct, attr FROM (SELECT max(idprodattr) [idprodattr], idproduct, b.idattrset FROM productattr a JOIN product_attr b on a.idattr = b.idattr WHERE idattrset = 1 GROUP BY a.idproduct, b.idattrset )t JOIN productattr a ON t.idprodattr = a.idprodattr JOIN product_attr b ON a.idattr = b.idattr) pa1 ON p.idproduct = pa1.idproduct
LEFT OUTER JOIN (SELECT t.idproduct, attr FROM (SELECT max(idprodattr)[idprodattr], idproduct, b.idattrset FROM productattr a JOIN product_attr b on a.idattr = b.idattr WHERE idattrset = 160 GROUP BY a.idproduct, b.idattrset )t JOIN productattr a ON t.idprodattr = a.idprodattr JOIN product_attr b ON a.idattr = b.idattr) pa2 ON p.idproduct = pa2.idproduct
LEFT OUTER JOIN (SELECT t.idproduct, attr FROM (SELECT max(idprodattr)[idprodattr], idproduct, b.idattrset FROM productattr a JOIN product_attr b on a.idattr = b.idattr WHERE idattrset = 6 GROUP BY a.idproduct, b.idattrset )t JOIN productattr a ON t.idprodattr = a.idprodattr JOIN product_attr b ON a.idattr = b.idattr) pa3 ON p.idproduct = pa3.idproduct
LEFT OUTER JOIN (SELECT t.idproduct, attr FROM (SELECT max(idprodattr)[idprodattr], idproduct, b.idattrset FROM productattr a JOIN product_attr b on a.idattr = b.idattr WHERE idattrset = 62 GROUP BY a.idproduct, b.idattrset )t JOIN productattr a ON t.idprodattr = a.idprodattr JOIN product_attr b ON a.idattr = b.idattr) pa4 ON p.idproduct = pa4.idproduct
)t
-- END QUERY #1
-- START QUERY #2
DECLARE #listRugSizes TABLE (idmanufacturer int, RugGroup VARCHAR(500), RugSizes VARCHAR(1000))
INSERT INTO #listRugSizes
SELECT
t1.idmanufacturer
, t1.RugGroup
, STUFF(( SELECT ', ' + RugSize FROM #TempProdForSiteGoingTrim t2 WHERE t2.RugGroup = t1.RugGroup and t2.idmanufacturer = t1.idmanufacturer FOR XML PATH(''), TYPE ).value('.', 'varchar(max)'), 1, 1, '') [Values]
FROM
#TempProdForSiteGoingTrim t1
GROUP BY
t1.RugGroup
, t1.idmanufacturer
INSERT INTO [NewTableForSiteGoingTrim]
SELECT
p.idProduct
, p.sku
, p.description
, p.listPrice
, p.price
, p.imageUrl
, p.smallImageUrl
, p.sortOrder
, p.idManufacturer
, p.pgroup
, p.ruggroup
, c.idCategory
, c.idCategory [fidcategory]
, c.idParentCategory
, c.idParentCategory [gidcategory]
, pc.idParentCategory [hidCategory]
, ppc.idParentCategory [iidCategory]
, m.Name
, rp.rewrite_key [rewrite_index]
, rm.rewrite_key [rewrite_key]
, color [Color]
, collection [Collection]
, rugsize [RugSize]
, ruggroup.rugcount
, ruggroup.maxprice
, ruggroup.minprice
, rs.RugSizes
, p.rugshape
FROM
#TempProdForSiteGoingTrim p
LEFT OUTER JOIN (
SELECT
MIN(c.idCategory) [idCategory]
, c.idProduct
FROM
(
SELECT
cp.idProduct
, cp.idCategory
FROM
dbo.categories_products cp
JOIN categories c ON cp.idcategory = c.idCategory
WHERE
c.idSite = 24
) c
GROUP BY
c.idProduct
) cp ON p.idProduct = cp.idProduct
LEFT OUTER JOIN categories c ON cp.idCategory = c.idCategory
LEFT OUTER JOIN categories pc ON c.idParentCategory = pc.idCategory
LEFT OUTER JOIN categories ppc ON pc.idParentCategory = ppc.idCategory
LEFT OUTER JOIN manufacturer m ON p.idManufacturer = m.idManufacturer
LEFT OUTER JOIN (SELECT * FROM rewrite WHERE type = 3) rm ON p.idManufacturer = rm.id
LEFT OUTER JOIN (SELECT * FROM rewrite WHERE type = 1) rp ON p.idProduct = rp.id
LEFT OUTER JOIN #listRugSizes rs ON p.RugGroup = rs.RugGroup and p.idmanufacturer = rs.idmanufacturer
LEFT OUTER JOIN
(
SELECT
p.ruggroup
, p.idmanufacturer
, min(price) [minPrice]
, count(*) [RugCount]
, m.maxprice
FROM
#TempProdForSiteGoingTrim p
LEFT OUTER JOIN
(
SELECT
r.ruggroup
, r.idmanufacturer
, max(price) [maxprice]
FROM
#TempProdForSiteGoingTrim r
WHERE
r.idproduct = (SELECT MAX(idproduct) FROM #TempProdForSiteGoingTrim WHERE ruggroup = r.ruggroup AND price = r.price and idmanufacturer = r.idmanufacturer)
GROUP BY
ruggroup
, idmanufacturer
) m ON p.ruggroup = m.ruggroup and p.idmanufacturer = m.idmanufacturer
GROUP BY
p.ruggroup
, m.maxprice
, p.idmanufacturer
) ruggroup ON p.ruggroup = ruggroup.ruggroup and p.idmanufacturer = ruggroup.idmanufacturer
-- END QUERY #2
Edit
When I change the last join "ruggroup" to only group and join by column "ruggroup" and not "ruggroup" and "idmanufacturer" then it works fine.