Link to home
Start Free TrialLog in
Avatar of jana
janaFlag for United States of America

asked on

Ms Script to group with totals

Hi,

Please view our script to display the Following:

2017-01-03, Hammer (3)
2017-01-03, Desk (2)
2017-05-10, Hammer (1)
2017-10-02, Desk (1)
2017-10-02, Hammer (2)

Open in new window


The script is not display what we need.  the script is:
SELECT Date1, tools, COUNT(*) AS "Same-Day-USe"
FROM TableName
GROUP BY tools,Date1
HAVING COUNT(*) > 1

Open in new window


Please advice
Avatar of jana
jana
Flag of United States of America image

ASKER

Here is a data to test our script (virtual table)):

	With TableName(Tools,Date1,Time1,Tech)
		AS (select 'Hammer','2017-01-03', '12:00:43', 'Joe' union all
		    select 'Hammer','2017-01-03', '12:15:00', 'Dereck' union all
		    select 'Hammer','2017-01-03', '12:17:00', 'Lillian' union all
		    select 'Desk','2017-01-03', '10:27:01', 'Sandy' union all
		    select 'Desk','2017-01-03', '11:00:00', 'Joe' union all
		    select 'Hammer','2017-05-10', '10:00:00', 'Joe' union all
		    select 'Desk','2017-10-02', '11:00:00', 'Joe' union all
		    select 'Hammer','2017-10-02', '14:27:00', 'Smith'  union all
		    select 'Hammer','2017-10-02', '16:00:00', 'Smith')
SELECT Date1, tools, COUNT(*) AS "Same-Day-Use"
FROM TableName
GROUP BY tools,Date1
HAVING COUNT(*) > 1

Open in new window


When running the result is:

Date1	tools	Same-Day--Use
2017-01-03	Desk	2
2017-01-03	Hammer	3
2017-10-02	Hammer	2

Open in new window


When it should display,
2017-01-03, Hammer (3)
2017-01-03, Desk (2)
2017-05-10, Hammer (1)
2017-10-02, Desk (1)
2017-10-02, Hammer (2)

Open in new window

Avatar of Ryan Chong
the issue is at:

SELECT Date1, tools, COUNT(*) AS "Same-Day-Use"
FROM TableName
GROUP BY tools,Date1
HAVING COUNT(*) > 1

hence, try this instead:

SELECT Date1, tools, COUNT(*) AS "Same-Day-Use"
FROM TableName
GROUP BY tools,Date1

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

I think your ORDER is ok, seem maybe our mistake.

Worked!

So to understand the script, it would sort & group by tools/Date1, then the ' (' + cast(COUNT(*) as varchar(3)) + ')'  would sum up by Tools.

Got it.