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
rayluvsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rayluvsAuthor Commented:
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

0
Ryan ChongCommented:
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

0
Ryan ChongCommented:
for similar output, try:

	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 + ' (' + cast(COUNT(*) as varchar(3)) + ')' AS ToolUse
FROM TableName
GROUP BY tools,Date1

Open in new window


and Order By like below when necessary (but it seems that I can't get your output sorting logic...)

Order by 1, COUNT(*) desc

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rayluvsAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.