如果匹配日期,我有两张桌子(dw和点击)并使用foreach
加入它。
两个表都有日期2016-06-01(dw表有1条记录,点击表有2条记录)。我想在{0}表格中#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
typedef int buffer_item;
#define BUFFER_SIZE 5
#define TRUE 1
buffer_item START_NUMBER;
buffer_item item;
int counter;
int insert_item(buffer_item item);
int remove_item(buffer_item *item);
buffer_item buffer[BUFFER_SIZE];
void* producer(void *ptr);
void* consumer(void *ptr);
pthread_cond_t condc, condp;
pthread_mutex_t mutex;
int sleepTime, producerThreads, consumerThreads, a, item;
pthread_attr_t attr;
sem_t full, empty;
int insert_item(buffer_item item)
{
if(counter < BUFFER_SIZE) {
buffer[counter] = item;
counter++;
return 0;
}
else {
return -1;
}
}
int remove_item(buffer_item *item)
{
if(counter > 0) {
*item = buffer[(counter-1)];
printf("consumer%u consumed %d\n", (unsigned int)pthread_self(),buffer[counter-1]);
counter--;
return 0;
}
else {
return -1;
}
}
void* producer(void *ptr) {
while(TRUE) {
sleep(sleepTime);
sem_wait(&empty);
pthread_mutex_lock(&mutex);
if(insert_item(START_NUMBER)) {
fprintf(stderr, "error \n");
}
else {
printf("producer%u produced %d\n", (unsigned int)pthread_self(),START_NUMBER);
START_NUMBER++;
}
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
}
void* consumer(void *ptr) {
while(TRUE) {
sleep(sleepTime);
sem_wait(&full);
pthread_mutex_lock(&mutex);
if(remove_item(&item)) {
fprintf(stderr, "error \n");
}
else {
// printf("consumer%u consumed %d\n", (unsigned int)pthread_self(),&START_NUMBER);
}
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
}
void initializeData() {
pthread_mutex_init(&mutex, NULL);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
pthread_attr_init(&attr);
counter = 0;
}
int main(int argc, char **argv) {
sleepTime = atoi(argv[1]);
producerThreads = atoi(argv[2]);
consumerThreads = atoi(argv[3]);
START_NUMBER = atoi(argv[4]);
item = START_NUMBER;
initializeData();
pthread_t pro, con;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&condc, NULL);
pthread_cond_init(&condp, NULL);
for(a=0; a< consumerThreads;a++)
pthread_create(&con, NULL, consumer, NULL);
for(a=0;a<producerThreads;a++)
pthread_create(&pro, NULL, producer, NULL);
pthread_join(con, NULL);
pthread_join(pro, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&condc);
pthread_cond_destroy(&condp);
sleep(sleepTime);
}
列winloss,但由于点击表有2条记录,日期为2016-06-01,并导致winloss双和。但我只想在dw表中总结winloss。有什么建议吗?请参考下图。
答案 0 :(得分:0)
您可以使用临时表或公用表表达式执行此操作。这是一个临时的。
If object_id('tempdb..#temp') is not null drop table #temp
select
sum(winloss) as winloss,
updated
--add any other columns you want
into #temp
group by
updated
--add other columns to group
select
t.updated,
t.winloss,
c.*
from #temp t
left join click on c.trandate = t.updated
答案 1 :(得分:0)
试试这个:
SELECT *
FROM DW D
INNER JOIN CLICK C
ON C.CLICKED = D.CLICKCOUNT
AND C.TRANDATE = D.UPDATED
答案 2 :(得分:-1)
试试这个
SELECT dw.[updated]
,sum(distinct [winloss])
from dw,Click
where dw.updated = Click.trandate
FROM dw, Click
group by dw.updated