group by not working SQL- very basic - sql

i did a few searches and i saw a few people try to do some kind of nested select statement in order to fix the issue. i did not understand it.
can someone help me please:
the data is already sorted by provider name, each provider name is listed more than once based on various other columns in the table. however, when i do this, i do not get one line per provider name. instead the provider names repeat as if i am not using group by
here is the code:
create table moopnjsummary2 as
select mnj.ProviderName
from moopnj mnj
group by mnj.ProviderName

Do you want a list of mnj.ProviderName without repeats? What is your final goal?
You could also try SELECT DISTINCT

select mnj.ProviderName, count(*) as Providernamecount
from moopnj mnj
group by mnj.ProviderName

If all you're looking for is distinct ProviderNames, try running
SELECT DISTINCT moopnj.ProviderName FROM moopnj

This is a not a full answer to your question but if all you want is a list of provider names you should use: "Select DISTINCT mnj.ProviderName ...". That will eliminiate all duplicates.

Related

In SQL how can I remove duplicates on a column without including it in the SELECT command/ the extract

I'm trying to remove duplicates on a column in SQL, without including that column in the extract (since it contains personally identifiable data). I thought I might be able to do this with nested queries (as below), however this isn't working. I also thought it might be possible to remove duplicates in the WHERE statement, but couldn't find anything from googling. Any ideas? Thanks in advance.
SELECT [ETHNIC], [RELIGION]
FROM
(SELECT DISTINCT [ID], [ETHNIC], [RELIGION]
FROM MainData)
Using distinct like that will apply distinct to the row, so if there are two rows with the same ID but different ETHNIC and RELIGION the distinct won't remove them. To do that you could use group by in your query, but then you need to use an aggregation (e.g. max):
SELECT [ETHNIC], [RELIGION]
FROM
(SELECT [ID], MAX([ETHNIC]) AS ETHNIC, MAX([RELIGION]) AS RELIGION
FROM MainData
GROUP BY [ID])
If that's not what you're looking for, some SQL dialects require that you name your inner select, so you could try adding AS X to the end of your query.

MS Access SQL how to display the unique result

I am new with the MS access SQL and the following is what I would like to do
in my database table
I have the following data
basically no duplicate, I tried "Distinct" but given my result is not one column only so I can't do that. I research online and look like it might need to use inner join but I only have one table only.
Appreciate if anyone can help me up..
Thanks
Regards,
Marc
You should be able to run the following query:
SELECT DISTINCT name, birthday
FROM [table];
select DISTINCT column1, column2, ...
from TABLENAME;
Replace columns and table name with respective values.
like this
select DISTINCT name, birthday from TABLENAME;

Counting unique text in query

I need to count how many distinct shipping methods there are in a query (the answer is 2). I am trying to use DISTINCT but it doesn't seem to be working the way I thought it would.
SELECT DISTINCT Count(Order.ship_method) AS CountOfship_method
FROM [Order];
Try this instead -
SELECT COUNT(*) as CountOfship_method
FROM
(SELECT DISTINCT Order.ship_method FROM [Order]);

Summarizing a table result in SQL

Given the below table as a SQL Result:
I want to use the above generated table and produce a table which clubs the given information into:
I have multiple areaName and multiple functionNames and multiple users. Please let me know if this is possible and how?
I have tried couple of things but I am just drained out now and need a direction. Any help is appreciated.
Even if you can provide a pseudo code, I can try and make use of it. Start from the SQL result as a given table.
Use correlated sub-queries to achieve the desired result. I've provided an example below. Test it for the first summary column, and then add in your other summary columns if it does. Hopefully this makes sense, and helps. Alternatively you could use a CTE (common table expression) to achieve similar results.
SELECT a.areaName, a.functionName
, (SELECT count(DISTINCT b.UserKey)
from AREAS b
where a.areaName = b.areaName
and a.functionName = b.functionName
and b.[1-add] = 1) as UsersinAdd
-- Lather/rinse/repeat for other summary columns
FROM AREAS a
group by a.areaName, a.functionName
Your problem stems from the de-normalised structure of your table. Columns [1-add],...,[8-correction] should be values in a column, not columns. This leads to more complex queries, as you have discovered.
The unpivot command allows you to correct this mistake.
select areaname, functionname, rights, count(distinct userkey)
from
(
select * from yourtable
unpivot (permission for rights in ([1-add], [2-update/display],[4-update/display all] , [8-correction] )) u
) v
group by areaname, functionname, rights

SQL alias select

I need to reorder my query to have the name in the first column, but I still need the rest of the columns populated as well.
How would you implement an alias in oracle to achieve this query?
select s.name, s.* from table as s
Thanks for the help.
There is no simple way around this, you must specify all columns in the order you would like them.
select s.name, s.column1, s.column2, ... from table as s