Splitting a row based on a condition - sql-server

i have a scenario. In a table i got 2 fields which checks for a patients. For example, if a patient has 'A' or if a patient has 'B'.
But when i pull the record, i want 2 records, one for A and one for B. I know this could be done using UNION ALL but is there a different way to do that because UNION ALL will be way too bulky code for me. Please help!
Example would be like this.
In Table Patients
PatientID Name Age HasA HasB
1234 Sad 18 Yes Yes
4567 Happy 40 Yes No
8901 confused 25 No Yes
so my query should return:
PatientID Name Age Has
1234 Sad 18 A
1234 Sad 18 B
4567 Happy 40 A
8901 confused 25 B
so a sort of duplicate row if it has both A and B.

Although I would assume union all would perform better, but at least this is one way to do it:
select PatientID, Name, Age, t2.Code as Has
from Table1 t1
left outer join Table2 t2 on
((t2.Code = 'A' and t2.Yes = T1.HasA) or
(t2.Code = 'B' and t2.Yes = T1.HasB))
Example in SQL Fiddle
Table2 is just a simple table that has A, Yes and B, Yes rows.

Try.
Fiddle Demo
select PatientId,Name,
(case
when HasA='Yes' Then 'A'
End) as Has
from Table1 where HasA='Yes'
Union All
select PatientId,Name,
(case
when HasB='Yes' Then 'B'
End) as Has
from Table1 where HasB='Yes'

Related

SSRS2008 - "GroupBy" in .rdl causes losing such data groupby filter repeating others not

I have a stored procedure in Sql Server 2008. The logic of the SP is looking like:
select Name, Surname, sum(Age)
From (
(Select t1.Name,t1.Surname,t1.sum(Age) as Age
From Table1 t1
group by t1.Name, t1.Surname) mt1
full outer join (Select t2.Name as N,t2.Surname as S,t2.sum(Age) as A
From Table2 t2
group by t2.Name, t2.Surname) mt2
on mt1.Name = mt2.N and mt1.Surname = mt2.S
) t
where sum(Age) <= 100
So this is returning something like:
Name Surname Age
A B 12
X B 13
C D 15
I have an SSRS matrix like:
Description Age
<<Expr>> sum([Age])
where Expr is: IIF(Parameters!check.Value=1,Fields!Name.Value,Fields!Surname.Value)
Without making any groupby under the related row group in SSRS2008, i am able to see it like tkat. However, when I add a groupby filter depending on "Surname", it somehow removes any Surname that occurring with at least 2 different name. So, I see only D 15 row instead of:
B 25
D 15
What is my mistake here? How can I show B 25 at this point as well? (I am supposed to not to change SP if possible)
Any help would be appreciated.

Rank Based on two tables

I need some help to calculate the rank from two table.
Suppose i have two table - table1 and table2.
In table1, i have below info
Disease value
A 20
B 10
C 35
In table2, i have below info
Diseaselist Othervalue
A 20
B 10
D 35
E 20
I want to check here, if A from table1 is available in table 2 then it will get high rank othewise less rank. Here C in table1 has more value than A but it is not available in table2 so it will get less rank than A and B.
Kindly sugges how would i accomplish this.
Regards,
Ratan
You can join the two tables using LEFT JOIN. And to order the rows, use CASE statement.
SELECT a.Disease, a.Value
FROM Table1 a
LEFT JOIN Table2 b
ON a.Disease = b.DiseaseList
ORDER BY CASE WHEN b.DiseaseList IS NULL THEN 1 ELSE 0 END,
a.Value DESC

how to select a row based on a single distinct value

If i have 4 rows and want to select rows based on a single column's distinct values and dont mind which info it uses for the rest of the row, how do i do this? There doesn't seem to be a 'distinct' function for single cols whilst maintaining rest of row data.
eg
Name, value
john 1
john 2
michael 3
michael 5
result
john 1
michael 5
note it could have been john 2 or michael 3, i dont care which row for John or Michael it uses for the rest of the values.
GROUP BY Name and use any aggregate function with the value MIN or MAX since you don't care about the value of it:
SELECT Name, MIN(value)
FROM table
GROUP BY Name
Try this
select a.* from TAbleName a
inner join
(
select distinct name,min(Id) as id from TAbleName
group by name
) as b
on a.name= b.name
and a.id=b.id

Get union of two table and taking data with a condition

I have two tables
table-a
id name
100 asd
101 ass
102 gdd
103 hgf
104 cvd
105 erf
table-b
id filter
100 red
101 blue
100 green
100 yellow
102 black
102 red
103 dark
Table-a is the master table and that have all the id's.but Table two is the one which has 'filter' data.
from these two table I want to find out all those 'id's which does not have minimum 2 filters.
note that table-b does not have all the itemnumbers in table-a, and i want all that itemnumber irrespective of if that is in table-a or table-b.I have tried inner joining these two tables and getting data out but nothing worked.
Select A.ID, A.Name, count(*)
from tableA A
LEFT JOIN tableB B on A.ID = B.ID
Group By A.ID, A.name
having count(*) <= 1
LEFT JOIN gives all records from A and only those in B which match.
The group by ID and name let us count the number of filters found in
each
The having says give me any items with a count less than or
equal to 1. (or less than the minimum 2)
Thus results would be.
101 ass 1
103 hgf 1
104 cvd 0
105 erf 0
select
*
from
table-a a
left join (
select id, count(id) as c from table-b group by id
) v on a.id = v.id
where isnull(v.id, 0) < 2
I think this would work in SQL Server (tested in SQLite and usually the two are fairly compatible when it comes to inline view syntax). But syntax issues aside, inline views can make working with sets easier to visualize.
select TA.id, name
from TA
inner join
(
select id from TA
where not exists (select id from TB where TA.id = TB.id)
UNION
select id from TB
group by id having count(filter) < 2
) as FOO
on TA.id = FOO.id
The default behavior of UNION is to remove duplicates.
The first UNIONed set consists of the ids from table A that have no filter (no counterpart in the filters table B).
The second UNIONed set consists of the ids from the filters table, table B, that have only 1 filter.
We inner join those unioned sets back to Table A to get the entity Name.

SQL Server Finding Duplicates

Say I have a table as follows
Employee RecNumber
Joe Bloggs 123456
Joe Bloggs 123456
Bob Bloggs 123457
Dup Bloggs 123456
And I just want to return all Rec Numbers where 2 People have had the same RecNumber which shouldn't happen.
Note that one person can have same request number multiple times I just want returned where 2 people have the same rec Number
So all I want returned is
123456
select record_number
from my_table
group by record_number
having count(distinct employee) > 1
SELECT A.RECNUMBER,COUNT(B.EMPLOYEE)
(SELECT DISTINCT RECNUMBER FROM YOURTABLE) A,
YOURTABLE B
WHERE A.RECNUMBER = B.RECNUMBER
GROUP BY A.RECNUMBER
HAVING COUNT(B.EMPLOYEE)=2;
Something like this?
SELECT RecNumber FROM Table GROUP BY RecNumber HAVING COUNT(*) > 1
SELECT RecNumber FROM [a table]
GROUP BY RecNumber HAVING COUNT(*) > 1;
Or perhaps
SELECT RecNumber FROM (SELECT RecNumber, Employee
FROM [a table]
GROUP BY RecNumber, Employee HAVING COUNT(*) > 1
) AS x;
You can write something like that, i think it's working :
use *DATABASE_NAME*
go
select *Your_Field*, Nbre=count(*Your_Field*) from *TABLE_NAME*
group by *Your_Field*
having count(*Your_Field*)>1
order by 2 desc
Enjoy

Resources