I have a table that returns results like
abc yes
abc no
cdef no
cdef no
cdef no
xyz yes
xyz no
xyz no
sample query
with abc as(
select 'abc' as b , 'yes' as a
union all
select 'abc', 'no'
union all
select 'cdef', 'no'
union all
select 'xyz', 'no'
union all
select 'xyz', 'no'
)
select * from abc
I want to return results that show accounts and indicate yes if they have a yes in any row and no if they dont so should look like this
abc yes
cdf no
xyz yes.
How can I do this?
A simple aggregation should do the trick.
Select Col1
,Col2 = max(Col2)
From YourTable
Group By Col1
Related
How to make single record from the record-set at the bottom of the list.
0. Select All
1. Apple
2. Banana
3. Mango
I need to place "Select All" at the bottom as :
1. Apple
2. Banana
3. Mango
0. Select All
Based on Id, not value .
You should union a calculated sort field with a value of -ALL- with your main table and then sort the results by the calculated field.
SELECT
*
FROM
(
SELECT
RowOrder=2,
RowValue='-ALL-'
UNION
SELECT
RowOrder=1,
RowValue=FruitTable.Value
FROM
FruitTable
)AS X
ORDER BY
X.RowOrder,
x.RowValue
similar to other answers, but I assume you understand the method:
;WITH T (List, Sort) AS
(
SELECT 'Apple' , 1
UNION ALL
SELECT 'Banana' ,1
UNION ALL
SELECT 'Mango' ,1
),
T2 (List , Sort) AS
(
SELECT *
FROM T
UNION ALL
SELECT 'Select All' , 2
)
SELECT List
FROM T2
ORDER BY Sort
Please try like this.
SELECT [Values] from
(
SELECT 'Select All' [Values] UNION ALL
SELECT 'Apple' UNION ALL
SELECT 'Banana' UNION ALL
SELECT 'Mango'
)u
ORDER BY Case [Values] WHEN 'SELECT All' THEN 1 ELSE 0 END,[Values]
I have 4 parts, each part is made out of 2 select statement. I need the 1st 2 part to be UNION'd with the 2nd part. Each part contains its own ORDER BY. I need the results to appear 1st part first and followed by the 2nd part. Only each part is sorted but not the overall result set.
(select col1,col2,col3,col4,col5 from tableA where col1 = 'x'
UNION ALL
select col1,col2,col3,col4,col5 from tableB where col1 = 'x'
ORDER BY Col3) --1st part ends here
--now I need to UNION the 2nd part
UNION ALL
(select col1,col2,col3,col4,col5 from tableA where col1 <> 'x'
UNION ALL
select col1,col2,col3,col4,col5 from tableB where col1 <> 'x'
ORDER BY Col3) --2nd part ends here
I tried wrapping each select in the SELECT * FROM (... but I'm having issues with ORDER BY. Just can't seem to get the syntax right.
Just add a column for this purpose:
select col1,col2,col3,col4,col5, ord = 1 from tableA where col1 = 'x'
UNION ALL
select col1,col2,col3,col4,col5, ord = 1 from tableB where col1 = 'x'
UNION ALL
select col1,col2,col3,col4,col5, ord = 2 from tableA where col1 <> 'x'
UNION ALL
select col1,col2,col3,col4,col5, ord = 2 from tableB where col1 <> 'x'
ORDER BY ord, Col3
The ORDER BY applies over the whole result set, after the UNION. See Example C: https://msdn.microsoft.com/en-us/library/ms180026%28v=sql.110%29.aspx
You can't have the ORDER BY in the queries within the UNION.
I have a select statement in which I am using in clause.
Here is my table : MyTable
Id SKU
1 112
2 223
3 445
4 456
5 678
If I write:
SELECT Id
FROM MyTable
WHERE SKU IN (112,223,445, 456, 678)
I an not getting result as
1
2
3
4
5
Is there any way to get select result based on items order in the in clause.?
For your case ORDER BY id will be sufficient.
SELECT Id
FROM MyTable
WHERE SKU IN (112,223,445, 456, 678)
ORDER BY id
For general approach you could use JOIN with derived table like:
Demo
SELECT m.Id
FROM MyTable m
JOIN (VALUES (1, 112) ,(2,223) ,(3,445), (4,456), (5,678)) AS t(num, SKU)
ON m.SKU = t.SKU
ORDER BY t.num
If you use SQL Server 2008 you can use UNION ALL:
Demo2
;WITH cte AS
(
SELECT 112 AS SKU, 1 AS orderNum
UNION ALL
SELECT 223 AS SKU, 2 AS orderNum
UNION ALL
SELECT 445 AS SKU, 3 AS orderNum
UNION ALL
SELECT 456 AS SKU, 4 AS orderNum
UNION ALL
SELECT 678 AS SKU, 5 AS orderNum
)
SELECT m.Id
FROM #MyTable m
JOIN cte c
ON m.SKU = c.SKU
ORDER BY c.orderNum;
General approach that does not force you to create custom query you could use temp table with IDENTITY column like:
Demo3
CREATE TABLE #mySKU( orderNum INT IDENTITY(1,1), SKU INT);
INSERT INTO #mySKU
VALUES (112),(223),(445), (456), (678);
SELECT m.Id
FROM #MyTable m
JOIN #mySKU c
ON m.SKU = c.SKU
ORDER BY c.orderNum;
"Is there any way to get select result based on items order in the in clause?"
For this particular question the answer is no.
I have a function that returns a table. It takes one parameter and returns one row.
I want to select the values from that function along with columns from another table in the same SELECT staement.
Something like this:
SELECT a.col1, a.col2, b.col1, b.col2
FROM tab1 a INNER JOIN
func1(a.col1) b ON a.col1 = b.col1
WHERE a.col1 IN (123,456,789)
This doesn't work, but is there a way I can do this.
So, for example, if func 1 returns the following for each of the three values in the example:
col1 col2
123 abc
456 def
789 xyz
then I am expecting something like the following results from my query:
col1 col2 col1 col2
123 xxx 123 abc
456 yyy 456 def
789 zzz 789 xyz
I can do it like this, but I'd rather not call the function multiple times for each column I want from the function:
SELECT col1, col2, (SELECT col1 FROM func1(a.col1)), (SELECT col2 FROM func1(a.col1))
FROM tab1 a
WHERE a.col1 IN (123,456,789)
this code is true
alter FUNCTION GETTABLE()
RETURNS #rtnTable TABLE
(
SUM_CD nvarchar(15) NOT NULL,
Name nvarchar(255) NOT NULL
)
AS
BEGIN
DECLARE #TempTable table (SUM_CD Nvarchar(15), name nvarchar(255))
insert into #TempTable(SUM_CD,name) values ('300001','Ahmed')
insert into #TempTable(SUM_CD,name) values ('300002','Mohamed')
insert into #rtnTable
select * from #TempTable
return
END
select * from GLSUMS g inner join dbo.gettable() s on s.SUM_CD=g.SUM_CD
in SQL SERVER 2012
Is there a way to union two tables, but keep the rows from the first table appearing first in the result set?
For example:
Table1
name surname
-------------------
John Doe
Bob Marley
Ras Tafari
Table2
name surname
------------------
Lucky Dube
Abby Arnold
I want the result set to look like:
name surname
-------------------
John Doe
Bob Marley
Ras Tafari
Lucky Dube
Abby Arnold
Unfortunately, union somehow reorders the table. Is there a way around this?
Try this :-
Select *
from
(
Select name,surname, 1 as filter
from Table1
Union all
Select name,surname , 2 as filter
from Table2
)
order by filter
The only way to guarantee output order is to use ORDER BY:
SELECT name,surname,1 as rs
FROM table1
UNION ALL
SELECT name,surname,2
FROM table2
ORDER BY rs
If you don't want rs to appear in the final result set, do the UNION as a subquery:
SELECT name,surname
FROM (
SELECT name,surname,1 as rs
FROM table1
UNION ALL
SELECT name,surname,2
FROM table2
) t
ORDER BY rs
;WITH cte as (
SELECT name, surname, 1 as n FROM table1
UNION ALL
SELECT name, surname, 2 as n FROM table2
UNION ALL
SELECT name, surname, 3 as n FROM table3
)
SELECT name, surname
FROM cte
ORDER BY n;
.Like this?
CREATE TABLE #Table1 (Names VARCHAR(50))
CREATE TABLE #Table2 (Names VARCHAR(50))
INSERT INTO #Table1
(
Names
)
VALUES
('John Doe'), ('Bob Marley'), ('Ras Tafari')
INSERT INTO #Table2
(
Names
)
VALUES
('Lucky Dube'), ('Abby Arnold')
SELECT ArbSeq = 1, *
FROM #Table1
UNION ALL
SELECT ArbSeq = 2, *
FROM #Table2
ORDER BY ArbSeq
It should be noted that ordering is not guaranteed when not explicitly defined.
If the table features a clustered index, the rows will typically be returned in the index's order - but this is not guaranteed.