1

This is my code:

SELECT DATEDIFF(shippedDate, orderDate) AS '# Days', COUNT(*) 
FROM datenumtest2 
GROUP BY '# Days' 
ORDER BY '# Days'

the results give me:

enter image description here

In the # Days, there are 3 number 5's. I would like the output in my code to be

Days: 5   COUNT(*): 3

Any help is greatly appreciated

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Sea W
  • 37
  • 1
  • 1
  • 5

2 Answers2

1

in mysql you should use backtics for column name and not quote

SELECT DATEDIFF(shippedDate, orderDate) AS `# Days`, COUNT(*) 
FROM datenumtest2 
GROUP BY `# Days`
ORDER BY `# Days`

or simple days

SELECT DATEDIFF(shippedDate, orderDate) AS Days, COUNT(*) 
FROM datenumtest2 
GROUP BY Days
ORDER BY Days
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • @JorgeCampos ..many thanks ...but for double quotes I seem to remember that functions as a single quote .. takea look at http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql – ScaisEdge Apr 03 '17 at 17:05
  • Thanks so much! Its working just like I needed it to. – Sea W Apr 03 '17 at 17:53
  • Single quotes is for string literals (SQL ANSI), backticks is mysql specific while double quotes is also SQL ANSI to define delimiters (fields names, etc.). I happen to have an online link of spec of the [SQL Ansi version 99](http://web.cecs.pdx.edu/~len/sql1999.pdf) take a look at the page at the page 129 in the definition of A regular identifier (item 26) :) – Jorge Campos Apr 04 '17 at 01:07
0

It should work that you pass in the "# Days" alias to the count() function: count(`# Days`)should do the trick. Be careful with your alias, as a note: treat it with the same formatting principles as column names. no_days,days_diff,or just days should do the trick.

Aaron Morefield
  • 952
  • 10
  • 18