Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ande
Starting Member
1 Post |
Posted - 2009-10-29 : 06:23:25
|
| Hi!Im completely new to SQL and fairly new to PHP too. I've been working on a small database for a school-project and there are two things i'd like to get out of this database using SQL.1) Which wares have been ordered at least two times of the same customer.2) Establish a list with all employees and customers that has the same e-mail.The tables i have in the DB is: Employee, Customer, Wares and Order. The columns in each tables is yet to be set, so if u have a solution feel free to call em whatever u want.Best RegardsAnders Levin |
|
|
Sachin.Nand
2937 Posts |
Posted - 2009-10-29 : 09:01:08
|
| DECLARE @wares AS TABLE(waresid INT ,custid INT)INSERT INTO @waresSELECT 1,1 UNION ALLSELECT 2,1 UNION ALLSELECT 3,1 UNION ALLSELECT 3,2 UNION ALLSELECT 4,3DECLARE @customers AS TABLE(custid INT,name VARCHAR(10),email VARCHAR(10))INSERT INTO @customersSELECT 1,'a','a@a.com'UNION ALLSELECT 2,'b','b@b.com'UNION ALLSELECT 3,'c','a@a.com'SELECT c.custid,c.name FROM @wares wINNER JOIN @customers c ON w.custid=c.custidGROUP BY c.custid,c.nameHAVING COUNT(waresid)>2SELECT custid,email FROM(SELECT custid,email,ROW_NUMBER()OVER(PARTITION BY email ORDER BY custid DESC)AS rowid1,ROW_NUMBER() OVER(PARTITION BY email ORDER BY custid)AS rowid2 FROM @customers c)t WHERE rowid1-rowid2<>0PBUH |
 |
|
|
|
|
|
|
|