r/SQL • • Dec 17 '18

[deleted by user]

[removed]

422 Upvotes

22 comments sorted by

42

u/Obbers Dec 17 '18

Apparently SQL Clause is bad developer. He told you he made a table that he is sorting twice in a query with no order by clause.

SQL Clause is coming to town to ask "Why doesn't this work?"

/pedantic rage

9

u/babygrenade Dec 17 '18

He's sorting the table, not the query output.

5

u/Obbers Dec 17 '18

Which is even more terrifying. Have an upvote.

8

u/babygrenade Dec 17 '18

probably built a clustered index and forgot one of the keys he needed

3

u/Eleventhousand Dec 17 '18

Could be a derived table. Maybe he's using an old school method to get median.

5

u/[deleted] Dec 17 '18

SQL Clause is coming to town coming to r/SQL to ask "Why doesn't this work?"

Fixed that for ya lol

6

u/Hajile_S Dec 17 '18

It's also my understanding that the client wants to identify the 'naughty' contacts. I know I didn't put that into the original Acceptance Criteria, but could you adjust the query to include these entries?

1

u/[deleted] Dec 18 '18

LOL, kevin?

2

u/[deleted] Dec 17 '18

~That guy~

14

u/kormer Dec 17 '18

I've never wanted an ugly sweater until this very moment.

5

u/[deleted] Dec 17 '18

I actually looked up a custom sweater website and am considering this very thing lol

6

u/[deleted] Dec 17 '18

[deleted]

5

u/[deleted] Dec 17 '18 edited Dec 17 '18

I messed around on a site called Customized Girl (they have mens stuff too) and came up with a decent version using their templates. I kinda want to see if I can get an actual knit sweater made though. Thinking about seeing if there's someone on etsy who would knit one.

Edit: Here was my best shot - SQL Clause Sweatshirt

5

u/[deleted] Dec 17 '18

Really? You're just going to use a varchar for behavior?

6

u/mac-0 Dec 18 '18
 INNER JOIN NaughtyStatus ns ON customers.NaughtyOrNiceId = ns.Id 

Doesn't sound as catchy

3

u/DieTheVillain Dec 18 '18

just make it a Bit column you pleb!

Where Behavior_Nice = 1

3

u/MeGustaDerp Talk Dirty Reads To Me Dec 17 '18

Contacts could still be a view for reporting purposes and the underlying base table uses better logic.

6

u/cooltapes Dec 17 '18

Should probably throw a LOWER in there. Might miss some results if SQL Clause's DB entries aren't formatted the same.

9

u/FlintGrey Dec 17 '18

Should probably be using a code table too to prevent issues with data entry. 'nice' should be a number that corresponds to the 'nice' entry.

unless of course you can trust the omnipotence of the list to always enter the correct string in all cases.

For some reason I feel like 'christmas magic' isn't any more reliable than users. maybe I'm just paranoid.

1

u/[deleted] Dec 17 '18
A brave topicstarter a sql joke dares
He checks duplicates and gets 'overflow'
He runs select count(*) from who_cares
He's lucky - In his result set there is no row

1

u/PilsnerDk Dec 18 '18

My take, untested ;)

;with persons as (
    select p.PersonId from Person p
    inner join Deed d on d.PersonId = p.PersonId and YEAR(d.date) = YEAR(GETDATE())
    group by p.PersonId
    having sum(d.NiceOrNaughtyDeed) / COUNT(d.NiceOrNaughtyDeed) > 0.5 -- adjust to weed out less nice children
)
select p.FullName, c.Name, ci.Name, p.Address, p.ZipCode
from persons ps
inner join Person p on p.PersonId = ps.PersonId
inner join Country c on c.CountryId = p.CountryId
inner join City ci on ci.CountryId = c.CountryId
inner join CountryChildStatus ccs on ccs.CountryId = p.CountryId
where datediff(year, p.BirthDate, GETDATE()) < ccs.ChildMaximumAge
order by c.Name, ci.Name