December 23, 2013 at 2:21 am
dwain.c (12/23/2013)
Brilliant Jeff, simply brilliant!
Tops my offering by a mile:
;WITH Subproducts AS (
SELECT PrimaryID = LEFT([Product Id],CHARINDEX('.',[Product Id],0)-1), *
FROM #Product_Table
WHERE IsPrimary <> 1)
SELECT
PrimaryID,
Subproduct = '',
quantity = CAST(EXP(SUM(LOG(quantity))) AS INT)
FROM Subproducts
GROUP BY PrimaryID
UNION ALL
SELECT
PrimaryID,
MAX([Product Id]),
0
FROM Subproducts
GROUP BY PrimaryID
ORDER BY PrimaryID, Subproduct
Isn't this all rather irrelevant though? The source table is denormalised - it contains two types of data, products and subproducts. The output contains two types of data too, and I'd guess it only looks that way in an attempt to model the source data...
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
December 23, 2013 at 5:08 am
ChrisM@Work (12/23/2013)
Isn't this all rather irrelevant though? The source table is denormalised - it contains two types of data, products and subproducts. The output contains two types of data too, and I'd guess it only looks that way in an attempt to model the source data...
Good point but no. The problem is that the table likely does exist in that form and, despite our concerns and as incorrect as it may be, the OP likely didn't make up the requirement. It was probably given to him and he needed our help. I just hope he goes back to whom ever the original designer was and the people giving him the requirement for this type of output and says "Here's what the people over at SQLServerCentral had to say about your design/requirements. You might want to fix them".
--Jeff Moden
Change is inevitable... Change for the better is not.
December 23, 2013 at 5:10 am
dwain.c (12/23/2013)
Brilliant Jeff, simply brilliant!
Thanks for the feedback, Dwain but it's not that. It was late and I had to do it with just dip on the table because I was too tired to figure out how to do a self join. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
December 23, 2013 at 5:25 am
Jeff Moden (12/23/2013)
ChrisM@Work (12/23/2013)
Isn't this all rather irrelevant though? The source table is denormalised - it contains two types of data, products and subproducts. The output contains two types of data too, and I'd guess it only looks that way in an attempt to model the source data...Good point but no. The problem is that the table likely does exist in that form and, despite our concerns and as incorrect as it may be, the OP likely didn't make up the requirement. It was probably given to him and he needed our help...
It's all too easy to stomp on the OP when they post up what is clearly a poor storage choice - without establishing beforehand if they had anything to do with it.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
December 23, 2013 at 5:40 am
ChrisM@Work (12/23/2013)
Jeff Moden (12/23/2013)
ChrisM@Work (12/23/2013)
Isn't this all rather irrelevant though? The source table is denormalised - it contains two types of data, products and subproducts. The output contains two types of data too, and I'd guess it only looks that way in an attempt to model the source data...Good point but no. The problem is that the table likely does exist in that form and, despite our concerns and as incorrect as it may be, the OP likely didn't make up the requirement. It was probably given to him and he needed our help...
It's all too easy to stomp on the OP when they post up what is clearly a poor storage choice - without establishing beforehand if they had anything to do with it.
Yeah... One person in particular was rather famous for that. I'm tickled that he doesn't appear to haunt these hallowed hallways anymore.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 23, 2013 at 9:29 am
Jeff Moden (12/23/2013)
ChrisM@Work (12/23/2013)
Jeff Moden (12/23/2013)
ChrisM@Work (12/23/2013)
Isn't this all rather irrelevant though? The source table is denormalised - it contains two types of data, products and subproducts. The output contains two types of data too, and I'd guess it only looks that way in an attempt to model the source data...Good point but no. The problem is that the table likely does exist in that form and, despite our concerns and as incorrect as it may be, the OP likely didn't make up the requirement. It was probably given to him and he needed our help...
It's all too easy to stomp on the OP when they post up what is clearly a poor storage choice - without establishing beforehand if they had anything to do with it.
Yeah... One person in particular was rather famous for that. I'm tickled that he doesn't appear to haunt these hallowed hallways anymore.
Heehee yes I know who you mean. Shame because he writes great articles. I remember Steve once saying that he was a really nice bloke to spend time with. His posts were strange for two reasons: the venom of the attacks, and the arcane nature of the detail. Folks post questions here very broadly speaking for two reasons too - they really want to learn, or they really don't - and that kind of attack offends both!
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 6 posts - 16 through 20 (of 20 total)
You must be logged in to reply to this topic. Login to reply