| Author |
Topic |
|
limericklad1974
Starting Member
39 Posts |
Posted - 2012-05-08 : 07:45:12
|
Hi thereI have a shopping plan tool and it allows people to be assigned to go and find certain types of food e.g beef, apples, milk. The foods are broken down into types :Meat: Beef, Ham, Turkey etcVeg: Carrots, Sprouts, Cabbage etcFruit: Oranges, Apples, Pears etcDrinks: Coffee, Milk, Water etcI want to assign the foods to categories such as Primary Target, Secondary Target and Other Target.There will be a person table where each person has an ID, and a Shopping Plan table which will store the date the shopping took place and which person did the shopping.Each person can have a Primary target for Meat, Veg, Fruit and Drinks. They can also have a Secondary target for them and they can have multiple options for them in the Other Target category....I need a stored procedure which shows me the latest shopping plan for each person. I want to pass in a variable for Category, and see a table of data which has the following columns:PersonDateMeatVegFruitDrinkse.g. Person Date Meat Veg Fruit DrinksMary Hughes 5/5/12 Ham Carrots Oranges CoffeeHere is the data to create the tables if anyone wants to help!!!GO/****** Object: Table [dbo].[Person] Script Date: 05/08/2012 12:54:08 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[Person]( [ID] [int] NOT NULL, [FirstName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[OptionsMappingLookup] Script Date: 05/08/2012 12:54:21 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[OptionsMappingLookup]( [ID] [int] NOT NULL, [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_OptionsMappingLookup] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[OptionsMapping] Script Date: 05/08/2012 12:54:30 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[OptionsMapping]( [ID] [int] NOT NULL, [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [OMLID] [int] NULL, CONSTRAINT [PK_OptionsMapping] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[ShoppingPlans] Script Date: 05/08/2012 12:54:43 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[ShoppingPlans]( [ID] [int] NOT NULL, [PersonID] [int] NULL, [StartDate] [smalldatetime] NULL, CONSTRAINT [PK_ShoppingPlans] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[FoodName] Script Date: 05/08/2012 12:54:54 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[FoodName]( [ID] [int] NOT NULL, [FoodName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [FoodType] [int] NULL, CONSTRAINT [PK_FoodName] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[ShoppingPlanFoodMapping] Script Date: 05/08/2012 12:55:05 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[ShoppingPlanFoodMapping]( [ID] [int] NOT NULL, [ShoppingPlanID] [int] NULL, [FoodCategoryID] [int] NULL, [FoodTypeID] [int] NULL, [FoodNameID] [int] NULL, CONSTRAINT [PK_ShoppingPlanFoodMapping] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]Here is the contents of the Person tableID FirstName LastName1 Mary Hughes2 John Smith3 Adam WellsHere is the contents of a table called OptionsMappingLookupID Name100 Food-Categories101 Food-TypesHere is the contents of a table called OptionsMappingID Name OMLID1 Primary Target 1002 Secondary Target 1003 Other Target 1004 Meat 1015 Veg 1016 Fruit 1017 Drinks 101Here is the contents of a table called ShoppingPlansID PersonID StartDate1 1 01/05/20122 3 03/05/20123 1 05/05/2012Here is the contents of a table called FoodNameID FoodName FoodType1 Beef 42 Ham 43 Turkey 44 Carrots 55 Sprouts 56 Cabbage 57 Turnips 58 Oranges 69 Apples 610 Pears 611 Tea 712 Coffee 713 Milk 714 Water 7Here is the contents of a table called ShoppingPlanFoodMappingID ShoppingPlanID FoodCategoryID FoodNameID FoodTypeID1 1 1 1 42 1 2 2 43 1 3 3 44 1 1 6 55 1 2 5 56 1 1 8 67 1 2 9 68 1 3 10 69 1 1 11 710 1 2 14 711 2 1 2 412 2 2 3 413 2 1 4 514 2 2 7 515 2 3 9 616 2 3 8 617 2 3 10 618 2 4 12 719 2 4 11 720 3 1 2 421 3 2 3 422 3 3 1 423 3 1 4 524 3 2 6 525 3 1 9 626 3 2 10 627 3 1 12 728 3 2 13 729 3 3 11 7 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-05-08 : 08:09:21
|
| I would suggest not uploading an Excel file. For one thing, SQLTeam does not have a feature that allows uploading of files. Even if you were to upload to another site and post the link people are reluctant to open such files for security reasons.You should simplify the description of your problem to a few rows of representative input data and figure out what the output data you would like to see for that input data is. Then, post the DDL for your tables along with that sample data. Brett's blog might be helpful: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWhen you post insert your code and tabular data between a [code] and a [/code]. That will preserve spaces to make code and tabular data more readable. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-08 : 12:11:26
|
| [code]SELECT *FROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') ORDER BY sp.StartDate DESC) AS Rn,COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') AS Person,sp.StartDate AS [Date],MAX(CASE WHEN om.Name = 'Meat' THEN fn.FoodName END) AS Meat,MAX(CASE WHEN om.Name = 'Veg' THEN fn.FoodName END) AS Veg,MAX(CASE WHEN om.Name = 'Fruit' THEN fn.FoodName END) AS Fruit,MAX(CASE WHEN om.Name = 'Drinks' THEN fn.FoodName END) AS DrinksFROM ShoppingPlanFoodMapping spfmINNER JOIN ShoppingPlans spON sp.ID = spfm.ShoppingPlanIDINNER JOIN Person pON p.ID = sp.PersonIDINNER JOIN OptionsMapping omON om.ID = spfm.FoodTypeIDINNER JOIN OptionsMappingLookup omlON oml.ID = om.OMLIDINNER JOIN FoodName fnON fn.ID = spfm.FoodNameIDWHERE oml.Name = 'Food-Types'GROUP BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,''),sp.StartDate )tWHERE Rn=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
limericklad1974
Starting Member
39 Posts |
Posted - 2012-05-08 : 12:30:26
|
quote: Originally posted by visakh16
SELECT *FROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') ORDER BY sp.StartDate DESC) AS Rn,COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') AS Person,sp.StartDate AS [Date],MAX(CASE WHEN om.Name = 'Meat' THEN fn.FoodName END) AS Meat,MAX(CASE WHEN om.Name = 'Veg' THEN fn.FoodName END) AS Veg,MAX(CASE WHEN om.Name = 'Fruit' THEN fn.FoodName END) AS Fruit,MAX(CASE WHEN om.Name = 'Drinks' THEN fn.FoodName END) AS DrinksFROM ShoppingPlanFoodMapping spfmINNER JOIN ShoppingPlans spON sp.ID = spfm.ShoppingPlanIDINNER JOIN Person pON p.ID = sp.PersonIDINNER JOIN OptionsMapping omON om.ID = spfm.FoodTypeIDINNER JOIN OptionsMappingLookup omlON oml.ID = om.OMLIDINNER JOIN FoodName fnON fn.ID = spfm.FoodNameIDWHERE oml.Name = 'Food-Types'GROUP BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,''),sp.StartDate )tWHERE Rn=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Hi thereMany thanks for trying to solve this query for me. However, when I run your query, I get the following results1 Adam Wells 2012-05-03 Sprouts NULL NULL Sprouts1 Mary Hughes 2012-05-05 Sprouts NULL Sprouts NULLIt should give me:Mary Hughes 2012-05-05 Ham Carrots Apples CoffeeAdam Wells 2012-05-03 Ham Carrots NULL NULLI need the code to find the latest Shopping plan for each person.Then I need it to find the Primary Targets (which is ID 1 in the Options Mapping table, and using the Food CategoryID column in the ShoppingPlanFoodMapping tableMany thanksD. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-08 : 12:40:04
|
| [code]SELECT *FROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') ORDER BY sp.StartDate DESC) AS Rn,COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') AS Person,sp.StartDate AS [Date],MAX(CASE WHEN om.Name = 'Meat' THEN fn.FoodName END) AS Meat,MAX(CASE WHEN om.Name = 'Veg' THEN fn.FoodName END) AS Veg,MAX(CASE WHEN om.Name = 'Fruit' THEN fn.FoodName END) AS Fruit,MAX(CASE WHEN om.Name = 'Drinks' THEN fn.FoodName END) AS DrinksFROM ShoppingPlanFoodMapping spfmINNER JOIN ShoppingPlans spON sp.ID = spfm.ShoppingPlanIDINNER JOIN Person pON p.ID = sp.PersonIDINNER JOIN OptionsMapping omON om.ID = spfm.FoodTypeIDINNER JOIN OptionsMappingLookup omlON oml.ID = om.OMLIDINNER JOIN FoodName fnON fn.ID = spfm.FoodNameIDINNER JOIN OptionsMapping om2ON om2.ID = spfm.FoodCategoryIDWHERE oml.Name = 'Food-Types'AND om2.Name = 'Primary Target'GROUP BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,''),sp.StartDate )tWHERE Rn=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
limericklad1974
Starting Member
39 Posts |
Posted - 2012-05-08 : 12:46:50
|
quote: Originally posted by visakh16
SELECT *FROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') ORDER BY sp.StartDate DESC) AS Rn,COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') AS Person,sp.StartDate AS [Date],MAX(CASE WHEN om.Name = 'Meat' THEN fn.FoodName END) AS Meat,MAX(CASE WHEN om.Name = 'Veg' THEN fn.FoodName END) AS Veg,MAX(CASE WHEN om.Name = 'Fruit' THEN fn.FoodName END) AS Fruit,MAX(CASE WHEN om.Name = 'Drinks' THEN fn.FoodName END) AS DrinksFROM ShoppingPlanFoodMapping spfmINNER JOIN ShoppingPlans spON sp.ID = spfm.ShoppingPlanIDINNER JOIN Person pON p.ID = sp.PersonIDINNER JOIN OptionsMapping omON om.ID = spfm.FoodTypeIDINNER JOIN OptionsMappingLookup omlON oml.ID = om.OMLIDINNER JOIN FoodName fnON fn.ID = spfm.FoodNameIDINNER JOIN OptionsMapping om2ON om2.ID = spfm.FoodCategoryIDWHERE oml.Name = 'Food-Types'AND om2.Name = 'Primary Target'GROUP BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,''),sp.StartDate )tWHERE Rn=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Again, many thanks for trying but your code reveals the following results1 Adam Wells 2012-05-03 Sprouts NULL NULL NULL1 Mary Hughes 2012-05-05 Sprouts NULL NULL NULLUnfortunately this is not correctIf you open the ShoppingPlanFoodMapping table, and restrict it to where foodcategoryid = 1 and shoppingplanid = 3, you will get 4 rows.These are the primary targets for Mary Hughes shoping plan on 5/5/2012 and you will see the Food Type ID's are 2, 4, 9 and 122 = Ham4 = Carrots9 = Apples12 = Coffee |
 |
|
|
limericklad1974
Starting Member
39 Posts |
Posted - 2012-05-09 : 05:04:05
|
quote: Originally posted by limericklad1974
quote: Originally posted by visakh16
SELECT *FROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') ORDER BY sp.StartDate DESC) AS Rn,COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') AS Person,sp.StartDate AS [Date],MAX(CASE WHEN om.Name = 'Meat' THEN fn.FoodName END) AS Meat,MAX(CASE WHEN om.Name = 'Veg' THEN fn.FoodName END) AS Veg,MAX(CASE WHEN om.Name = 'Fruit' THEN fn.FoodName END) AS Fruit,MAX(CASE WHEN om.Name = 'Drinks' THEN fn.FoodName END) AS DrinksFROM ShoppingPlanFoodMapping spfmINNER JOIN ShoppingPlans spON sp.ID = spfm.ShoppingPlanIDINNER JOIN Person pON p.ID = sp.PersonIDINNER JOIN OptionsMapping omON om.ID = spfm.FoodTypeIDINNER JOIN OptionsMappingLookup omlON oml.ID = om.OMLIDINNER JOIN FoodName fnON fn.ID = spfm.FoodNameIDINNER JOIN OptionsMapping om2ON om2.ID = spfm.FoodCategoryIDWHERE oml.Name = 'Food-Types'AND om2.Name = 'Primary Target'GROUP BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,''),sp.StartDate )tWHERE Rn=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Again, many thanks for trying but your code reveals the following results1 Adam Wells 2012-05-03 Sprouts NULL NULL NULL1 Mary Hughes 2012-05-05 Sprouts NULL NULL NULLUnfortunately this is not correctIf you open the ShoppingPlanFoodMapping table, and restrict it to where foodcategoryid = 1 and shoppingplanid = 3, you will get 4 rows.These are the primary targets for Mary Hughes shoping plan on 5/5/2012 and you will see the Food Type ID's are 2, 4, 9 and 122 = Ham4 = Carrots9 = Apples12 = Coffee
hi VisakhHave you had a chanc eot look again at this, any further help would be greatly appreciated!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-09 : 22:01:20
|
i'm getting correct results. So either your data representation of scenario is not correct or you're not using query in correct way.see full illustration beloeGO/****** Object: Table [dbo].[Person] Script Date: 05/08/2012 12:54:08 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[Person]( [ID] [int] NOT NULL, [FirstName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[OptionsMappingLookup] Script Date: 05/08/2012 12:54:21 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[OptionsMappingLookup]( [ID] [int] NOT NULL, [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_OptionsMappingLookup] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[OptionsMapping] Script Date: 05/08/2012 12:54:30 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[OptionsMapping]( [ID] [int] NOT NULL, [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [OMLID] [int] NULL, CONSTRAINT [PK_OptionsMapping] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[ShoppingPlans] Script Date: 05/08/2012 12:54:43 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[ShoppingPlans]( [ID] [int] NOT NULL, [PersonID] [int] NULL, [StartDate] [smalldatetime] NULL, CONSTRAINT [PK_ShoppingPlans] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[FoodName] Script Date: 05/08/2012 12:54:54 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[FoodName]( [ID] [int] NOT NULL, [FoodName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [FoodType] [int] NULL, CONSTRAINT [PK_FoodName] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[ShoppingPlanFoodMapping] Script Date: 05/08/2012 12:55:05 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[ShoppingPlanFoodMapping]( [ID] [int] NOT NULL, [ShoppingPlanID] [int] NULL, [FoodCategoryID] [int] NULL, [FoodTypeID] [int] NULL, [FoodNameID] [int] NULL, CONSTRAINT [PK_ShoppingPlanFoodMapping] PRIMARY KEY CLUSTERED ( [ID] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]--Here is the contents of the Person tableINSERT INTO PErson(ID, FirstName, LastName)VALUES(1,'Mary','Hughes'),(2, 'John', 'Smith'),(3, 'Adam', 'Wells')--Here is the contents of a table called OptionsMappingLookupINSERT INTO OptionsMappingLookup(ID, Name)VALUES(100, 'Food-Categories'),(101 ,'Food-Types')--Here is the contents of a table called OptionsMappingINSERT INTO OptionsMapping(ID, Name, OMLID)VALUES (1, 'Primary Target', 100),(2, 'Secondary Target', 100),(3, 'Other Target', 100),(4, 'Meat', 101),(5, 'Veg', 101),(6, 'Fruit', 101),(7, 'Drinks', 101)--Here is the contents of a table called ShoppingPlansINSERT INTO ShoppingPlans(ID, PersonID, StartDate)VALUES(1, 1, '01/05/2012'),(2, 3, '03/05/2012'),(3, 1, '05/05/2012')--Here is the contents of a table called FoodNameINSERT INTO FoodName(ID, FoodName, FoodType)VALUES(1, 'Beef', 4),(2, 'Ham', 4),(3, 'Turkey', 4),(4, 'Carrots', 5),(5, 'Sprouts', 5),(6, 'Cabbage', 5),(7, 'Turnips', 5),(8, 'Oranges', 6),(9, 'Apples', 6),(10, 'Pears', 6),(11, 'Tea', 7),(12, 'Coffee', 7),(13, 'Milk', 7),(14, 'Water', 7)--Here is the contents of a table called ShoppingPlanFoodMappingINSERT INTO ShoppingPlanFoodMapping(ID, ShoppingPlanID, FoodCategoryID, FoodNameID, FoodTypeID)VALUES(1, 1, 1, 1, 4),(2, 1, 2 ,2, 4),(3, 1, 3, 3, 4),(4, 1, 1, 6, 5),(5, 1, 2, 5, 5),(6, 1, 1, 8, 6),(7, 1, 2, 9, 6),(8, 1, 3, 10, 6),(9, 1, 1, 11, 7),(10, 1, 2, 14, 7),(11, 2, 1, 2, 4),(12, 2, 2, 3, 4),(13, 2, 1, 4, 5),(14, 2, 2, 7, 5),(15, 2, 3, 9, 6),(16, 2, 3, 8, 6),(17, 2, 3, 10, 6),(18, 2, 4, 12, 7),(19, 2, 4, 11, 7),(20, 3, 1, 2, 4),(21, 3, 2, 3, 4),(22, 3, 3, 1, 4),(23, 3, 1, 4, 5),(24, 3, 2, 6, 5),(25, 3, 1, 9, 6),(26, 3, 2, 10, 6),(27, 3, 1, 12, 7),(28, 3, 2, 13, 7),(29, 3, 3, 11, 7)SELECT Person,[Date],Meat,Veg,Fruit,DrinksFROM(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') ORDER BY sp.StartDate DESC) AS Rn,COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,'') AS Person,sp.StartDate AS [Date],MAX(CASE WHEN om.Name = 'Meat' THEN fn.FoodName END) AS Meat,MAX(CASE WHEN om.Name = 'Veg' THEN fn.FoodName END) AS Veg,MAX(CASE WHEN om.Name = 'Fruit' THEN fn.FoodName END) AS Fruit,MAX(CASE WHEN om.Name = 'Drinks' THEN fn.FoodName END) AS DrinksFROM ShoppingPlanFoodMapping spfmINNER JOIN ShoppingPlans spON sp.ID = spfm.ShoppingPlanIDINNER JOIN Person pON p.ID = sp.PersonIDINNER JOIN OptionsMapping omON om.ID = spfm.FoodTypeIDINNER JOIN OptionsMappingLookup omlON oml.ID = om.OMLIDINNER JOIN FoodName fnON fn.ID = spfm.FoodNameIDINNER JOIN OptionsMapping om2ON om2.ID = spfm.FoodCategoryIDWHERE oml.Name = 'Food-Types'AND om2.Name = 'Primary Target'GROUP BY COALESCE(p.FirstName + ' ','') + COALESCE(p.LastName,''),sp.StartDate )tWHERE Rn=1ouput-------------------------------------------------------------------------Person Date Meat Veg Fruit Drinks-------------------------------------------------------------------------Adam Wells 2012-03-05 00:00:00 Ham Carrots NULL NULLMary Hughes 2012-05-05 00:00:00 Ham Carrots Apples Coffee ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|