Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, sql experts

I have two tables called CommissioningDetails and MachineTechnicalDetails

CommissioningDetails:

MachineNumber  CommisioningStartDate  CommisioningEndDate

7001           01/01/2013              21/01/2013
7001           01/01/2013              21/02/2013 
7002           02/02/2013              02/02/2013


MachineTechnicalDetails:
MachineNumber  machineType
7001             ssp
7002             mmt


If i pass jan from UI

I want a output like this
MachineType total  Completed Pending
ssp           2      1           1
mmt           1      1           -  


here total is count of commissioning startdate in the month of jan and

completed is count of (commissioningStartDate and comissioningEndDate) in the month of jan

Can any one help me to solve this issue

Thanks in advance
(Keerthi Kumar)
Posted
Updated 27-Aug-13 21:37pm
v4

1 solution

Your Explanation is Different and the Output which you have shown is Different...
in Explanation u wanted only those belonging to month 'Jan' but in the output u have shown records for 'Feb' Month too...
Anyway... try this... i think you are looking for this kind of output:
SQL
Create Table #CommisionDetails(MachineNumber Nvarchar(40),CommisioningStartDate Date,CommisioningEndDate Date)
Insert into   #CommisionDetails
Select '7001','2013-01-01','2013-01-21' Union All
Select '7001','2013-01-01','2013-02-21' Union All
Select '7002','2013-02-02','2013-02-02'

Create Table #MachineTechDetails (MachineNumber NVarchar(40),MachineType Nvarchar(40))
Insert into #MachineTechDetails
Select '7001','ssp' Union
Select '7002','mmt'

Select mt.MachineType,isnull(a.Started,0) as Total,Isnull(b.Completed,0) as Completed,a.Started-isnull(b.Completed,0) as Pending From 
	(Select MachineNumber,count(MachineNumber) as Started from #CommisionDetails 
	Where Convert(Varchar(3),DateName(Month,CommisioningStartDate),0) ='Jan' 
	Group by MachineNumber
	)a 
LEFT OUTER JOIN 
	(Select MachineNumber,count(MachineNumber) as Completed from #CommisionDetails 
	Where Convert(Varchar(3),DateName(Month,CommisioningStartDate),0) ='Jan' 
	and Convert(Varchar(3),DateName(Month,CommisioningEndDate),0) ='Jan' 
	Group by MachineNumber
	)b
ON a.MachineNumber=b.MachineNumber 
Inner join #MachineTechDetails mt on a.MachineNumber=mt.MachineNumber 

Drop Table #CommisionDetails
Drop Table #MachineTechDetails

Output:
MachineType	Total	Completed	Pending
------------     --------   ----------  -----------
ssp	           2	     1	          1
 
Share this answer
 
v3
Comments
Keerthi Kumar(Andar) 28-Aug-13 7:36am    
Thanks raja,

if i try to execute your answer Machine type is not grouping
so i have created view as CommissioningChart that view contains
<pre lang="text">
MachineType commissioningStartDate CommissioningEndDate
sst 1/1/2013 1/1/2013
sst 1/1/2013 1/2/2013
mmt 1/2/2013 1/2/2013
mmt 1/2/2013 1/2/2013

</pre>
user will select a month name from drop down list
ill pass startDate as 1/1/2013 and endDate as 31/1/2013
can you please help me to group based on machine type
Completed and pending
Completed will be count of machinetype its start and enddate both present in the selected month
Raja Sekhar S 28-Aug-13 7:53am    
In the Query which i Wrote it is Grouping Based on Machine Type..u Can see the sample Output Showing only one Record for Machine Type... u can even add some Sample Data to the above Query and check the output...
If not please Specify your Table Structures, Keys...
Keerthi Kumar(Andar) 28-Aug-13 8:06am    
Raja

Can you please group the view what i have specified above?
it is not grouping so i have created view as CommissioningChart

Required Output is same
Keerthi Kumar(Andar) 28-Aug-13 8:28am    
Sir your answer is not working can you please help me?
am not getting any ideas its urgent sir please help me....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900