1

SQLServerデータベースの一意の行を表示したい。とという2つのテーブルがUploadedDataありDummyReportDetailます。これらのテーブルを結合したいのですが、distinctを使用して一意の行を取得できません。

これは私の質問です:

SELECT DISTINCT 
    D.TokenNo, D.UploadDate AS "Date", D.[Time], 
    D.SMSContent AS [Message], D.SMSType AS [SMS Type], 
    (SELECT COUNT(d.MobNo) FROM dbo.DummyReportDetail AS d 
     WHERE d.UploadDate = '31/12/2012' AND d.UserID = '2' 
       AND d.UserType = 'Reseller' AND d.TokenNo = U.TokenNo) AS "Count",
    D.DStatus AS "Status" 
FROM    
    dbo.DummyReportDetail AS D 
INNER JOIN 
    dbo.UploadedData AS U ON D.TokenNo = U.TokenNo 
WHERE 
    D.UploadDate = '31/12/2012' AND D.UserID = '2' 
    AND D.UserType = 'Reseller' AND D.TokenNo = U.TokenNo  
ORDER BY 
    D.TokenNo DESC

このクエリの出力は次のとおりです。

Token-4 31/12/2012   11:23:59 AM   संपादकीय Unicode  2  SUBMITED
Token-4 31/12/2012   11:24:9 AM    संपादकीय Unicode  2  SUBMITED
Token-3 31/12/2012   10:21:31 AM   TestSMS Normal  1 SUBMITED

しかし、私はこの出力が欲しいです:

Token-4 31/12/2012  11:24:9 AM  संपादकीय    Unicode  2  SUBMITED
Token-3 31/12/2012 10:21:31 AM  TestSMS    Normal   1  SUBMITED

助けてください...よろしくお願いします

4

2 に答える 2

1
 select distinct D.TokenNo, D.UploadDate as "Date",D.[Time], D.SMSContent as "Message",D.SMSType as "SMS Type", 
        (select count (d.MobNo) from dbo.DummyReportDetail as d 
           where d.UploadDate='31/12/2012' and d.UserID='2' 
           and d.UserType='Reseller' and d.TokenNo=U.TokenNo) as "Count"
          ,D.DStatus as "Status"
    from dbo.DummyReportDetail as D join dbo.UploadedData as U
    on D.TokenNo=U.TokenNo 
    where D.UploadDate='31/12/2012' and D.UserID='2' 
    and D.UserType='Reseller' and D.TokenNo=U.TokenNo  group by D.tokenNo  order by D.TokenNo desc

TokenNoでグループを追加

于 2012-12-31T08:57:30.533 に答える
0

必要なものを実現するには、グループ化を適用する必要があります。この場合、すべての列をグループ化し、最大日付を取得する必要があります。

select D.TokenNo, D.[Time], D.SMSContent as "Message",D.SMSType as "SMS Type", 
(select count (d.MobNo) from dbo.DummyReportDetail as d 
   where d.UploadDate='31/12/2012' and d.UserID='2' 
   and d.UserType='Reseller' and d.TokenNo=U.TokenNo) as "Count"
  ,D.DStatus as "Status", MAX(D.UploadDate) as "Date"
from dbo.DummyReportDetail as D join dbo.UploadedData as U
on D.TokenNo=U.TokenNo 
where D.UploadDate='31/12/2012' and D.UserID='2'
group by D.TokenNo, D.[Time], D.SMSContent, D.SMSType, Count, D.DStatus
and D.UserType='Reseller' and D.TokenNo=U.TokenNo  order by D.TokenNo desc

さらに、「カウント」フィールドの現在の内部クエリを置き換えるためにグループ化を使用するようにクエリを書き直すことを検討することもできます。テーブルの結合は、結合が1回実行されるため、現在のクエリよりも効率的ですが、サブクエリは、結果セットに含まれる行ごとに1回実行されます。

于 2012-12-31T08:57:50.080 に答える