MySQL SELECT UNION Distinct on single column - c#

I have two tables for storing exchange rates in my system. the first one
agent_rate is as follows.
ID Currency Rate
=======================
3 GBP 0.65
4 EUR 0.70
5 JPY 57.4
the second table exchange_rate is as follows.
ID Currency Rate
=======================
1 USD 1
2 ZMK 200
3 GBP 0.5
4 EUR 0.75
5 JPY 60.4
6 CHF 0.9
I want to select all the rows in the first table agent_rate and then add all the missing values of ID from the exchange_rate table. I wanted to use a union statement with distinct on a single column but I failed to. My current solution as follows (visual studio)
select from agent_rate table and fill rate datatable
set unique field i.e. ID in rate datatable
select and fill from exchange_rate table into a temp datatable
move records from temp datatable to rate datatable and ignore errors
the resulting table is(should be) as follows:
ID Currency Rate
=======================
1 USD 1
2 ZMK 200
3 GBP 0.65
4 EUR 0.70
5 JPY 57.4
6 CHF 0.9
Is there a better way to do this in Sql?

You can join the table and select the agent values first if they exist, otherwise the vaolue from the exchange table
select coalesce(a.id, e.id) as id,
coalesce(a.currency, e.currency) as currency,
coalesce(a.rate, e.rate) as rate
from exchange_rate e
left agent_rate a on a.id = e.id
coalesce returns the first non-null value in the list provided.

Related

How to find the column name of maximum value in a row of table in SQL

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

Monthly Report - SQL

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

Subtracting 2 columns from different tables with the same ID

I have 2 tables TblAddToInventory and TblWithdrawnFromInventory. Both have ProductID and Quantity. When a withdrawal is made, naturally the Inventory should deduct the quantity of items but only items that have been withdrawn. Example:
TblAddToInventory
ProductID | Quantity | Amount | Date
1 2 2.00 7/7/2012
2 3 3.00 7/7/2012
3 4 4.00 7/7/2012
2 2 2.00 7/8/2012
3 3 3.00 7/8/2012
TblWithdrawnFromInventory
ProductID | Quantity | Amount | Date
2 4 4.00 7/9/2012
3 5 5.00 7/10/2012
With this, when I join the two tables and deduct the specific columns, I should have a DataGridView using C# with this data:
ProductID | Quantity | Amount
1 2 2.00
2 1 1.00
3 2 2.00
I know how to use SUM and JOIN but I just don't know how to create a syntax that will subtract two columns from different tables with the same ID.
I don't know if this is the right way but what I have in mind is SUM all from TblAddToInventory using GROUP BY then SUM all from TblWithdrawnFromInventory using GROUP BY and then SUBTRACT columns from TblAddToInventory and TblWithdrawnFromInventory using GROUP BY. But I don't think that's a good idea. Can you help?
Thank you.
I know how to use SUM and JOIN but I just don't know how to create a
syntax that will subtract two columns from different tables with the
same ID.
This is code how you to do this:
SELECT inventory.ProductId,
inventory.Quantity - ISNULL(withdrawal.Quantity,0) AS Quantity,
inventory.Amount - ISNULL(withdrawal.Amount,0) AS Amount
FROM (
SELECT ProductId, SUM(Quantity) AS Quantity, SUM(Amount) AS Amount
FROM TblAddToInventory
GROUP BY ProductId
) AS inventory
LEFT JOIN (
SELECT ProductId, SUM(Quantity) AS Quantity, SUM(Amount) AS Amount
FROM TblWithdrawnFromInventory
GROUP BY ProductId
) AS withdrawal ON inventory.ProductId = withdrawal.ProductId
Preparation:
-- create temp table with the data, cast the first row's date to set the proper data type
select * into #tblAddToInventory from (
select 1 as ProductID, 2 as Quantity, 2.00 as Amount, cast('7/7/2012' as date) as [Date]
union all select 2 as ProductID, 3 as Quantity, 3.00 as Amount, '7/7/2012' as Date
union all select 3 as ProductID, 4 as Quantity, 4.00 as Amount, '7/7/2012' as Date
union all select 2 as ProductID, 2 as Quantity, 2.00 as Amount, '7/8/2012' as Date
union all select 3 as ProductID, 3 as Quantity, 3.00 as Amount, '7/8/2012' as Date
) a
-- create temp table with the data, cast the first row's date to set the proper data type
select * into #tblWithdrawnFromInventory from (
select 2 as ProductID, 4 as Quantity, 4.00 as Amount, cast('7/9/2012' as date) as [Date]
union all select 3 as ProductID, 5 as Quantity, 5.00 as Amount, '7/10/2012' as Date
) b
-- verify the data looks correct
select * from #tblAddToInventory
-- ProductID Quantity Amount Date
-- ----------- ----------- ----------- ----------
-- 1 2 2.00 2012-07-07
-- 2 3 3.00 2012-07-07
-- 3 4 4.00 2012-07-07
-- 2 2 2.00 2012-07-08
-- 3 3 3.00 2012-07-08
-- verify the data looks correct
select * from #tblWithdrawnFromInventory
-- ProductID Quantity Amount Date
-- ----------- ----------- ----------- ----------
-- 2 4 4.00 2012-07-09
-- 3 5 5.00 2012-07-10
Begin solution:
-- use Union All to join the queries, and multiply the second query by -1 to make them negative
select * from #tblAddToInventory union all
select ProductID, (Quantity * -1) as Quantity, Amount, Date from #tblWithdrawnFromInventory
-- ProductID Quantity Amount Date
-- ----------- ----------- ----------- ----------
-- 1 2 2.00 2012-07-07
-- 2 3 3.00 2012-07-07
-- 3 4 4.00 2012-07-07
-- 2 2 2.00 2012-07-08
-- 3 3 3.00 2012-07-08
-- 2 -4 4.00 2012-07-09
-- 3 -5 5.00 2012-07-10
select ProductID, sum(Quantity) as Quantity, sum(Amount) as Amount from (
select * from #tblAddToInventory union all
select ProductID, (Quantity * -1) as Quantity, (Amount * -1) as Amount, Date from #tblWithdrawnFromInventory
) joinedData
where [Date] >= '7/6/2012' and [Date] <= '7/11/2012'
group by ProductID
-- ProductID Quantity Amount
-- ----------- ----------- -----------
-- 1 2 2.00
-- 2 1 1.00
-- 3 2 2.00
-- delete temp tables
drop table #tblAddToInventory
drop table #tblWithdrawnFromInventory

select max value from oracle table in c#

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

select two columns from two different tables in the same db using mysql + use the output of query in C#

Dear Friends,
i want to select two columns from two different tables in the same db using mysql and set the output of the query to a variable in c#.
currently my code is as shown below:
MySqlCommand logcmdCheck = new MySqlCommand(query, connectionCheck);
string query = "SELECT DB.table1.column1,DB.table1.column2,DB.table2.column1,DB.table2.column2,DB.table2.column3 FROM DB.table1 WHERE DB.table1.column1=?x,DB.table2 WHERE DB.table2.column1=?y";
logcmdCheck.Parameters.AddWithValue("?x",UserName);
logcmdCheck.Parameters.AddWithValue("?y",emailID);
MySqlDataReader ldr = logcmdCheck.ExecuteReader();
A = ldr[0].ToString();
B = ldr[1].ToString();
C = ldr[2].ToString();
D = ldr[3].ToString();
E = ldr[4].ToString();
Error: Mysql query syntax is wrong.
Kindly please help me out with the mysql command to perform the requirement.
Thanks in advance
Suraj
You're going to have to use a SQL Join. Check it out here http://www.w3schools.com/sql/sql_join.asp. You need to have a foreign key in one of the tables that allows you to connect to the primary key of the other table. Every good database should be set up with tables that have foreign keys.
For example:
Table 1:
OrderNumber Name Order Total
1 John Smith 10.00
2 Sally Smith 5.00
3 Berry Jones 25.00
Table 2:
Item Number ItemTotal OrderNumber
1 5.00 1
2 5.00 1
3 2.50 2
4 2.50 2
5 25.00 3
In table 2 the OrderNumber is the foreign key that is able to join to table one. So your syntax would be:
SELECT * FROM table1 JOIN table2 ON table2.OrderNumber = table1.OrderNumber
That will give you one table which you can read from.

Categories