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 |
cyborgnaren
Starting Member
3 Posts |
Posted - 2013-01-15 : 13:33:09
|
SELECT [SegmentS95Id] AS 'Seg' ,[MatS95Id] as 'Mat' ,[r_Use] AS 'Use' ,[Quantity] AS 'Qty' FROM S95DefinitionI get the below resultSeg Mat Use Qty1815 FK19929 Con 11610 FK19929 Con 12400 FK19929 Pro 12400 FKFKFK Con 11410 FK19929 Con 11001 FK19929 Con 11210 FK19929 Con 1I need the below resultSeg Mat-C C-Qty Mat-P P-Qty1815 FK19929 1 NULL 01610 FK19929 1 NULL 02400 FK19929 1 FKFKFK 11410 FK19929 1 NULL 01001 FK19929 1 NULL 01210 FK19929 1 NULL 0Any Help ? |
|
Robowski
Posting Yak Master
101 Posts |
Posted - 2013-01-15 : 14:49:50
|
quote: Originally posted by cyborgnaren SELECT [SegmentS95Id] AS 'Seg' ,[MatS95Id] as 'Mat' ,[r_Use] AS 'Use' ,[Quantity] AS 'Qty' FROM S95DefinitionI get the below resultSeg Mat Use Qty1815 FK19929 Con 11610 FK19929 Con 12400 FK19929 Pro 12400 FKFKFK Con 11410 FK19929 Con 11001 FK19929 Con 11210 FK19929 Con 1I need the below resultSeg Mat-C C-Qty Mat-P P-Qty1815 FK19929 1 NULL 01610 FK19929 1 NULL 02400 FK19929 1 FKFKFK 11410 FK19929 1 NULL 01001 FK19929 1 NULL 01210 FK19929 1 NULL 0Any Help ?
The second result has more columns and isn't using all the fields from S95Definitions I.E 'Use'Are you trying to get results from two tables? If so you need to join them MatS95ID look to be the same in both?What is the other table? Where it is NULL and FKFKF are they displayed like that in the second table? you could sue a case statement for your select otherwise...something like:SELECT [SegmentS95Id] AS 'Seg',[MatS95Id] as 'Mat','Mat-P' = CASE WHEN r_use = 'CON' THEN NULL ELSE 'FKFKFK' ENDFROM S95Definition |
|
|
cyborgnaren
Starting Member
3 Posts |
Posted - 2013-01-15 : 21:02:32
|
Its a joined table , I have not indicated for simplicity. The result set is what is displayed in select. The MatDef_P and MatDef_C and Qty_P and Qty_C are columns that i additionally need from the rwo sets : MatDef and Qty , which would hold values for Con(MatDef_C) and Pro(MatDef_P) along with corresponding values. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-16 : 23:11:25
|
[code]SELECT Seg,MAX(CASE WHEN Use='Con' THEN Mat END) AS [Mat-C],MAX(CASE WHEN Use='Con' THEN Qty END) AS [C-Qty],MAX(CASE WHEN Use='Pro' THEN Mat END) AS [Mat-P],MAX(CASE WHEN Use='Pro' THEN Qty END) AS [P-Qty]FROM(Yourquery) qGROUP BY Seg[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|