说我有这样一张桌子:
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection; // Connecting the command to the connection.
command.CommandText = "insert into [TestEntry] (TestTableID, StudentID, DateOfTest, [DeadLine]) values(" + int.Parse(txtTestName.Text) + ", " + int.Parse(studentID) + ", #" + this.dateTimeStartD.Text + "# , #" + this.dateTimeEndD.Text + "#)";
command.ExecuteNonQuery();
connection.Close();
我想输出一个看起来像这样的表;
++++++++++++++++++
NAME |PRODUCT
Bob |Apple
Bob |Orange
Bob |Banana
Amy |Apple
Amy |Watermelon
Chris |Orange
++++++++++++++++++
我该怎么做? 我知道我可以创建列作为临时字段但是如何编写代码来获取这些值?
答案 0 :(得分:1)
这应该做你想要的:
select p.name, count(distinct p.product) as numproducts,
count(distinct p2.name) as numpeople
from products p left join
products p2
on p.product = p2.product and p.name <> p2.name
group by p.name;
这似乎是一个相当昂贵的查询。但是,没有什么比这更简单了。
答案 1 :(得分:0)
with c as (select count(a.name) cnt ,a.name from test a where exists(select 1 from test b where a.product =b.product and a.name<>b.name)
group by a.name)
SELECT A.NAME, COUNT(A.name) products_bought, cnt
ppl_buying_same_product FROM TEST A JOIN c on(a.name = c.name)
group by A.name,cnt;