SQL Order By

  • I have a SQL statement which selects specific data from my database, I would like to add an ORDER BY statement to sort the final column in ascending order.

    DATEDIFF('end_date', CURDATE()) <31

    I have tried putting ORDER BY in various places with no luck.

    SELECT 'Supplier_Details_tbl'.'Supplier_Name', 'Documents_tbl'.'Document_Type', 'Documents_tbl'.'Start_Date', 'Documents_tb'l.'End_Date', 'Documents_tbl'.'Tarrif_Name', 'House_Details_tbl'.'House_Name', DateDiff('end_date, CURDATE()) FROM 'HDMS'.'House_Details_tbl' AS 'House_details_tbl', 'HDMS'.Documents_tbl' AS 'Documents_tbl', 'HDMS'.Supplier_Details_tbl' AS 'Supplier_Details_tbl' WHERE 'House_Details_tbl'.'ID' = 'Documents_tbl'.Supplier_Address' AND 'Supplier_Details_tbl'.'ID' = 'Documents_tbl'.'Supplier_ID' AND 'Documents_tbl'.'R_Sent' = 0 AND 'Documents_tbl'.'Superceed' = 0 AND DATEDIFF('end_date', CURDATE()) <31

    Thanks

  • Let's turn this into something more readable first and take out the invalid quotes.

    SELECT Supplier_Details_tbl.Supplier_Name,

    Documents_tbl.Document_Type,

    Documents_tbl.Start_Date,

    Documents_tbl.End_Date,

    Documents_tbl.Tarrif_Name,

    House_details_tbl.House_Name,

    DATEDIFF(end_date, CURDATE())

    FROM HDMS.House_Details_tbl AS House_details_tbl,

    HDMS.Documents_tbl AS Documents_tbl,

    HDMS.Supplier_Details_tbl AS Supplier_Details_tbl

    WHERE House_details_tbl.ID = Documents_tbl.Supplier_Address

    AND Supplier_Details_tbl.ID = Documents_tbl.Supplier_ID

    AND Documents_tbl.R_Sent = 0

    AND Documents_tbl.Superceed = 0

    AND DATEDIFF(end_date, CURDATE()) < 31

    The query's still syntatically wrong, DATEDIFF takes 3 parameters, not 2

    Edit: Hang on. This isn't T-SQL at all. Which database engine is this actually intended for?

    This is a Microsoft SQL Server forum. You'll probably get better help from a forum for whatever database engine this is for.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Edit: nm

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Looks like MySQL/MariaDb, the CURDATE() and DATEDIFF with two parameters fits there.

    😎

Viewing 4 posts - 1 through 3 (of 3 total)

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