Microsoft
Software
Hardware
Network
Question : Need help with Sql Query
I have a table named tblDestination with columns DestID, Description, Addr1, Addr2, City, Deleted.
I need to retrieve only those records with distinct Description and Description != NULL and deleted != false
Any help would be appreciated!!
Answer : Need help with Sql Query
Select DestID, Description, Addr1, Addr2, City, Deleted
FROM
(
SELECT DestID, Description, Addr1, Addr2, City, Deleted,
--- row_number() - best to check books online
--- in brief, it allows you to analytically number the rows based on a particular order
--- you can also break up the data (partition), in this case each new description restarts the row number
rn=row_number() over (partition by Description order by DestID desc)
FROM tblDestination
WHERE Description IS NOT NULL
-- WHERE Description > '' --- omit nulls and empty strings
--and Deleted != 0 ---- assuming deleted is bit?
and Deleted != 'false ' --- string 'false'
) SQ
--- this filters the subquery for where the rn value is 1, i.e. the first record in a partition
Where rn=1
To see what it actually does, run the below on its own
SELECT DestID, Description, Addr1, Addr2, City, Deleted,
rn=row_number() over (partition by Description order by DestID desc)
FROM tblDestination
WHERE Description IS NOT NULL
-- WHERE Description > '' --- omit nulls and empty strings
--and Deleted != 0 ---- assuming deleted is bit?
and Deleted != 'false ' --- string 'false'
ORDER BY Description, DestID desc
Random Solutions
Blackberry not displaying text on FWD emails.
Public Desktop Icons not showing in RDP sessions
Install issue: XenApp 5 Feature Pack 3 on W2K8x64
mouseleave event on panel, vb.net winforms
NDMP backup failure(99)
RAID1 in MAX OSx
The object invoked has disconnected from its clients.
Rsyslog template regular expression
How to insert a single blob?
obtain source (/usr/src) for 8.0-RELEASE-p3