我正在尝试使用python 3.7中的sqlalchemy引擎运行两个sql查询。但是,我在连接两个查询的结果列时遇到麻烦。有没有一种有效的方法可以对MSSQL执行此操作?
以下是正在查询的表
timestamp startX startY Number
2019-05-13-10:31 695 384 0
2019-05-13-10:32 3914 256 25ZLH3300MEPACC16x25
2019-05-13-10:32 3911 442 25ZLH3300MEPACC16x25
2019-05-13-10:32 3904 2109 25ZLH3300MEPACC16x25
2019-05-13-10:32 3910 627 25ZLH3300MEPACC16x25
2019-05-13-10:32 3904 1445 25ZLH3300MEPACC16x25
我需要将其作为输出
timestamp startX startY Number Quantity
2019-05-13-10:31 695 384 0 1
2019-05-13-10:32 3914 256 25ZLH3300MEPACC16x25 5
第一个查询根据Number返回唯一的记录,如下所示
SELECT * FROM
(SELECT
[timestamp]
,[startX]
,[startY]
,[Number]
,ROW_NUMBER() OVER(Partition by [Table].Number,
[Table].Number,
type order by [timestamp] DESC) rownumber
FROM [Table]) a WHERE rownumber = 1
第二个查询将重复记录的计数作为数量列和数字列返回。
SELECT [Table].Number, count(*) AS 'Quantity'
FROM [Table]
GROUP BY [TABLE].Number
HAVING count(*) >= 1
我想基于数字作为主键来连接查询1的结果和查询2的列数。
connection = engine.connect()
connection.execute(""" Query """)
答案 0 :(得分:2)
您可以在单个查询中执行此操作,例如:
SELECT
*
FROM
(
SELECT
[timestamp]
,[startX]
,[startY]
,[Number]
,ROW_NUMBER()
OVER (PARTITION BY [Table].Number,
[Table].type
ORDER BY [timestamp] DESC
)
AS rownumber
,COUNT(*)
OVER (PARTITION BY [Table].Number
)
AS Quantity
FROM
[Table]
)
a
WHERE
rownumber = 1
AND quantity > 1
答案 1 :(得分:0)
您可以尝试使用pandas
并加入DataFrames:
import pandas as pd
import sqlalchemy
engine = slqalchemy.create_enging(my_sql_settings)
df_unique_records = pd.read_sql(sql=my_query_1, conn=engine, index_col=Number)
df_duplicate_counts = pd.read_sql(sql=my_query_2, conn=engine, index_col=Number)
df = df_unique_records.merge(df_duplicate_counts, left_index=True, right_index=True)