1

元のユーザーのデータをほぼ複製するビューを作成していますが、ユーザーがダウンロード テーブルに持っている発生回数を入れたい属性もあります。

CREATE VIEW customer_count_streams_vw(
sid_customer
systemuser_id
postcode
age
gender
num_streams) 
AS 
SELECT
user.sid_customer,
user.systemuser_id,
user.postcode,
user.age,
user.gender,
num_streams 
FROM md_customer 
INNER JOIN ods_streaming AS num_streams (
SELECT COUNT(ods_streaming.sid_customer) 
WHERE ods_streaming.sid_customer = md_customer.sid_customer)

私が望むのは、パーツの結果を配置することです:

SELECT COUNT(ods_streaming.sid_customer) 
WHERE ods_streaming.sid_customer = md_customer.sid_customer

num_streamsフィールドに。

4

3 に答える 3

1

あなたのクエリは

SELECT
user.sid_customer,
user.systemuser_id,
user.postcode,
user.age,
user.gender,
num_streams 
FROM md_customer 
INNER JOIN 
( 
        SELECT sid_customer, COUNT(ods_streaming.sid_customer) num_streams 
        FROM ods_streaming group by sid_customer
) AS ods_streaming  
ON ods_streaming.sid_customer = md_customer.sid_customer

上記のクエリは、md_customer の行と ods_streaming の行を持つ顧客の行を返します。すべての顧客とその数 (0 を含む) が必要な場合、クエリは次のようになります。

SELECT
cust.sid_customer,
cust.systemuser_id,
cust.postcode,
cust.age,
cust.gender,
COUNT(strm.sid_customer) num_streams 
FROM 
   md_customer cust
LEFT OUTER JOIN ods_streaming  strm   
   ON cust.sid_customer = strm.sid_customer
group by 
cust.sid_customer,
cust.systemuser_id,
cust.postcode,
cust.age,
cust.gender
于 2012-10-17T18:04:13.753 に答える
0

たぶん、サブセレクトの代わりにグループ化をカウントに使用することができます。

SELECT
md_customer.sid_customer,
md_customer.systemuser_id,
md_customer.postcode,
md_customer.age,
md_customer.gender,
count(ods_streaming.num_streams)
FROM md_customer 
INNER JOIN ods_streaming 
on ods_streaming.sid_customer = md_customer.sid_customer
group by 1,2,3,4,5;

このようなサブ選択を行うことは避けるべきです...このグループ化により、物事が少し速くなるはずです

于 2012-10-17T18:08:59.243 に答える
0
SELECT
    u.sid_customer,
    u.systemuser_id,
    u.postcode,
    u.age,
    u.gender,
    num_streams.amount
FROM 
    md_customer u INNER JOIN (
            SELECT 
                ods_streaming.sid_customer,
                COUNT(ods_streaming.sid_customer) as amount 
            FROM
                ods_streaming 
            GROUP BY ods_streaming.sid_customer

        ) num_streams  ON ( num_streams.sid_customer = u.sid_customer )

さらに、user は、すべてではないにしても、ほとんどのデータベース エンジンで予約語です。

于 2012-10-17T18:09:14.717 に答える