Hi
Is it possible to group in a subquery.
declare @.ext char(4)
declare @.site int
declare @.calltype char(1)
set @.site = 1
set @.calltype = 'E'
select digits, count(*) as total,
(select count(*) from V2Tickets where durationofconversation>0 and
siteid=@.site and calltype=@.calltype and len(digits)=4) as besvaret,
(select count(*) from V2Tickets where durationofconversation=0 and
releasecause='OC' and siteid=@.site and calltype=@.calltype and len(digits)=4)
as optaget,
(select count(*) from V2Tickets where durationofconversation=0 and
durationofcall<40 and releasecause='RL' and siteid=@.site and
calltype=@.calltype and len(digits)=4) as opgivet,
(select count(*) from V2Tickets where durationofconversation=0 and
durationofcall>=40 and siteid=@.site and calltype=@.calltype and
len(digits)=4) as ubesvaret
from [V2tickets]
where
siteid=@.site and calltype=@.calltype and len(digits)=4
group by digits
order by 1
As you can see this gives me the correct total (colum 2) for digits, but all
the subqueries (column 3-6) computes the total.
1000 ,19167,370251,109377,58621,52809
1001 ,74,370251,109377,58621,52809
1002 ,139,370251,109377,58621,52809
1003 ,113,370251,109377,58621,52809
1004 ,81,370251,109377,58621,52809
1005 ,385,370251,109377,58621,52809
1006 ,307,370251,109377,58621,52809
1007 ,237,370251,109377,58621,52809
1008 ,122,370251,109377,58621,52809
1009 ,79,370251,109377,58621,52809
1010 ,255,370251,109377,58621,52809
1011 ,288,370251,109377,58621,52809
regards
HenryOn Mon, 14 Feb 2005 22:02:10 +0100, Henry wrote:
>Is it possible to group in a subquery.
>declare @.ext char(4)
>declare @.site int
>declare @.calltype char(1)
>set @.site = 1
>set @.calltype = 'E'
>select digits, count(*) as total,
>(select count(*) from V2Tickets where durationofconversation>0 and
>siteid=@.site and calltype=@.calltype and len(digits)=4) as besvaret,
>(select count(*) from V2Tickets where durationofconversation=0 and
>releasecause='OC' and siteid=@.site and calltype=@.calltype and len(digits)=4
)
>as optaget,
>(select count(*) from V2Tickets where durationofconversation=0 and
>durationofcall<40 and releasecause='RL' and siteid=@.site and
>calltype=@.calltype and len(digits)=4) as opgivet,
>(select count(*) from V2Tickets where durationofconversation=0 and
>durationofcall>=40 and siteid=@.site and calltype=@.calltype and
>len(digits)=4) as ubesvaret
>from [V2tickets]
>where
>siteid=@.site and calltype=@.calltype and len(digits)=4
>group by digits
>order by 1
>As you can see this gives me the correct total (colum 2) for digits, but al
l
>the subqueries (column 3-6) computes the total.
(snip)
Hi Henry,
You can fix this by expanding the subqueries with a reference to the row
in the outer query:
SELECT digits
, COUNT(*) AS total
,(SELECT COUNT(*)
FROM V2Tickets AS b
WHERE b.durationofconversation > 0
AND b.siteid = @.site
AND b.calltype = @.calltype
AND LEN(b.digits) = 4
AND b.digits = a.digits) AS besvaret
(other subqueries snipped)
FROM V2tickets AS a
WHERE siteid = @.site
AND calltype = @.calltype
AND LEN(digits) = 4
GROUP BY digits
ORDER BY digits
However, in your case there's a faster method to get the various counts:
SELECT digits
, COUNT(*) AS total
, COUNT(CASE WHEN durationofconversation > 0
THEN 1 END) AS besvaret
, COUNT(CASE WHEN durationofconversation = 0
AND releasecouse = 'OC'
THEN 1 END) AS optaget
, COUNT(CASE WHEN durationofconversation = 0
AND releasecouse = 'RL'
AND durationofcall < 40
THEN 1 END) AS opgivet
, COUNT(CASE WHEN durationofconversation = 0
AND durationofcall >= 40
THEN 1 END) AS ubesvaret
FROM V2tickets
WHERE siteid = @.site
AND calltype = @.calltype
AND LEN(digits) = 4
GROUP BY digits
ORDER BY digits
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||> However, in your case there's a faster method to get the various counts:
> SELECT digits
> , COUNT(*) AS total
> , COUNT(CASE WHEN durationofconversation > 0
> THEN 1 END) AS besvaret
> , COUNT(CASE WHEN durationofconversation = 0
> AND releasecouse = 'OC'
> THEN 1 END) AS optaget
> , COUNT(CASE WHEN durationofconversation = 0
> AND releasecouse = 'RL'
> AND durationofcall < 40
> THEN 1 END) AS opgivet
> , COUNT(CASE WHEN durationofconversation = 0
> AND durationofcall >= 40
> THEN 1 END) AS ubesvaret
> FROM V2tickets
> WHERE siteid = @.site
> AND calltype = @.calltype
> AND LEN(digits) = 4
> GROUP BY digits
> ORDER BY digits
>
Hi Hugo
Hey, that was an excellent suggestion, that simplifies this quite a lot, and
it even works ;o)
I was think about, if it is possible to do calculations on the result
"fields" e.g
unidentifiedcalls = total - (besvaret+optaget+opgivet+ubesvaret)
or
percent besvaret = (total-unidentifiedcalls)/100 * besvaret
This is like a hole new world, I used to do this "manually" in my
application.
regards
Henry|||On Tue, 15 Feb 2005 19:06:48 +0100, Henry wrote:
>Hey, that was an excellent suggestion, that simplifies this quite a lot, an
d
>it even works ;o)
>I was think about, if it is possible to do calculations on the result
>"fields" e.g
>unidentifiedcalls = total - (besvaret+optaget+opgivet+ubesvaret)
>or
>percent besvaret = (total-unidentifiedcalls)/100 * besvaret
Hi Henry,
That is possible, but not directly. Since the column aliases are applied
after the select clause is evaluated (conceptually at least - optimizers
may and will change order of evaluation, but without changing the result),
you can't use these aliases is any part of the SELECT.
The workaround is to use a derived table.
So this query would fail:
SELECT Column1 + Column2 AS Sum1
Column1 - Column2 AS Diff1,
Sum1 * Diff1 AS Prod1
FROM SomeTable
But this query would work
SELECT Sum1, Diff1,
Sum1 * Diff1 AS Prod1
FROM (SELECT Column1 + Column2 AS Sum1
Column1 - Column2 AS Diff1
FROM SomeTable) AS D
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)
Showing posts with label char. Show all posts
Showing posts with label char. Show all posts
Wednesday, March 28, 2012
Friday, March 9, 2012
query to find a sum of char values present in different rows
the table is
col1
---
M
S
S
Q
L
I got the answer
col1
---
M
S
S
Q
L
The expected Result is
Name
----
MSSQL
I need a single query which solves the above problem. please help me out?
Quote:
Originally Posted by Anu139
the table is
col1
---
M
S
S
Q
L
The expected Result is
Name
----
MSSQL
I need a single query which solves the above problem. please help me out?
I got the answer
declare @.name varchar(50)
select @.name = coalesce( @.name,'' )+ col1 from tab_name
select @.name 'Name'
Query to concatenate results from multiple rows
I have a database where comments are stored in a separate table where the comment is split into max 80 char lengths and stored in separate rows.
eg.
RecordID Comment
001 This is a comment and the nex
001 t bit of the comment appears o
001 n the next line.
002 This is the start of the next com
002 ment.
I need a SQL query that will put the text back together again.
Many thanks
MUHow do you determine which order the segments should be assembled? Can they be put together in random order, or is there a definite sequence?
Do you want a solution that is simple, but SQL dialect specific, or do you want a generic solution that will work with most/all SQL dialects?
Do you want a solution for a single ID, or does it need to be able to work for the entire table in a single operation?
-PatP|||Pat,
Thanks for the response.
There is a LineNum field in the table to order the comments by.
The solution only needs to work with SQLServer.
Ideally I am looking for a solution that produces an entire set of rows showing details from a master table with the comment appearing from this table as a single field with the RecordID being used as the join field.
MarkU|||Ok, if you need to process multiple rows in a single set operation (ie SELECT statement), the best answer I've got is:CREATE TABLE #phrog (
recordId CHAR(3)
, comment VARCHAR(80)
, lineNum INT)
INSERT INTO #phrog (recordID, comment, lineNum)
SELECT '001', 'This is a comment and the nex', 1
UNION ALL SELECT '001', 't bit of the comment appears o', 2
UNION ALL SELECT '001', 'n the next line.', 3
UNION ALL SELECT '002', 'This is the start of the next com', 1
UNION ALL SELECT '002', 'ment.', 2
SELECT a.recordID, a.comment + Coalesce(b.comment, '') + Coalesce(c.comment, '')
FROM #phrog AS a
LEFT JOIN #phrog AS b
ON (b.recordID = a.recordID
AND b.lineNum = (SELECT Min(z1.lineNum)
FROM #phrog AS z1
WHERE z1.recordID = a.recordID
AND a.lineNum < z1.lineNum))
LEFT JOIN #phrog AS c
ON (c.recordID = a.recordID
AND c.lineNum = (SELECT Min(z1.lineNum)
FROM #phrog AS z1
WHERE z1.recordID = a.recordID
AND b.lineNum < z1.lineNum))
WHERE a.lineNum = (SELECT Min(z0.lineNum)
FROM #phrog AS z0
WHERE z0.recordID = a.recordID)
DROP TABLE #phrogBe forewarned that this code raises the kludge factor of the universe significantly, but it does work.
-PatP|||Many thanks for your help - I will check this out.
What I don't quite understand is that since I don't know upfront how many lines of comments there may be or what is in them, how can I do the UNION statements?
I was hoping that there would be some form of the UNION statement where I could say UNION ALL comment WHERE recordId = n (or similar).
MarkU|||On second thought, lets apply a very "Oracle-ish" solution. You could also use:CREATE TABLE dbo.phrog (
recordId CHAR(3)
, comment VARCHAR(80)
, lineNum INT)
INSERT INTO dbo.phrog (recordID, comment, lineNum)
SELECT '001', 'This is a comment and the nex', 1
UNION ALL SELECT '001', 't bit of the comment appears o', 2
UNION ALL SELECT '001', 'n the next line.', 3
UNION ALL SELECT '002', 'This is the start of the next com', 1
UNION ALL SELECT '002', 'ment.', 2
GO
CREATE FUNCTION dbo.phrogComment(@.recordID CHAR(3))
RETURNS VARCHAR(8000) AS
BEGIN
DECLARE
@.c VARCHAR(8000)
, @.r VARCHAR(8000)
SET @.r = ''
DECLARE z CURSOR FOR SELECT
comment
FROM dbo.phrog
WHERE recordID = @.recordID
ORDER BY lineNum
OPEN z
FETCH z INTO @.c
WHILE 0 = @.@.fetch_status
BEGIN
SET @.r = @.r + @.c
FETCH z INTO @.c
END
CLOSE z
DEALLOCATE z
RETURN @.r
END
GO
SELECT a.recordID, dbo.PhrogComment(a.recordID)
FROM dbo.phrog AS a
GROUP BY a.recordID
DROP FUNCTION dbo.phrogComment
DROP TABLE dbo.phrogThis will grieviously disturb the relational purist (me included), but it will get the job done quickly and simply.
-PatP|||I tried the second bit of code on my own tables, and it almost works perfectly. The problem I have is that the concatenated field being returned is being truncated at 256 total characters/spaces, yet I need it to be larger.
I tried to use a CAST on the PhrogComment(a.ID), as well as changing the VARCHAR sizes for @.c and @.r and the RETURNS value, all to no avail.
Any suggestions on how I could tweak the code to make the result "larger"?
Thanks,
Mark|||'taint the SQL code what's cuttin' ya off. It's the client.
In Query Analyzer:
1) Press shift-control-o to bring up the Options window.
2) Click the results tab.
3) At the right edge, near the middle, type in whatever column width seems kozy but not extravagant.
4) Re-run your query for optimum viewing pleasure!
Sorry if I'm a bit punchy... Things could charitably be described as "interesting" today.
-PatP|||Praise God! I've been losing my mind for the last 24 hours (it's been - how did you say it? - "interesting" :-)
Thanks so much. I should've known to blame it on SQL Query Analyzer - I've had some queries not work (i.e., a query will return 0 rows and throw no errors) in the Analyzer yet the same query works (return the expected results) if cut and pasted into and then run as a stored procedure - go figure.
Then again, I'm an econ major so the problem is probably behind the keyboard...
Mark|||Are you just wanting to do this for one message at a time in your procedure or are you wanting to return several messages.
eg.
RecordID Comment
001 This is a comment and the nex
001 t bit of the comment appears o
001 n the next line.
002 This is the start of the next com
002 ment.
I need a SQL query that will put the text back together again.
Many thanks
MUHow do you determine which order the segments should be assembled? Can they be put together in random order, or is there a definite sequence?
Do you want a solution that is simple, but SQL dialect specific, or do you want a generic solution that will work with most/all SQL dialects?
Do you want a solution for a single ID, or does it need to be able to work for the entire table in a single operation?
-PatP|||Pat,
Thanks for the response.
There is a LineNum field in the table to order the comments by.
The solution only needs to work with SQLServer.
Ideally I am looking for a solution that produces an entire set of rows showing details from a master table with the comment appearing from this table as a single field with the RecordID being used as the join field.
MarkU|||Ok, if you need to process multiple rows in a single set operation (ie SELECT statement), the best answer I've got is:CREATE TABLE #phrog (
recordId CHAR(3)
, comment VARCHAR(80)
, lineNum INT)
INSERT INTO #phrog (recordID, comment, lineNum)
SELECT '001', 'This is a comment and the nex', 1
UNION ALL SELECT '001', 't bit of the comment appears o', 2
UNION ALL SELECT '001', 'n the next line.', 3
UNION ALL SELECT '002', 'This is the start of the next com', 1
UNION ALL SELECT '002', 'ment.', 2
SELECT a.recordID, a.comment + Coalesce(b.comment, '') + Coalesce(c.comment, '')
FROM #phrog AS a
LEFT JOIN #phrog AS b
ON (b.recordID = a.recordID
AND b.lineNum = (SELECT Min(z1.lineNum)
FROM #phrog AS z1
WHERE z1.recordID = a.recordID
AND a.lineNum < z1.lineNum))
LEFT JOIN #phrog AS c
ON (c.recordID = a.recordID
AND c.lineNum = (SELECT Min(z1.lineNum)
FROM #phrog AS z1
WHERE z1.recordID = a.recordID
AND b.lineNum < z1.lineNum))
WHERE a.lineNum = (SELECT Min(z0.lineNum)
FROM #phrog AS z0
WHERE z0.recordID = a.recordID)
DROP TABLE #phrogBe forewarned that this code raises the kludge factor of the universe significantly, but it does work.
-PatP|||Many thanks for your help - I will check this out.
What I don't quite understand is that since I don't know upfront how many lines of comments there may be or what is in them, how can I do the UNION statements?
I was hoping that there would be some form of the UNION statement where I could say UNION ALL comment WHERE recordId = n (or similar).
MarkU|||On second thought, lets apply a very "Oracle-ish" solution. You could also use:CREATE TABLE dbo.phrog (
recordId CHAR(3)
, comment VARCHAR(80)
, lineNum INT)
INSERT INTO dbo.phrog (recordID, comment, lineNum)
SELECT '001', 'This is a comment and the nex', 1
UNION ALL SELECT '001', 't bit of the comment appears o', 2
UNION ALL SELECT '001', 'n the next line.', 3
UNION ALL SELECT '002', 'This is the start of the next com', 1
UNION ALL SELECT '002', 'ment.', 2
GO
CREATE FUNCTION dbo.phrogComment(@.recordID CHAR(3))
RETURNS VARCHAR(8000) AS
BEGIN
DECLARE
@.c VARCHAR(8000)
, @.r VARCHAR(8000)
SET @.r = ''
DECLARE z CURSOR FOR SELECT
comment
FROM dbo.phrog
WHERE recordID = @.recordID
ORDER BY lineNum
OPEN z
FETCH z INTO @.c
WHILE 0 = @.@.fetch_status
BEGIN
SET @.r = @.r + @.c
FETCH z INTO @.c
END
CLOSE z
DEALLOCATE z
RETURN @.r
END
GO
SELECT a.recordID, dbo.PhrogComment(a.recordID)
FROM dbo.phrog AS a
GROUP BY a.recordID
DROP FUNCTION dbo.phrogComment
DROP TABLE dbo.phrogThis will grieviously disturb the relational purist (me included), but it will get the job done quickly and simply.
-PatP|||I tried the second bit of code on my own tables, and it almost works perfectly. The problem I have is that the concatenated field being returned is being truncated at 256 total characters/spaces, yet I need it to be larger.
I tried to use a CAST on the PhrogComment(a.ID), as well as changing the VARCHAR sizes for @.c and @.r and the RETURNS value, all to no avail.
Any suggestions on how I could tweak the code to make the result "larger"?
Thanks,
Mark|||'taint the SQL code what's cuttin' ya off. It's the client.
In Query Analyzer:
1) Press shift-control-o to bring up the Options window.
2) Click the results tab.
3) At the right edge, near the middle, type in whatever column width seems kozy but not extravagant.
4) Re-run your query for optimum viewing pleasure!
Sorry if I'm a bit punchy... Things could charitably be described as "interesting" today.
-PatP|||Praise God! I've been losing my mind for the last 24 hours (it's been - how did you say it? - "interesting" :-)
Thanks so much. I should've known to blame it on SQL Query Analyzer - I've had some queries not work (i.e., a query will return 0 rows and throw no errors) in the Analyzer yet the same query works (return the expected results) if cut and pasted into and then run as a stored procedure - go figure.
Then again, I'm an econ major so the problem is probably behind the keyboard...
Mark|||Are you just wanting to do this for one message at a time in your procedure or are you wanting to return several messages.
Subscribe to:
Posts (Atom)