I am trying to get a single record in the case statement, where am i suppose to put the seelect statement top(1)
CASE WHEN CAST(VehicleCC AS MONEY) BETWEEN -1 AND 50 THEN '33.94'
WHEN CAST(VehicleCC AS MONEY) BETWEEN 51 AND 125 THEN '39.65'
WHEN CAST(VehicleCC AS MONEY) BETWEEN 126 AND 500 THEN '55.90'
WHEN CAST(VehicleCC AS MONEY) BETWEEN 501 AND 650 THEN '67.31'
WHEN CAST(VehicleCC AS MONEY) BETWEEN 651 AND 1000 THEN '78.65'
WHEN CAST(VehicleCC AS MONEY ) > 1001 THEN '79.85'
else
(SELECT TOP(1) cast (VehicleCC as varchar (50)) FROM HIRE_INSTRUCTION HIRP
INNER JOIN VEHICLE VEH ON HIRP.FKFleetVehicleID=VEH.vehicleid
WHERE HIRP.FKBLDRefID=cl.FKBLDRefID AND HIRP.Deleted=0 AND VEH.Deleted=0
ORDER BY veh.VehicleCC DESC) END AS [Hire Rate Charged Per Day],
If TOP(1) isn't working, try RANK():
SELECT VehicleCC
FROM (
SELECT CAST(VEH.VehicleCC as varchar (50)),
RANK() OVER(ORDER BY CAST(VEH.VehicleCC as varchar (50))) AS CCRank
FROM HIRE_INSTRUCTION HIRP
INNER JOIN VEHICLE VEH
ON HIRP.FKFleetVehicleID=VEH.vehicleid
WHERE HIRP.FKBLDRefID=cl.FKBLDRefID
AND HIRP.Deleted=0
AND VEH.Deleted=0
ORDER BY veh.VehicleCC DESC
) CC
WHERE CCRank = 1
You can use set rowcount to limit the number of rows returned by a query.
set rowcount 1 -- the query will return one record
select * from table
set rowcount 0 --back to normal
Related
I'm using SQL Server 2000 (80). So, it's not possible to use the LAG function.
I have a code a data set with four columns:
Purchase_Date
Facility_no
Seller_id
Sale_id
I need to identify missing Sale_ids. So every sale_id is a 100% sequential, so the should not be any gaps in order.
This code works for a specific date and store if specified. But i need to work on entire data set looping looping through every facility_id and every seller_id for ever purchase_date
declare #MAXCOUNT int
set #MAXCOUNT =
(
select MAX(Sale_Id)
from #table
where
Facility_no in (124) and
Purchase_date = '2/7/2020'
and Seller_id = 1
)
;WITH TRX_COUNT AS
(
SELECT 1 AS Number
union all
select Number + 1 from TRX_COUNT
where Number < #MAXCOUNT
)
select * from TRX_COUNT
where
Number NOT IN
(
select Sale_Id
from #table
where
Facility_no in (124)
and Purchase_Date = '2/7/2020'
and seller_id = 1
)
order by Number
OPTION (maxrecursion 0)
My Dataset
This column:
case when
Sale_Id=0 or 1=Sale_Id-LAG(Sale_Id) over (partition by Facility_no, Purchase_Date, Seller_id)
then 'OK' else 'Previous Missing' end
will tell you which Seller_Ids have some sale missing. If you want to go a step further and have exactly your desired output, then filter out and distinct the 'Previous Missing' ones, and join with a tally table on not exists.
Edit: OP mentions in comments they can't use LAG(). My suggestion, then, would be:
Make a temp table that that has the max(sale_id) group by facility/seller_id
Then you can get your missing results by this pseudocode query:
Select ...
from temptable t
inner join tally N on t.maxsale <=N.num
where not exists( select ... from sourcetable s where s.facility=t.facility and s.seller=t.seller and s.sale=N.num)
> because the only way to "construct" nonexisting combinations is to construct them all and just remove the existing ones.
This one worked out
; WITH cte_Rn AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY Facility_no, Purchase_Date, Seller_id ORDER BY Purchase_Date) AS [Rn_Num]
FROM (
SELECT
Facility_no,
Purchase_Date,
Seller_id,
Sale_id
FROM MyTable WITH (NOLOCK)
) a
)
, cte_Rn_0 as (
SELECT
Facility_no,
Purchase_Date,
Seller_id,
Sale_id,
-- [Rn_Num] AS 'Skipped Sale'
-- , case when Sale_id = 0 Then [Rn_Num] - 1 Else [Rn_Num] End AS 'Skipped Sale for 0'
, [Rn_Num] - 1 AS 'Skipped Sale for 0'
FROM cte_Rn a
)
SELECT
Facility_no,
Purchase_Date,
Seller_id,
Sale_id,
-- [Skipped Sale],
[Skipped Sale for 0]
FROM cte_Rn_0 a
WHERE NOT EXISTS
(
select * from cte_Rn_0 b
where b.Sale_id = a.[Skipped Sale for 0]
and a.Facility_no = b.Facility_no
and a.Purchase_Date = b.Purchase_Date
and a.Seller_id = b.Seller_id
)
--ORDER BY Purchase_Date ASC
SQL Server 2008. I need to count my id column and also count the ids that are duplicates when the dates exceed a 90 day time period.
Table:
ID Date
101 01/01/2015
101 01/02/2015
101 04/01/2015
201 01/01/2015
201 01/03/2015
301 05/01/2015
401 06/01/2015
401 07/01/2015
401 07/02/2015
What I would like to see:
ID TotalCount Counts&WithDateGaps
101 3 2
201 2 1
301 1 1
401 3 1
declare #t table(
id int,
datet date
)
insert into #t
select 101,'01/01/2015' union
select 101,'01/02/2015' union
select 101,'04/01/2015' union
select 201,'01/01/2015' union
select 201,'01/03/2015' union
select 301,'05/01/2015' union
select 401,'06/01/2015' union
select 401,'07/01/2015' union
select 401,'07/02/2015'
Select T.ID, T.Total, Coalesce(C.Total, 0) As CountDtGaps
From
(Select Id, count(*) As Total From #t Group By Id) As T
Left Outer Join (Select Id, count(*) As Total From #t Where datet < dateadd(dd, -90, Cast(getdate() as date)) Group By Id) As C
On T.Id = C.Id
--PS. r u sure about results expected? 90 days ago.. all rows are selected.
I'm trying to get a count of 'number of transactions' that occurred'.
The data could look like this.
Cust # Trans# TransType LineItem
42 5000 1 1
42 6000 1 1
42 6000 1 2
42 6000 2 1
42 6000 2 2
42 6000 2 3
There can be multiple transaction types for any given transaction number. In this example, my desired returned 'number of transactions' count is '3', as Trans# 5000 only had one different TransType and 6000 had two. If I do a distinct count of Trans# I get '2' and if I do just a count, I get '6'.
I've tried working with:
COUNT(DISTINCT CASE Trans# WHEN ???? THEN 1 ELSE null END) AS [Num of Transactions],
But I know that I'm not quite on the right track. If anyone could point me in the right direction, it'd be much appreciated.
Try this :-
with cte as
(
Select Cust,Trans,row_number() over (partition by trans,TransType order by cust) rn
from Sample
)
Select count(*) as TransCount from cte
where rn=1
SQL FIDDLE DEMO
You can use the following to get the count of distinct transtype for each customer and transaction:
select cust,
trans,
count(distinct transtype) cnt
from yourtable
group by cust, trans;
Then if you want a total of that count, you can apply sum() to the query:
select sum(cnt) Total
from
(
select cust,
trans,
count(distinct transtype) cnt
from yourtable
group by cust, trans
) src
See SQL Fiddle with Demo of both queries.
I have a table with the following data
Bldg Suit SQFT Date
1 1 1,000 9/24/2012
1 1 1,500 12/31/2011
1 2 800 8/31/2012
1 2 500 10/1/2005
I want to write a query that will sum the max date for each suit record, so the desired result would be 1,800, and must be in one cell/row. This will ultimately be part of subquery, I am just not getting what I expect with the queries I have writtren so far.
Thanks in advance.
You can use the following (See SQL Fiddle with Demo):
select sum(t1.sqft) Total
from yourtable t1
inner join
(
select max(dt) mxdt, suit, bldg
from yourtable
group by suit, bldg
) t2
on t1.dt = t2.mxdt
and t1.bldg = t2.bldg
and t1.suit = t2.suit
; With Data As
(
Select Bldg, Suit, SQFT, Row_Number() Over (Partition By Bldg, Suit Order By Date DESC) As RowID
From YourTableNameHere
)
Select Bldg, Sum(SQFT) As TotalSQFT
From Data
Where RowId = 1
Group By Bldg
Let's suppose I have balance 2000, and want to select balance as
balance=balance-Cr+Dr
So my balance column will give values as below.
balance DR Cr
40000 0 60000
100000 60000 0
0 0 100000
How is this possible in SQL query?
Please check similar question like me
enter link description here
Here is a recursive CTE that calculates the balance using the balance from the previous row. You need something that defines the order of the rows. I use the ID column in the sample table.
-- Test table
declare #T table
(
ID int identity primary key,
DR int,
Cr int
)
-- Sample data
insert into #T (DR, Cr)
select 0, 60000 union all
select 60000, 0 union all
select 0, 100000
-- In value
declare #StartBalance int
set #StartBalance = 100000
-- Recursive cte calculating balance as a running sum
;with cte as
(
select
T.ID,
#StartBalance - T.Cr + T.DR as Balance,
T.DR,
T.Cr
from #T as T
where T.ID = 1
union all
select
T.ID,
C.Balance - T.Cr + T.DR as Balance,
T.DR,
T.Cr
from cte as C
inner join #T as T
on C.ID+1 = T.ID
)
select Balance, DR, Cr
from cte
option (maxrecursion 0)
Result:
Balance DR Cr
----------- ----------- -----------
40000 0 60000
100000 60000 0
0 0 100000
This should work:
SELECT (T.BALANCE-T.CR+T.DR) as "Balance", T.DR, T.CR
FROM <table-name> T
If you use Oracle, there is a function called LAG to reach the previous row data: http://www.adp-gmbh.ch/ora/sql/analytical/lag.html
If you read this link I think you will see that this is exactly what you need. But only if you use Oracle..