Avatar of Zack
Zack
Flag for Australia asked on

SQL - Syntax Queries

Hi EE,

I have the following query:

SELECT
    A.[VENDOR NAME] AS 'VENDOR NAME',
    A.[VENDOR NUMBER] AS 'VENDOR NUMBER'
FROM
    (SELECT
        PURCHTABLE.PURCHNAME AS 'VENDOR NAME',
        PURCHTABLE.ORDERACCOUNT AS 'VENDOR NUMBER',
        COUNT(DISTINCT PURCHTABLE.PURCHID) AS 'PURCHASE ORDER',
        COUNT(PURCHLINE.LINENUMBER) AS 'NUMBER OF LINES',
        SUM(PURCHLINE.LINEAMOUNT) AS 'PO PRICE TOTAL',
        DATEDIFF(dd, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE))-(DATEDIFF(wk, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) * 2)-(CASE WHEN DATENAME(dw, PURCHTABLE.CONFIRMEDDLV) = 'Sunday' THEN 1 ELSE 0 END)-(CASE WHEN DATENAME(dw, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) = 'Saturday' THEN 1 ELSE 0 END) AS 'DAYS LATE'
    FROM
    PURCHTABLE
    JOIN
    PURCHLINE ON PURCHLINE.PURCHID = PURCHTABLE.PURCHID
    JOIN
    VENDPACKINGSLIPJOUR ON VENDPACKINGSLIPJOUR.PURCHID = PURCHTABLE.PURCHID
    WHERE
    PURCHTABLE.DELIVERYDATE >= '2019-01-01'
    AND
    PURCHTABLE.DELIVERYDATE <= '2019-01-20'
    AND
    PURCHTABLE.ORDERACCOUNT = 'FDS5834') as A
   
GROUP BY  
A.[VENDOR NAME],
A.[VENDOR NUMBER],
A.[PURCHASE ORDER],
PURCHTABLE.CONFIRMEDDLV

Open in new window


Getting the following errors

Msg 8120, Level 16, State 1, Line 6
Column 'PURCHTABLE.PURCHNAME' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 4104, Level 16, State 1, Line 29
The multi-part identifier "PURCHTABLE.CONFIRMEDDLV" could not be bound.

Both these tables/columns exist and the PURCHTABLE.PURCHNAME' have appropriate aliases.

Any assistance is welcome.

Thank you. 


SQLMicrosoft SQL Server

Avatar of undefined
Last Comment
Sharath S

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Peter Chan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Sharath S

You don't have any of the aggregated metrics in the final SELECT clause. What is the purpose of deriving them in sub-query?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes