I have a table with the following data:
ID NAME ATTRIBUTE CODE
=============================
1 XX MM GC
1 XX ST GC
2 ZZ LL GC
2 ZZ ST GC
3 AA MM PC
I need the ID, name of from the table which contains either MM Attribute or GC code but not both. Like the query should retrieve the ID numbers only 2 and 3 but not 1 .
How do I do that?
Select Id, Name
From #t
Where (Attribute = 'MM' Or Code = 'GC')
And Id Not In (Select Id From #t
Where (Attribute = 'MM' And Code = 'GC'))
select distinct T1.ID,
T1.NAME
from T as T1
where (T1.ATTRIBUTE = 'MM' or T1.CODE = 'GC') and
not exists (select *
from T as T2
where T1.ID = T2.ID and
T2.Attribute = 'MM' and
T2.CODE = 'GC')
Result:
ID NAME
2 ZZ
3 AA
Try this:
select distinct ID
from TableName
where (Attribute = 'MM' or Code = 'GC')
and not (Attribute = 'MM' AND Code = 'GC')
Try this:
SELECT ID, NAME
FROM <YOUR-TABLE-NAME>
WHERE
(ATTRIBUTE = 'MM' AND CODE <> 'GC') OR
(ATTRIBUTE <> 'MM' AND CODE = 'GC') ;
select distinct Id, Name
from tbl?
where
(attribute = 'MM' or code = 'GC')
and not (attribute = 'MM' and code = 'GC')
(
SELECT ID, NAME FROM TableName WHERE CODE = 'GC'
EXCEPT
SELECT ID, NAME FROM TableName WHERE ATTRIBUTE = 'MM'
)
UNION
(
SELECT ID, NAME FROM TableName WHERE ATTRIBUTE = 'MM'
EXCEPT
SELECT ID, NAME FROM TableName WHERE CODE = 'GC'
)
Related
I'm trying to pull a dataset that returns records ONLY when there are two QUALIFERs present. I've tried left joins, populating data in temp tables then manipulating something, then numerous having clauses (resulting in subquery selects, and additional groups). I would appreciate any assistance on what I can do further.
Query:
Select E, CASE WHEN QUALIFER = '1' THEN 'NAME1' WHEN QUALIFER = '2' then 'NAME2' ELSE 'FINALNAME' END AS TYPE, count(rt.ID) 'Number '
from TABLE_ONE co (nolock)
join TABLE_TWO rt (nolock)
on co.ID = rt.ID
where co.E in (select * from #tempEmail)
AND convert(date,co.INSERTED_TIMESTAMP)between '1/1/2020' and '8/15/2020'
AND TRANS_STATUS = 'APPROVED'
group by E, QUALIFER
order by E, QUALIFER
Current resultset:
E TYPE Number
FAKEEMAIL1#Gmail NAME1 1
FAKEEMAIL1#Gmail NAME2 1
otheremailj#gmail.com Name1 21
Desired resultset:
E TYPE Number
FAKEEMAIL1#Gmail NAME1 1
FAKEEMAIL1#Gmail NAME2 1
Thank you.
Let's try the below query. I used a temp table to make things more simple for my mind.
if object_id('tempdb.dbo.#email') is not null drop table #email
create table #email
(
email varchar(50),
typeValue varchar(15),
Number int
)
insert into #email(email, typeValue, Number)
Select
E,
CASE WHEN QUALIFER = '1' THEN 'NAME1' WHEN QUALIFER = '2' then 'NAME2' ELSE 'FINALNAME' END AS TYPE,
count(rt.ID) 'Number '
from TABLE_ONE co (nolock)
join TABLE_TWO rt (nolock)
on co.ID = rt.ID
where co.E in (select * from #tempEmail)
AND convert(date,co.INSERTED_TIMESTAMP)between '1/1/2020' and '8/15/2020'
AND TRANS_STATUS = 'APPROVED'
group by E, QUALIFER
select a.email, a.typeValue
from #email a
inner join
(
select email, typeValue, rank() over (partition by email order by typeValue) as typeCount
from #email t
) as b
on b.email = a.email
and b.typeCount > 1
Name Value AnotherColumn
-----------
Pump 1 8000.0 Something1
Pump 1 10000.0 Something2
Pump 1 10000.0 Something3
Pump 2 3043 Something4
Pump 2 4594 Something5
Pump 2 6165 Something6
My table looks something like this. I would like to know how to select max value for each pump.
select a.name, value from out_pumptable as a,
(select name, max(value) as value from out_pumptable where group by posnumber)g where and g.value = value
this code does the job, but i get two entries of Pump 1 since it has two entries with same value.
select name, max(value)
from out_pumptable
group by name
select name, value
from( select name, value, ROW_NUMBER() OVER(PARTITION BY name ORDER BY value desc) as rn
from out_pumptable ) as a
where rn = 1
SELECT
b.name,
MAX(b.value) as MaxValue,
MAX(b.Anothercolumn) as AnotherColumn
FROM out_pumptabl
INNER JOIN (SELECT
name,
MAX(value) as MaxValue
FROM out_pumptabl
GROUP BY Name) a ON
a.name = b.name AND a.maxValue = b.value
GROUP BY b.Name
Note this would be far easier if you had a primary key. Here is an Example
SELECT * FROM out_pumptabl c
WHERE PK in
(SELECT
MAX(PK) as MaxPK
FROM out_pumptabl b
INNER JOIN (SELECT
name,
MAX(value) as MaxValue
FROM out_pumptabl
GROUP BY Name) a ON
a.name = b.name AND a.maxValue = b.value)
select Name, Value, AnotherColumn
from out_pumptable
where Value =
(
select Max(Value)
from out_pumptable as f where f.Name=out_pumptable.Name
)
group by Name, Value, AnotherColumn
Try like this, It works.
select * from (select * from table order by value desc limit 999999999) v group by v.name
Using analytic function is the easy way to find max value of every group.
Documentation : https://learn.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql?view=sql-server-ver15
Select name,
value,
AnotherColumn
From(
SELECT Row_Number() over(partition by name order by value desc)as
row_number, *
FROM students
)
Where row_number = 1
SELECT t1.name, t1.Value, t1.AnotherColumn
FROM mytable t1
JOIN (SELECT name AS nameMax, MAX(Value) as valueMax
FROM mytable
GROUP BY name) AS t2
ON t2.nameMax = t1.name AND t2.valueMax = t1.Value
WHERE 1 OR <anything you would like>
GROUP BY t1.name;
SELECT DISTINCT (t1.ProdId), t1.Quantity FROM Dummy t1 INNER JOIN
(SELECT ProdId, MAX(Quantity) as MaxQuantity FROM Dummy GROUP BY ProdId) t2
ON t1.ProdId = t2.ProdId
AND t1.Quantity = t2.MaxQuantity
ORDER BY t1.ProdId
this will give you the idea.
for example i have column that look like this:
name | id | value
A 1 aa
A 2 ab
B 3 bc
C 4 ca
C 5 cb
Is there any way to change it to this ?
name | id | value
A 1 aa
A 1 ab
B 3 bc
C 4 ca
C 4 cb
You can do this with a window function that numbers the rows, and use that select statement to supply the values for the update:
update the_table
set id = t.rn
from (
select name,
id,
dense_rank() over (order by name) as rn
from the_table
) t
where (t.name, t.id) = (the_table.name, the_table.id);
SQLFiddle example: http://sqlfiddle.com/#!15/0e987/1
This assumes that the existing combination (id, name) is unique. If that is not the case, you would need to use the ctid column to match the rows between the inner select and the table itself:
update the_table
set id = t.rn
from (
select name,
id,
ctid,
dense_rank() over (order by name) as rn
from the_table
) t
where t.ctid = the_table.ctid;
How I can put name on subquery's output, instead of ?column??
The SELECT's output is:
._______.__________.__________.
|__id___|_?column?_|_?column?_|
|_31886_|____12____|____13____|
What I want is:
._______.__________.__________.
|__id___|___ages___|_validate_|
|_31886_|____12____|____13____|
What my SELECT look like:
SELECT g.id,
(select dt from pcdhidro where nomepcd=31886 order by datahora asc limit 1),
(select dt from pcdhidro_2003_96 where nomepcd=31886)
FROM gestpcd_2 g
WHERE g.tipo = 'HIDRO' and g.id = 31886
(Select X from Y) AS alias
So in your case:
SELECT g.id,
(select dt from pcdhidro where nomepcd=31886 order by datahora asc limit 1) as ages,
(select dt from pcdhidro_2003_96 where nomepcd=31886) as validate
FROM gestpcd_2 g
WHERE g.tipo = 'HIDRO' and g.id = 31886
I have a table and it's data looks like this:
id name date
--------- --------- ----------
1 a 2012-08-30 10:36:27.393
1 b 2012-08-30 14:36:27.393
2 c 2012-08-30 13:36:27.393
2 d 2012-08-30 16:36:27.393
I retrieve the max date time with this query:
select
t1.id
,t1.name
,t1.date
from
table1 t1
inner join (
SELECT id,Max(date) as mymaxdate
FROM table1
group by id
) mt1
on t1.id = mt1.id
and t1.date = mt1.mymaxdate
result:
1 b 2012-08-30 14:36:27.393
2 d 2012-08-30 16:36:27.393
how can write this query in entity?
Thanks
The tricky part is that you also want the name of the item having the max date, otherwise the grouping would be much simpler. But it is possible by an almost 1:1 reproduction of the sql query:
from t in table1
join m in
(from t in table1
group t by t.id into g
select new { g.Key, mymaxdate = g.Max (x => x.date) })
on new { t.id, t.date } equals new { id = m.Key, date = m.mymaxdate }
select t
A join on multiple fields is done by creating anonymous types (new {t.id, t.date} and new {id = m.Key, date = m.mymaxdate}) where the names and types of the properties (id and date) should match.