I have a view in which there are multiple duplicates. I want to get only one duplicate. There is no condition for extracting that duplicate .
View :
State|ZIP|Sales
AZ 231 asdas
AZ 231 qwerq
FL 12 wqeq
FL 12 lak
KY 1 zxc
KY 1 qwe
expected output:
State|ZIP|Sales
AZ 231 asdas
FL 12 wqeq
KY 1 zxc
There are more than 5000 records. If you do distinct it will still give the same dataset as these are the fields I am trying to extract from the view which is bigger and as you can see the records are not exactly distinct.
Thank You.
You can use aggregation for this since any of the many value is acceptable to you:
select state, zip, max(sales)
from your_table
group by state, zip
A way could be based on group by and aggregation function eg:
select state, ZIP, min(Sale)
from my_table
group by state, ZIP
or max length
select state, ZIP, max(length(Sale))
from my_table
group by state, ZIP
Related
I want to fetch records from a table that contains duplicate records. I want the output to be like only two duplicate records from each set of duplicate records in overall record output set.
example-
Name
Country
John
India
Mark
India
Chris
Russia
Feggy
England
Rain
Russia
Monesy
Russia
Bhumi
India
Peter
England
Bruice
England
Radhe
India
Output should have only two duplicate set of records from all duplicate of similar type as we can see in output below the country is repeating only two times and it took only first two counters of duplicate records in final record set -
Name
Country
John
India
Mark
India
Chris
Russia
Feggy
England
Rain
Russia
Peter
England
You can number the lines by the window and select only the first N.
Sorting should be chosen according to the business logic of the query.
For example:
;WITH numbered_name AS
(
SELECT *
, ROW_NUMBER() OVER (PARTITION BY t.Country ORDER BY t.Name) rn
FROM table t
)
SELECT Name
, Country
FROM numbered_name
WHERE rn <= 2
I am trying to learn Group By and Having but I can't seem to understand what happened here. I used w3shools SQL Tryit Editor.
The table I created is:
name age country
------------------------
Sara 17 America
David 21 America
Jared 27 America
Jane 54 Canada
Rob 32 Canada
Matthew 62 Canada
The Query I used:
select
sum(age), country
from
NewTable
group by
country
having
age>25;
I expected the query to categorize the information by country and use age>25 filter to create the results but here is the output:
sum(age) country
--------------------
65 America
148 Canada
What happened?! The result is sum of American and Canadian people in all ages.
The piece you're missing is specific to the having keyword. Using the having clause in your query is applied to the dataset after the grouping occurs.
It sounds like you are expecting the records with age less than 25 to be excluded from your query before grouping occurs. But, the way it works is the having clause excludes the total age for each group that sums to a total over 25.
If you want to exclude individual records before totaling the sum of the age, you could do something like this (using a where clause which is applied prior to grouping):
select sum(age), country from NewTable where age > 25 group by country;
A where clause puts a condition on which rows participate in the results.
A having clause is like a where, but puts a condition on which grouped (or aggregated) values participate in the results.
Either, try this:
select sum(age), country
from NewTable
where age > 25 -- where puts condition on raw rows
group by country
or this:
select sum(age), country
from NewTable
group by country
having sum(age) > 25 -- having puts a condition on groups
depending on what you're trying to do.
Drill Environment:-
OS:- Window 10,
Version:- 1.9,
Mode:- embedded mode,
I have a column name 'State' in db, which have data like(e.g:- Florida,
florida,texas, etc).
My problem is while using SUM(Price) and group by in query,
Florida and florida are showing two seperate rows.
So how to avoid case sensitivity so that both Florida and florida act as 1
rows while firing count(Sate).?
EXAMPLE:->
This is my input table in db:-
State Price
Alaska 75
Texas 80
Alaska 90
Florida 100
florida 70
Sql Server Query:- select State, Sum(Price) from testTable group by State.
Sql Server Output:-
State Price
Alaska 165
Texas 80
Florida 170
Drill Query:- select T1.State, Sum(T1.Price) from . T1 group by T1.State.
Drill Output:-
State Price
Alaska 165
Texas 80
Florida 100
florida 70
I want same output as shown in Sql Server Output. Please help.
Drill provides a lowercase string function. You can do a subquery to first convert all your states to lower case and then do the group by.
select lstate, sum(lprice) from (select lower(T1.State) as lstate, T1.Price as lprice from . T1) group by lstate
Not sure what exactly it is I should be looking for, so I'm reaching out for help.
I have two tables that through queries I need to spit out one. the two tables are as follows:
Transactions:
TransactionID SiteID EmployeeName
520 2 Michael
521 3 Gene
TransactionResponse:
TransactionID PromptMessage Response PromptID
520 Enter Odometer 4500 14
520 Enter Vehicle ID 345 13
521 Enter Odometer 5427 14
521 Enter Vehicle ID 346 13
But what I need is the following, let's call it TransactionSummary:
TransactionID SiteID EmployeeName 'Odometer' 'VehicleID'
520 2 Michael 4500 345
521 3 Gene 5427 346
The "PromptID" column is the number version of "PromptMessage" so I could query off that if it's easier.
A good direction for what this query would be called is the least I'm hoping for. True extra credit for working examples or even using this provided example would be awesome!
For a predefined number of possible PromptID values you can use something like the following query:
SELECT t.TransactionID, t.SiteID, t.EmployeeName,
MAX(CASE WHEN PromptID = 13 THEN Response END) AS 'VehicleID',
MAX(CASE WHEN PromptID = 14 THEN Response END) AS 'Odometer'
FROM Transactions AS t
LEFT JOIN TransactionResponse AS tr
ON t.TransactionID = tr.TransactionID AND t.SiteID = tr.SiteID
GROUP BY t.TransactionID, t.SiteID, t.EmployeeName
The above query uses what is called conditional aggregation: a CASE expression is used within an aggregate function, so as to conditionally account for a subset of records within a group.
I have a table containing: ID, FEATURE_NAME (that would be a name of the city), STATE_ALPHA (two letter country identifier like 'AL' for Alabama), and POPULATION_DATA.
I need to:
find entries that have same FEATURE_NAME and STATE_ALPHA
take values for POPULATION_DATA in both(or more) appearances and add them
write down the sum in POPULATION_DATA where all addends are from.
Example:
- ID !FEATURE NAME ! STATE_ALPHA ! POPULATION DATA
- 1 Woodland WA 83
- 2 Woodland WA 5426
- 3 Vining KS 354
- 4 Vining KS 276
- 5 Vining KS 1450
What I need to get is:
- ID !FEATURE NAME ! STATE_ALPHA ! POPULATION DATA
- 1 Woodland WA 5509
- 2 Woodland WA 5509
- 3 Vining KS 2080
- 4 Vining KS 2080
- 5 Vining KS 2080
I googled for hours and dont even know where to start. Also I'll run that script on a view not on original table, I don't know does it changes anything. Please help.
You can do this with an aggregation and a join:
select t.id, t.feature_name, t.state_alpha, sumpop as Population_Data
from t join
(select feature_name, state_alpha, sum(population_data) as sumpop
from t
group by feature_name, state_alpha
) fs
on t.feature_name = fs.feature_name and
t.state_alpha = fs.state_alpha;
The aggregation sums the population (in the subquery fs). This result is joined back to the original data.
If I understand your question, a simple query should work where the table name is tablename
SELECT ID, FEATURE_NAME, STATE_ALPHA, POPULATION_DATA FROM tablename
FROM tablename
GROUP BY FEATURE_NAME, STATE_ALPHA