Tag Info

New answers tagged

0

Your inner query should look like SELECT DISTINCT rp.post_id, CASE WHEN pm.meta_key = 'htk_premiere_date' THEN pm.meta_value ELSE '' END AS premiere_date FROM wp_related_people AS rp JOIN wp_posts AS p ON rp.post_id = p.ID JOIN wp_postmeta AS pm ON pm.post_id = p.ID WHERE ...


1

in ANSI SQL the DISTINCT is totally pointless here. UNION automatically filters duplicates: test=# SELECT 1 UNION SELECT 1; ?column? ---------- 1 (1 row) There is a sharp distinction between UNION and UNION ALL: test=# SELECT 1 UNION ALL SELECT 1; ?column? ---------- 1 1 (2 rows) in your case the second subselect can never ...


0

Granted DBID and UPC are not guaranteed to be the same for Price1 and Price2 your code should be similar to this: SELECT (SELECT TOP 1 DBID FROM sub WHERE Price1=(SELECT MIN(Price1) FROM sub)) AS DBID1 ,(SELECT TOP 1 UPC FROM sub WHERE Price1=(SELECT MIN(Price1) FROM sub)) AS UPC1 ,(SELECT MIN(Price1) FROM sub) as Price1 ,(SELECT TOP 1 DBID FROM sub WHERE ...


0

Where minimum is Price2 or Price1? If both of them then SELECT DBID,UPC,Price1,Price2 FROM MyTable WHERE Price2 = (SELECT MIN(Price2) FROM MyTable) AND Price1 = (SELECT MIN(price1) FROM MyTable)


1

You can treat the union as a subquery. SELECT TOP 1 DBID, UPC, Price1, Price2 FROM (SELECT DBID, UPC, Price1, Price2 FROM Table1 UNION SELECT DBID, UPC, Price1, Price2 FROM Table2) sub ORDER BY Price1 DESC


1

This might help you to create simple dynamic query for partitioned tables using COALESCE function of SQL Server Step1: Create function to split tokens CREATE FUNCTION [dbo].[fnSplit] ( @List VARCHAR(8000), @Delimiter CHAR(1) = ',' ) RETURNS @Temp1 TABLE ( ItemId INT IDENTITY(1, 1) NOT NULL PRIMARY KEY, Item VARCHAR(8000) NULL ) AS BEGIN DECLARE @item ...



Top 50 recent answers are included