Author |
Topic |
arhicoc
Starting Member
2 Posts |
Posted - 2009-02-05 : 01:12:08
|
Two tables1. Distributors (id, name)- 1 Carrie Bradshaw
- 2 AnnaLynne McCord
- 3 Brad Pitt
- ..............
2. Flyers (id, distributor_id, person_who_received_the_flyer, (datetime)datereceived, (int)received, (int)special)- 1 1 "John" 01/01/2009 1 1
- 2 1 "Michael" 02/01/2009 1 0
- 3 1 "Caine" 02/01/2009 1 1
- 4 1 "Loris" 03/01/2009 1 1
- 5 2 "Carpathian" 03/01/2009 0 0
- ...........
Flyers table already has a number of potential clients.I need to get the total number of flyers for each distribuitor, the total number of flyers given for each distribuitor and the number of deliveries marked as special for every distinct date.something likeCarrie Bradshah- 01/01/2009 total:1, given 1, special 1
- 02/01/2009 total:2, given 2, special 1
- 03/01/2009 total:1, given 1, special 1
AnnaLynne McCord- 03/01/2009 total:1, given:0, special 0
This is what I have but it does not work.SELECT DISTINCT convert(datetime,floor(convert(float, flyers.datereceived))) as datereceived, distribuitors.id, distribuitors.name, COUNT(flyers.id) AS total, SUM(CASE WHEN (dbo.flyers.received = 1) THEN 1 ELSE 0 END) AS received, SUM(CASE WHEN (dbo.flyers.received = 1 AND dbo.flyers.special = 1)) THEN 1 ELSE 0 END) AS specials, FROM distribuitors INNER JOIN flyers ON distribuitors.id = flyers.distribuitor_id GROUP BY flyers.datereceived, distribuitors.id, distribuitors.name ORDER BY distribuitors.name -- SQL Manager 2008 for SQL Server 3.2.0.2-- ----------------------------------------- Host : (local)-- Database : testorama-- Version : Microsoft SQL Server 9.00.1399.06CREATE DATABASE [testorama]ON PRIMARY ( NAME = [testorama], FILENAME = 'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\testorama.mdf', SIZE = 2240 KB, MAXSIZE = UNLIMITED, FILEGROWTH = 1 MB )LOG ON ( NAME = [testorama_log], FILENAME = 'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\testorama_log.LDF', SIZE = 504 KB, MAXSIZE = 0 MB, FILEGROWTH = 10 % )COLLATE SQL_Latin1_General_CP1_CI_ASGOUSE [testorama]GO---- Definition for contract DEFAULT : --CREATE CONTRACT [DEFAULT] AUTHORIZATION [dbo] ([DEFAULT] SENT BY ANY)GO---- Definition for table flyers : --CREATE TABLE [dbo].[flyers] ( [id] int IDENTITY(1, 1) NOT NULL, [distribuitor_id] int NULL, [person_who_received_the_flier] varchar(75) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [datereceived] datetime NULL, [received] int NULL, [special] int NULL)ON [PRIMARY]GO---- Definition for table distributors : --CREATE TABLE [dbo].[distributors] ( [id] int IDENTITY(1, 1) NOT NULL, [name] varchar(75) COLLATE SQL_Latin1_General_CP1_CI_AS NULL)ON [PRIMARY]GO---- Data for table dbo.distributors (LIMIT 0,500)--SET IDENTITY_INSERT [dbo].[distributors] ONGOINSERT INTO [dbo].[distributors] ([id], [name])VALUES (1, N'Carrie Bradshoaw')GOINSERT INTO [dbo].[distributors] ([id], [name])VALUES (2, N'AnnaLynne McCord')GOINSERT INTO [dbo].[distributors] ([id], [name])VALUES (3, N'Brad Pitt')GOSET IDENTITY_INSERT [dbo].[distributors] OFFGO---- Data for table dbo.flyers (LIMIT 0,500)--SET IDENTITY_INSERT [dbo].[flyers] ONGOINSERT INTO [dbo].[flyers] ([id], [distribuitor_id], [person_who_received_the_flier], [datereceived], [received], [special])VALUES (1, 1, N'John', '20090101', 1, 1)GOINSERT INTO [dbo].[flyers] ([id], [distribuitor_id], [person_who_received_the_flier], [datereceived], [received], [special])VALUES (2, 1, N'Michael', '20090102', 1, 0)GOINSERT INTO [dbo].[flyers] ([id], [distribuitor_id], [person_who_received_the_flier], [datereceived], [received], [special])VALUES (3, 1, N'Caine', '20090102', 1, 1)GOINSERT INTO [dbo].[flyers] ([id], [distribuitor_id], [person_who_received_the_flier], [datereceived], [received], [special])VALUES (4, 1, N'Loris', '20090103', 1, 1)GOINSERT INTO [dbo].[flyers] ([id], [distribuitor_id], [person_who_received_the_flier], [datereceived], [received], [special])VALUES (5, 2, N'Carpathian', '20090103', 0, 0)GOSET IDENTITY_INSERT [dbo].[flyers] OFFGO---- Definition for indices : --ALTER TABLE [dbo].[distributors]ADD CONSTRAINT [distributors_pk] PRIMARY KEY CLUSTERED ([id])WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]GOALTER TABLE [dbo].[flyers]ADD PRIMARY KEY CLUSTERED ([id])WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]GO---- Definition for foreign keys : --ALTER TABLE [dbo].[flyers]ADD CONSTRAINT [flyers_fk] FOREIGN KEY ([distribuitor_id]) REFERENCES [dbo].[distributors] ([id]) ON UPDATE NO ACTION ON DELETE NO ACTIONGO |
|
arhicoc
Starting Member
2 Posts |
Posted - 2009-02-05 : 04:02:38
|
Got it. It was obvious, obviously.Instead of GROUP BY flyers.datereceived, it was suppposed to beGROUP BY DISTINCT convert(datetime,floor(convert(float, flyers.datereceived))) |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-07 : 20:40:58
|
You don't need distinct over here as you are using Group By.quote: Originally posted by arhicoc Two tables1. Distributors (id, name)- 1 Carrie Bradshaw
- 2 AnnaLynne McCord
- 3 Brad Pitt
- ..............
2. Flyers (id, distributor_id, person_who_received_the_flyer, (datetime)datereceived, (int)received, (int)special)- 1 1 "John" 01/01/2009 1 1
- 2 1 "Michael" 02/01/2009 1 0
- 3 1 "Caine" 02/01/2009 1 1
- 4 1 "Loris" 03/01/2009 1 1
- 5 2 "Carpathian" 03/01/2009 0 0
- ...........
Flyers table already has a number of potential clients.I need to get the total number of flyers for each distribuitor, the total number of flyers given for each distribuitor and the number of deliveries marked as special for every distinct date.something likeCarrie Bradshah- 01/01/2009 total:1, given 1, special 1
- 02/01/2009 total:2, given 2, special 1
- 03/01/2009 total:1, given 1, special 1
AnnaLynne McCord- 03/01/2009 total:1, given:0, special 0
This is what I have but it does not work.SELECT DISTINCT convert(datetime,floor(convert(float, flyers.datereceived))) as datereceived, distribuitors.id, distribuitors.name, COUNT(flyers.id) AS total, SUM(CASE WHEN (dbo.flyers.received = 1) THEN 1 ELSE 0 END) AS received, SUM(CASE WHEN (dbo.flyers.received = 1 AND dbo.flyers.special = 1)) THEN 1 ELSE 0 END) AS specials, FROM distribuitors INNER JOIN flyers ON distribuitors.id = flyers.distribuitor_id GROUP BY convert(datetime,floor(convert(float, flyers.datereceived)), distribuitors.id, distribuitors.name ORDER BY distribuitors.name -- SQL Manager 2008 for SQL Server 3.2.0.2-- ----------------------------------------- Host : (local)-- Database : testorama-- Version : Microsoft SQL Server 9.00.1399.06CREATE DATABASE [testorama]ON PRIMARY ( NAME = [testorama], FILENAME = 'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\testorama.mdf', SIZE = 2240 KB, MAXSIZE = UNLIMITED, FILEGROWTH = 1 MB )LOG ON ( NAME = [testorama_log], FILENAME = 'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\testorama_log.LDF', SIZE = 504 KB, MAXSIZE = 0 MB, FILEGROWTH = 10 % )COLLATE SQL_Latin1_General_CP1_CI_ASGOUSE [testorama]GO---- Definition for contract DEFAULT : --CREATE CONTRACT [DEFAULT] AUTHORIZATION [dbo] ([DEFAULT] SENT BY ANY)GO---- Definition for table flyers : --CREATE TABLE [dbo].[flyers] ( [id] int IDENTITY(1, 1) NOT NULL, [distribuitor_id] int NULL, [person_who_received_the_flier] varchar(75) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [datereceived] datetime NULL, [received] int NULL, [special] int NULL)ON [PRIMARY]GO---- Definition for table distributors : --CREATE TABLE [dbo].[distributors] ( [id] int IDENTITY(1, 1) NOT NULL, [name] varchar(75) COLLATE SQL_Latin1_General_CP1_CI_AS NULL)ON [PRIMARY]GO---- Data for table dbo.distributors (LIMIT 0,500)--SET IDENTITY_INSERT [dbo].[distributors] ONGOINSERT INTO [dbo].[distributors] ([id], [name])VALUES (1, N'Carrie Bradshoaw')GOINSERT INTO [dbo].[distributors] ([id], [name])VALUES (2, N'AnnaLynne McCord')GOINSERT INTO [dbo].[distributors] ([id], [name])VALUES (3, N'Brad Pitt')GOSET IDENTITY_INSERT [dbo].[distributors] OFFGO---- Data for table dbo.flyers (LIMIT 0,500)--SET IDENTITY_INSERT [dbo].[flyers] ONGOINSERT INTO [dbo].[flyers] ([id], [distribuitor_id], [person_who_received_the_flier], [datereceived], [received], [special])VALUES (1, 1, N'John', '20090101', 1, 1)GOINSERT INTO [dbo].[flyers] ([id], [distribuitor_id], [person_who_received_the_flier], [datereceived], [received], [special])VALUES (2, 1, N'Michael', '20090102', 1, 0)GOINSERT INTO [dbo].[flyers] ([id], [distribuitor_id], [person_who_received_the_flier], [datereceived], [received], [special])VALUES (3, 1, N'Caine', '20090102', 1, 1)GOINSERT INTO [dbo].[flyers] ([id], [distribuitor_id], [person_who_received_the_flier], [datereceived], [received], [special])VALUES (4, 1, N'Loris', '20090103', 1, 1)GOINSERT INTO [dbo].[flyers] ([id], [distribuitor_id], [person_who_received_the_flier], [datereceived], [received], [special])VALUES (5, 2, N'Carpathian', '20090103', 0, 0)GOSET IDENTITY_INSERT [dbo].[flyers] OFFGO---- Definition for indices : --ALTER TABLE [dbo].[distributors]ADD CONSTRAINT [distributors_pk] PRIMARY KEY CLUSTERED ([id])WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]GOALTER TABLE [dbo].[flyers]ADD PRIMARY KEY CLUSTERED ([id])WITH ( PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]GO---- Definition for foreign keys : --ALTER TABLE [dbo].[flyers]ADD CONSTRAINT [flyers_fk] FOREIGN KEY ([distribuitor_id]) REFERENCES [dbo].[distributors] ([id]) ON UPDATE NO ACTION ON DELETE NO ACTIONGO
|
|
|
|
|
|