i am trying to take the difference of the counts of 2 sub queries. But i get the following error. I executed the two sub queries separately and it works fine. The error may be because way i try to convert them to a single query. Any ideas?
select
(select count(*) FROM TESTEA.AIT_P_NUMBER where
rtrim(ltrim(translate(PNU_POL,' ','1234567890'))) = '')
-
(select count(*) FROM TESTEA.AIT_P_NUMBER )
Can you try selecting from DUAL:
SELECT
(SELECT COUNT(*) FROM TESTEA.AIT_P_NUMBER
WHERE RTRIM(LTRIM(TRANSLATE(PNU_POL, ' ', '1234567890'))) = '') -
(SELECT COUNT(*) FROM TESTEA.AIT_P_NUMBER)
FROM DUAL;
Actually, there is a single query workaround you can use involving conditional aggregation:
SELECT
SUM(CASE WHEN RTRIM(LTRIM(TRANSLATE(PNU_POL, ' ', '1234567890'))) = ''
THEN 1 ELSE 0 END) - COUNT(*)
FROM TESTEA.AIT_P_NUMBER
Solution 1 :
values (SELECT COUNT(*) FROM TESTEA.AIT_P_NUMBER where trim(translate(PNU_POL,' ','1234567890')) = '')
- (SELECT COUNT(*) nb FROM TESTEA.AIT_P_NUMBER)
Solution 2 :
SELECT sum(case when trim(translate(PNU_POL,' ','1234567890')) = '' then 1 else 0 end) - count(*)
FROM TESTEA.AIT_P_NUMBER
Solution 3 :
select (SELECT COUNT(*) FROM TESTEA.AIT_P_NUMBER where trim(translate(PNU_POL,' ','1234567890')) = '')
- (SELECT COUNT(*) nb FROM TESTEA.AIT_P_NUMBER)
from SYSIBM.SYSDUMMY1
Solution 4 :
select sum(nb) from
(
select count(*) nb
from TESTEA.AIT_P_NUMBER where trim(translate(PNU_POL,' ','1234567890')) = ''
union all
select -count(*) nb
from TESTEA.AIT_P_NUMBER
) nb
Related
My Query:
select table.attribute, count(table.attribute) AS cnt from table
group by table.attribute
order by cnt desc;
The output is something like:
attribute | cnt
-----------+-----
A | 2
B | 2
G | 1
F | 1
But i only want the max values (A & B).
You can do this with a single level of nesting:
select attribute,
cnt
from (
select attribute,
count(*) AS cnt,
max(count(*)) over () as max_cnt
from t
group by attribute
) t
where cnt = max_cnt;
You can use the power of CTE to achieve this:
WITH count_values
AS (SELECT table.attribute,
Count(table.attribute) AS cnt
FROM table
GROUP BY table.attribute),
max_values
AS (SELECT Max(cnt) AS max_cnt
FROM (SELECT cnt
FROM count_values) sub)
SELECT *
FROM count_values cv
JOIN max_values mv
ON mv.max_cnt = cv.cnt;
you can use rank as below
with cte as (
select *, Rank() over(order by cnt desc) as rnk from yourattribute
) select * from cte where rnk = 1
I have a sql query to form a parent/child structure to a tree-like view, the outcome is like this:
lvl1a
lvl1a/lvl2a
lvl1a/lvl2b
lvl1b/lvl2a/lvl3a
lvl1c
lvl1d/lvl2a/lvl3a/lvl4a
...
the query itself doesn't have a limited range, for instance, if i only want to get this tree-like view for the first and second level
can someone modify the sql query to add such function? tks
;with cte as
(
select
labelID,
Title,
ParentLevel,
cast(Title as varchar(max)) as [treePath]
from TestTable
where ParentLevel = 0
union all
select
t.labelID,
t.Title,
t.ParentLevel,
[treePath] + '/' + cast(t.Title as varchar(255))
from
cte
join TestTablet on cte.labelID = t.ParentLevel
)
select
labelID,
Title,
ParentLevel,
[treePath]
from cte
order by treePath
All we did here was add lvl 0 for the first part of the union in the CTE
then increment it by 1 each time the recursion occurs (after the union all)
then add a where clause to the select to eliminate levels beyond 2.
Though I find it odd this works since t isn't aliased in your code...
.
;with cte as
(
select
labelID,
Title,
ParentLevel,
cast(Title as varchar(max)) as [treePath],
0 as lvl
from TestTable
where ParentLevel = 0
union all
select
t.labelID,
t.Title,
t.ParentLevel,
[treePath] + '/' + cast(t.Title as varchar(255)),
cte.lvl+1 as lvl
from
cte
join TestTablet t on cte.labelID = t.ParentLevel
)
select
labelID,
Title,
ParentLevel,
[treePath]
from cte
where lvl <=2
order by treePath
I want to SUM the COUNT values from 2 COUNT statements on different tables.
I tried:
SELECT(
SELECT COUNT(*) FROM articlegroups
UNION
SELECT COUNT(*) FROM emails
)
as t
I tried:
SELECT(
SELECT COUNT(*) FROM articlegroups
+
SELECT COUNT(*) FROM emails
)
as t
I tried:
SELECT SUM(
SELECT COUNT(*) FROM articlegroups
+
SELECT COUNT(*) FROM emails
)
as t
I don't know what else to try...
You didn't try:
SELECT SUM(cnt) as cnt FROM
(
SELECT COUNT(*) as cnt FROM articlegroups
UNION ALL
SELECT COUNT(*) as cnt FROM emails
) t
Or:
SELECT (SELECT COUNT(*) FROM articlegroups) +
(SELECT COUNT(*) FROM emails) as cnt
You can use:
SELECT (SELECT COUNT(*) FROM articlegroups) +
(SELECT COUNT(*) FROM emails) AS cnt
If either articlegroups or emails can be empty, then you should also use COALESCE:
SELECT COALESCE((SELECT COUNT(*) FROM articlegroups),0) +
COALESCE((SELECT COUNT(*) FROM emails),0) AS cnt
I have a field called "Users", and I want to run SUM() on that field that returns the sum of all DISTINCT records. I thought that this would work:
SELECT SUM(DISTINCT table_name.users)
FROM table_name
But it's not selecting DISTINCT records, it's just running as if I had run SUM(table_name.users).
What would I have to do to add only the distinct records from this field?
Use count()
SELECT count(DISTINCT table_name.users)
FROM table_name
SQLFiddle demo
This code seems to indicate sum(distinct ) and sum() return different values.
with t as (
select 1 as a
union all
select '1'
union all
select '2'
union all
select '4'
)
select sum(distinct a) as DistinctSum, sum(a) as allSum, count(distinct a) as distinctCount, count(a) as allCount from t
Do you actually have non-distinct values?
select count(1), users
from table_name
group by users
having count(1) > 1
If not, the sums will be identical.
You can see for yourself that distinct works with the following example. Here I create a subquery with duplicate values, then I do a sum distinct on those values.
select DistinctSum=sum(distinct x), RegularSum=Sum(x)
from
(
select x=1
union All
select 1
union All
select 2
union All
select 2
) x
You can see that the distinct sum column returns 3 and the regular sum returns 6 in this example.
You can use a sub-query:
select sum(users)
from (select distinct users from table_name);
SUM(DISTINCTROW table_name.something)
It worked for me (innodb).
Description - "DISTINCTROW omits data based on entire duplicate records, not just duplicate fields." http://office.microsoft.com/en-001/access-help/all-distinct-distinctrow-top-predicates-HA001231351.aspx
;WITH cte
as
(
SELECT table_name.users , rn = ROW_NUMBER() OVER (PARTITION BY users ORDER BY users)
FROM table_name
)
SELECT SUM(users)
FROM cte
WHERE rn = 1
SQL Fiddle
Try here yourself
TEST
DECLARE #table_name Table (Users INT );
INSERT INTO #table_name Values (1),(1),(1),(3),(3),(5),(5);
;WITH cte
as
(
SELECT users , rn = ROW_NUMBER() OVER (PARTITION BY users ORDER BY users)
FROM #table_name
)
SELECT SUM(users) DisSum
FROM cte
WHERE rn = 1
Result
DisSum
9
If circumstances make it difficult to weave a "distinct" into the sum clause, it will usually be possible to add an extra "where" clause to the entire query - something like:
select sum(t.ColToSum)
from SomeTable t
where (select count(*) from SomeTable t1 where t1.ColToSum = t.ColToSum and t1.ID < t.ID) = 0
May be a duplicate to
Trying to sum distinct values SQL
As per Declan_K's answer:
Get the distinct list first...
SELECT SUM(SQ.COST)
FROM
(SELECT DISTINCT [Tracking #] as TRACK,[Ship Cost] as COST FROM YourTable) SQ
I just want it to return
1
2
WITH
CTE1 AS
(
select value
UNION ALL
select value=value+1
FROM CTe1
WHERE value =2
)
select * from cte1
how come that doesnt work.
The following will print out 1, 2:
WITH
CTE1 AS
(
select 1 as value
UNION ALL
select value=value+1
FROM CTe1
WHERE value = 1
)
select * from cte1
The problem was that value was not defined for your first CTE clause. I assume you wanted 1. Then the second CTE clause self-references the first one and adds 1.
The CTE will print 1 and 2 as rows. Is this what you are after?
WITH
CTE1 AS
(
select 1 as value
UNION ALL
select value=value+1
FROM CTe1
WHERE value < 2
)
select * from cte1
here is a code to search a string for a character macth thank you all.
Begin
with recursiveCTE(matchNumber, foundAt) as (
select 1, charindex(#toFind, #ToSearch, 0)
union all
select matchNumber + 1, charindex(#toFind, #ToSearch, foundAt + 1)
from recursiveCTE where foundAt > 0
)
select
matchNumber as "Match Number",
(case when foundAt = 0 then null else foundAt end) as "Found At"
from recursiveCTE
where foundAt 0 or matchNumber = 1
;
end;