jana
asked on
Ms SQL script to extract data from a resulting script and present new results
We have a Ms Sql script that groups transactions by dates, tools and how many techs used them. When running the query, the result is as follows:
We are trying to extract from that result the the tools used the same days.
Based on the above the result we are looking for is:
Notice that the date 2017-05-10 is not present because only 1 tool was used that day.
We are looking into the "with" clause as to extract from the first result to get the second.
What would be the best way to go about this?
Date1 tools ToolUse
---------- ------ -------
2017-01-03 Desk 2
2017-01-03 Hammer 3
2017-05-10 Hammer 1
2017-10-02 Desk 1
2017-10-02 Hammer 2
We are trying to extract from that result the the tools used the same days.
Based on the above the result we are looking for is:
Date Tools Date Tools
-------- ---------- --------- ----------
1/3/2017 Desk (2) 10/2/2017 Desk (1)
1/3/2017 Hammer (3) 10/2/2017 Hammer (2)
Notice that the date 2017-05-10 is not present because only 1 tool was used that day.
We are looking into the "with" clause as to extract from the first result to get the second.
What would be the best way to go about this?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi! Will check!
ASKER
(I was gettging 1 column)
ASKER
Thank you very much, worked! But when running against the actual msSql table, gives an error.
Noticed that if I take out the ")" from "GROUP BY tools,Date1)", it runs but displays but wrong values:
Am I missing something in the script when running against a SQL table rather than a virtual table?
with grp as
(SELECT Date1, tools, COUNT(*) AS ToolUse FROM TableName where Tech='Jon'
GROUP BY tools,Date1)
select min(date1) as Sdate, tools + ' (' + cast(sum(tooluse) as varchar(3))+')' [tools used],
max(date1) as Edate, tools + ' ('+cast(count(date1) as varchar(3))+')' as [Dates Used]
from grp
where tooluse > 1
group by tools
Noticed that if I take out the ")" from "GROUP BY tools,Date1)", it runs but displays but wrong values:
with grp as
(SELECT Date1, tools, COUNT(*) AS ToolUse FROM TableName where Tech='Jon'
GROUP BY tools,Date1
select min(date1) as Sdate, tools + ' (' + cast(sum(tooluse) as varchar(3))+')' [tools used],
max(date1) as Edate, tools + ' ('+cast(count(date1) as varchar(3))+')' as [Dates Used]
from grp
where tooluse > 1
group by tools
Am I missing something in the script when running against a SQL table rather than a virtual table?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi! My mistake! Since your script worked I reviewed my again ans it was that SQL studio design windows made it dificult for me to see that specific ")" that was in the row below.
Listen, thank you very much!
Listen, thank you very much!
Phwew... Easy enough to overlook, and very pleased you found the rogue ')'
Hope it is now working for you :)
Hope it is now working for you :)
ASKER
It is! Thanx!
Will proceed to close the question.
Will proceed to close the question.
ASKER
Open in new window