bring back single line

  • How can I turn this query to report back single line for different 'TEST" values. each one of the .test will have a differnt .value, but instead of writing 2 lines of data is it possible to just produce single record?

    SELECT UpCastProduction.ItemID, UpCastProduction.ContainerID, UpCastProduction.GrossWeight, UpCastProduction.EquipmentID, UpCastItems.PackageCode, UpCastProduction.OnHold, UpCastProduction.Scrapped, 
    UpCastProduction.Uploaded,
    UpCastProductionQuality.Value

    FROM UpCastProduction INNER JOIN
    UpCastItems ON UpCastProduction.ItemID = UpCastItems.ItemID INNER JOIN
    UpCastProductionQuality ON UpCastProduction.ContainerID = UpCastProductionQuality.ContainerID
    WHERE (UpCastProduction.OnHold = 0) AND (UpCastProduction.Scrapped = 0) AND (UpCastProduction.Uploaded = 0)
    and
    UpCastProductionQuality.Test in ('Diameter','Oxygen')
  • Probably, but how can we write a solution for you if you don't provide DDL, sample data etc?

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • GROUP BY or DISTINCT or ROW_NUMBER() or RANK() or DENSE_RANK() - or maybe a CROSSTAB/PIVOT.

    Any of those might be the right solution, but without sample data and expected results - really difficult to provide a solution.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • CREATE TABLE [dbo].[UpCastProduction](
    [ContainerID] [nvarchar](20) NOT NULL,
    [WOID] [int] NULL,
    [Destination] [nvarchar](20) NULL,
    [GrossWeight] [float] NOT NULL,
    [TareWeight] [float] NOT NULL,
    [NetWeight] AS ([GrossWeight]-[TareWeight]),
    [DateTime] [datetime] NULL,
    [EquipmentID] [nvarchar](15) NOT NULL,
    [EmployeeID] [nvarchar](15) NOT NULL,
    [OnHold] [bit] NULL,
    [Scrapped] [bit] NULL,
    [Uploaded] [bit] NULL,
    [ItemID] [nvarchar](20) NULL,
    CONSTRAINT [PK_UpCastProduction] PRIMARY KEY CLUSTERED
    (
    [ContainerID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO



    CREATE TABLE [dbo].[UpCastProductionQuality](
    [ContainerID] [nvarchar](20) NOT NULL,
    [Test] [nvarchar](20) NOT NULL,
    [Value] [nvarchar](50) NULL,
    [PassFail] [bit] NULL,
    CONSTRAINT [PK_UpCastProductionQuality] PRIMARY KEY CLUSTERED
    (
    [ContainerID] ASC,
    [Test] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
  • Data values for the tables.

     

    Thanks.

    INSERT INTO [dbo].[UpCastProduction]
    ([ContainerID]
    ,[GrossWeight]
    ,[TareWeight]
    ,[DateTime]
    ,[EquipmentID]
    ,[EmployeeID]
    ,[OnHold]
    ,[Scrapped]
    ,[Uploaded]
    ,[ItemID])
    VALUES
    (88883
    ,400
    ,50
    ,'11/10/2021'
    ,50
    ,777
    ,0
    ,0
    ,1
    ,'zzbf')

    go

    Insert into UpCastProductionQuality
    ([ContainerID]
    ,Test
    ,[Value]
    ,[passfail])
    VALUES
    (88883
    ,'OXYGEN'
    ,0.00011
    ,1)

    go

    Insert into UpCastProductionQuality
    ([ContainerID]
    ,Test
    ,[Value]
    ,[passfail])
    VALUES
    (88883
    ,'DIAMETER'
    ,8.004
    ,1)
  • If this was 2017, I'd use STRING_AGG() to do this, but as we're in a 2016 forum, FOR XML PATH will have to do:

    SELECT p.ContainerID
    ,p.GrossWeight
    ,p.TareWeight
    ,p.NetWeight
    ,p.DateTime
    ,p.EquipmentID
    ,p.EmployeeID
    ,p.OnHold
    ,p.Scrapped
    ,p.Uploaded
    ,p.ItemID
    ,Tests = COALESCE(STUFF((
    SELECT ', ' + q.Test
    FROM dbo.UpCastProductionQuality q
    WHERE p.ContainerID = q.ContainerID
    ORDER BY q.Test
    FOR XML PATH('')
    ) ,1 ,2 ,'')
    ,'')
    FROM dbo.UpCastProduction p;

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Can it report back the ,[Value] for each of the TESTs on the same line?

    Thx.

  • What is the expected result?  How do you want the data returned?

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • I would like to return the ContainerID,ItemID,Grosswgt,equipID,Onhold,Scrapped,TEST and Value as a single line per

    Container.

  • Replace the SELECT ', ' row of my code with this.

    SELECT CONCAT(', ', q.Test, ' - ', q.Value)

    Following the same logic, you should easily be able to get it looking the way you want.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Instead of concat could it show values in separate fields for the value of different tests?

    example:

    Oxygen_Value Diameter_Value

    Thanks.

  • Bruin wrote:

    Instead of concat could it show values in separate fields for the value of different tests?

    example:

    Oxygen_Value Diameter_Value

    Thanks.

    By "separate fields", do you mean in separate columns? No, that is not possible.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Bruin wrote:

    Instead of concat could it show values in separate fields for the value of different tests?

    example:

    Oxygen_Value Diameter_Value

    Thanks.

    Sounds like you want a dynamic pivot now. Like asked for earlier, if you show us what the expected results you actually want are, it'll make thin easier for us.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • yes dynamic pivot...

    ContainerID,ItemID,Grosswgt,equipID,Onhold,Scrapped,TEST, and TESTValue for the indiviual test

  • any examples of the dynamic pivot?

     

     

Viewing 15 posts - 1 through 15 (of 18 total)

You must be logged in to reply to this topic. Login to reply