我目前在使用标准SQL查询时遇到问题。我有一封电子邮件列表,其中每封电子邮件都可以具有多种功能。有关表格的外观,请参见下面的示例。
Email Function
peter@gmail.com engineer
peter@gmail.com specialist
dave@gmail.com analyst
dave@gmail.com tester
dave@gmail.com manager
michael@gmail.com intern
我想要的是一个查询,该查询返回具有找到的第一个功能的每封电子邮件一次。因此,上表应返回以下内容:
Email Function
peter@gmail.com engineer
dave@gmail.com analyst
michael@gmail.com intern
我该怎么做?
我现在拥有的是查询的简化版本。
SELECT Email, Function
FROM database
GROUP BY Email, Function
这里的问题是,我必须将Email和Function都放在GROUP BY中。如果仅将电子邮件放入“分组依据”查询中,即使我只希望将查询发送到“分组依据”电子邮件,也无法运行。
谢谢!
答案 0 :(得分:1)
使用row_number()
窗口功能
select * from
(
select *, row_number() over(partition by email order by funcion) as rn
from tablename
)a where rn=1
答案 1 :(得分:1)
使用using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class AudioPeer : MonoBehaviour
{
AudioSource _audioSource;
public static float[] _samples = new float[2048];
public static float[] _freqBand = new float[10];
public static float[] _bandBuffer = new float[10];
float[] _bufferDecrease = new float[10];
// Start is called before the first frame update
void Start()
{
_audioSource = GetComponent<AudioSource>();
}
void Update()
{
GetSpectrumAudioSource();
makeFrequencyBands();
BandBuffer();
}
void BandBuffer()
{
for (int i = 0; i < _freqBand.Length; ++i)
{
if (_freqBand[i] > _bandBuffer[i])
{
_bandBuffer[i] = _freqBand[i];
_bufferDecrease[i] = 0.005f;
}
if (_freqBand[i] < _bandBuffer[i])
{
_bandBuffer[i] -= _bufferDecrease[i];
_bufferDecrease[i] *= 1.3f;
}
}
}
void GetSpectrumAudioSource()
{
_audioSource.GetSpectrumData(_samples, 0, FFTWindow.Blackman);
}
void makeFrequencyBands()
{
int currentBin = 0;
for (int i = 0; i < 10; i++)
{
float average = 0;
int windowSize = (int)Mathf.Pow(2, i) * 2;
print(currentBin);
for (int j = 0; j < windowSize; j++)
{
average += _samples[currentBin] * (currentBin + 1);
currentBin++;
}
average /= windowSize;
_freqBand[i] = average * 10;
}
}
}
分析函数
row_number()
答案 2 :(得分:1)
没有“第一”功能,因为SQL表表示无序集,尤其是在像BigQuery这样从头开始设计的并行数据库中。 >
您需要使用某种聚合功能。
一个简单的是any_value()
:
SELECT Email, ANY_VALUE(Function)
FROM database
GROUP BY Email;
如果您还有另一列指定顺序,则可以使用它来获取与该列的最小值关联的function
。
答案 3 :(得分:0)
您需要指定列排序的排序列,然后您可以执行以下操作:
select t.*
from table t
where t.pk = (select min(t1.pk) from table t1 where t1.email = t.email);
这里假设pk
是标识列。
答案 4 :(得分:0)
您是否考虑过BigQuery导航功能? 例如,FIRST_VALUE。 您可以在文档here中进行查看,并可能执行以下操作:
SELECT Email,
FIRST_VALUE(Function)
OVER (PARTITION BY Email ORDER BY x ) AS First_Function
FROM database
但是,其他注释提示订购问题。
因此,您将需要某种排序变量(x),例如时间,甚至是索引。