views:

52

answers:

2

Hi all,

Getting a problem in writing an sql query. two tables:

1st: created patient table                        2nd: already created doc table
     patientid  patientname  docid  workstatus          docid  docname
      1          aaa          2          10               1      ggg
      2          bbb          2          20               2      hhh
      3          ccc          1          10               3      iii 
      4          ddd          3          10
      5          eee          3          20
      6          fff          2          10

expected output:

docname workstatus(10) workstatus(20)
ggg      1               0
hhh      2               1
iii      1               1

can also use temporary tables between the queries

Thanks in advance

+4  A: 

Try this

Full working example

declare @patient as table(
patientID int IDENTITY(1,1) NOT NULL,
patientName varchar(25),
docID int,
workstatus smallint
)

declare @doc as table(
docID int IDENTITY(1,1) NOT NULL,
docname varchar(25)
)

insert into @patient
select 'aaa', 2, 10
union all
select 'bbb', 2, 20
union all
select 'ccc', 1, 10
union all
select 'ddd', 3, 10
union all
select 'eee', 3, 20
union all
select 'fff', 2, 10


insert into @doc
select 'ggg'
union all
select 'hhh'
union all
select 'iii'

select docname, 
      SUM(case when t1.workstatus = 10 THEN 1 ELSE 0 END) as [workstatus(10)],
      SUM(case when t1.workstatus = 20 THEN 1 ELSE 0 END) as [workstatus(20)] 
    from @patient t1
    inner join @doc t2 on t1.docid=t2.docid
    GROUP BY docname
Laramie
Thanks for help but its not showing anything in workstatus column
satwik
Hmmm. Can I see your exact Query?
Laramie
I just got this much and i asked to get the output like this as shown above and they asked me to use the temp tables
satwik
create table patients(patientid,patientname,doctorid,workflowstatus)1 'john' 2 4002 'steve' 2 3703 'dave' 1 4004 'yyyy' 3 4005 'zzzz' 3 3706 'dddd' 2 400create table Doctor(doctorid,doctorname).1 'ravi'2 'kkk'3 'ttt'write a query to displaydoctorname,totalpatientcount(workflowstatus=400),totalpatientcount(workflowstatus=370)expected output:'lll',1,0'kkk',2,1'ttt',1,1Clue:try to think of using temporary tables in between your queries
satwik
thank you very much...its working...... thanks a lot......
satwik
This looks like homework. I'm not sure what your prof meant by "try to think of using temp tables". If the answer helped you learn, please mark it as correct!
Laramie
exactly its a homework only....im just new to this technology and i just signed in to this community......
satwik
A: 
Select  d.docname, 
        SUM(case when c.workstatus = 10 THEN 1 ELSE 0 END) as [WorkStatus(10)],
        SUM(case when c.workstatus = 20 THEN 1 ELSE 0 END) as [WorkStatus(20)] 
from created_patient_table c
inner join already_created_doc_table d on c.docid=d.docid
group by d.docid,d.docname
Misnomer
Thanks for helpin but its not showing anything in the workstatus columns.....
satwik