SQL Server 2008, how to count distinct values that change - sql

I have entities that have 2 answers y/n.
I need to count the number of entities who change answers from 'n' to 'y' between stage1 and stage2.
entity || answer || stage
a || y || 1
a || n || 2
b || y || 1
b || y || 2
c || n || 1
c || n || 1
d || n || 1
d || y || 2
I tried this but this doesn't work (because it counts all entities who change answers)
select
entity, count(distinct answer)
from
myDB
where
stage between '1' and '2'
group by
entity, answer
but I don't understand why this doesn't work, the result comes out all O's
select
entity,
case
when stage = '1' and answer = 'n' and
stage = '2' and answer = 'y' then 1
else 0
end as 'result'
from
myDB
where
stage between '1' and '2'
group by
entity, stage, answer

select count(*)
from myDB s2
where
s2.stage ='2' and s2.answer='y'
and exists (select * from myDB s1
where s1.entity=s2.entity
and s1.stage ='1' and s1.answer='n'
)

select
count(*)
from
[myDb] as [s1]
inner join
[myDb] as [s2]
on
[s1].[entity] = [s2].[entity]
and [s1].[answer] = 'n'
and [s1].[stage] = 1
and [s2].[answer] = 'y'
and [s2].[stage] = 2;
But, it works with your provided data only.
If your have duplicated enities, it does not works, because at this case there is not impossible to identify uniqueness of entity. You need additional data then.
a y 1
a n 2
b y 1
b y 2
c n 1
c n 2
d n 1
d y 2
d n 1
d y 2
Lets make assumption, that same logical entity values are stored one after another. Then you can handle this by using this query:
declare #myDB TABLE
(
[rec_id] int identity(1, 1)
,[entity] varchar(10)
,[answer] varchar(10)
,[stage] int
);
insert into #myDB
(
[entity]
,[answer]
,[stage]
)
select
[entity]
,[answer]
,[stage]
from
[myDB];
select
[s1].[entity]
,count([s1].[entity])
from
#myDB as [s1]
inner join
#myDB as [s2]
on
[s1].[entity] = [s2].[entity]
and [s1].[answer] = 'n'
and [s1].[stage] = 1
and [s2].[answer] = 'y'
and [s2].[stage] = 2
and [s1].[rec_id] = [s2].[rec_id] - 1
group by
[s1].[entity];

select count(*)
from (select entity,stage,answer from myDB) t
pivot (max(answer) for stage in([1],[2])) p
where [1] = 'n' and [2] = 'y'

select count(*)
from (select 1 as x
from myDB
where stage in (1,2)
group by entity
having min(case when stage = 1 then answer end) = 'n'
and max(case when stage = 2 then answer end) = 'y'
) t

Related

Sql query to create following tables

use the below table 1 to generate table 2
Table 1
Col1 A B C
------------------
N1 1 0 0
N2 0 1 0
N3 1 0 0
Table 2
output
new_col
-------
N1 A
N2 B
N3 A
Also how to use Table 2 to generate table 1 above
Following SQL query can be help to get requested output:
SELECT Col1,IF(A=1,'A',IF(B=1,'B','C')) AS result FROM `table_name`;
You can use conditinal with CONCAT() Function :
SELECT CONCAT(col1,' ',
CASE WHEN A = 1 THEN 'A' ELSE '' END,
CASE WHEN B = 1 THEN 'B' ELSE '' END,
CASE WHEN C = 1 THEN 'C' ELSE '' END)
FROM table1
provided you're on a DBMS with brand name such as MySQL, PostGRES, SQL Server. As an example, Oracle DB won't allow using more than two arguments for CONCAT() Function.
In order to create table2, use for most of the DBMS :
CREATE TABLE table2 AS
SELECT CONCAT(col1,' ',
CASE WHEN A = 1 THEN 'A' ELSE '' END,
CASE WHEN B = 1 THEN 'B' ELSE '' END,
CASE WHEN C = 1 THEN 'C' ELSE '' END) AS new_col
FROM table1
except for SQL Server in which prefer using :
SELECT CONCAT(col1,' ',
CASE WHEN A = 1 THEN 'A' ELSE '' END,
CASE WHEN B = 1 THEN 'B' ELSE '' END,
CASE WHEN C = 1 THEN 'C' ELSE '' END) AS new_col
INTO table2
FROM table1
In order to implement a reverse engineering(go back to original table), you need to consider the dialectics for each seperate database to handle string values. Assume you're using MySQL DB, then consider using :
CREATE TABLE table3 AS
SELECT SUBSTRING(new_col,1,instr(new_col,' ')-1) AS col1,
CASE WHEN instr(new_col,'A') > 0 THEN 1 ELSE 0 END AS A,
CASE WHEN instr(new_col,'B') > 0 THEN 1 ELSE 0 END AS B,
CASE WHEN instr(new_col,'C') > 0 THEN 1 ELSE 0 END AS C
FROM table2
where I used a different table name (table3), since table1 already exists.
Demo
Btw, if Oracle DB is the case, then use :
SELECT CONCAT(CONCAT(col1,' '),
CASE WHEN A = 1 THEN 'A' ELSE '' END||
CASE WHEN B = 1 THEN 'B' ELSE '' END||
CASE WHEN C = 1 THEN 'C' ELSE '' END) AS new_col
FROM table1
You can use one case statement. This works in Oracle.
SELECT col1
|| CASE
WHEN A = 1
THEN 'A'
WHEN B = 1
THEN 'B'
WHEN C = 1
THEN 'C'
END
NEW_COL
FROM table1
It is unclear what you want when there are multiple "1"s in a row. This is a simple solution:
select id, A from t where A = 1
union all
select id, B from t where B = 1
union all
select id, C from t where C = 1;
In databases that support lateral joins, I would recommend:
select t.id, v.which
from t cross join lateral
(values ('A', t.A), ('B', B), ('C', C)
) v(which, val)
where val = 1;

case statement if table present

I am having problem in finding table from different database (table sometimes get dropped due to procedure) if the table is present then condition else direct ans as 1
select (case when exists (select * from SysSet.INFORMATION_SCHEMA .TABLES where TABLE_NAME = 'CHQPASS') then (
case when left(tranm.docu_no,1) <>'3' or tranm.docu_dt <'01/01/2015' then 1
when (select top 1 ACHD_KEY from TRAND k where k.TRN_NO = TRANM.TRN_NO and k.DR_CR ='D' ) = 'A000100010002' THEN 1
WHEN (select COUNT(*) from SYSSET..chqpass D where D.COMP_DIR ='PSAGR' and D.DOCU_DT = TRANM.DOCU_DT and D.AMT = TRAND.TOT_AMT ) <> 0
THEN 1
else cast(isnull (trand.RECONCILE,0)as int)
end)
else 1
end )as pend
if table is not present it show the error
Msg 208, Level 16, State 1, Line 2 Invalid object name
'SYSSET..chqpass'.
table not present how can i stop it to see that table
Replace following line
WHEN (select COUNT(*) from SYSSET..chqpass D where D.COMP_DIR ='xyz' and D.DOCU_DT >'01/01/2015' and D.AMT = 2556 ) <> 0
with this line
WHEN EXEC('(select COUNT(*) from SYSSET..chqpass D where D.COMP_DIR =''xyz'' and D.DOCU_DT >''01/01/2015'' and D.AMT = 2556 )') <> 0
It should work this way...
Try this try replacing the sys.tables with INformationschema so you can specify which schema the table is present in.
SELECT
(
CASE WHEN EXISTS(SELECT * FROM SYSSET.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'CHQPASS' AND TABLE_SCHEMA = 'dbo')
THEN (
CASE WHEN LEFT(tranm.docu_no,1) <>'3' or tranm.docu_dt < '01/01/2015' THEN 1
WHEN (SELECT TOP 1 ACHD_KEY FROM TRAND k WHERE k.TRN_NO = TRANM.TRN_NO and k.DR_CR ='D' ) = 'A000100010002' THEN 1
WHEN (select COUNT(*) from SYSSET..chqpass D WHERE D.COMP_DIR ='PSAGR' and D.DOCU_DT = TRANM.DOCU_DT and D.AMT = TRAND.TOT_AMT ) <> 0 THEN 1
ELSE cast(isnull (trand.RECONCILE,0)as int)
END
)
ELSE 1
END
) as pend

Joining 2 queries...error while pulling certain fields out

I am trying to join 2 queries & get certain columns out of the join. But I am getting an error. Can you please help me understand where I am going wrong -
SELECT X.*,Y.* FROM
(
(
SELECT
C1,C2,C3
COUNT(C4) AS CNT -- count
FROM [dbo].[Tb1]
WHERE C1 <> 0 AND -- amount not = zero
C2 = 'F' -- flag
GROUP BY C1,C2,C3
HAVING COUNT(C4) > 1
)X
INNER JOIN
(SELECT * FROM [dbo].[Tb1])Y
ON
X.C1 = Y.C1
AND X.C2 = Y.C2
AND X.C3=Y.C3
AND X.C4=Y.C4
)
The first query helps me get the duplicates & the second query will help me get the other fields out of the same table.
Thanks.
Solution #1:
SELECT X.*,Y.* FROM
--( <-- (1) comment this line
(
SELECT
C1,C2,C3, -- <-- (2) add , after C3
COUNT(C4) AS CNT -- count
FROM [dbo].[Tb1]
WHERE C1 <> 0 AND -- amount not = zero
C2 = 'F' -- flag
GROUP BY C1,C2,C3
HAVING COUNT(C4) > 1
)X
INNER JOIN
(SELECT * FROM [dbo].[Tb1])Y
ON
X.C1 = Y.C1
AND X.C2 = Y.C2
AND X.C3=Y.C3
AND X.CNT=Y.C4 <-- see anir's comment
--) <-- (3) comment this line
Or
Solution #2:
SELECT X.*, Y.*
FROM
(
SELECT
C1,C2,C3,
COUNT(C4) AS CNT -- count
FROM [dbo].[Tb1]
WHERE
C1 <> 0 AND -- amount not = zero
C2 = 'F' -- flag
GROUP BY C1,C2,C3
HAVING COUNT(C4) > 1
) X
INNER JOIN [dbo].[Tb1] Y
ON X.C1 = Y.C1
AND X.C2 = Y.C2
AND X.C3=Y.C3
AND X.CNT=Y.C4 <-- see anir's comment
Note #1: When CNT > 1 and x.C1 , y.C1 contains NULLs then X.C1 = Y.C1 <=> NULL = NULL which is evaluated to UNKNOWN if ANSI_NULLS is ON. This means that these rows will be eliminated from final resultset. The same applies to X.C2 = Y.C2 and X.C3=Y.C3.
SET ANSI_NULLS ON
SELECT CASE WHEN NULL = NULL THEN 1 ELSE 0 END AS T1
SET ANSI_NULLS OFF
SELECT CASE WHEN NULL = NULL THEN 1 ELSE 0 END AS T2
/*
T1
-----------
0
T2
-----------
1
*/
Note #2: "In a future version of SQL Server, ANSI_NULLS will always be ON and any applications that explicitly set the option to OFF will generate an error.".
Or
Solution #3:
SELECT y.*
FROM
(
SELECT x.*, COUNT(x.C4) OVER(PARTITION BY x.C1, x.C2, x.C3) AS CNT -- count
FROM [dbo].[Tb1] x
WHERE
x.C1 <> 0 AND -- amount not = zero
x.C2 = 'F' -- flag
-- AND x.C1 IS NOT NULL AND x.C2 IS NOT NULL AND x.C3 IS NOT NULL ?
) y
WHERE y.CNT > 1 AND y.CNT = y.C4

Counting if data exists in a row

Hey guys I have the below sample data which i want to query for.
MemberID AGEQ1 AGEQ2 AGEQ2
-----------------------------------------------------------------
1217 2 null null
58458 3 2 null
58459 null null null
58457 null 5 null
299576 6 5 7
What i need to do is to lookup the table and if any AGEx COLUMN contains any data then it counts the number of times there is data for that row in each column
Results example:
for memberID 1217 the count would be 1
for memberID 58458 the count would be 2
for memberID 58459 the count would be 0 or null
for memberID 58457 the count would be 1
for memberID 299576 the count would be 3
This is how it should look like in SQL if i query the entire table
1 Children - 2
2 Children - 1
3 Children - 1
0 Children - 1
So far i have been doing it using the following query which isnt very efficient and does give incorrect tallies as there are multiple combinations that people can answer the AGE question. Also i have to write multiple queries and change the is null to is not null depending on how many children i am looking to count a person has
select COUNT (*) as '1 Children' from Member
where AGEQ1 is not null
and AGEQ2 is null
and AGEQ3 is null
The above query only gives me an answer of 1 but i want to be able to count the other columns for data as well
Hope this is nice and clear and thank you in advance
If all of the columns are integers, you can take advantage of integer math - dividing the column by itself will yield 1, unless the value is NULL, in which case COALESCE can convert the resulting NULL to 0.
SELECT
MemberID,
COALESCE(AGEQ1 / AGEQ1, 0)
+ COALESCE(AGEQ2 / AGEQ2, 0)
+ COALESCE(AGEQ3 / AGEQ3, 0)
+ COALESCE(AGEQ4 / AGEQ4, 0)
+ COALESCE(AGEQ5 / AGEQ5, 0)
+ COALESCE(AGEQ6 / AGEQ6, 0)
FROM dbo.table_name;
To get the number of people with each count of children, then:
;WITH y(y) AS
(
SELECT TOP (7) rn = ROW_NUMBER() OVER
(ORDER BY [object_id]) - 1 FROM sys.objects
),
x AS
(
SELECT
MemberID,
x = COALESCE(AGEQ1 / AGEQ1, 0)
+ COALESCE(AGEQ2 / AGEQ2, 0)
+ COALESCE(AGEQ3 / AGEQ3, 0)
+ COALESCE(AGEQ4 / AGEQ4, 0)
+ COALESCE(AGEQ5 / AGEQ5, 0)
+ COALESCE(AGEQ6 / AGEQ6, 0)
FROM dbo.table_name
)
SELECT
NumberOfChildren = y.y,
NumberOfPeopleWithThatMany = COUNT(x.x)
FROM y LEFT OUTER JOIN x ON y.y = x.x
GROUP BY y.y ORDER BY y.y;
I'd look at using UNPIVOT. That will make your wide column into rows. Since you don't care about what value was in a column, just the presence/absence of value, this will generate a row per not-null column.
The trick then becomes mashing that into the desired output format. It could probably have been done cleaner but I'm a fan of "showing my work" so that others can conform it to their needs.
SQLFiddle
-- Using the above logic
WITH HadAges AS
(
-- Find everyone and determine number of rows
SELECT
UP.MemberID
, count(1) AS rc
FROM
dbo.Member AS M
UNPIVOT
(
ColumnValue for ColumnName in (AGEQ1, AGEQ2, AGEQ3)
) AS UP
GROUP BY
UP.MemberID
)
, NoAge AS
(
-- Account for those that didn't show up
SELECT M.MemberID
FROM
dbo.Member AS M
EXCEPT
SELECT
H.MemberID
FROM
HadAges AS H
)
, NUMBERS AS
(
-- Allowable range is 1-6
SELECT TOP 6
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS TheCount
FROM
sys.all_columns AS SC
)
, COMBINATION AS
(
-- Link those with rows to their count
SELECT
N.TheCount AS ChildCount
, H.MemberID
FROM
NUMBERS AS N
LEFT OUTER JOIN
HadAges AS H
ON H.rc = N.TheCount
UNION ALL
-- Deal with the unlinked
SELECT
0
, NA.MemberID
FROM
NoAge AS NA
)
SELECT
C.ChildCount
, COUNT(C.MemberID) AS Instances
FROM
COMBINATION AS C
GROUP BY
C.ChildCount;
Try this:
select id, a+b+c+d+e+f
from ( select id,
case when age1 is null then 0 else 1 end a,
case when age2 is null then 0 else 1 end b,
case when age3 is null then 0 else 1 end c,
case when age4 is null then 0 else 1 end d,
case when age5 is null then 0 else 1 end e,
case when age6 is null then 0 else 1 end f
from ages
) as t
See here in fiddle http://sqlfiddle.com/#!3/88020/1
To get the quantity of persons with childs
select childs, count(*) as ct
from (
select id, a+b+c+d+e+f childs
from
(
select
id,
case when age1 is null then 0 else 1 end a,
case when age2 is null then 0 else 1 end b,
case when age3 is null then 0 else 1 end c,
case when age4 is null then 0 else 1 end d,
case when age5 is null then 0 else 1 end e,
case when age6 is null then 0 else 1 end f
from ages ) as t
) ct
group by childs
order by 1
See it here at fiddle http://sqlfiddle.com/#!3/88020/24

counting records on the same table with different values possibly none sql server 2008

I have a inventory table with a condition i.e. new, used, other, and i am query a small set of this data, and there is a possibility that all the record set contains only 1 or all the conditions. I tried using a case statement, but if one of the conditions isn't found nothing for that condition returned, and I need it to return 0
This is what I've tried so far:
select(
case
when new_used = 'N' then 'new'
when new_used = 'U' then 'used'
when new_used = 'O' then 'other'
end
)as conditions,
count(*) as count
from myDB
where something = something
group by(
case
when New_Used = 'N' then 'new'
when New_Used = 'U' then 'used'
when New_Used = 'O' then 'other'
end
)
This returns the data like:
conditions | count
------------------
new 10
used 45
I am trying to get the data to return like the following:
conditions | count
------------------
new | 10
used | 45
other | 0
Thanks in advance
;WITH constants(letter,word) AS
(
SELECT l,w FROM (VALUES('N','new'),('U','used'),('O','other')) AS x(l,w)
)
SELECT
conditions = c.word,
[count] = COUNT(x.new_used)
FROM constants AS c
LEFT OUTER JOIN dbo.myDB AS x
ON c.letter = x.new_used
AND something = something
GROUP BY c.word;
try this -
DECLARE #t TABLE (new_used CHAR(1))
INSERT INTO #t (new_used)
SELECT t = 'N'
UNION ALL
SELECT 'N'
UNION ALL
SELECT 'U'
SELECT conditions, ISNULL(r.cnt, 0) AS [count]
FROM (
VALUES('U', 'used'), ('N', 'new'), ('O', 'other')
) t(c, conditions)
LEFT JOIN (
SELECT new_used, COUNT(1) AS cnt
FROM #t
--WHERE something = something
GROUP BY new_used
) r ON r.new_used = t.c
in output -
new 2
used 1
other 0
You can do it as a cross-tab:
select
sum(case when new_used = 'N' then 1 else 0 end) as N,
sum(case when new_used = 'U' then 1 else 0 end) as U,
sum(case when new_used = 'O' then 1 else 0 end) as Other
from myDB
where something = something