简体   繁体   English

SQL和分组依据,选择问题

[英]SQL and group by, select issues

Hello and sorry for uninformative title. 您好,抱歉标题不详。 Could not think of better. 想不到更好。 So, my problem is thus. 所以,我的问题就是这样。 I have two key variables in two tables, table1 has only key1 and table2 has both key1 and key2 to link key1 to key2. 我在两个表中有两个键变量,table1仅具有key1,table2同时具有key1和key2来将key1链接到key2。 Table1 also has a year-specific variable. 表1还具有特定于年份的变量。

Now here is the problem. 现在这是问题所在。 What I want to know is the list of all key1 variables that are linked to such key2 variables that have more than one key1 variable on same year variable. 我想知道的是链接到此类key2变量的所有key1变量的列表,这些变量在同一个year变量上具有多个key1变量。 So table1 has say key1 cases A and B on year 2000 that are linked to one and same key2 variable in table2. 因此,table1在2000年说出key1案例A和B,它们链接到table2中的一个和相同的key2变量。 So in effect, I am interested in key1 doubles of a single key2 on specific year. 因此,实际上,我对特定年份的单个key2的key1双精度感兴趣。

SELECT query.key2
FROM (
    SELECT DISTINCT a.key1, b.key2, a.year
    FROM table1 AS a JOIN table2 AS b ON a.key1=b.key1
    WHERE a.year IS NOT NULL
    )
    AS query
GROUP BY query.key2, query.year
HAVING COUNT(*)>1

This is the code I have used to get the list of key2 cases that have more than 1 key1 case linked to it for same year. 这是我用来获取与同一年关联的key1案例超过1个的key2案例列表的代码。 To get the list of all key1 cases linked to these key2 cases it is simple to add that as a subquery for a query on table2. 要获取链接到这些key2案例的所有key1案例的列表,只需将其添加为table2上查询的子查询即可。 But that results with ALL the key1 cases linked to that specific key2. 但这是所有与该特定key2关联的key1案例的结果。 What I want are specifically the same year doubles. 我想要的是同年翻倍。 I am dumbstruck at this seemingly easy task, I can't have key in the SELECT because it is not listed in the group by clause and using key2 cases in subquery again won't filter out that non-wanted key1 cases from different years. 我对这个看似简单的任务不知所措,我无法在SELECT中使用键,因为它没有在group by子句中列出,并且再次在子查询中使用key2例不会过滤掉不同年份的不需要的key1例。 Any ideas? 有任何想法吗?

Notulysses, that doesn't quite do the trick. Notuslys,这并不能解决问题。 It manages to group key1 and key2 cases under desired required year but links too many key1 cases, ie. 它设法将key1和key2案例分组到所需的要求年份以下,但是链接了太多key1案例,即。 linking key1 to key2 becomes unconnected of year. 将key1链接到key2将在一年中断开连接。

Coder of code: table1 would be like 代码编码器:table1就像

key 1 year plus bunch of uninteresting variables here 关键的1年加上一堆无趣的变量

Table 2: 表2:

key1 key2 plus few other uninteresting variables. key1 key2以及其他一些无关紧要的变量。

And what I essentially want is a list of such key1 when key1 cases A and B, both in year 2000, are linked to one key2. 我本质上想要的是当2000年的key1案例A和B都链接到一个key2时的此类key1的列表。 Badly explained, I know, but English is not my primary language and without visual aid the idea is not the simplest to convey I find when guys don't know the tables and data. 我知道不好解释,但是英语不是我的主要语言,没有视觉帮助,这个想法并不是我不容易理解的。

Anyway, I managed to do the trick. 无论如何,我设法做到了。 What I essentially did was first create temp table where I saved all key2 cases that have more than one case linked to it for one year. 我本质上所做的就是首先创建临时表,在其中我将与一个以上案例相关联的所有key2案例保存了一年。 Then II used DENSE_RANK-function and flagged year,key2 doubles with it and rest was easy. 然后II使用DENSE_RANK函数并标记年份,key2翻倍,其余部分变得简单。 As such: 因此:

CREATE TABLE #temp2 (key2 VARCHAR(50), key1 VARCHAR(50), year DATE, dr INT)
INSERT INTO #temp2
SELECT b.key2, a.key1, a.year,
DENSE_RANK() OVER (ORDER BY b.key2, a.year DESC) AS dr
FROM table1 AS a JOIN table2 AS b ON a.key1=b.key1
WHERE b.key2 IN (SELECT key2 FROM #temp1) --#temp1 being table with key2's from previous query
ORDER BY b.key2, a.year DESC

SELECT *
FROM #temp2
WHERE dr IN (
    SELECT dr
    FROM #temp2
    GROUP BY dr
    HAVING COUNT(*)>1)

It is a clumsy solution but it works. 这是一个笨拙的解决方案,但可以。 Better ideas are welcomed! 欢迎有更好的主意!

SELECT t1.*
     , t2.key1
FROM ( 
    SELECT DISTINCT  b.key2
         , a.year
    FROM table1 a 
    LEFT JOIN table2 b ON a.key1 = b.key1
    WHERE b.key1 IS NOT NULL
    GROUP BY b.key2
           , a.year
    HAVING COUNT(a.key1) > 1) t1
JOIN table2 t2 ON t1.key2 = t2.key2
WITH TABLECTE 
AS
    SELECT DISTINCT a.key1, b.key2, a.year
    FROM   table1 AS a JOIN table2 AS b ON a.key1=b.key1
    WHERE  a.year IS NOT NULL
SELECT TABLECTE.key2 
FROM   TABLECTE
GROUP BY TABLECTE .key2, TABLECTE .year
HAVING COUNT(*)>1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM