2017-05-22 5 views
0

Ich habe eine Frage zu SQL-Abfragen. Was ich eigentlich brauche, ist, alle Benutzer auszuwählen und ihre 3 bevorzugten Kategorien auszuwählen. Für jetzt, ich teile es zwei verschiedene Anfragen:SQL-Abfrage. Wie man sie kombiniert

async.waterfall([ 
    cbUsersArray => { 
     sqlRequest(` 
        SELECT st.id as studentID, st.firstName as studentFirstName, st.email as studentEmail 
        FROM dbo.Students st 
        WHERE st.isActive = 1 
        AND st.deleted = 0 
        AND IsNull(st.firstName, '') != '' 
        AND IsNull(st.email, '') != '' 
       `, (sqlErr, sqlRes) => { 
     if(sqlErr){ 
      cbUsersArray(sqlErr) 
     } else { 
      cbUsersArray(null, sqlRes) 
     } 
     }) 
    }, 
    (usersArray, cbUsersArrayWithFavCats) => { 
     async.eachLimit(usersArray, asyncEachLimit, (u, cb) => { 
     sqlRequest(`SELECT TOP 3 bts.BrandCategoryID as catID FROM Students st 
        JOIN SaleView ss ON (st.ID=ss.userID) 
        JOIN Sales sa ON (sa.ID=ss.SaleID) 
        JOIN Brands b ON (b.ID=sa.BrandID) 
        JOIN KEY_BrandcategoryToSale bts ON (bts.SaleID=sa.ID) 
        WHERE st.ID = ${u['studentID']} 
        GROUP BY bts.BrandCategoryID 
        ORDER BY COUNT(bts.BrandCategoryID) desc`,(sqlErr, sqlRes) => { 
      if(sqlErr){ 

      } else { 

      } 
      cb() 
     }) 
     },() => { 

     }) 
    } 
    ],() => { 

    }) 

Gibt es einen Vorschlag, wie ich es in einer Abfrage kombinieren und ähnliches Ergebnis hat?

+----+-----------+-----------+-------------+------------------------------+ 
| ID | StudentID | FirstName | Email |  FavoriteCategories  | 
+----+-----------+-----------+-------------+------------------------------+ 
| 1 | 123456 | Edward | [email protected] | [{c1ID:1},{c2ID:2},{c3ID:3}] | 
+----+-----------+-----------+-------------+------------------------------+ 

ODER

+----+-----------+-----------+-------------+--------+--------+--------+ 
| ID | StudentID | FirstName | Email | Cat1ID | Cat2ID | Cat3ID | 
+----+-----------+-----------+-------------+--------+--------+--------+ 
| 1 | 123456 | Edward | [email protected] |  1 |  2 |  3 | 
+----+-----------+-----------+-------------+--------+--------+--------+ 
+3

Zeigen Sie uns Probenergebnisse aus den aktuellen Abfragen und wie sie sollen kombiniert werden. (So ​​gut formatierter Text.) – jarlh

+0

Hallo, so etwas. –

+0

Wenn Sie nur die Students-Tabelle mit der zweiten sql-Anweisung verbinden, erhalten Sie drei Zeilen (eine für jede Kategorie), die Sie für jeden Student analysieren können. – Sourcery

Antwort

0

Setzen Sie die zweite Abfrage in einer gemeinsamen Tabelle expresions und verwendet, die in Ihrer ersten Abfrage. Verwenden Sie Zeilennummer, um die erste, zweite und dritte beliebteste Kategorie zu finden. Verwenden Sie Outer-Joins für den Fall, dass ein Student weniger als drei Kategorien haben kann.

with categories_cte as 
(
    SELECT row_number() over (partition by st.id order by COUNT(bts.BrandCategoryID) desc) rn, 
     st.id , bts.BrandCategoryID 
    FROM Students st 
    JOIN SaleView ss ON (st.ID=ss.userID) 
    JOIN Sales sa ON (sa.ID=ss.SaleID) 
    JOIN Brands b ON (b.ID=sa.BrandID) 
    JOIN KEY_BrandcategoryToSale bts ON (bts.SaleID=sa.ID) 
    GROUP BY st.id, bts.BrandCategoryID 
) 

SELECT st.id as studentID, st.firstName as studentFirstName, st.email as studentEmail, 
    c1.brandCategoryID cat1ID,c2.brandCategoryID cat2ID,c3.brandCategoryID cat3ID 
FROM dbo.Students st 
left join categories_cte c1 on st.id = c1.id and c1.rn = 1 
left join categories_cte c2 on st.id = c2.id and c2.rn = 2 
left join categories_cte c3 on st.id = c3.id and c3.rn = 3 
WHERE st.isActive = 1 AND st.deleted = 0 
    AND IsNull(st.firstName, '') != '' AND IsNull(st.email, '') != '' 
+0

Danke, es funktioniert. Aber ich weiß, ich habe ein anderes Problem. Die lange Ausführungszeit und Server DTU 96-100%. –

0

könnten Sie versuchen, die folgende Abfrage

WITH favorites 
    AS (SELECT id, 
       MAX(CASE 
         WHEN row_seq = 1 THEN brandcategoryid 
        END) AS cat1ID, 
       MAX(CASE 
         WHEN row_seq = 2 THEN brandcategoryid 
        END) AS cat2ID, 
       MAX(CASE 
         WHEN row_seq = 3 THEN brandcategoryid 
        END) AS cat3ID 
     FROM (SELECT st.id, 
         bts.brandcategoryid, 
         ROW_NUMBER() OVER ( 
          PARTITION BY st.id 
          ORDER BY bts.categorycount DESC) row_seq 
       FROM students st 
         JOIN saleview ss 
          ON (st.id = ss.userid) 
         JOIN sales sa 
          ON (sa.id = ss.saleid) 
         JOIN brands b 
          ON (b.id = sa.brandid) 
         JOIN (SELECT saleid, 
            COUNT(brandcategoryid) AS CategoryCount 
           FROM key_brandcategorytosale 
           GROUP BY saleid) bts 
          ON (bts.saleid = sa.id)) data 
     WHERE row_seq <= 3 
     GROUP BY id) 
SELECT st.id  AS studentID, 
     st.firstname AS studentFirstName, 
     st.email  AS studentEmail, 
     favorites.cat1id, 
     favorites.cat2id, 
     favorites.cat3id 
FROM dbo.students st 
     LEFT JOIN favorites 
       ON st.id = favorites.id 
WHERE st.isactive = 1 
     AND st.deleted = 0 
     AND ISNULL(st.firstname, '') != '' 
     AND ISNULL(st.email, '') != ''