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 |
|
timothymannah
Starting Member
14 Posts |
Posted - 2007-03-12 : 22:18:22
|
Below are 2 tablesStockmasterST_ID DESCRIPTION PRICE GR_ID------------------------------------------------------SD001 BD Safeclip Device 12.73 NULLSD002 BD Sharps Collector 1.4L 4.19 GR05SD003 BD Sharps Collector 3.1L 6.1 GR05SD004 Needle Safe-Desk Top 3.73 NULLSD005 Sharps Collector 1L 4.55 GR06SD006 Sharps Collector 1.7L 5.19 GR07SD007 Sharps Collector 4L - RE4LR 6.82 GR07SD008 Sharps Collector 4L 6.91 GR07SD009 Sharps Collector 10L - RE10LS 17.46 GR07SD010 Sharps Collector 16.5L- RE15LS 20.73 NULLSD011 Sharps Collector 0.5L 3.73 GR06GroupsGR_ID Name-----------------------------------------------------------------GR05 BD SharpsGR06 IDC Sharps Collector (Round)GR07 IDC Sharps Collector (Square) I want to return these:ST_ID DESCRIPTION PRICE GR_ID Name--------------------------------------------------------------------SD001 BD Safeclip Device 12.7 NULL NULL SD002 BD Sharps Collector 1.4L 4.1 GR05 BD SharpsSD004 Needle Safe-Desk Top 3.73 NULL NULLSD005 Sharps Collector 1L 4.55 GR06 IDC Sharps Collector (Round)SD006 Sharps Collector 1.7L 5.19 GR07 IDC Sharps Collector (Square) What I am trying to achieve is to return a set of results of all values in both tables where GR_ID is "NULL" AND the first unique instance of records that have equal values for GR_ID.I have a solution that only half works. It returns the values in stockmaster but nothing from groups. LIKe THISST_ID DESCRIPTION PRICE GR_ID ----------------------------------------------------SD001 BD Safeclip Device 12.7 NULL SD002 BD Sharps Collector 1.4L 4.1 GR05 SD004 Needle Safe-Desk Top 3.73 NULL SD005 Sharps Collector 1L 4.55 GR06 SD006 Sharps Collector 1.7L 5.19 GR07
SELECT StockMaster.ST_ID, StockMaster.DESCRIPTION , StockMaster.PRICE, StockMaster.GR_ID, FROM StockMaster INNER JOIN (SELECT MIN(StockCode) AS StockCode FROM StockMaster AS StockMaster_1 WHERE (StockCode LIKE 'SD%') AND (GroupCode IS NOT NULL) GROUP BY GroupCode) AS tblMin ON StockMaster.StockCode = tblMin.StockCodeUNIONSELECT ST_ID, DESCRIPTION , PRICE, GR_IDFROM StockMaster AS StockMaster_2WHERE (ST_ID LIKE 'SD%') AND (GR_ID IS NULL)ORDER BY StockMaster.GroupCode DESC Any one have an idea? |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-12 : 23:23:20
|
Did you missed out SD010 ?declare @Stockmaster table( ST_ID varchar(10), [DESCRIPTION] varchar(30), PRICE decimal(10,2), GR_ID varchar(10))insert into @Stockmasterselect 'SD001', 'BD Safeclip Device', 12.73, NULL union allselect 'SD002', 'BD Sharps Collector 1.4L', 4.19, 'GR05' union allselect 'SD003', 'BD Sharps Collector 3.1L', 6.1, 'GR05' union allselect 'SD004', 'Needle Safe-Desk Top', 3.73, NULL union allselect 'SD005', 'Sharps Collector 1L', 4.55, 'GR06' union allselect 'SD006', 'Sharps Collector 1.7L', 5.19, 'GR07' union allselect 'SD007', 'Sharps Collector 4L - RE4LR', 6.82, 'GR07' union allselect 'SD008', 'Sharps Collector 4L', 6.91, 'GR07' union allselect 'SD009', 'Sharps Collector 10L - RE10LS', 17.46, 'GR07' union allselect 'SD010', 'Sharps Collector 16.5L- RE15LS', 20.73, NULL union allselect 'SD011', 'Sharps Collector 0.5L', 3.73, 'GR06'declare @Groups table( GR_ID varchar(10), [Name] varchar(30))insert into @Groupsselect 'GR05', 'BD Sharps' union allselect 'GR06', 'IDC Sharps Collector (Round)' union allselect 'GR07', 'IDC Sharps Collector (Square)'select s.ST_ID, s.[DESCRIPTION], s.PRICE, s.GR_ID, g.[Name]from @Stockmaster s left join @Groups g on s.GR_ID = g.GR_IDwhere s.GR_ID is nullor s.ST_ID = (select min(ST_ID) from @Stockmaster x where x.GR_ID = s.GR_ID)/*ST_ID DESCRIPTION PRICE GR_ID Name ---------- ------------------------------ ------------ ---------- ------------------------------ SD001 BD Safeclip Device 12.73 NULL NULLSD002 BD Sharps Collector 1.4L 4.19 GR05 BD SharpsSD004 Needle Safe-Desk Top 3.73 NULL NULLSD005 Sharps Collector 1L 4.55 GR06 IDC Sharps Collector (Round)SD006 Sharps Collector 1.7L 5.19 GR07 IDC Sharps Collector (Square)SD010 Sharps Collector 16.5L- RE15LS 20.73 NULL NULL*/ KH |
 |
|
|
timothymannah
Starting Member
14 Posts |
Posted - 2007-03-12 : 23:36:03
|
I gave you just one example of the tables I have over 20 to do so I really cant hardcode the solution into a store procedure.This is what I am using nowSELECT s.StockCode, s.StockName1, s.StockName2, s.PD1DateCreatedBrief, s.PD2DateCreatedDetailed, s.PD3DateCreatedFeatures, s.ProductDescriptionBrief, s.ProductDescriptionDetailed, s.ProductDescriptionFeatures, s.StockCategory, s.StockUnit, s.StockSellingPrice1, s.StockSellingPrice2, s.StockSellingPrice3, s.StockSellingPrice4, s.StockSellingPrice5, s.StockSellingPrice6, s.SpecialFlag, s.SpecialStartDate, s.SpecialEndDate, s.NewProductFlag, s.NewProductStartDate, s.NewProductEndDate, s.Image1Small, s.Image2Large, s.NonOnlineProduct, s.DeleteFlag, s.GroupCode, s.GroupInclude, s.GroupExclude, s.CategoryInclude, s.CategoryExclude, s.ClassInclude, s.ClassExclude, s.StockWeight, s.ImportedFROM StockMaster AS s INNER JOIN (SELECT GroupCode, MIN(StockCode) AS min_ST_ID FROM StockMaster WHERE (GroupCode IS NOT NULL) GROUP BY GroupCode) AS m ON s.StockCode = m.min_ST_IDWHERE (s.StockCode LIKE 'SD%')UNION ALLSELECT StockCode, StockName1, StockName2, PD1DateCreatedBrief, PD2DateCreatedDetailed, PD3DateCreatedFeatures, ProductDescriptionBrief, ProductDescriptionDetailed, ProductDescriptionFeatures, StockCategory, StockUnit, StockSellingPrice1, StockSellingPrice2, StockSellingPrice3, StockSellingPrice4, StockSellingPrice5, StockSellingPrice6, SpecialFlag, SpecialStartDate, SpecialEndDate, NewProductFlag, NewProductStartDate, NewProductEndDate, Image1Small, Image2Large, NonOnlineProduct, DeleteFlag, GroupCode, GroupInclude, GroupExclude, CategoryInclude, CategoryExclude, ClassInclude, ClassExclude, StockWeight, ImportedFROM StockMaster AS sWHERE (StockCode LIKE 'SD%') AND (GroupCode IS NULL) I just want to add Groups.GR_ID to the selection.Is there any way to edit the above query to include the field Groups.GR_ID and have the final selection ofST_ID DESCRIPTION PRICE GR_ID Name--------------------------------------------------------------------SD001 BD Safeclip Device 12.7 NULL NULL SD002 BD Sharps Collector 1.4L 4.1 GR05 BD SharpsSD004 Needle Safe-Desk Top 3.73 NULL NULLSD005 Sharps Collector 1L 4.55 GR06 IDC Sharps Collector (Round)SD006 Sharps Collector 1.7L 5.19 GR07 IDC Sharps Collector (Square)SD007 BLAH BLAH BLAH BLAH the value of SD is changes in my code. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-12 : 23:39:32
|
have you tried the query ?select s.ST_ID, s.[DESCRIPTION], s.PRICE, s.GR_ID, g.[Name]from Stockmaster s left join Groups g on s.GR_ID = g.GR_IDwhere s.GR_ID is nullor s.ST_ID = (select min(ST_ID) from Stockmaster x where x.GR_ID = s.GR_ID) What do you mean by hardcode the solution in stored procedure ? KH |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2007-03-12 : 23:51:06
|
| [code]declare @Stockmaster table( ST_ID varchar(10), [DESCRIPTION] varchar(30), PRICE decimal(10,2), GR_ID varchar(10))insert into @Stockmasterselect 'SD001', 'BD Safeclip Device', 12.73, NULL union allselect 'SD002', 'BD Sharps Collector 1.4L', 4.19, 'GR05' union allselect 'SD003', 'BD Sharps Collector 3.1L', 6.1, 'GR05' union allselect 'SD004', 'Needle Safe-Desk Top', 3.73, NULL union allselect 'SD005', 'Sharps Collector 1L', 4.55, 'GR06' union allselect 'SD006', 'Sharps Collector 1.7L', 5.19, 'GR07' union allselect 'SD007', 'Sharps Collector 4L - RE4LR', 6.82, 'GR07' union allselect 'SD008', 'Sharps Collector 4L', 6.91, 'GR07' union allselect 'SD009', 'Sharps Collector 10L - RE10LS', 17.46, 'GR07' union allselect 'SD010', 'Sharps Collector 16.5L- RE15LS', 20.73, NULL union allselect 'SD011', 'Sharps Collector 0.5L', 3.73, 'GR06'declare @Groups table( GR_ID varchar(10), [Name] varchar(30))insert into @Groupsselect 'GR05', 'BD Sharps' union allselect 'GR06', 'IDC Sharps Collector (Round)' union allselect 'GR07', 'IDC Sharps Collector (Square)'select s.*,g.namefrom @Stockmaster sinner join (select GR_ID,ST_ID from @Stockmaster where GR_ID IS NULL UNION ALLselect GR_ID,MIN(ST_ID) from @Stockmaster where GR_ID IS NOT NULLGroup by GR_ID) A on s.ST_ID=A.ST_IDLEFT JOIN @Groups g on s.GR_ID=g.GR_ID[/code]mk_garg |
 |
|
|
timothymannah
Starting Member
14 Posts |
Posted - 2007-03-14 : 17:37:45
|
That seems to work fine. The example table i gave you is much smaller than what i'm really using. I need the "Where" clause to give me the freedom to set record parameters.Is there a way I can make your query:select s.ST_ID, s.[DESCRIPTION], s.PRICE, s.GR_ID, g.[Name]from Stockmaster s left join Groups g on s.GR_ID = g.GR_IDwhere s.GR_ID is nullor s.ST_ID = (select min(ST_ID) from Stockmaster x where x.GR_ID = s.GR_ID) To give me the option of adding criteria in the where clause to also include a selection of ST_ID's between a range. I am working with 1000 records and they have stockcodes that can be chosen easily if i can add WHERE (s.ST.ID BETWEEN 'CH001 ' AND 'CH006') |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-14 : 18:31:47
|
| [code]-- prepare sample datadeclare @Stockmaster table (ST_ID varchar(10), [DESCRIPTION] varchar(30), PRICE decimal(10,2), GR_ID varchar(10))insert @Stockmasterselect 'SD001', 'BD Safeclip Device', 12.73, NULL union allselect 'SD002', 'BD Sharps Collector 1.4L', 4.19, 'GR05' union allselect 'SD003', 'BD Sharps Collector 3.1L', 6.1, 'GR05' union allselect 'SD004', 'Needle Safe-Desk Top', 3.73, NULL union allselect 'SD005', 'Sharps Collector 1L', 4.55, 'GR06' union allselect 'SD006', 'Sharps Collector 1.7L', 5.19, 'GR07' union allselect 'SD007', 'Sharps Collector 4L - RE4LR', 6.82, 'GR07' union allselect 'SD008', 'Sharps Collector 4L', 6.91, 'GR07' union allselect 'SD009', 'Sharps Collector 10L - RE10LS', 17.46, 'GR07' union allselect 'SD010', 'Sharps Collector 16.5L- RE15LS', 20.73, NULL union allselect 'SD011', 'Sharps Collector 0.5L', 3.73, 'GR06'declare @Groups table (GR_ID varchar(10), [Name] varchar(30))insert @Groupsselect 'GR05', 'BD Sharps' union allselect 'GR06', 'IDC Sharps Collector (Round)' union allselect 'GR07', 'IDC Sharps Collector (Square)'-- show the expected outputselect m.st_id, m.[description], m.price, m.gr_id, g.[name]from @stockmaster as mleft join @groups as g on g.gr_id = m.gr_idleft join ( select gr_id, min(st_id) AS st_id from @stockmaster group by gr_id ) as x on x.st_id = m.st_idwhere nullif(m.gr_id, x.gr_id) is null and m.ST_ID BETWEEN 'SD001' AND 'SD006' -- add additional filtering here[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
timothymannah
Starting Member
14 Posts |
Posted - 2007-03-14 : 18:50:21
|
| This seems to work fine at first glance.....thanks....I'll try it out further........Thank you all for your efforts |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-14 : 21:07:15
|
received from emailquote: That seems to work fine. The example table i gave you is much smaller than what i'm really using. I need the "Where" clause to give me the freedom to set record parameters.Is there a way I can make your query:select s.ST_ID, s.[DESCRIPTION], s.PRICE, s.GR_ID, g.[Name]from Stockmaster s left join Groups g on s.GR_ID = g.GR_IDwhere s.GR_ID is nullor s.ST_ID = (select min(ST_ID) from Stockmaster x where x.GR_ID = s.GR_ID)To give me the option of adding criteria in the where clause to also include a selection of ST_ID's between a range. I am working with 1000 records and they have stockcodes that can be chosen easily if i can add WHERE (s.ST.ID BETWEEN 'CH001 ' AND 'CH006')
Yesselect s.ST_ID, s.[DESCRIPTION], s.PRICE, s.GR_ID, g.[Name]from Stockmaster s left join Groups g on s.GR_ID = g.GR_IDwhere ( s.GR_ID is nullor s.ST_ID = (select min(ST_ID) from Stockmaster x where x.GR_ID = s.GR_ID) )and (s.ST.ID BETWEEN 'CH001 ' AND 'CH006') KH |
 |
|
|
|
|
|
|
|