比较按行分组的行

时间:2019-06-07 13:34:54

标签: sql sql-server

我有一张表格,其中列出了不同的金额,我希望将来自不同来源的这些金额分组,以查看它们是否匹配。所以我的桌子看起来像这样:

id   name  amount description startDate            endDate              location type sourceId linkId   isMatch
A111 name1 111.11 desc        2019-06-06T00:00:00Z 2019-06-06T00:00:00Z location a    INTERNAL A111X111 True
A111 name1 222.22 desc        2019-06-06T00:00:00Z 2019-06-06T00:00:00Z location b    INTERNAL A111X111 True
A111 name1 555.55 desc        2019-06-06T00:00:00Z 2019-06-06T00:00:00Z location a&b  INTERNAL A111X111 True
A111 name1 444.44 desc        2019-06-06T00:00:00Z 2019-06-06T00:00:00Z location a    EXTERNAL A111X111 True
A111 name1 444.44 desc        2019-06-06T00:00:00Z 2019-06-06T00:00:00Z location b    EXTERNAL A111X111 True

我可以像这样将它们分组:

SELECT
  a.name,
  a.id,
  SUM(a.amount) AS total,
  a.description,
  DATEDIFF(day, a.startDate, a.endDate) AS days,
  a.location,
  a.sourceId,
  a.linkId,
  a.isMatch
FROM
  DataHistory a
GROUP BY
a.name, a.id, a.description, DATEDIFF(day, a.startDate, a.endDate), a.location, a.sourceId, a.linkId, a.isMatch 

获得几乎我想要的东西:

name  id   total  description days location sourceId linkId   isMatch
name1 A111 888.88 desc        0    location EXTERNAL A111X111 True
name1 A111 888.88 desc        0    location INTERNAL A111X111 True

但是我想要的是有一列两个数量(内部和外部)之差的列,所以我实际上希望这两个记录显示在同一行上(而且因为实际上会有很多这样的对这些记录)。在示例中,差异为零,但我将寻找差异不为零的情况。可能是没有外部或内部记录(因此金额为0),或者金额总计为不同的金额。我考虑过可能将分组记录插入到临时表中,然后尝试某种形式的自我联接,但是我不确定这是否在所有情况下都有效,并且如果我实际上有很多这样的记录,那肯定不太有效。

我还在这里创建了一个SQL Fiddle:http://www.sqlfiddle.com/#!18/7a5ff3/1

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

尝试此操作,从import { Router } from '@angular/router'; import { Component } from '@angular/core'; import { LoadingController } from '@ionic/angular'; import { WordPressRestapiService, Post } from '../services/wordpress-restapi.service'; @Component({ selector: 'app-home', templateUrl: 'home.page.html', styleUrls: ['home.page.scss'], }) export class HomePage { categoryId: number; private posts : Post[] = []; constructor( public loadingController: LoadingController, private wordpressService: WordPressRestapiService) { } async ngOnInit() { const loading = await this.loadingController.create(); await loading.present(); this.loadPosts().subscribe((posts: Post[]) => { this.posts = posts loading.dismiss(); }); } loadPosts() { return this.wordpressService.getRecentPosts(this.categoryId); } openPost(postId) { this.router.navigateByUrl('/post/' + postId); } } sourceId中删除group by,然后将总数总计,但是对于select,请选择external。这将为您带来不同:

-a.amount

答案 1 :(得分:1)

您需要旋转。无论如何,在进行分组时,条件聚合是最自然的方法

SELECT
  a.name,
  a.id,
  SUM(CASE a.sourceId WHEN 'INTERNAL' THEN  a.amount ELSE 0 END) AS totalINTERNAL,
  SUM(CASE a.sourceId WHEN 'EXTERNAL' THEN  a.amount ELSE 0 END) AS totalEXTERNAL,
  a.description,
  DATEDIFF(day, a.startDate, a.endDate) AS days,
  a.location,
  a.linkId,
  a.isMatch
FROM
  DataHistory a
GROUP BY
a.name, a.id, a.description, DATEDIFF(day, a.startDate, a.endDate), a.location, a.linkId, a.isMatch