I have table like this
Id | Name | Status
------+------------+--------------
1 example1 3
1 example2 2
2 example3 3
2 example4 1
3 example5 1
4 example6 3
How To Write SELECT That Get To ME Result Like This
Id | Name | Status | Count_All
------+------------+--------------+------------
1 example1 3 6
2 example2 2 6
3 example3 3 6
4 example4 1 6
5 example5 1 6
6 example6 3 6
The Value Of Column Count_All, Is Count All Rows
Please use this solution..
SELECT Id , [Name] , [Status], COUNT(*) OVER() Count_All
FROM yourTableName
If I am understanding you correctly, you want a count of all rows in the table as the column Count_All, so Add a Count on column id to get all rows as a new column named Count_All then Group by your other columns to allow for the aggregate Count method.
SELECT [Id], [Name], [Status], COUNT([Id]) AS [Count_All]
FROM [dbo].[YourTable]
GROUP BY [Id], [Name], [Status]
This will do it:
SELECT
y.ID,
y.Name,
y.Status,
Count_All = (SELECT COUNT(*) FROM yourtable)
FROM
yourtable AS y
SELECT *,COUNT(1) OVER() AS COUNT FROM TABLE
Related
I have a query that returns something like this:
Id | Value
1 | Hi,
1 | I'm
2 | just
2 | an
2 | example
3 | message.
What I want to do is number the rows based on the id. So with the example above, I want to return something like this:
Id | Value | Number
1 | Hi, | 1
1 | I'm | 2
2 | just | 1
2 | an | 2
2 | example | 3
3 | message. | 1
Is there a simple way to do this in the same query as the one you would use in the first example?
The problem is there is NOTHING in that table that guarantees the order of rows, so the result you want cannot be guaranteed.
select *
, row_number() over(partition by id order by (select 1)) as Number
from yourtable
You can make use of the rownumber function.
SELECT ROW_NUMBER OVER (PARTITION BY ID ORDER BY (SELECT 1)) AS RowNumb
You Can Use the ROW_NUMBER() Function and Partition by Id. Like this
DECLARE #MyTable AS TABLE
(
Id INT,
[Value] VARCHAR(50)
)
INSERT INTO #MyTable
(
Id,
[Value]
)
SELECT '1','Hi,' UNION
SELECT '1','I''m' UNION
SELECT '2','just' UNION
SELECT '2','an' UNION
SELECT '2','example' UNION
SELECT '3','message.'
SELECT
*,
Number = ROW_NUMBER() OVER(PARTITION BY Id ORDER BY Id)
FROM #MyTable
Result
Id Value Number
----------- -------------------------------------------------- --------------------
1 Hi, 1
1 I'm 2
2 an 1
2 example 2
2 just 3
3 message. 1
Please try following script.
DECLARE #MyTable AS TABLE
(
Id INT,
[Value] VARCHAR(50)
)
INSERT INTO #MyTable
(
Id,
[Value]
)
SELECT '1','Hi,' UNION
SELECT '1','I''m' UNION
SELECT '2','just' UNION
SELECT '2','an' UNION
SELECT '2','example' UNION
SELECT '3','message.'
select *, dense_rank()over(partition by Id order by Value) as Number from #MyTable
/*
Id Value Number
----------- -------------------------------------------------- --------------------
1 Hi, 1
1 I'm 2
2 an 1
2 example 2
2 just 3
3 message. 1
*/
Best Regards,
Rachel
I have a non-normalized table with several columns. I would like to return all columns that have a positive number along with a negative number of the same value.
Example:
ID | Value
-------------
1 | 10
1 | -10
3 | 15
3 | 15
4 | -1
5 | 4
Current Output:
ID | Values
-------------
1 | 10
1 | -10
3 | 15
3 | 15
Desired Output:
ID | Value
-------------
1 | 10
1 | -10
I have made a windows function as seen below that will select absolute values that are the same, but this includes pairs where there are a positive number.
select Count(*) Over (Partition By DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([Maintenance Expense])) As cnt , *
From WorkTemp.dbo.Customer2700Combine
Where [Maintenance Expense] Is Not Null
Order By 1 Desc,DVN, [Tran Date], [Reference Number],Description,Vendor, Abs([NonRental Total])
Not sure if your requirement is by [ID], looking at your example, description and desired output, this is how I would do it:
DROP TABLE IF EXISTS #sopg;
SELECT [ID],
[VALUE]
INTO #sopg
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x;
-- Assuming that one ID can only have maximum 2 rows (like your example above) and want this by ID
SELECT s.[ID],
s.[VALUE]
FROM #sopg s
INNER JOIN
(
SELECT ID,
SUM(VALUE) SumZero
FROM #sopg
GROUP BY ID
HAVING SUM(VALUE) = 0
) SumZero ON SumZero.ID = s.ID
-- Another way, assuming that ID can have more than 2 rows and different values
DROP TABLE IF EXISTS #sopg2;
SELECT [ID],
[VALUE]
INTO #sopg2
FROM
(
SELECT 1 AS ID,
10 AS VALUE
UNION
SELECT 1 AS ID,
-10 AS VALUE
UNION
SELECT 1 AS ID,
-9 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 3 AS ID,
15 AS VALUE
UNION
SELECT 4 AS ID,
-1 AS VALUE
UNION
SELECT 5 AS ID,
4 AS VALUE
) x
SELECT a.[ID],
a.[VALUE]
FROM #sopg2 a
INNER JOIN #sopg b ON b.ID = a.ID AND a.VALUE = -b.VALUE
I would want to check ID in consecutive months, IF Same ID is present in two consecutive months then consider that ID only for 1st month.
If ID's are not in consecutive month then show the distinct ID's grouped by start date month.(We consider only start date)
For example, ID 1 is present in start date months january and Feb , then Distinct count of this ID will be 1 in Jan, how ever ID 2 and 3 are
present in Jan and March and Feb and May Resp, now I would like to see this distinct count of ID in Jan and March.
Current Data
Table1:
ID StartDate EndDate
1 2017-01-12 2017-01-28
1 2017-01-19 2017-01-28
1 2017-01-29 2017-02-11
1 2017-02-01 2017-02-11
1 2017-02-19 2017-02-24
2 2017-01-12 2017-01-28
2 2017-01-19 2017-01-28
2 2017-03-09 2017-03-20
3 2017-02-12 2017-02-28
3 2017-02-19 2017-02-28
3 2017-05-05 2017-05-29
3 2017-05-09 2017-05-29
I tried with below logic bt I know I am missing on something here.
select t.* from Table1 t
join Table1 t t1
on t1.ID=t.ID
and datepart(mm,t.StartDate)<> datepart(mm,t1.StartDate)+1
Expected Result:
DistinctCount StartDateMonth(In Numbers)
1 1(Jan)
2 1(Jan)
2 3(March)
3 2(Feb)
3 5(May)
Any help is appreciated!
Here's my solution. The thinking for this is:
1) Round all the dates to the first of the month, then work with the distinct dataset of (ID, StartDateRounded). From your dataset, the result should look like this:
ID StartDateRounded
1 2017-01-01
1 2017-02-01
2 2017-01-01
2 2017-03-01
3 2017-02-01
3 2017-05-01
2) From this consolidated dataset, find all records by ID that do not have a record for the previous month (which means it's not a consecutive month and thus is a beginning of a new data point). This is your final dataset
with DatesTable AS
(
SELECT DISTINCT ID
,DATEADD(month,DateDiff(month,0,StartDate),0) StartDateRounded
,DATEADD(month,DateDiff(month,0,StartDate)+1,0) StartDateRoundedPlusOne
FROM Table1
)
SELECT t1.ID, DatePart(month,t1.StartDateRounded) AS StartDateMonth
FROM DatesTable t1
LEFT JOIN DatesTable t2
ON t1.ID = t2.ID
AND t1.StartDateRounded = t2.StartDateRoundedPlusOne
WHERE t2.ID IS NULL; --Verify no record exists for prior month
sqlfiddler for reference. Let me know if this helps
Just need to take advantage of the lag on the inner query to compare values between rows, and apply the logic in question on the middle query, and then do a final select.
/*SAMPLE DATA*/
create table #table1
(
ID int not null
, StartDate date not null
, EndDate date null
)
insert into #table1
values (1, '2017-01-12', '2017-01-28')
, (1, '2017-01-19', '2017-01-28')
, (1, '2017-01-29', '2017-02-11')
, (1, '2017-02-01', '2017-02-11')
, (1, '2017-02-19', '2017-02-24')
, (2, '2017-01-12', '2017-01-28')
, (2, '2017-01-19', '2017-01-28')
, (2, '2017-03-09', '2017-03-20')
, (3, '2017-02-12', '2017-02-28')
, (3, '2017-02-19', '2017-02-28')
, (3, '2017-05-05', '2017-05-29')
, (3, '2017-05-09', '2017-05-29')
/*ANSWER*/
--Final Select
select c.ID
, c.StartDateMonth
from (
--Compare record values to rule a record in/out based on OP's logic
select b.ID
, b.StartDateMonth
, case when b.StartDateMonth = b.StartDateMonthPrev then 0 --still the same month?
when b.StartDateMonth = b.StartDateMonthPrev + 1 then 0 --immediately prior month?
when b.StartDateMonth = 1 and b.StartDateMonthPrev = 12 then 0 --Dec/Jan combo
else 1
end as IncludeFlag
from (
--pull StartDateMonth of previous record into current record
select a.ID
, datepart(mm, a.StartDate) as StartDateMonth
, lag(datepart(mm, a.StartDate), 1, NULL) over (partition by a.ID order by a.StartDate asc) as StartDateMonthPrev
from #table1 as a
) as b
) as c
where 1=1
and c.IncludeFlag = 1
Output:
+----+----------------+
| ID | StartDateMonth |
+----+----------------+
| 1 | 1 |
| 2 | 1 |
| 2 | 3 |
| 3 | 2 |
| 3 | 5 |
+----+----------------+
Try the below query,
SELECT ID,MIN(YEARMONTH) AS YEARMONTH
FROM (
SELECT ID
,YEAR([StartDate])*100+MONTH([StartDate]) AS YEARMONTH
,LAG(YEAR([StartDate])*100+MONTH([StartDate]))
OVER(ORDER BY ID) AS PREVYEARMONTH
,ROW_NUMBER() OVER(ORDER BY ID) AS ROW_NO
FROM #Table1
GROUP BY ID,((YEAR([StartDate])*100)+MONTH([StartDate]))
) AS T
GROUP BY ID
,(CASE WHEN YEARMONTH - PREVYEARMONTH > 1 THEN ROW_NO ELSE 0 END)
ORDER BY ID
Output:
ID YEARMONTH
1 201701
2 201701
2 201703
3 201702
3 201705
Thank you all guys. most of the logic seemed to work..but I tried just with below one and I Was good with thiis.
SELECT t1.ID, DatePart(month,t1.Startdate) AS StartDateMonth
FROM DatesTable t1
LEFT JOIN DatesTable t2
ON t1.ID = t2.ID
AND DatePart(month,t1.Startdate) = DatePart(month,t2.Startdate)+1
WHERE t2.ID IS NULL;
Thanks again
Ok, I wrote my first query without checking, believed that will work correctly. This is my updated version, should be faster than other solutions
select
id
, min(st)%12 --this will return start month
, min(st)/12 + 1 --this will return year, just in case if you need it
from (
select
id, st, gr = st - row_number() over (partition by ID order by st)
from (
select
distinct ID, st = (year(StartDate) - 1) * 12 + month(StartDate)
from
#table2
) t
) t
group by id, gr
I have a table named "letters" with two columns looking like this:
case_nr | date
--------+-----------------------
1 | 2015-06-13 12:45:04
1 | NULL
2 | 2015-06-11 12:45:09
3 | 2015-06-12 17:41:49
3 | 2015-06-13 18:42:99
case_nr 1 have printed 2 letters but only one was sent
I want to filter all cases where all letters was sent (have a date)
So in this case result should be:
2
3
You can use DISTINCT with NOT IN:
SELECT DISTINCT case_nr
FROM TableName
WHERE case_nr NOT IN
(SELECT case_nr FROM TableName WHERE [date] IS NULL )
Result:
case_nr
--------
2
3
Sample result in SQL Fiddle.
Group by the case_nr and take only those having no record with date is null
select case_nr
from your_table
group by case_nr
having sum(case when date is null then 1 else 0 end) = 0
SQLFiddle demo
As an alternative:
SELECT
case_nr
FROM (
SELECT
case_nr,
COUNT(CASE WHEN [date] IS NULL THEN 1 END) AS cnt
FROM
yourTable
GROUP BY
case_nr
) t
WHERE
cnt = 0
I have a table with pallets, items, item quantity:
pallet | item | qty
-------------------
1 1 2
1 2 4
2 3 2
2 5 3
3 4 4
I need to find count(pallet), count(item), sum(qty)
count(pallets) | count(items) | sum(qty)
----------------------------------------
3 5 15
I can get the sum(qty) and count(item) with
select count(0) as totalItems, sum(qty) as total from table
Is there a way to get the number of pallets without a sub-query?
Yes, use DISTINCT
select count(distinct pallet) as pallets,
sum(qty) as total,
count(*) as totalItems
from your_table
Simply use Distinct to avoid duplicate records to be count.
count(Distinct pallet)
Your query like this
select
count(distinct pallet) as pallets,
sum(qty) as Total,
count(item) AS [Total Items]
it will give output AS :