且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

sql查询从三个表中获取数据

更新时间:2023-02-26 09:58:49

  SELECT   CONVERT  varchar  50 ),pds.edatetime, 106 ),AVG(pds.caustic) AS  caustic_average,
AVG(ast_wash_gpl_soda.nt_6o) AS nt_6o_average,AVG(dms1.soliddms) AS soliddms_average
FROM ast_wash_gpl_soda INNER JOIN
dms1 ON CONVERT varchar 50 ),ast_wash_gpl_soda.edatetime, 106 )= CONVERT varchar 50 ),dms1.edatetime, 106 INNER JOIN
pds ON CONVERT varchar 50 ),dms1.edatetime, 106 )= CONVERT varchar 50 ),pds.edatetime, 106
GROUP BY pds.edatetime



您可以使用日期范围的条件

 其中 pds.edatetime   cast之间( @Month   as   varchar  2 ))+ '  / 01 /' + cast( @Year   as   varchar  4 )) DATEADD(DAY,-1,DATEADD(MONTH, 1 ,CAST(强制转换) ( @ Month   as   varchar  2 ))+ '  / 01 /' + cast( @ Year   as   varchar ( 4 )) as   datetime )))


HI


尝试这个



  SELECT  PD.edatetime,AVG(PD.caustic) AS  CAUSTIC,AVG(AW.nt_6o) AS  NT_60,AVG(AW.nt_6u) AS  NT6U,
AVG(DM.sodagpldms) AS SODA

FROM pds PD
INNER JOIN ast_wash_gpl_soda AS AW ON AW.edatetime = PD.edatetime
INNER JOIN dms1 AS DM ON DM.edatetime = PD.edatetime
GROUP BY PD.edatetime







谢谢

MANGESH te>

HI


添加条件

AS MONTH(PD.edatetime)='8',年份(PD.edatetime) )='2014'



谢谢

MANGESH


hello friends ....

i have three tables as follows

create table pds(
edatetime datetime not null,
	caustic decimal(10,5),
	ratio decimal(10,5),
	solid decimal(10,5),
)


create table ast_wash_gpl_soda(
edatetime datetime not null,
	nt_6o decimal(10,5),
	nt_6u decimal(10,5),
)

create table dms1
(
edatetime datetime not null,
	sodagpldms decimal(10,5),
	soliddms decimal(10,5),
)



all three tables have no reationship among them. they just contained edatetime field as common.
i want to generate report in which user will provide month no and year as parameter. i need to fetch data (average of every fields) from these three tables. it should show average of day of every field in month such as follows

date         caustic_average         nt_6o_average        soliddms_average
1-apr-2014      25.00                   23.00                 18.32
2-apr-2014      89.65                   65.47                 56.21
3-apr-2014      25.95                   65.65                 51.71
.
.
.
31-apr-2014     95.36                   68.47                 51.25



caustic_avarage is average for day in that month and year.
i.e 25.00 is a caustic average for 1-apr-2014.

nt_6o_average is average for day in that month and year.
i.e 65.47 is a nt_6o average for 2-apr-2014.

soliddms_average is average for day in that month and year.
i.e 51.71 is a soliddms average for 3-apr-2014.

i wanted to make such query in sql server.
plz help

SELECT CONVERT(varchar(50), pds.edatetime, 106) , AVG(pds.caustic) AS caustic_average,
 AVG(ast_wash_gpl_soda.nt_6o) AS nt_6o_average, AVG(dms1.soliddms) AS soliddms_average
FROM ast_wash_gpl_soda INNER JOIN
dms1 ON CONVERT(varchar(50), ast_wash_gpl_soda.edatetime, 106) = CONVERT(varchar(50), dms1.edatetime, 106) INNER JOIN
pds ON CONVERT(varchar(50), dms1.edatetime, 106) = CONVERT(varchar(50), pds.edatetime, 106)
GROUP BY pds.edatetime


You can use where condition for date range

Where pds.edatetime between cast(@Month as varchar(2)) + '/01/' + cast(@Year as varchar(4)) and DATEADD(DAY, -1, DATEADD(MONTH, 1, CAST(cast(@Month as varchar(2)) + '/01/' + cast(@Year as varchar(4)) as datetime)))


HI
TRY THIS

SELECT PD.edatetime,AVG(PD.caustic ) AS CAUSTIC ,AVG(AW.nt_6o) AS NT_60,AVG(AW.nt_6u) AS NT6U,
   AVG(DM.sodagpldms) AS SODA

  FROM pds PD
INNER JOIN ast_wash_gpl_soda  AS AW ON AW.edatetime=PD.edatetime
INNER JOIN dms1 AS DM ON DM.edatetime=PD.edatetime
GROUP BY PD.edatetime




THANKS
MANGESH


HI
ADD WHERE CONDITION
AS MONTH(PD.edatetime)='8',YEAR(PD.edatetime)='2014'

THANKS
MANGESH