I have a data set that contains the columns Date, Cat, and QTY. What I want to do is add a unique column that only counts unique Cat values when it does the row count. This is what I want my result set to look like:
By using the SQL query below, I'm able to get row using the row_number() function.
However, I can't get that unique column that I have depicted above. When I add group by to the OVER clause, it does not work. Does anybody have any ideas as how I could get this unique count column to work?
SELECT
Date,
ROW_NUMBER() OVER (PARTITION BY Date ORDER By Date, Cat) as ROW,
Cat,
Qty
FROM SOURCE
Here is a solution.
You need not worry about the ordering of Cat. Using following SQL you will be able to get unique values for your Date & Cat combination.
SELECT
Date,
ROW_NUMBER() OVER (PARTITION BY Date, Cat ORDER By Date, Cat) as ROW,
Cat,
Qty
FROM SOURCE
DENSE_RANK() OVER (PARTITION BY date ORDER BY cat)
Related
I am trying to flag if a row was actually summed to another row or not while using SUM(). There are also rows that calculate min(), max(), and I am keeping track of a row number to take only the first row.
I have been looking around on google, but haven't had much luck in figuring this out, I have a SQL Fiddle here: http://sqlfiddle.com/#!18/eb4f3/6, which I know is complex. You can take out the WHERE rn = 1 and add a column to see the row numbers if it helps. Basically what I need is to add a column that says if it was aggregated or not... I believe the best way would to somehow see in row 1 if there is a second row or not. Does anyone know an easy way to do this?
select
userid, favoriteanimal,
row_number() over (partition by name, email, favoriteanimal, grp order by firstday) as rn,
min(firstday) over (partition by name, email, favoriteanimal, grp) as firstday,
max(lastday) over (partition by name, email, favoriteanimal, grp) as lastday,
sum(daysused) over (partition by name, email, favoriteanimal, grp) as daysused
from concatenated
This would be the SQL I'm trying to get information from, pretty much if the row was aggregated (min, max or sum) there is more than one row number entry for it. So I imagine there's a way to "update" row 1 if there's more than one row?
This is probably very simple but I'm stupid and stuck and failed to find a thread that quite matched my problem...
I need to do an insert from a table, say tblGameRecords, that looks something like this:
tblGameRecords(ID:match_no, soccer_team_id, stadium, fake_injuries, hair_wax, date)
...into another table, tblTeamRecords, that needs to look like this:
tblTeamRecords(ID:soccer_team_id, stadium, fake_injuries, hair_wax, date)
Now, my problem is that in tblGameRecords:
1. There are natural multiple occurrences of the same soccer_team_id's.
2. There are natural multiple occurrences of the same date.
3. There are sometimes multiple occurrences of the same soccer_team_id on the same date (sigh...)
I want to insert into tblTeamRecord one row per soccer_team_id. I want the earliest record of that team from tblGameRecords.
If the team makes its entrée in tblGameRecords as a duplicate, several times on the same date, I'm fine with any one row of those, because the other columns need to be filled with the respective values from that row, regardless of the actual values which may or may not differ from the other duplicates.
And I'm obviously having trouble formulating a query that lets me narrow down these multiples to just one. This is part of a stored procedure btw.
* EDIT again: Deleted the redundant additional info *
You can use ROW_NUMBER to generate row numbers per date for each value of soccer_team_id (to partition the numbering by it) and then insert only those rows, where row number equals one:
;with cte as (
select soccer_team_id, stadium, fake_injuries, hair_wax, date, row_number() over(partition by soccer_team_id order by date) as row_no
from tblGameRecords
)
insert into tblTeamRecords(soccer_team_id, stadium, fake_injuries, hair_wax, date)
select soccer_team_id, stadium, fake_injuries, hair_wax, date
from cte
where row_no = 1
I've two tables
income
expense
the problem is I want to query all the data from both tables
SELECT income.date AS IN_DATE, expense.date AS EX_DATE FROM income, expense
I get weird result data is double times from db as you can see
you can try this out HERE
how can I get distinct results not double and at last wanna ask don't have idea of getting data from both tables and sort by date descending.
My guess is that you want union all:
select 'income' as which, id, title, date
from income
union all
select 'expense' as which, id, title, date
from expense;
This will give you a result set containing the rows from the two tables, with an identifier of which table each row comes from.
You can order by date and do other manipulations if you use a subquery:
select ie.*
from (select 'income' as which, id, title, date
from income
union all
select 'expense' as which, id, title, date
from expense
) ie
order by date desc;
Your simple SELECT does a cross product with the two columns (IN_DATE, EX_DATE). Hence, you get every possible combination of the values from both columns. INNER JOIN income ON expense.id=income.id or WHERE income.id == expense.id should do the trick.
You need to match the same ids, else SQL will just output any possible combination.
SELECT income.date AS IN_DATE, expense.date AS EX_DATE FROM income, expense WHERE income.id LIKE expense.id
My question needs little explanation so I'd like to explain this way:
I've got a table (lets call it RootTable), it has one million records, and not in any proper order. What I'm trying to do is to get number of rows(#ParamCount) from RootTable and at the same time these records must be sorted and also have an additional column(with unique data) added on the fly to maintain a key for row identification which will be used later in the program. It can take any number of parameters but my basic parameters are the two which mentioned below.
It's needed for SQL SERVER environment.
e.g.
RootTable
ColumnA ColumnB ColumnC
ABC city cellnumber
ZZC city1 cellnumber
BCD city2 cellnumber
BCC city3 cellnumber
Passing number of rows to return #ParamCount and columnA startswith
#paramNameStartsWith
<b>#paramCount:2 <br>
#ParamNameStartsWith:BC</b>
desired result:
Id(added on the fly) ColumnA ColumnB ColumnC
101 BCC city3 cellnumber
102 BCD city2 cellnumber
Here's another point about Id column. Id must maintain its order, like in the above result it's starting from 101 because 100 is already assigned to the first row when sorted and added column on the fly, and because it starts with "ABC" so obviously it won't be in the result set.
Any kind of help would be appreciated.
NOTE: My question title might not reflect my requirement, but I couldn't get any other title.
So first you need your on-the-fly-ID. This one is created by the ROW_NUMBER() function which is available from SQL Server 2005 onwards. What ROW_NUMBER() will do is pretty self-explaining i think. However it works only on a partition. The Partition is specified by the OVER clause. If you include GROUP BY within the OVER clause, you will have multiple partitions. In your case, there is only one partition which is the whole table, therefor GROUP BY is not necessary. However an ORDER BY is required so that the system knows which record should get which row number in the partition. The query you get is:
SELECT ROW_NUMBER() OVER (ORDER BY ColumnA) ID, ColumnA,ColumnB,ColumnC
FROM RootTable
Now you have a row number for your whole table. You cannot include any condition like your #ParamNameStartsWith parameter here because you wanted a row number set for the whole table. The query above has to be a subquery which provides the set on which the condition can be applied. I use a CTE here, i think that is better for readability:
;WITH OrderedList AS (
SELECT ROW_NUMBER() OVER (ORDER BY ColumnA) ID, ColumnA,ColumnB,ColumnC
FROM RootTable
)
SELECT *
FROM OrderedList
WHERE ColumnA LIKE #ParamNameStartsWith+'%'
Please note that i added the wildcard % after the parameter, so that the condition is basically "starts with" #ParamNameStartsWith.
Finally,if i got you right you wanted only #ParamCount rows. You can use your parameter directly with the TOP keyword which is also only possible with SQL Server 2005 or later.
;WITH OrderedList AS (
SELECT ROW_NUMBER() OVER (ORDER BY ColumnA) ID, ColumnA,ColumnB,ColumnC
FROM RootTable
)
SELECT TOP (#ParamCount) *
FROM OrderedList
WHERE ColumnA LIKE #ParamNameStartsWith+'%'
Apologies for goofy title. I am not sure how to describe the problem.
I have a table in SQL Server with this structure;
ID varchar(15)
ProdDate datetime
Value double
For each ID there can be hundreds of rows, each with its own ProdDate. ID and ProdDate form the unique key for the table.
What I need to do is find the maximum Value for each ID based upon the first 12 samples, ordered by ProdDate ascending.
Said another way. For each ID I need to find the 12 earliest dates for that ID (the sampling for each ID will start at different dates) and then find the maximum Value for those 12 samples.
Any idea of how to do this without multiple queries and temporary tables?
You can use a common table expression and ROW_NUMBER to logically define the TOP 12 per Id then MAX ... GROUP BY on that.
;WITH T
AS (SELECT *,
ROW_NUMBER() OVER (PARTITION BY Id ORDER BY ProdDate) AS RN
FROM YourTable)
SELECT Id,
MAX(Value) AS Value
FROM T
WHERE RN <= 12
GROUP BY Id