2015-04-23 105 views
1

好的,我有3個表格: [AXprod].[dbo].[RMSPOSINVOICE],[AXPROD].[dbo].[discountcard],[IntegrationProd].[dbo].[POS_KvitoGalva]。而且我想知道什麼時候折扣卡在一個發明位置和使用時間中被多次使用過。表[IntegrationProd].[dbo].[POS_KvitoGalva]有這些時間。我用這個代碼來獲取時間使用,每天每張卡是:Sql select count查詢

sELECT a.discountcardid,count(a.discountcardid) 
    FROM [AXprod].[dbo].[RMSPOSINVOICE] a 
    inner join [AXPROD].[dbo].[discountcard] b 
    on a.discountcardid = b.discountcardid 
    inner join [IntegrationProd].[dbo].[POS_KvitoGalva] c 
    on a.possalesid = c.id 
    where a.dataareaid = 'ermi' and len(a.discountcardid) > '0' and b.dataareaid = 'ermi' and ('500' = a.inventlocationid) 
and (a.invoicedate >= '2015-04-22 00:00:00.000' and a.invoicedate <= '2015-04-22 00:00:00.000') 
    group by a.discountcardid,a.inventlocationid,a.posnumber 
    having count(a.discountcardid) > '1' 

,我得到以下結果:

DISCOUNTCARDID COUNT 
123456   2 
145962   2 

和我有一個查詢使用各卡時,發現(日期和時間)

SELECT a.discountcardid,a.inventlocationid,a.posnumber,year,month,day,hour,minute,c.id 
FROM [AXprod].[dbo].[RMSPOSINVOICE] a 
    inner join [AXPROD].[dbo].[discountcard] b 
    on a.discountcardid = b.discountcardid 
    inner join [IntegrationProd].[dbo].[POS_KvitoGalva] c 
    on a.possalesid = c.id 
    where a.dataareaid = 'ermi' and len(a.discountcardid) > '0' and b.dataareaid = 'ermi' and ('500' = a.inventlocationid) 
and (a.invoicedate >= '2015-04-22 00:00:00.000' and a.invoicedate <= '2015-04-22 00:00:00.000') 
    group by a.discountcardid,a.inventlocationid,a.posnumber,year,month,day,hour,minute,c.id 
    order by DISCOUNTCARDID 

,我得到的結果是:

discountcardid inventlocationid posnumber year month day hour minute id 
123456  500 7 2015 4 22 12 44 6355302 
123456  500 7 2015 4 22 14 24 6355302 
145962  500 7 2015 4 22 13 56 6355302 
145962  500 7 2015 4 22 13 24 6355302 
145555  500 7 2015 4 22 12 11 5465465 

問題: 我不想讓只使用的優惠卡一度讓我試試這個:

SELECT a.discountcardid,a.inventlocationid,a.posnumber,year,month,day,hour,minute,c.id, 
(sELECT count(s.discountcardid) 
    FROM [AXprod].[dbo].[RMSPOSINVOICE] s 
    inner join [AXPROD].[dbo].[discountcard] b 
    on s.discountcardid = b.discountcardid 
    inner join [IntegrationProd].[dbo].[POS_KvitoGalva] c 
    on s.possalesid = c.id 
    where s.dataareaid = 'ermi' and len(s.discountcardid) > '0' and b.dataareaid = 'ermi' and ('500' = s.inventlocationid) 
and (s.invoicedate >= '2015-04-22 00:00:00.000' and s.invoicedate <= '2015-04-22 00:00:00.000') and s.DISCOUNTCARDID = a.DISCOUNTCARDID 
    group by s.discountcardid,s.inventlocationid,s.posnumber 
    having count(a.discountcardid) > '1') 
FROM [AXprod].[dbo].[RMSPOSINVOICE] a 
    inner join [AXPROD].[dbo].[discountcard] b 
    on a.discountcardid = b.discountcardid 
    inner join [IntegrationProd].[dbo].[POS_KvitoGalva] c 
    on a.possalesid = c.id 
    where a.dataareaid = 'ermi' and len(a.discountcardid) > '0' and b.dataareaid = 'ermi' and ('500' = a.inventlocationid) 
and (a.invoicedate >= '2015-04-22 00:00:00.000' and a.invoicedate <= '2015-04-22 00:00:00.000') 
    group by a.discountcardid,a.inventlocationid,a.posnumber,year,month,day,hour,minute,c.id 
    order by DISCOUNTCARDID 

但我得到的是相同數量的值和NULL在最後一個字段中的所有列。我希望我讓自己清楚;)。

+0

對不起,但這些SQL語句很難閱讀。如果您對它們進行了更好的格式化,那麼您和我們都可以更輕鬆地找到問題所在。 – jarlh

回答

1

您應該可以調用一次查詢並使用窗口函數來計算。我不相信你可以在where語句中使用分析函數,所以我添加了一個額外的SELECT語句,以便爲計數添加WHERE> 1。

SELECT * 
FROM (SELECT 
    a.discountcardid, 
    a.inventlocationid, 
    a.posnumber, 
    year, 
    month, 
    day, 
    hour, 
    minute, 
    c.id, 
    COUNT(*) OVER (PARTITION BY a.discountcardid, a.inventlocationid, a.posnumber) AS CardCount 
FROM AXprod.dbo.RMSPOSINVOICE a 
JOIN AXprod.dbo.discountcard b 
    ON b.discountcardid = a.discountcardid 
JOIN IntegrationProd.dbo.POS_KvitoGalva c 
    ON c.id = a.possalesid 
WHERE a.dataareaid = 'ermi' 
AND len(a.discountcardid) > '0' 
AND b.dataareaid = 'ermi' 
AND a.inventlocationid = 500 
AND a.invoicedate >= '2015-04-22 00:00:00.000' 
AND a.invoicedate <= '2015-04-22 00:00:00.000' 
) d 
WHERE d.CardCount > 1 
ORDER BY d.discountcardid 
+0

很棒!你能解釋一下這個說法是什麼意思? COUNT(*)OVER(分區由a.discountcardid,a.inventlocationid,a.posnumber)AS CardCount – user2893780

+0

這是一個窗口函數,它允許您爲每行使用函數(MAX,COUNT,SUM等),而不使用通過...分組。在這種情況下,PARTITION BY中的部分基本上是GROUP BY,但您不必像往常一樣將所選列的其餘部分限制在GROUP BY列中。在這個例子中,COUNT計算的是discountcardid,inventlocationid和posnumber的確切分組發生的次數。 simple-talk.com/sql/t-sql-programming/window-functions-in-sql - – hines