sql generate group on related groups [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Sorry, if question unclear, i had misstakes it first tables. I made some updates:
Database: PostgreSQL
I want to group table based on transition (if a=b & b=c then a=c)
Adding a pair (4,c) will merge 2 groups to one "group1".

i assume u want a.b.c to be group1 and the d as group2..
the groupby will work perfectly fine with aliases..
but the number of group op wants is 3 millions groups so a stored proc with a incremental in the end and group by will work fine..

From your comments, it looks like you want to find out transitive relationship.
You can do that with following query. But if the goal here is just to identify the relationship among different groups with their respective id, i guess you can afford to have groups which are not getting incremented with 1.
According to your given example in OP, i think it won't affect you if end result has group1 and group5 instead of group2.
If mention result is fine then you can do that with following updated query. Giving group names in successive manner will impact on query performance which you don't want as you've 3 million of groups.
Please try following query:
select t1.id, concat('group', min(t2.minId)) groups
from t1
join
(select min(id) minId, groups
from t1
group by groups
) t2
on t1.groups = t2.groups
join (select #cnt := 1)y
group by t1.id;
Demo : Click here

Related

oracle sql 12 how to display the number of current account is not displaying , any suggestions please?the query and table below [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
select distinct b.brid,
(select count(1) from account a
where a.brid= b.brid
and a.acctype='current'
) as num_accounts,
b.baddress.street, b.baddress.city, b.baddress.p_code
from branch b;
the table should display the number of current account,but nothing is showing
Query you posted doesn't make sense. What is b.baddress.street? The way you put it,
b is user
baddress is table
street is column
but what is b.brid, then?
You posted screenshot of ... what? Desired result? Can't be as it displays 2 columns, while query returns 5 of them.
Anyway: this is how it might be done. Try to adjust it to your table(s). Mind letter case (is it really 'current'? Maybe 'CURRENT'? Or ...?)
SELECT b.brid,
COUNT (*) num_accounts,
b.street,
b.city,
b.p_code
FROM branch b JOIN account a ON a.brid = b.brid
WHERE a.acctype = 'current'
GROUP BY b.brid,
b.street,
b.city,
b.p_code;
You can use a correlated subquery for this purpose:
select b.*, -- or whatever columns you want
(select count(1)
from account a
where a.brid = b.brid and a.acctype = 'current'
) as num_accounts
from branch b;
In other words, you appear to have an error in how your are referencing columns in b, but the actual part of the query that does the count is correct.

SQL Server Temp Table to a Select Distinct Count Distinct quetsion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Ok, basically I've got a lot of temp tables I've created and I'm trying to create Validation for the ProvDiff table.
DROP TABLE #ProvDiff;
IF OBJECT_ID ('temp.dbo.#ProvDiff') IS NOT NULL
DROP TABLE #ProvDiff;
SELECT *
INTO #ProvDiff
FROM
(SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]) ProvDiff;
SELECT DISTINCT COUNT(DISTINCT ???) AS 'Unique EI NPIS'
FROM #ProvDiff
In my head it seems like the differences should be able to produce a result and I should be able to do a count on that. But for the life of me I can't figure out how to do that. If I do a count on rendering or pay to then those numbers wouldn't necessarily reflect the value for what are above. I know how many of each are produced for above validation.
Any help would be greatly appreciated
Is this what you want?
SELECT COUNT(*)
FROM (SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]
) ProvDiff;
I don't see why a temporary table would be used for this.
For better or worse, SQL Server does not support select count(distinct *), so you pretty much need a subquery.

Remove null values in SQL server [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Hello, Does anuone knows how can I remowe the null values corresponding to the description of the same product key? So, I have one product Key and its description in each language.
I'm trying to use the coalesce function, but it doesnt return me anything.
It looks like you want to combine the rows together for each ProductKey.
So instead of 5 lines, each with a single column populated, you want one line, with all 5 columns populated.
Do it like this:
Select Distinct T.ProductKey, C1.Column1, C2.Column2, C3.Column3 from MyTable T
left join MyTable C1 on C1.ProductKey = T.ProductKey and C1.Column1 is not null
left join MyTable C2 on C2.ProductKey = T.ProductKey and C2.Column2 is not null
left join MyTable C3 on C3.ProductKey = T.ProductKey and C3.Column3 is not null
Just replace "MyTable" above with your table name, and "Column1, Column2, Column3" with the names of the columns your data is in.
Pretend that each column is on its own separate table, and you need to use joins to connect all the tables back to your master set of ProductKeys.
Think about it in sets:
Basically you are going to make one master list of the keys that is distinct/unique (step 1), and then do a new left join for each column you want to attach to the master list (step 2), and as part of the joins, tell it to get the non-NULL values and ignore the NULLs.
you can use GROUP BY as below.
SELECT ProductKey,
MAX(C_ar_description) AS C_ar_description,
MAX(C_en_description) AS C_en_description,
MAX(C_fr_description) AS C_fr_description
FROM YourTable
GROUP BY ProductKey
You should use the results to fix the data so you dont need to do this every time.

how this query is working.....can anyone explain [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
SELECT productid,productname,price
FROM products E1
WHERE 4 = (SELECT count(*)
FROM products E2
WHERE E1.price =E2.price)
It's working like this
SELECT productid,productname,price
FROM products E1
WHERE (SELECT count(*) FROM products E2 WHERE E1.price =E2.price) = 4
:) Now does it make more sense?
Although it can be simplified
SELECT productid,productname,price,COUNT(*) AS c
FROM products
GROUP BY PRICE
HAVING COUNT(*) = 4
The outer query ill scan all products.
For each product the subquery ill count how many products got the same price.
The filter (where clause) ill avoid subquery counts different from four.
The output ill be all products where there are four products with same price.
If anyone is using MS-SQL the same thing can be done using aggregates (count and having)
Edit Hanky already posted the MS-SQL equivalent query using count and having

How to find rows with similar data as a specific row in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let me make it clear -
I have a table with information such as CourseID, Semester, GPA
I need to find all the CourseID's that have the same GPA(and some more fields) as CourseID='999'
I would also like a solution with AND without nested SELECT
Thanks!
So I have to find all the courseCode that has the same GPA and FailPerc as (Code 999, Year 2011, Sem B, Date 2)
Hope it's more clean now
this might work...
select c1.*
from course c1
inner join course c2 on c1.pga= c2.pga
where c2.courseid = 999
and c1.courseid <> c2.courseid
with subselects
select c1.*
from couser c1
where pga = (select pga
from course c2
where c2.courseid=999)
and c1.courseid <> 999
Before you run any query you need to somehow retrieve the data for the original data row. Unless you're writing your SQL for something like MS Access and can use domain functions like DLOOKUP(), I don't see any other way how you can get this information. This means, you need at least 2 SELECT queries and they must be nested.