sql Query to get active Columnnames of table - sql

I'm working with sql server 2008.I have 2 tables table1 contains,
id name chk1 chk2 chk3
1 aaa 0 0 1
2 bbb 2 2 1
3 ccc 1 0 2
table2 contains
chk_code chk_name flag
CH1 chk1 1
CH2 chk2 0
CH3 chk3 1
i want to get the result set like,
Column_Name
id
name
chk1
chk3
the check having flag value 0 doesnot appear in the resultset?

Use this query:
with x as (
select *
from sys.columns c
left join table2 t2 on c.name = t2.chk_name
where c.object_id = object_id('table1')
)
select name
from x
where flag = 1 or flag is null

You can achieve that using unpivot... But problem with unpivot is all unpivoted column name must be of same data type that to same size... if that is okay then you can use unpivot like this
SELECT Column_Name
FROM (SELECT *
FROM
(SELECT TOP 1 * FROM
tab1) p
UNPIVOT
(tab1 FOR Column_Name IN
([id], [name], [chk1], [chk2], [chk3])
)AS unpvt) AS vv WHERE Column_Name NOT IN
(SELECT chk_name FROM tab2 WHERE flag = 0)
SQLFiddle of above

Related

Concat query result with equals values into one row

I have the following query result:
Id
FKId
Count1
Count2
CountAll
Name
OriginalName
1
100
1
0
1
Food
Some value
1
101
0
1
1
Drink
Some value
1
102
1
0
1
Drink
Some value
What I want to do is to return all the FKId with same Name value into one row, something like this:
Id
FKId
Count1
Count2
CountAll
Name
OriginalName
1
100
1
0
1
Food
Some value
1
101, 102
1
1
2
Drink
Some value
At the moment this is the query that returns the 3 rows:
SELECT
t1.Id, t2.FkId,
COUNT(some case when here) [Count1],
COUNT(some case when here) [Count2],
COUNT(t2.FkId) [CountAll],
t3.Name, t3.OriginalName
FROM
table1 t1
JOIN
table2 t2 ON t1.Id = t2.Id
JOIN
table3 t3 ON t2.FKId = t3.FkId
GROUP BY
t1.Id, td.FkId, t3.Name, t3.OriginalName
I tried adding this to my current query:
DECLARE #Ids VARCHAR(8000)
(SELECT #Ids= COALESCE(#Ids+ ', ', '') + t2.FkId),
But I'm getting an error
Incorrect syntax near '='
You want to use STRING_AGG function.... something like
SELECT STRING_AGG(t2.FkId, ', ') as ID_LIST
Here is the MS documentation on it:
https://learn.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-ver16

Update a table based on the count of specific records in the same table

I'm using Microsoft SQL Server and I have a table shown below in my database:
Id groupName flag type
--------------------------------
1 aaa 0 0
2 aaa 1 0
3 aaa 0 0
4 bbb 0 0
5 bbb 0 0
6 bbb 0 0
I'd like to update the type column values based on the count of specific records in the same table.
Assuming that the records are grouped by their groupName, if a group has at least 1 record whose flag = 1, the typevalues of all the records in the same group should be 1, else 0.
In my case, I'd like to get the following output.
Id groupName flag type
--------------------------------
1 aaa 0 1
2 aaa 1 1
3 aaa 0 1
4 bbb 0 0
5 bbb 0 0
6 bbb 0 0
How can I do this with a SQL statement?
Edit
The following, of course, does not work.
UPDATE t1
SET type = CASE
WHEN ((SELECT COUNT(*) FROM tg) > 0)
THEN 1
ELSE 0
END
FROM [sampleDB].[dbo].[groups] t1
JOIN (SELECT t2.groupName, t2.flag
FROM [sampleDB].[dbo].[groups] t2) tg ON tg.groupName = t1.groupName
AND tg.flag = 1;
You can try the below -
UPDATE t
SET
type = case when cnt>=1 then 1 else 0 end
FROM
tablename t
INNER JOIN (select group_name, count(case when flag=1 then 1 end) as cnt from tablename
group by group_name)t1
ON t.group_name= t1.group_name
I would use window functions:
with toupdate as (
select g.*, max(flag) over (partition by groupname) as new_flag
from [sampleDB].[dbo].[groups] g
)
update toupdate
set flag = new_flag
where flag <> new_flag;
Note the where clause so only rows where the flag is changing are updated. SQL Server attempts to update row even when no values change, incurring overhead. The where clause saves most of this overhead.
You can simply do it by using a simple JOIN as:
UPDATE D
SET Type = 1
FROM Data D JOIN
(
SELECT GroupName
FROM Data
WHERE Flag = 1
) T ON D.GroupName = T.GroupName;
Here is a db-fiddle
This is the most performant way to solve this problem by using exists statement:
update t1
set type = 1
from table t1
where exists ( select 1
from table t2
where t1.groupname = t2.groupname
and flag = 1
)
This could be other option
;with data
As (
Select count(groupname) groupname
,max(flag) flag
,type
From Tablename
Group by type
Having count(groupname)>1
And max(flag)=1
)
Update c
Set type =1
From data c

TSQL - Select rows with same column A but different column B

I'm trying to find rows (Name) that does not have ID = 1. For example, if my table looked like this:
Name ID
--------------
A 1
A 0
B 1
B 0
C 0
D 2
D 0
The answer to this query would be:
Name
-----
C
D
Do you have any idea?
SELECT Name
FROM myTable
GROUP BY Name
HAVING SUM(CASE WHEN ID = 1 THEN 1 ELSE 0 END) = 0
Here is one way to do it:
SELECT DISTINCT Name
FROM Table t0
WHERE NOT EXISTS
(
SELECT 1
FROM Table t1
WHERE t0.Name = t1.Name
AND t1.Id = 1
)
Try this query:
SELECT DISTINCT(name)
FROM tbl t1
WHERE
NOT EXISTS (SELECT name FROM tbl t2 WHERE ID=1 AND t1.name=t2.name)
Select Distinct name
From myTable
Where name not in (Select name From myTable Where id= 1)

Select Grouped Column Values Where Have Same Id In SQL Server

I have a table like this.
TABLE-1
id Code
-----------------
1 N188
1 N1Z2
1 N222
2 N189
2 N1Z2
2 N1Z3
3 N188
3 A123
3 B321
4 N188
4 A333
4 B444
I want to select id and code only code has N188.Result should like this:
TABLE-2
id Code
---------------
1 N188
1 N1Z2
1 N222
3 N188
3 A123
3 B321
4 N188
4 A333
4 B444
How can I write sql for this in SQL Server?
Thanks
You can use EXISTS for this:
SELECT id, code
FROM table1 t
WHERE EXISTS (
SELECT 1
FROM table1 t2
WHERE t.id = t2.id
AND t2.Code = 'N188'
)
Condensed SQL Fiddle Demo
Using INNER JOIN
SELECT *
FROM tablename A
JOIN (SELECT id
FROM tablename
WHERE code = 'N188') B
ON a.id = b.id
Here is an alternative method that uses window functions:
select id, code
from (select t.*,
sum(case when code = 'N188' then 1 else 0 end) over (partition by id) as cnt_n188
from table t
) t
where cnt_n188 > 0;

Join and a Pivot statement SQL Server

I want to pivot and join to select from 3 tables
Table 1: INT,VARCHAR,FLOAT
ID Name value
---------------------------
1 a1 32116580
2 a2 50785384
3 a3 54327508
4 a4 61030844
Table 2: INT, VARCHAR, FLOAT
ID Name value
---------------------------
1 x11 61326085092
2 x12 80368184260
3 x13 83023398776
4 x14 91144307692
5 x22 95486535484
6 x23 90357090612
7 x24 100588807668
8 x33 707811916752
9 x34 93128452928
10 x44 84566653668
Table 3: INT, VARCHAR, FLOAT
ID Name value
---------------------------
1 q1 61326085092
2 q2 95486535484
3 q3 707811916752
4 q4 84566653668
output table:
column1 column2 column3 column4
--------------------------------------------------------------------------
a1*a1/(q1+q1+x11) a1*a2/(q1+q2+x12) a1*a3/(q1+q3+x13) a1*a4/(q1+q4+x14)
null a2*a2/(q2+q2+x22) a2*a3/(q2+q3+x23) a2*a4/(q2+q4+x24)
null null a3*a3/(q3+q3+x339 a3*a4/(q3+q4+x34)
null null null a4*a4/(q4+q4+x44)
(I'm putting the 'Name' of the column of the 3 different tables instead of numbers)
How to do this?
I guess I have to do two pivots? and
unpivot?...
Well do not how to complete it..
SELECT *
FROM (
SELECT
t1.a1,
t1.a2,
t2.x,
t3.q
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
...
) Output
PIVOT (
name IN (
...
PIVOT(name ... )
)
) PivotTable
UPDATE
Previously I have *'s I have changed it to division and sum, the *'s were just an example,
Sample tables
create table Table1(ID int, Name varchar(10), value float)
insert table1 select
1 ,'a1', 32116580 union all select
2 ,'a2', 50785384 union all select
3 ,'a3', 54327508 union all select
4 ,'a4', 61030844
create table Table2(ID int, Name varchar(10), value float)
insert Table2 select
1 ,'x11', 61326085092 union all select
2 ,'x12', 80368184260 union all select
3 ,'x13', 83023398776 union all select
4 ,'x14', 91144307692 union all select
5 ,'x22', 95486535484 union all select
6 ,'x23', 90357090612 union all select
7 ,'x24', 100588807668 union all select
8 ,'x33', 707811916752 union all select
9 ,'x34', 93128452928 union all select
10 ,'x44', 84566653668
create table Table3(ID int, Name varchar(10), value float)
insert Table3 select
1 ,'q1', 61326085092 union all select
2 ,'q2', 95486535484 union all select
3 ,'q3', 707811916752 union all select
4 ,'q4', 84566653668
The query you need, for N = 4. For any other N, just use dynamic SQL to build the query, changing the 2 lines required as indicated by **.
;with coords(i,row,col,total,N) as (
select 1,1,1,N.N*(N.N+1)/2, N.N
from (select count(*) N from table1) N
union all
select i+1,
case when col+1>N then row+1 else row end,
case when col+1>N then row+1 else col+1 end,
total, N
from coords
where i<total
)
select [1],[2],[3],[4] -- **, e.g. ,[5],[6],etc
from
(
select
c.row,
c.col,
cellvalue= ar.value*ac.value/(qr.value+qc.value+x.value)
from coords c
inner join table1 ar on ar.id = c.row
inner join table1 ac on ac.id = c.col
inner join table3 qr on qr.id = c.row
inner join table3 qc on qc.ID = c.col
inner join table2 x on x.ID = c.i
) p
pivot (max(cellvalue) for col in ([1],[2],[3],[4])) pv -- **
order by row
Output:
1 2 3 4
---------------------- ---------------------- ---------------------- ----------------------
5606.50338459295 6876.83326310711 2047.51559459649 8269.17991568225
NULL 9003.55641750708 3087.36780924588 11044.2303130135
NULL NULL 1389.95405212248 3744.35614651666
NULL NULL NULL 14681.7678040306
The dynamic version
declare #Sql nvarchar(max)
select #Sql = ISNULL(#sql + ',', '') + QUOTENAME(RIGHT(number,10))
from master..spt_values
where type='P' and number between 1 and (select COUNT(*) From table1)
set #Sql = '
;with coords(i,row,col,total,N) as (
select 1,1,1,N.N*(N.N+1)/2, N.N
from (select count(*) N from table1) N
union all
select i+1,
case when col+1>N then row+1 else row end,
case when col+1>N then row+1 else col+1 end,
total, N
from coords
where i<total
)
select ' + #sql + '
from
(
select
c.row,
c.col,
cellvalue= ar.value*ac.value/(qr.value+qc.value+x.value)
from coords c
inner join table1 ar on ar.id = c.row
inner join table1 ac on ac.id = c.col
inner join table3 qr on qr.id = c.row
inner join table3 qc on qc.ID = c.col
inner join table2 x on x.ID = c.i
) p
pivot (max(cellvalue) for col in (' + #sql + ')) pv
order by row
option (maxrecursion 0) -- ! use with caution
'
exec(#sql)