SQL:如何从GROUPBY语句中获取更多信息
考虑以下SQL语句:SQL:如何从GROUPBY语句中获取更多信息,sql,group-by,Sql,Group By,考虑以下SQL语句: SELECT invoice.customer_name, invoice.customer_id, invoice.shop_location, sum(invoice.cost) FROM invoice GROUP BY invoice.customer_name, invoice.customer_id, invoice.shop_location 如果不是发票.shop\u location的值,可以对语句执行哪些操作来标识哪些行与其他行相同?尝试此操作-
SELECT
invoice.customer_name,
invoice.customer_id,
invoice.shop_location,
sum(invoice.cost)
FROM invoice
GROUP BY
invoice.customer_name, invoice.customer_id, invoice.shop_location
如果不是发票.shop\u location的
值,可以对语句执行哪些操作来标识哪些行与其他行相同?尝试此操作-删除发票.ShopLocation
SELECT
invoice.customer_name,
invoice.customer_id,
sum(invoice.cost)
FROM invoice
GROUP BY
invoice.customer_name, invoice.customer_id
这将提供与其余字段匹配的所有行尝试此操作-删除Invoice.ShopLocation
SELECT
invoice.customer_name,
invoice.customer_id,
sum(invoice.cost)
FROM invoice
GROUP BY
invoice.customer_name, invoice.customer_id
Select customer_name, Customer_id, count(Shop_location) from
(
SELECT
invoice.customer_name,
invoice.customer_id,
invoice.shop_location,
sum(invoice.cost)
FROM invoice
GROUP BY
invoice.customer_name,
invoice.customer_id,
invoice.shop_location
)
Group by customer_name, Customer_id
Having count(Shop_location) > 1
这将提供与其余字段匹配的所有行
Select customer_name, Customer_id, count(Shop_location) from
(
SELECT
invoice.customer_name,
invoice.customer_id,
invoice.shop_location,
sum(invoice.cost)
FROM invoice
GROUP BY
invoice.customer_name,
invoice.customer_id,
invoice.shop_location
)
Group by customer_name, Customer_id
Having count(Shop_location) > 1
上述查询将为您提供具有多个店铺位置的Customername和id的组合
注意:我不确定这是否是优化的
上述查询将为您提供具有多个店铺位置的Customername和id的组合
注意:我不确定这是否是优化的也许我的问题不清楚。我想知道结果中存在哪些行只是因为不同的发票。shop_location将计数(不同的发票。shop_location)>1添加到Sachin的查询中正好可以。也许我的问题不清楚。我想知道结果中存在哪些行仅仅是因为不同的发票。shop_location向Sachin的查询添加计数(不同的发票。shop_location)>1
就可以做到这一点。我希望原始查询结果的行中有一个值来识别这一点。在我的头顶上,我想你可以把这个查询和你的查询连接起来,得到你的结果。。。我会研究并发布一些更为冗长的内容…我希望原始查询结果的行中有一个值来标识这一点。在我的脑海中,我认为您可以将此查询与您的查询结合起来以获得结果。。。我会研究和张贴,如果有什么更优雅的。。。