I have an OracleCommand to select maximum value of type NUMBER from oracle table in a C# application using OracleConnection. when code is excuted I get the value with M letter at the end! so if we suppose max value is 2544, then I get 2544M as maximum value.
here is the code:
OracleCommand command = new OracleCommand("SELECT GREATEST(TA_Counts) FROM Test3.Trn_Sfd", Con2);
Con2.Open();
var returvalue = command.ExecuteScalar();
why value is not return as 2544 int value?!
Because the value is a decimal. This happens due to the type of your column in table. You could verify this looking at this table. If your data type is either integer or float the .net type returned by the reader is decimal.
Greatest doesn't give you the max value of your table:
SQL> with t as (
2 select 1 id, 2 value from dual union all
3 select 2 id, 4 value from dual
4 )
5 select GREATEST(id)
6 from t
7 /
GREATEST(ID)
------------
1
2
The GREATEST function gives you the max value among several values, like:
SQL> ed
Wrote file afiedt.buf
1 with t as (
2 select 1 id, 2 value from dual union all
3 select 2 id, 4 value from dual
4 )
5 select GREATEST(id, value)
6* from t
SQL> /
GREATEST(ID,VALUE)
------------------
2
4
You have to use the max function:
SQL> ED
Wrote file afiedt.buf
1 with t as (
2 select 1 id, 2 value from dual union all
3 select 2 id, 4 value from dual
4 )
5 select max(id)
6* from t
SQL> /
MAX(ID)
----------
2
you might need to use implicit conversion:
(int)command.ExecuteScalar()
use the type you need
Related
i have this table
series
id
1975
1
1985
1
1995
2
2000
2
what we trying to achieve is we add alphabet increment where id is same, example series will be 1975A, 1985B, and then 1995A 2000B when id is same, it is possible to do that? in query or in c# mvc code?
You can use below query.
At first,
I have added row number par group/ partition
Then as we know CHR(65)='A', Thus I have added 64 + auto row_rumber
And Convert to CHAR.
Add result with series field.
SELECT
CONCAT
(
series ,
CHR(64 + CAST ( row_number() OVER(PARTITION BY id order by id) AS integer ))
)
,id from table1
Hi i want to find the column name of max vaue of a row in a table
ID col1 col2 col3
1 15 12 10
2 6 10 3
3 25 50 100
4 150 80 90
Above is my table structure what i need is to find the max value of each row and then find the column name of that max value
eg In first row max value is 15 now i want to find that max value of first row is 15 and at the same time i want to find the column name of that max value 15 is col1(15 =>col1)
I need result like below one
ID Maxval colname
1 15 col1
2 10 col2
3 100 col3
4 150 col1
After finding the max value and column name i have to insert into another temporary table in the format which is mention above
SELECT ID,
(SELECT MAX(LastUpdateDate)
FROM (VALUES (col1,col2,col3)) AS UpdateDate(LastUpdateDate))
AS LastUpdateDate
FROM TestTable
This is the sql query i used to find out the max value of each row in a table but i donno how to find the column name of max value in each row after that only i can able to insert into temp table. Please any one understand my issue and help me to resolve this .Thanks
In SQL Server, you can use apply:
select t.*, v.*
from t cross apply
(select top (1) v.*
from (values ('col1', col1), ('col2', col2), ('col3', col3)) v(colname, val)
order by val desc
) v
You can use IIF fuinction. The IIF() function returns a value if a condition is TRUE, or another value if a condition is FALSE:
select ID,
IIF(col1>col2 and col1>col3,col1,IIF(col2>col1 and col2>col3,col2,col3))max_val,
IIF(col1>col2 and col1>col3,'col1',IIF(col2>col1 and col2>col3,'col2','col3'))col_nam
from TestTable
I have the table below, how would I select in SQL the last date of each month (from the list) in each categoryID?
I want to end up with something in the line off:
CategoryID | Current | Date
1 | 5 | 2016-09-30
1 | 3 | 2016-10-30
1 | 7 | 2016-11-30
1 | 2 | 2016-12-30
etc. as history builds up.
Image :
There are a few ways to approaches to do this, one of them could be using windowing function rownumber. Within the CTE (WITH) you get local order of the records within date(using covert to get rid of the time here)+CategoryID partition by datetime DESC (-> first is latest). You need to do this because you cannot use windowing functions in WHERE clause. Then, in the main query, you actually use this CTE as your source table and get only the latest record per partition.
WITH LocallyOrdered AS (
SELECT CategoryID,
StockCurrent,
ROW_NUMBER() OVER (
PARTITION BY CategoryID, CONVERT(date, RecordAdded)
ORDER BY RecordAdded DESC)
AS RowNumberOneIsLatest
FROM OriginalTable)
SELECT CategoryID, StockCurrent FROM LocallyOrdered WHERE RowNumberOneIsLatest = 1
Considering you're using MySQL, since you haven't mentioned.
Suppose this is your table named : 'Dummy'
cat_id current date
------ ------- --------
1 5 2016-09-30
1 3 2016-10-30
1 7 2016-11-30
1 2 2016-12-30
2 4 2016-10-31
2 6 2016-10-04
Executing this query :
select
o.cat_id,
(SELECT DISTINCT
a.date
from
Dummy a
where a.cat_id = o.cat_id
ORDER BY date DESC
LIMIT 1) as 'date'
from
Dummy o
group by o.cat_id ;
Gives you the Latest date of each category :
cat_id date
------ ------------
1 2016-12-30
2 2016-10-31
EDIT
This is supposed to work specifically for your table. Just replace "yourTable" with the table's actual name.
select
o.CategoryID,
o.StockCurrent
(SELECT DISTINCT
a.RecordAdded
from
yourTable a
where a.CategoryID = o.CategoryID
ORDER BY RecordAdded DESC
LIMIT 1) as 'RecordAdded'
from
yourTable o
group by o.CategoryID ;
EDIT 2 :
This Query returns the latest date of each month within a certain category. Hope this is what you want.
SELECT
o.CategoryID,
o.StockCurrent,
o.RecordAdded
FROM
`yourTable` o
WHERE o.RecordAdded IN
(SELECT
MAX(i.RecordAdded)
FROM
`yourTable` i
GROUP BY MONTH(i.RecordAdded))
GROUP BY o.CategoryID,
o.RecordAdded ;
Suppose the table contains the following sample data:
CategoryID StockCurrent RecordAdded
---------- ------------ -------------
1 5 2016-09-01
1 3 2016-09-02
1 7 2016-10-01
1 2 2016-10-02
2 4 2016-09-01
2 6 2016-09-02
2 66 2016-10-01
2 77 2016-10-02
Running this query returns the following result set :
CategoryID StockCurrent RecordAdded
---------- ------------ -------------
1 3 2016-09-02
1 2 2016-10-02
2 6 2016-09-02
2 77 2016-10-02
try this:
WITH Temp As
(
select CategoryId, [Current], RecordAdded,
Dense_Rank() over( partition by CategoryId order by RecordAdded desc) as CatergoryWiseRank
from tblCategory
)
select CategoryId, [Current], RecordAdded from Temp where CatergoryWiseRank=1
SELECT
CASE MONTH(date_field)
WHEN 1 THEN 'Enero'
WHEN 2 THEN 'Febrero'
WHEN 3 THEN 'Marzo'
WHEN 4 THEN 'Abril'
WHEN 5 THEN 'Mayo'
WHEN 6 THEN 'Junio'
WHEN 7 THEN 'Julio'
WHEN 8 THEN 'Agosto'
WHEN 9 THEN 'Septiembre'
WHEN 10 THEN 'Octubre'
WHEN 11 THEN 'Noviembre'
WHEN 12 THEN 'Diciembre'
END as Mes, COUNT(date_field) as cantidad FROM nacimientos
WHERE YEAR(date_field)='1991'
GROUP BY MONTH(date_field)asc
Result
I know this question has already been asked many times.
The problem is that other solutions don't work..
I tried the following:
SELECT ID FROM TABLE_1 AS T1 WHERE NOT EXISTS (SELECT COLUMN_1,COLUMN_2 FROM TABLE_2 AS T2 WHERE T1.COLUMN_1 = T2.COLUMNS_1 AND T1.COLUMN_2 = T2.COLUMN_2);
It always go in timeout, both from Workbench and from code (I am using Visual Studio 2013 C#).
I don't know how to make the query easier in order to make it work.. Maybe split it in 2..
Example:
Table 1 Table 2
ID COLUMN_1 COLUMN_2 ID COLUMN_1 COLUMN_2
1 0 1 1 0 1
2 0 1 2 0 1
3 0 1 3 0 1
4 1 2
5 1 2
6 1 2
It should return
1 2
Or also only the ID (2).
SELECT
Table_1.ID
,Table_1.COLUMN_1
,Table_1.COLUMN_2
FROM Table_1
LEFT JOIN Table_2
ON Table_1.ID = Table_2.ID
AND Table_1.COLUMN_1 = Table_2.COLUMN_1
AND Table_1.COLUMN_2 = Table_2.COLUMN_2
WHERE Table_2.ID IS NULL
Edit:
Well, if you don't need to match the id, then it's simply:
SELECT
Table_1.ID
,Table_1.COLUMN_1
,Table_1.COLUMN_2
FROM Table_1
LEFT JOIN Table_2
ON Table_2.COLUMN_1 = Table_1.COLUMN_1
AND Table_2.COLUMN_2 = Table_1.COLUMN_2
WHERE Table_2.ID IS NULL
If that's still too slow, maybe an index can help.
If an index doesn't help, you can still increase the command timeout.
Still, another option would be:
SELECT
Table_1.ID
,Table_1.COLUMN_1
,Table_1.COLUMN_2
FROM Table_1
WHERE
(COLUMN_1, COLUMN_2) NOT IN (SELECT COLUMN_1, COLUMN_2 FROM Table_2)
I have following table structures.
**Table_A**
A_Id(BigInt) Category_Ids(varchar(50))
1 1,2,3
2 2,3
**Table_B**
B_Id(BigInt) C_Id(Bigint) Name(varchar(50))
1 2 A
2 1 C
3 3 B
First Query:
In this query want to get the record where A_Id=1. I have executed following code.
Select [Category_Ids] from Table_A where A_Id=1
This returns the data table with single column and single row with values “1, 2, 3”
Assume that above query fills the data into the A_datatable. I get the string from following code.
String ids = A_datatable.column[0][“Category_Ids”];
Second Query:
Now, I have to fetch the values from Table_B where C_Id in (1, 2, 3). I have executed following code and passed the string value to following query.
Select * from Table_B where C_Id in (ids)
When I execute above query getting the error, failed to convert parameter value from a String to a Int64.
You can actually do it in a single query.
SELECT b.*
FROM Table_A a
INNER JOIN Table_B b
ON ',' + a.Category_IDs + ',' LIKE '%,' + CAST(C_ID AS VARCHAR(10)) + ',%'
WHERE a.A_ID = 1
Caveat: This query is an index-killer. You should have properly normalize your tables.
UPDATE 1
Suggested Schema Design,
Table_A
A_ID
Category_ID
Table_C
B_ID
C_ID
Name
Records
Table_A
A_ID Category_ID
1 1
1 2
1 3
2 4
2 5
Table_B
B_Id C_Id Name
1 2 A
2 1 C
3 3 B
You can use a string splitter function like the one described here http://www.sqlservercentral.com/articles/Tally+Table/72993/
scroll down to the 'CREATE FUNCTION' script and run it to create your function, then you can split the comma separate string for use in your 'where in' clause
select * from Table_B
where C_Id in (select item from dbo.DelimitedSplit8K(IDS,','))