Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used Union to select multiple table in one crystal report using one table in dataSet, how i display data from max date to min date
Posted
Comments
Jawad Ahmed Tanoli 2-Feb-16 3:44am    
you can do in query ?where is problem

When you combine result sets using UNION the ORDER BY part must be on the last query and it will apply to all the combined record sets...
If you have to apply different ORDER BY to each and every query (like when you have TOP(n) in them so ORDER BY makes all the differences), you have to enclose the queries like this:
SQL
SELECT * FROM (SELECT fld1, fld1 FROM tbl1 ORDER BY fld1))
UNION  
SELECT * FROM (SELECT fld1, fld1 FROM tbl2 ORDER BY fld1))
 
Share this answer
 
Comments
Member 12244977 2-Feb-16 3:58am    
this is code how i add order by here :-

select Cars.Car_id,convert(varchar, Change_date,101) as Change_date , Service_Type.Servce_desc, Current_dist,Prev_distance as Prev_dist,(Current_dist - Prev_distance) as Change_dist,value_Tashhem as Value,NULL as [Qty] from Tashhem s1 inner join cars on s1.Car_id= Cars.Car_id inner join Service_Type on s1.Servce_id = Service_Type.Servce_id where s1.Car_id = 555 and s1.Change_date between 1-1-2000 and 1-1-2016
union
select Cars.Car_id,convert(varchar, Change_date,101) as Change_date, Service_Type.Servce_desc,Current_Dist as Current_dist,Prev_Dist as Prev_dist,(Current_dist - Prev_Dist) as Change_dist,Value_Oil as Value,QTY as Qty from Change_Oil s1 inner join cars on s1.Car_id= Cars.Car_id inner join Service_Type on s1.Servce_id = Service_Type.Servce_id where s1.Car_id =555 and s1.Change_date between 1-1-2000 and 1-1-2016
union
select Cars.Car_id,convert(varchar, Change_Date,101) as Change_date, Service_Type.Servce_desc,Current_dist ,Prev_dist ,(Current_dist - Prev_dist) as Change_dist,Value_Fuel as Value,Qty from Full_Fuel s1 inner join cars on s1.Car_id= Cars.Car_id inner join Service_Type on s1.Servce_id = Service_Type.Servce_id where s1.Car_id =555 and s1.Change_date between 1-1-2000 and 1-1-2016
union
select Cars.Car_id,convert(varchar, Change_date,101) as Change_date, Service_Type.Servce_desc,Current_dist ,Prev_dist ,(convert(float,Current_dist) - convert(float,Prev_dist)) as Change_dist,value_tire as Value,NULL as [Qty] from Change_Tire s1 inner join cars on s1.Car_id= Cars.Car_id inner join Service_Type on s1.Servce_id = Service_Type.Servce_id where s1.Car_id =555 and s1.Change_date between 1-1-2000 and 1-1-2016
union
select Cars.Car_id,convert(varchar, Change_Date,101) as Change_date, Service_Type.Servce_desc,Current_dist ,Prev_dist ,(convert(float,Current_dist) - convert(float,Prev_dist)) as Change_dist,Value_Batteries as Value,NULL as [Qty] from Change_Batteries s1 inner join cars on s1.Car_id= Cars.Car_id inner join Service_Type on s1.Servce_id = Service_Type.Servce_id where s1.Car_id =555 and s1.Change_date between 1-1-2000 and 1-1-2016
union
select Auto_Parts.Car_id,convert(varchar, Auto_Parts.Change_date,101) as Change_date,Service_Type.Servce_desc,Auto_Parts.Current_dist,Auto_Parts.Prev_dist,(convert(float,Current_dist) - convert(float,Prev_dist)) as Change_dist,(s1.Value_Part) as Value,NULL as [Qty] from Auto_Part_Details s1 inner join Auto_Parts on s1.Job_Card = Auto_Parts.Job_Card inner join Service_Type on Auto_Parts.Servce_id = Service_Type.Servce_id where Auto_Parts.Car_id = 555 and Auto_Parts.Change_date between 1-1-2000 and 1-1-2016
SQL
SELECT  * 
FROM 
        (
            SELECT *, sortby FROM tblA
            UNION ALL 
            SELECT *, sortby FROM tblB
        ) tbl
ORDER   BY sortby DESC
 
Share this answer
 
Comments
Member 12244977 2-Feb-16 4:18am    
why order by not work here?

select Auto_Parts.Car_id,convert(varchar, Auto_Parts.Change_date,101) as Change_date,Service_Type.Servce_desc,Auto_Parts.Current_dist,Auto_Parts.Prev_dist,(convert(float,Current_dist) - convert(float,Prev_dist)) as Change_dist,(s1.Value_Part) as Value,NULL as [Qty] from Auto_Part_Details s1 inner join Auto_Parts on s1.Job_Card = Auto_Parts.Job_Card inner join Service_Type on Auto_Parts.Servce_id = Service_Type.Servce_id where Auto_Parts.Car_id = 555 and Auto_Parts.Change_date between 1-1-2000 and 1-1-201 order by s1.Change_date
Jawad Ahmed Tanoli 2-Feb-16 9:10am    
there is no union in your query
try
order by convert(varchar, Auto_Parts.Change_date,101)
Jawad Ahmed Tanoli 2-Feb-16 9:15am    
select * from (select Cars.Car_id,convert(varchar, Change_date,101) as Change_date , Service_Type.Servce_desc, Current_dist,Prev_distance as Prev_dist,(Current_dist - Prev_distance) as Change_dist,value_Tashhem as Value,NULL as [Qty] from Tashhem s1 inner join cars on s1.Car_id= Cars.Car_id inner join Service_Type on s1.Servce_id = Service_Type.Servce_id where s1.Car_id = 555 and s1.Change_date between 1-1-2000 and 1-1-2016
union
select Cars.Car_id,convert(varchar, Change_date,101) as Change_date, Service_Type.Servce_desc,Current_Dist as Current_dist,Prev_Dist as Prev_dist,(Current_dist - Prev_Dist) as Change_dist,Value_Oil as Value,QTY as Qty from Change_Oil s1 inner join cars on s1.Car_id= Cars.Car_id inner join Service_Type on s1.Servce_id = Service_Type.Servce_id where s1.Car_id =555 and s1.Change_date between 1-1-2000 and 1-1-2016
union
select Cars.Car_id,convert(varchar, Change_Date,101) as Change_date, Service_Type.Servce_desc,Current_dist ,Prev_dist ,(Current_dist - Prev_dist) as Change_dist,Value_Fuel as Value,Qty from Full_Fuel s1 inner join cars on s1.Car_id= Cars.Car_id inner join Service_Type on s1.Servce_id = Service_Type.Servce_id where s1.Car_id =555 and s1.Change_date between 1-1-2000 and 1-1-2016
union
select Cars.Car_id,convert(varchar, Change_date,101) as Change_date, Service_Type.Servce_desc,Current_dist ,Prev_dist ,(convert(float,Current_dist) - convert(float,Prev_dist)) as Change_dist,value_tire as Value,NULL as [Qty] from Change_Tire s1 inner join cars on s1.Car_id= Cars.Car_id inner join Service_Type on s1.Servce_id = Service_Type.Servce_id where s1.Car_id =555 and s1.Change_date between 1-1-2000 and 1-1-2016
union
select Cars.Car_id,convert(varchar, Change_Date,101) as Change_date, Service_Type.Servce_desc,Current_dist ,Prev_dist ,(convert(float,Current_dist) - convert(float,Prev_dist)) as Change_dist,Value_Batteries as Value,NULL as [Qty] from Change_Batteries s1 inner join cars on s1.Car_id= Cars.Car_id inner join Service_Type on s1.Servce_id = Service_Type.Servce_id where s1.Car_id =555 and s1.Change_date between 1-1-2000 and 1-1-2016
union
select Auto_Parts.Car_id,convert(varchar, Auto_Parts.Change_date,101) as Change_date,Service_Type.Servce_desc,Auto_Parts.Current_dist,Auto_Parts.Prev_dist,(convert(float,Current_dist) - convert(float,Prev_dist)) as Change_dist,(s1.Value_Part) as Value,NULL as [Qty] from Auto_Part_Details s1 inner join Auto_Parts on s1.Job_Card = Auto_Parts.Job_Card inner join Service_Type on Auto_Parts.Servce_id = Service_Type.Servce_id where Auto_Parts.Car_id = 555 and Auto_Parts.Change_date between 1-1-2000 and 1-1-2016) as t order by t.Change_date
Try this...
C#
CrystalReport1.SelectionFormula = "{Exp.ETD} In Date(" & Format(fromDateVariableHere, "yyyy,mm,dd") & ") To Date(" & Format(toDateVariableHere, "yyyy,mm,dd"))"
 
Share this answer
 

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