I can't get this to sort. I want the entire result set ordered by DateId then productName and can't get it to work.
SELECT d.DateId,
s.product_name1 as productName
FROM dbo.SaleDates d
INNER JOIN dbo.Sale s ON s.saleId = d.saleId
UNION ALL
SELECT d.DateId,
s.product_name2 as productName
FROM dbo.SaleDates d
INNER JOIN dbo.Sale s ON s.saleId = d.saleId
UNION ALL
SELECT d.DateId,
s.product_name3 as productName
FROM dbo.SaleDates d
INNER JOIN dbo.Sale s ON s.saleId = d.saleId
order by d.DateId, productName
Not sure where and how to add this order by basically. I don't want to add an order by to each select because then I'd have subsets of orderings. I want to order the -entire- ending result set...
You could try this:
SELECT * FROM
(
SELECT d.DateId as DateId,
s.product_name1 as productName
FROM dbo.SaleDates d
INNER JOIN dbo.Sale s ON s.saleId = d.saleId
UNION ALL
SELECT d.DateId as DateId,
s.product_name2 as productName
FROM dbo.SaleDates d
INNER JOIN dbo.Sale s ON s.saleId = d.saleId
UNION ALL
SELECT d.DateId as DateId,
s.product_name3 as productName
FROM dbo.SaleDates d
INNER JOIN dbo.Sale s ON s.saleId = d.saleId
) AS A
order by DateId, ProductName
refer to the column alias (in your case DateId, Productname)
select 1 a
union all
select 2 a
order by a desc
or refer to the column number
select 1 a
union all
select 2 a
order by 1
Select Col1, Col2 from tblA where <some condition>
Union All
Select Col1,Col2 from tblB where <some condition>
Union All
Select Col1,Col2 from tblC where <some condition>
Order By 1,2 -- means by first column,second column
-- or Order By Col1, Col2
But yours should work fine Order by DateId,productName
Also instead of hitting the tables Sales and SalesDate 3 times, you can improve the performance of your query by catching the common result set in some temporary table and then performing the union on the desired columns e.g.
SELECT d.DateId,s.product_name1,s.product_name2,s.product_name3
INTO #temp
FROM dbo.SaleDates d
INNER JOIN dbo.Sale s ON s.saleId = d.saleId
Select t.DateId,t.product_name1 As productName From #temp t Union All
Select t.DateId,t.product_name2 From #temp t Union All
Select t.DateId,t.product_name3 From #temp t
Order By DateId,productName
Drop Table #temp
Hope this helps
You could use a CTE for this. Or just do:
Select res.DateID, res.productName from
(
SELECT d.DateId,
s.product_name1 as productName
FROM dbo.SaleDates d
INNER JOIN dbo.Sale s ON s.saleId = d.saleId
UNION ALL
SELECT d.DateId,
s.product_name2 as productName
FROM dbo.SaleDates d
INNER JOIN dbo.Sale s ON s.saleId = d.saleId
UNION ALL
SELECT d.DateId,
s.product_name3 as productName
FROM dbo.SaleDates d
INNER JOIN dbo.Sale s ON s.saleId = d.saleId
)
order by res.DateId, res.productName
Related
I am trying to run a query, but the result is showing the error message "Subquery has too many columns". I have to change where the clause with array to string but the error is the same. could you help me, if you have any suggestions or different queries as modify to solve this?
the query is:
create table my_schema.table_1 as select a.*
, O.ID1
, J.ID2
, K.ID3
, L.ID4
, M.ID5
, S.ID6
, O.plan_name plan_name1
, J.plan_name plan_name2
, K.plan_name plan_name3
, L.plan_name plan_name4
, M.plan_name plan_name5
, S.plan_name plan_name6
from (
select
PRD_ID
, NBR
, SI
, START_TIME
, ATTR4
, G_ID
, BYTE_UP
, BYTE_DOWN
, LIST
, TYPE_ID1
, TYPE_ID2
, TYPE_ID3
, TYPE_ID4
, TYPE_ID5
, TYPE_ID6
, CHARGE1, CHARGE2, CHARGE3, CHARGE4, CHARGE5, CHARGE6
from my_schema.source where prd_id = '20200101'
and TYPE IN (3) ) a
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) O on split_part(split_part(list,';',1),',',1) = O.id
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) J on split_part(split_part(list,';',2),',',1) = J.id
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) K on split_part(split_part(list,';',3),',',1) = K.id
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) L on split_part(split_part(list,';',4),',',1) = L.id
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) M on split_part(split_part(list,';',5),',',1) = M.id
left outer join (select distinct id, price_plan_name from my_schema.id_ref_v9) S on split_part(split_part(list,';',6),',',1) = S.id
WHERE (
O.id in (select * from my_schema.LIST_PRICEID4)or
J.id in (select * from my_schema.LIST_PRICEID4)or
K.id in (select * from my_schema.LIST_PRICEID4)or
L.id in (select * from my_schema.LIST_PRICEID4)or
M.id in (select * from my_schema.LIST_PRICEID4)or
S.id in (select * my_schema.LIST_PRICEID4))
I'm quite stuck with this problem for sometime now..
How do I sort column A depending on the contents of Column B?
I have this sample:
ID count columnA ColumnB
-----------------------------------
12 1 A B
13 2 C D
14 3 B C
I want to sort it like this:
ID count ColumnA ColumnB
-----------------------------------
12 1 A B
14 3 B C
13 2 C D
so I need to sort the rows if the previous row of ColumnB = the next row of ColumnA
I'm thinking a loop? but can't quite imagine how it will work...
I was thinking it will go like this (maybe)
SELECT
a.ID, a.ColumnA, a.ColumnB
FROM
TableA WITH a (NOLOCK)
LEFT JOIN
TableA b WITH (NOLOCK) ON a.ID = b.ID AND a.counts = b.counts
WHERE
a.columnB = b.ColumnA
the above code isn't working though and I was thinking more on the lines of...
DECLARE #counts int = 1
DECLARE #done int = 0
--WHILE #done = 0
BEGIN
SELECT
a.ID, a.ColumnA, a.ColumnB
FROM
TableA WITH a (NOLOCK)
LEFT JOIN
TableA b WITH (NOLOCK) ON a.ID = b.ID AND a.counts = #counts
WHERE
a.columnB = b.ColumnA
set #count = #count +1
END
If this was a C code, would be easier for me but T-SQL's syntax is making it a bit harder for a noobie like me.
Any help is greatly appreciated!
Edit: sample code
drop table tablea
create table TableA(
id int,
colA varchar(10),
colb varchar(10),
counts int
)
insert INTO TableA
(id, cola, colb, counts)
select 12, 'Bad', 'Cat', 3
insert INTO TableA
(id, cola, colb, counts)
select 13, 'Apple', 'Bad', 1
insert INTO TableA
(id, cola, colb, counts)
select 14, 'Cat', 'Dog', 2
select * FROM TableA
SELECT a.ID, a.ColA, a.ColB
FROM TableA a WITH (NOLOCK)
LEFT JOIN TableA b WITH (NOLOCK)
ON a.ID = b.ID
Where a.colB = b.ColA
ORDER BY a.ColA ASC
you just need to add ORDER BY clause
-- SELECT a.ID, a.ColumnA, a.ColumnB
-- FROM TableA WITH a (NOLOCK)
-- LEFT JOIN TableA b WITH (NOLOCK)
-- ON a.ID = b.ID
-- and a.counts = b.counts
-- Where a.columnB = b.ColumnA
ORDER BY a.ColumnA ASC
This is all you need. Sometimes you have to think simple
select * from table A
order by columnA asc
I have 2 tables.
Table 1:
Id Name
1 John
2 Mike
3 Sam
Table 2:
Name Data
John Data1
John Data1
John Data1
Mike Data2
Mike Data2
Sam Data3
If I write
select Table2.Name, Table2.Data
from Table1
inner join Table2 on Table1.Name= Table2.Name
I get all the duplicate data.
I would like to be able to retrieve something like:
John Data1
Mike Data2
Sam Data3
SELECT DISTINCT NAME
, DATA
FROM Table2
WHERE NAME IN (SELECT NAME
FROM Table1)
There are a few different options here...
There are two ways to do that.
You can use distinct clause:
select distinct t2.Name, t2.Data
from Table1 t1
inner join Table2 t2 on t1.Name= t2.Name
Here is link to MSDN.
You can use group by :
select t2.Name, t2.Data
from Table1 t1
inner join Table2 t2 on t1.Name= t2.Name
group by t2.Name, t2.Data
Here is link to MSDN.
I prefer second solution, because I always can add grouping functions.
Note:
In both queries I used aliases (t1,t2). It's more readable.
You can use CTE for this and apply a row_number()
;with cte as
(
select t1.name, t2.data,
row_number() over(partition by t1.id order by t1.id) rn
from table1 t1
inner join table2 t2
on t1.name = t2.name
)
select *
from cte
where rn = 1
Or a non-CTE version:
select *
from
(
select t1.name, t2.data,
row_number() over(partition by t1.id order by t1.id) rn
from table1 t1
inner join table2 t2
on t1.name = t2.name
) x
where rn = 1
see SQL Fiddle with Demo
Are you looking for something like:
select Table2.Name, Table2.Data, count(*) from Table1
inner join Table2 on Table1.Name= Table2.Name
group by Table2.Name, Table2.Data;
OK so I have a query I am trying to build.. I have 2 tables, table1 has a bunch of regular records as normal with a unique ID (auto increment) and table2 has records that include some of those ids from table1. I am trying to order by the highest records with that same ID in table1.. Heres what I've got:
SELECT * FROM table1
WHERE table1.status = 1
AND (SELECT COUNT(*) FROM table2 WHERE table2.tbl1_id = table1.id)
ORDER BY table1.id DESC
Thanks :)
SELECT table1.id
FROM table1
LEFT JOIN table2 ON table2.tbl1_id = table1.id
WHERE table1.status = 1
GROUP BY table1.id
ORDER BY COUNT(table2.tbl1_id) DESC
Try this:
SELECT a.*, b.cnt
FROM table1 a LEFT JOIN
(
SELECT tbl1_id, COUNT(*) cnt
FROM table2
GROUP BY tbl1_id
) b
ON a.id = b.tbl1_id
WHERE table1.status = 1
ORDER BY cnt DESC
I have a problem with recursive CTE query
Let's say that I have that category tree (Category table)
In my CTE query, I search for all children of the 1 category:
(that query works fine)
with mq as
(
select c.Id as parent, c.Id as child
from dbo.Category c
where c.Id = 1
union all
select q.child, c.Id
from mq q
inner join dbo.Category c on q.child = c.IdParentCategory
)
The output
Then, I want to get that Category ID, wchih doesn't have a child: categories 9,10,12,14,15
with mq as
(
select c.Id as parent, c.Id as child
from dbo.Category c
where c.Id = 1
union all
select q.child, c.Id
from mq q
inner join dbo.Category c on q.child = c.IdParentCategory
where child in
(
select c1.Id
from dbo.Category c1
where not exists(select c2.Id
from dbo.Category c2
where c2.Id = c1.IdParentCategory)
)
)
but the output is wrong:
why ? Any ideas will be helpful !
if I separate the query from CTE, everything is OK
declare #tab table
(parent int, child int);
insert into #tab
select * from mq
delete from #tab
where child in (
select c1.parent
from #tab c1
where not exists(select c2.parent from #tab c2 where c2.parent = c1.child)
)
with mq as
(
select c.Id as parent, c.Id as child
from dbo.Category c
where c.Id = 1
union all
select q.child, c.Id
from mq q
inner join dbo.Category c on q.child = c.IdParentCategory
)
select child from mq where child not in (select parent from mq)
Would seem to give the output you want - in fact your description of the problem almost took this form.