我有以下数据库:
Account Month Pass 1 July No 2 July No 3 July Yes 4 July No 5 July Yes 1 August No 2 August Yes 3 August Yes 4 August Yes 5 August Yes
我想要:
1)在7月失败但在8月通过的事情
2)与上一个查询相同的帐号,但在8月而不是7月
因此,先前示例的结果将是:
Account Month 2 July 2 August 4 July 4 August
答案 0 :(得分:0)
如果您可以将给定帐户的结果放在一个行而不是多行中,那么--protocol=TCP --port=3307
应该做您想要的事情:
man mysqldump
也就是说,我强烈建议您按照Tim在评论中的建议,将数据放入带有日期列的单个表中。
答案 1 :(得分:0)
select a.Account from temp a where a.Month = 'July' and a.pass = 'No'
INTERSECT
select a.Account from temp a where a.Month = 'August' and a.pass = 'Yes'
或
select a.Account from temp a where a.Month = 'July' and a.pass = 'No'
and a.Account in (select a.Account from temp a where a.Month = 'August' and a.pass = 'Yes')
或
select * from temp a, temp b
where a.Account = b.Account and a.month <> b.Month
and a.Month = 'July' and a.pass = 'No' and b.Month = 'August' and b.pass = 'Yes'
您可以对问题2使用相同的查询,只需更改条件
答案 2 :(得分:0)
我没有Teradata。以下查询在mssql中运行正常。除非有一些语法更改,否则大多数sql中应该相同
您将需要对帐号(标识符)进行联接,以通过以下表中的表通过失败/通过条件来标识帐号,
select select a.AccountNumber from dbo.MonthlyResultAugust a join dbo.MonthlyResultJuly b on a.AccountNumber=b.AccountNumber where lower(a.Pass)='yes' and lower(b.Pass)='no';
然后对于所需的输出,您可以合并两个源表并从上一个查询中提取与帐号匹配的数据。 组合查询看起来像
select AccountNumber, Month from
(select * from MonthlyResultAugust union select * from MonthlyResultJuly) as combined
where accountNumber in
( select a.AccountNumber from MonthlyResultAugust a join MonthlyResultJuly b
on a.AccountNumber=b.AccountNumber where lower(a.Pass)='yes' and lower(b.Pass)='no') order by AccountNumber;
答案 3 :(得分:0)
这是我的解决方案:
SELECT Account, [Month]
FROM pass_table t
WHERE [Month] = 'July' AND Pass = 'No'
AND EXISTS (SELECT 1 FROM pass_table [Month] = 'August' AND Pass = 'Yes' AND Account = t.Account)
UNION
SELECT Account, [Month]
FROM pass_table t
WHERE [Month] = 'August' AND Pass = 'Yes'
AND EXISTS (SELECT 1 FROM pass_table [Month] = 'July' AND Pass = 'No' AND Account = t.Account)