How to achive this without using Sub Query, CTE and Prodedure - sql

I have a table with 2 fields
CREATE TABLE Temp_tab
(
id int identity primary key,
value float
);
INSERT INTO Temp_tab(value)
VALUES (65.09),(17.09);
I want to select all the records that are greater than Avg(Value).
Say... Select * from temp_tab where value > (select avg(value) from temp_tab);
This above query(using subquery) gives me the expected output
1 65.09
I want to achieve this without using Sub Query, CTE and Prodedure, since i am using Spark DB. Spark Db does not support Sub Queries, CTE and Prodedures

You can do this quite painfully with a cross join and aggregation:
Select t1.id, t1.value
from temp_tab t1 cross join
temp_tab t2
group by t1.id, t1.value
having t1.value > avg(t2.value);
As a note: Spark SQL claims to support subqueries (see here). So, your original query should work. If it only supports subqueries in the from clause, then you can do:
Select t.*
from temp_tab t join
(select avg(value) as avgvalue from temp_tab) a
on t.value > a.avgvalue;

spark-sql accept this query under version of 1.6.x
select * from (select * from tenmin_history order by TS_TIME DESC limit 144) a order by TS_TIME
This query solved my problem.

Related

How to select corresponding value on last month on SQL Server (I think it is a simple query)

I have the following table:
I need to make query that returns the corresponding value for each code on last month, like the following:
Like I said, I think it is a simple query, but I can't do.
Just use lag():
select t.*, lag(value) over (partition by code order by date) as value_last_month
from t;
In older versions of SQL Server, you can use apply:
select t.*, t2.value as value_last_month
from t outer apply
(select top (1) t2.*
from t t2
where t2.code = t.code and t2.date < t.date
order by date desc
) t2;

Select count(*) from table where (multiple id) in (table)

Is there a way to write
SELECT count(*) from tablename where (multiple_ids_here) in (SELECT id from tablename)
Normally, I would write:
select count(*) from tablename
where id_1 in (SELECT id from tablename)
OR id_2 in (SELECT id from tablename)
id_3 in (SELECT id from tablename)
which very inefficient if we have multiple values.
Anyone?
EDIT: Question updated. What if I want to select count?
Your version with three ins is probably the most efficient way of doing this. If you want a comparison to try, you can use exists:
select . . .
from t t1
where exists (select 1
from tablename t2
where t2.id in (t1.id_1, t1.id_2, t1.id_3)
);
I should also note that storing ids in multiple columns like this is usually a sign of a problem with the data model. You probably want a table with one row per id, rather than one column per id. Such a format would also simplify this type of query.
For the updated question regarding getting a count(*)... using cross apply() with values() to unpivot your data in a common table expression:
;with cte as (
select t.Id, v.RelatedId
from t
cross apply (values (id_1),(id_2),(id_3)) v(RelatedId)
)
select
cte.Id
, RelationCount = count(*)
from cte
inner join RelatedTable r
on cte.RelatedId = r.Id
group by cte.Id
I am not sure i understand your question could you give an example of the data you are using and the out come.
From what i understand you could use a cte like this .
;WITH Sales_CTE ([counts],CustomerID, SalespersonPersonID,PickedByPersonID)
AS
(
select count(*),CustomerID,SalespersonPersonID ,PickedByPersonID
from [WideWorldImporters].[Sales].[Orders]
group by CustomerID,SalespersonPersonID,PickedByPersonID
)
SELECT sum([counts])
FROM Sales_CTE
GO
It would give you a result like this . You would jsut have to change the columns around .

How to get duplicate text values from SQL query

I have to get table only with duplicate text values using SQL query. I have used Having count(columnname) > 1 but I'm not getting result, only with duplicate values instead getting all values.
Can anyone suggest whether I have to add anything to my query?
Thanks.
Use the below query. mention the column which is getting duplicated in the patition by clause..
with CTE_1
AS
(SELECT *,COUNT(1) OVER(PARTITION BY LTRIM(RTRIM(REPLACE(yourDuplicateColumn,' ',''))) Order by -anycolunm- ) cnt
FROM YourTable
)
SELECT *
FROM CTE_1
WHERE cnt>1
Assuming id is a primary key
select *
from myTable t1
where exists (select 1
from myTable t2
where t2.text = t1.text and t2.id != t1.id)
You can use similar to following query:
SELECT
column1, COUNT(*)
FROM table
GROUP BY column1
HAVING COUNT(*) > 1

SUM and SELECT ALL

I need a correction please
SELECT *, SUN(mytable2.quantite)
FROM mytable1
INNER JOIN mytable2
ON mytable1.id = mytable2.id_table_article
I want to select all columns and SUM one column. How I can do that please ?
I have a problem because I think SUM works with ExecuteScalar and SELECT * works with ExecuteReader()
:( how I cant fusion this result because I need to show this result at on my listview so I need one request :/
I work with SQLIte
I suspect you want every column from mytable1 and the corresponding sum from mytable2. If so, you can use a subquery:
SELECT t1.*,
(SELECT SUM(t2.quantite)
FROM mytable2 t2
WHERE t1.id = t2.id_table_article
) as quantite
FROM mytable1 t1;

Sql Server Pagening with large amount of records

I have this SQL query for pagination:
SELECT * FROM
(
SELECT T1.*,T2.*, ROW_NUMBER() over(ORDER BY ID DESC) row
FROM
table1 t1
LEFT JOIN
table2 t2 on t1.id = t2.pid
) tbl
WHERE row>= #start and row<#end
Now the problem is that the select result can be thousands of records, that will be executed for each page of each users.
Any suggestion that I can part the select (select less records?)
the ROW_NUMBER could be over order by ID or DATE.
and by the way, selecting * is just for simplicity of the sample code.
If you have SQL Server 2012 or above you can use the Offset and Fetch keywords as stated here