how to use 'NoT IN' and 'IN' at same time in Query - sql

I have Two Tables Let Say "Table1" and "Table2"
Table1
ID AccountId BranchId otherColumn
----------- ---------- ---------- -----------
1 15 58 data
2 22 62 data
3 31 89 data
4 49 45 data
. . . .
. . . .
. . . .
. . . .
. . . .
Table2
ID fromAccount toAccount ExcludeAccount IncludeAccount FromBranch ToBranch IncludeBranch ExcludeBranch
----------- ---------- ---------- ----------- ---------- --------- ------------ -------------- ------------
1 1 90 89,34,3 101 30 100 205,207,250 35,40
1 5 67 90 22 50 70,90 20
2 7 4 3 5 200
2 1 5 7 10 16 9
3 5 89 6,7 200 55 243 34 35,200,201,234
Now I want to select All data from Table1 using expression from Table1
I have Function that converts the comma separated text to table
select data from dbo.split('23,45,2', ',')
this will return
data
------
23
45
2
My Desired Output for row 1 is
ID AccountId BranchId otherColumn
----------- ---------- ---------- -----------
. 1 . data
. 2 . data
. 4 . data
. 5 . data
. 6 . data
. 7 . data
. 8 . data
. . . .
. . . .
. 33 . .
. 35 . .
. . . .
. 88 . .
. 90 . .
. 101 . .
. . 30 .
. . 31 .
. . . .
. . 34 .
. . 36 .
. . . .
I have created query to get data with relation of these two table But it always returns no row
here is my query
select * from Table1
inner join Table2 on Table1.AccountId between Table2.fromAccount and Table2.toAccount
and Table1.AccountId not in (select data from dbo.split(Table2.ExcludeAccount, ','))
and Table1.AccountId in (select data from dbo.split(Table2.IncludeAccount, ','))
and Table1.BranchId between Table2.FromBranch and Table2.ToBranch
and Table1.BranchId not in (select data from dbo.split(Table2.ExcludeAccount, ','))
and Table1.BranchId in (select data from dbo.split(Table2.IncludeAccount, ','))
my question is, why it always returns no data is any thing wrong in my query or i am doing it in wrong way

not in is generally not what you want with a subquery. If any of the values returned by the subquery are NULL, then nothing at all passes the filter. Instead, get used to using not exists.
For instance, instead of:
not in (select data from dbo.split(Table2.ExcludeAccount, ','))
Use:
not exists (select 1
from dbo.split(Table2.ExcludeAccount, ',') s(p)
where Table1.BranchId = s.p
)
You may also have a problem with data types, but SQL Server should be converting them correctly.

Related

Create Multiple Rows based on condition

I have a table that looks like this
id
name
count
1
Nishu
4
2
Shivam
2
3
Himanshu
1
I want to get the Output like this:-
id
name
count
1
Nishu
4
1
Nishu
4
1
Nishu
4
1
Nishu
4
2
Shivam
2
2
Shivam
2
3
Himanshu
1
3
Himanshu
1
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
You can use a cross join against generate_series()
select t.*
from the_table t
cross join generate_series(1, t.count) as g
order by t.id;
Online example
Using RECURSIVE CTE you can do:
WITH RECURSIVE cte as (
SELECT 1 as x,m.* FROM mytable m
union all
SELECT x+1,m.*
FROM cte,mytable m
WHERE x<m.count)
SELECT DISTINCT *
FROM cte
ORDER BY count DESC;
see: DBFIDDLE
more info:
WITH Queries (Common Table Expressions)
Learn PostgreSQL Recursive Query By Example

Select entries between two dates with different years

I have the following table structure:
id val1 val2 val3 date
1 1 2 3 14.12.2021
2 2 3 5 17.12.2021
3 4 6 8 18.12.2021
. . . . .
. . . . .
. . . . .
9 3 4 5 04.01.2022
so far i use the following command:
SELECT * FROM table WHERE `date` >= '13.12.2021' and `date` <= '24.12.2021'
but if i want to go into the new year, the command returns me no value
SELECT * FROM table WHERE `date` >= '13.12.2021' and `date` <= '04.01.2022'
Does anyone have an idea how I have to modify the command so that it works?
Because you store date in wrong format you should transform it in where clause:
select *
from test
where substr(date,7)||'-'||substr(date,4,2)||'-'||substr(date,1,2) between '2021-12-13' and '2022-01-04';
SQLite sandbox here
But better way is - store dates in proper format YYYY-MM-DD

Need the expected output from Hive/SQL query

Input:
1
2
3
4
.
.
.
Expected Output:
1
2
2
3
3
3
4
4
4
4
.
.
..
.
Need to find the Hive /SQL query for the expected output like this
Below is the query, have used CTE with 3 records as my input.
hive> with t1 as (select 1 as col1 union select 2 as col1 union select 3 as col1) select t11.col1 from t1 as t11,t1 as t12 where t11.col1>=t12.col1;

How to print the oracle db table like shown below?

I have a table like shown below:
name field value
---- ----- -----
a p 1
a q 2
b p 5
c q 7
d p 4
e p 3
e q 2
. . .
. . .
. . .
Expected Output :
name p q . . . . .
---- --- --- --- --- --- --- ---
a 1 2
b 5
c 7
d 4
e 3 2
. . .
. . .
. . .
How to the field elements in column like shown above...
I think you can use a query like this:
select name,
sum(case when field = "p" then value else 0 end) p,
sum(case when field = "q" then value else 0 end) q,
...
from yourTable
group by name;

Dynamically building a table for a report

I have a temporary table that hold all items available
#TableA
ItemId ItemName ItemVer
1 Name1 2
2 Name2 9
3 Name3 5
.
.
.
500 Name500 3
I then have another that holds all the dates a Snapshot was taken along with the SnapshotId
CREATE TABLE #tmpSnapshot_Dates(
[SnapshotId] [uniqueidentifier] NOT NULL,
[DateTaken] datetime NOT NULL)
INSERT INTO #tmpSnapshot_Dates SELECT SnapshotId, DateTaken From Snapshot_Info Where DateTaken <= #EndDate
Now for each date in the #tmpSnapshot_Dates table I want to get a count that relates it each Item
SELECT ItemId, Count(*) From Items Where SnapshotId = #SnapshotId
And then update #TableA by adding a column
DECLARE #ColName VARCHAR(100)
SET #ColName = 'Installed ' + CONVERT(Varchar, #DateA, 6)
exec('ALTER TABLE #TableA ADD ['+#ColName+'] int NOT NULL CONSTRAINT [DF_#TableA_Installed on] DEFAULT 0')
And then insert the data into the new column. I then need to repeat this for each date in #tmpSnapshot_Dates
giving a resulting #TableA that looks something like
#TableA
ItemId ItemName ItemVer Installed 01 Jan 12 Installed 07 Feb 12
1 Name1 2 34 33
2 Name2 9 56 59
3 Name3 5 12 26
.
.
.
500 Name500 3 98 106
My questions are
Am I creating a nightmare here by approaching it the wrong way?
How am I best to loop through #tmpSnapshot_Dates and then get the data to create the next column? I always try to avoid cursors but is this one of those situation I need to use one?
There could be 12 columns to add and not every item will be returned for each date.
Definitely it won't be nice, but yes, you need to use cursors and iterate over the #tmpSnapshot_Dates, but if you have the option i suggest you to use a reporting software with the capability to create pivot grids.
Would it not be better to have a separate (temp?) table referencing the ItemId in #TableA eg:
InstalledDate ItemId Count
------------- ------ -----
2012-01-01 1 34
2012-01-01 2 56
2012-01-01 3 12
. . .
. . .
. . .
2012-01-01 500 98
2012-02-07 1 33
2012-02-07 2 59
2012-02-07 3 26
. . .
. . .
. . .
2012-02-07 500 106