I have a dataset in my VB.NET solution. Let's say the dataset has 2 data tables (MyTable1 and MyTable2). Lets assume MyTable1 and MyTable2 are like:
MyTable1:
Order
Account_Number
Account_Name
10000
10000000000001
BlahA
20000
10000000000002
BlahB
30000
10000000000003
BlahC
MyTable2:
Order_Number
Line_Number
Model
10000
00000000034
MD35Z
15000
00000000530
MX25A
25000
00000024535
P231Y
20000
00000027735
A511L
30000
00000000910
M232C
Now I want to left join MyTable2 to MyTable1 and get the following result as my MyTable1:
Order
Account_Number
Account_Name
Line_Number
Model
10000
10000000000001
BlahA
00000000034
MD35Z
20000
10000000000002
BlahB
00000027735
A511L
30000
10000000000003
BlahC
00000000910
M232C
To achieve this, I used LINQ, however, I was wondering if there is any way to use SQL queries when the datatables are not in a SQL database? (There is no db connection).
Related
I want to get the subscriber that has maximum value of Bill (Total Bill).
I tried using the following script but SQL did not execute successflly.
Please help me on what I did wrong on this.
I have 2 tables:
Subscriber
FirstName
MIN
Ben
258999542
Reed
458524896
Steve
586692155
Clint
1007772121
Frank
1287548752
Jane
2345824215
Total Bill
Total
MIN
131.5
258999542
139.4
458524896
164
586692155
101
1007772121
224.12
1287548752
97.52
2345824215
And here's the code I tried:
SELECT MAX(B.Total), S.FirstName
FROM Subscriber AS S
JOIN Bill AS B ON S.MIN = B.MIN
It seems you just need TOP + ORDER BY:
SELECT TOP 1 B.Total, S.FirstName
FROM Subscriber AS S
JOIN Bill AS B ON S.MIN = B.MIN
ORDER BY B.Total DESC;
That's based on the fact that your sample data isn't showing multiple Bill records per Subscriber therefore you don't need a sum.
I have a very big transaction table on DB2 v11, and I need to query a subset of it as efficiently as possible. All I need is the total count of the set (not known in advance, it's based on criteria, lets say 1 day) and the ID of the first record, and the ID of the last record.
The old code was fetching the entire table, then just using the 1st record ID, and the last record ID, and size, and not making use of the rest. Now this code is timing out. It's a complex query of several joins.
IS there a way to just fetch the size of the set, 1st record, last record all in one select query ?
I've read that reordering the list in order to fetch the 1st record(so fetch with Desc, then change to Asc) is not efficient.
sample table 1 TRANSACTION_RECORDS:
tdID TIMESTAMP name
-------------------------------
123 2020-03-31 john
234 2020-03-31 dan
456 2020-03-01 Eve
675 2020-04-01 joy
sample table 2 TRANSACTION_TYPE:
invoiceId tdID account
------------------------------
897 123 abc
898 123 def
877 234 mnc
899 456 opp
Sample query
select Min(tr.transaction_id), Max(tr.transaction_id)
from TRANSACTION_RECORDS TR
join TRANSACTION_TYPE TT
on TR.tdID=tt.tdID
WHERE Date(TR.TIMESTAMP) = '2020-03-31'
group by tr.tdID
order by TR.tdID ASC
This results in multiple columns, (but it requires the group by)
123,123
234,234
456,456
What I want is:
123,456
As I mentioned in the comments, for this query you don't need Group BY and neither Order by, just do:
select Min(tr.transaction_id), Max(tr.transaction_id)
from TRANSACTION_RECORDS TR
join TRANSACTION_TYPE TT
on TR.tdID=tt.tdID
WHERE Date(TR.TIMESTAMP) = '2020-03-31'
It should work as expected
I have a set of related rows which I need to display in a single line. For example, the data I have is in different rows.
"ID" RecordDate "ExpType" "OrigBudget" "ActualCost"
1001 1-5-2017 Hardware $ 5000
1001 2-6-2017 Hardware $ 5200
The Original budget is approved at an earlier time for the same record but the Actual cost often differs and is recorded at a later date. I want the output as
ProjectID YearofEntry ExpenseType OrgBudget ActualCost <BR>
1001 2017 Hardware $ 5000 $ 5200 <BR>
I have tried group query to aggregate it based on ExpenseType and ProjectId but not successful in getting it into a single row so far.
if you always just have two rows for each ExpType - one with the original budget and one with the actual costs - you could simply use a GROUP BY:
SELECT ID AS ProjectID
,YEAR(RecordDate) AS YearofEntry
,ExpType AS ExpenseType
,MAX(OrigBudget) AS OrgBudget
,MAX(ActualCost) AS ActualCost
FROM yourtable
GROUP BY ID
,YEAR(RecordDate)
,ExpType
Try This:
SELECT ID,
Year([RecordDate]) AS YEARofEntry,
ExpType,
Sum(OrigBudget) AS SumOfOrigBudget,
Sum(ActualCost) AS SumOfActualCost
FROM youtable
GROUP BY ID,
Year([RecordDate]),
ExpType;
I have to write Hive SQL.
I want to find the lowest price for each catalog. I want to get one row for each catalog with the catalog ID, the product ID, the price (lowest price), the image url, the large_ctgr_id, the small_ctgr_id, the large_ctgr_name, and the small_ctgr_name columns.
In the example below, we finally need two rows.
In addition, there are multiple products in a catalog, and each product is classified into a large category and a small category.
Eventually we have to join the three tables.
There is a similar issue with the link I asked.
(Finding the lowest price for each category using join sql in Hive) In this case, we joined only two tables, and the names of the columns changed slightly.
Help me. Thank you.
"catalog_product_match" table
catalog_id product_id
1001 500001
1001 500002
1002 500101
1002 500102
1002 500103
"product_info" table
prd_id price img_url large_ctgr_id small_ctgr_id
500001 29000 /app/url/img/500001.jpg 200 34543
500002 29500 /app/url/img/500002.jpg 201 2345234
500101 8100 /app/url/img/500101.jpg 1020 23252
500102 8100 /app/url/img/500102.jpg 42133 2349823
500103 8500 /app/url/img/500103.jpg 3435 342514
"category_info" table
l_ctgr_id s_ctgr_id l_ctgr_name s_ctgr_name
200 34543 computer notebook
200 3423984 computer tablet
201 2345234 phone smartphone
1020 23252 clothes top
42133 2349823 cup cup
3435 342514 pen ink
There seems to be a lot of threads on this topic but few that work with Excel.
I have a simple table from which i want to select:
ideally all columns i.e. using * if possible so if a user adds new columns they do not need to edit SQL. (is this a pipe dream?) if so a solution specifying all the returned columns is OK.
only return rows where [name]&[date] (concatenated) is distinct
all other columns i don't care about which row is returned. first, last, limit 1... anything. they are a mix of all types.
this must not create a new table or delete rows, just selecting and joining
name date sales
andy 01/01/2010 100
andy 01/01/2010 900
andy 05/01/2010 100
alex 02/02/2010 200
alex 02/02/2010 200
alex 05/01/2010 200
dave 09/09/2010 300
dave 09/09/2010 300
dave 01/09/2010 300
Also code simplicity is prefered over speed. This is going to be left to run over night so nice looking but slow is fine... and excel doesn't have millions of rows!
Many thanks to everyone in advance.
UPDATE
I would expect the table to look like this:
name date sales
andy 01/01/2010 100
andy 05/01/2010 100
alex 02/02/2010 200
alex 05/01/2010 200
dave 09/09/2010 300
dave 01/09/2010 300
or
andy 01/01/2010 900
andy 05/01/2010 100
alex 02/....
I can select all the 'unique things with this:
SELECT MAX(joined)
FROM
(SELECT [Single$].[date] AS [date],
[Single$].[name] AS [name],
name & date AS [joined]
FROM [Single$]
)
GROUP BY joined
HAVING MAX(joined) IS NOT NULL
But i don't know how to somehow join this back to the original table keeping any single row where the join matches. And i don't know if a join is the right way about this? Thanks
Simply run an aggregate query grouped by [Name] and [Date]. For all other columns run an aggregate like MAX() or MIN() which should work on numeric and string values.
SELECT [Single$].[name] AS [name], [Single$].[date] AS [date],
MAX([Single$].[sales]) As [sales],
MAX(...)
FROM [Single$]
GROUP BY [Single$].[name] AS [name], [Single$].[date] AS [date]