Discussion on:

5
Comments

Join the conversation!

Follow via:
RSS
Email Alert
0 Votes
+ -
A quibble
Tony Hopkinson Updated - 29th Jul 2011
SELECT 'BOOKS' AS Genre, ''
should be
Null cast as int, as if you pull this resultset in ItemCount will be a varchar.
Or you could make it the last line and not need the cast, geiven you replace '' with a null.

Presumably this is a one off as this is an inflatable dartboard solution
Denormalising with a Genres table with a displayorder column, and replacing Count(*) with Count(Category) , would make it a simple join and aggregate and cope with new categories, and reordering without having to find an rewrite the sql.
0 Votes
+ -
Contributr
Yup, this was a one-time report. In addition, I can't monkey with it anyways, because I'm pulling from Microsoft CRM's tables, and it wasn't worth making a View over... Also, didn't care about the types, since I was doing a copy/paste from the SQL Management Studio results window straight into Excel. happy

J.Ja
you would have figured it out, or casted it and coped with the empty string. silly
Saying that you'd have needed to use is IsNull, even with my quibble on board.
I do a fair bit of this sort of stuff becasue an immovable leagcy database, so I've got in the habit of forcing the logical datatype.
0 Votes
+ -
Great tip
oldbaritone 2nd Aug 2011
Thanks for a handy work-around.!
Here's a slightly cleaner work-around using CASE logic:
--===============================
SELECT
CASE ISNULL( Genre , '' )
WHEN '' THEN Category
ELSE ' ' + Genre
END AS Details ,
Item_Count
FROM
(
SELECT DISTINCT
Category AS Category ,
NULL AS Genre , NULL AS Item_Count
FROM Items
UNION
SELECT
Category ,
Genre ,
COUNT( * ) AS Item_Count
FROM Items
GROUP BY Category , Genre
) AS demo
ORDER BY Category , ISNULL( Genre , '' ) ;
--===============================
Keyboard Shortcuts:
Prev
Next
Toggle
Join the conversation
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.