So I have 5 tables which need to go into Result; 2-digit, 3-digit, 4-digit, 5-digit, and 6-digit. They are of the same structure. Would the following code accomplish the task:
Insert into Result select * from 2-digit, 3-digit, 4-digit, 5-digit, 6-digit;
Or does it need to look like this
Insert into Result select * from 2-digit, select * from 3-digit, select * from 4-digit,select * from 5-digit,select * from 6-digit;
Below is some sample data. The desired result is to simply consolidate these three tables into one with no manipulation of the data or rows. the end result should have 12 rows.
2 digit
x job code employment
32 10 4569
32 11 4521
3 digit
x job code employment
32 101 1203
32 102 3366
32 111 1000
32 112 3521
4 digit
32 1011 1203
32 1025 1000
32 1028 2366
32 1111 500
32 1112 500
32 1123 2899
32 1124 45
32 1125 577
You can solve this problem with SELECT . . . UNION:
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
UNION ALL
SELECT * FROM table3
. . . and so on
If the table structures are not exactly the same (they have a different number of columns or the columns have slightly different names, or are in a different order) then you will have to replace * with the column names explicitly listed.
Having the five tables in the first place is a flaw in the database design -- why not keep them all in one table and then just SELECT out the rows you want.
use SUBSTRING()
INSERT INTO Results
SELECT SUBSTRING('102100',1,2) [2-digits],
SUBSTRING('102100',1,3) [3-digits],
SUBSTRING('102100',1,4) [4-digits],
SUBSTRING('102100',1,5) [5-digits],
'102100' [6-digits]
FROM tablename
Result
2-digits 3-digits 4-digits 5-digits 6-digits
10 102 1021 10210 102100
substitute '102100' with the column name you have from your table
Related
I'm using microsoft access and I need a sql query to return the top x (40 in my case) most recent sales for each neighborhood (NBHD). My data looks something like this:
PARID PRICE SALEDT SALEVAL NBHD
04021000 140000 1/29/2016 11 700
04021000 160000 2/16/2016 11 700
04018470 250000 4/23/2015 08 701
04018470 300000 4/23/2015 08 701
04016180 40000 5/9/2017 11 705
04023430 600000 6/12/2017 19 700
And what I need is the top 40 most recent SALEDT entries for each NBHD, and if the same PARID would show up in that top 40 twice or more, I only want the most recent one. If the rows have the same PARID and the same SALEDT, I need the only most expensive one. For this small set of sample data, I would get:
PARID PRICE SALEDT SALEVAL NBHD
04021000 160000 2/16/2016 11 700
04023430 600000 6/12/2017 19 700
04018470 300000 4/23/2015 08 701
04016180 40000 5/9/2017 11 705
I get row 2 (as it has a later SALEDT than row 1), row 4 (as it has a higher PRICE than row 3, and row 5 and row 6. Hopefully that is clear. Also, I'm using MS access SQL to do this, but wouldn't be opposed to some VBA solution if that is easier. Thanks in advance.
Here you go:
select a.parid, max(a.price)price, a.saledt, a.saleval, a.nbhd from #table a join (
select parid, max(saledt) saledt from #table
group by parid ) b on a.parid=b.parid and a.saledt=b.saledt
group by a.parid, a.saledt, a.saleval, a.nbhd
order by a.nbhd
In MS Access, you can do the following to get the 40 most recent entries for each neighborhood:
select t.*
from t
where t.salesdt in (select top 40 t2.salesdt
from t as t2
where t2.nbhd = t.nbhd
order by t2.salesdt desc
);
Your additional constraints are rather confusing. I'm not sure I fully follow them because I don't know what the columns really refer to.
I have a TSQL query which gives a max value of the trend data for the given result set. I would like to get this data based on each month's result. (I have a datetime column in the result set ). If I select data for three months it has to give this vaules for each month. Right now it looks for the max values and give the same result for all the months.
Below is the expression i use to get the trend result. It's part of select statement with number of other columns.
select col1, col2, sampledatecollected, (Select MAX(AvailMemSlope) FROM SlopeCTE) * MetricID + (Select MAX (AvailMemIntercept) FROM InterceptCTE) AS AvailMemTrend
I think i might need to do something like this but given the expression i'm little confused as to how to get the desired results
select name, max(value)
from tbl1
group by name
CPUTrend id the data i get from the expression i specified in the first query.
sample data:
Date AVGCPU MAXCPU CPUTrend
8/22/2016 20 40 44
8/23/2016 20 40 44
8/24/2016 20 40 44
8/25/2016 20 40 44
9/22/2016 20 50 44
9/23/2016 20 50 44
9/24/2016 20 50 44
Expected result:
Date AVGCPU MAXCPU CPUTrend
8/22/2016 20 40 32
8/23/2016 20 40 32
8/24/2016 20 40 32
8/25/2016 20 40 32
9/22/2016 20 50 44
9/23/2016 20 50 44
9/24/2016 20 50 44
Right now all i get is 44 as it's the maximum value.
I think you just want a subquery:
with t as (
select col1, col2, sampledatecollected,
(Select MAX(AvailMemSlope) FROM SlopeCTE) * MetricID +
(Select MAX (AvailMemIntercept) FROM InterceptCTE) AS AvailMemTrend
from ?? -- the SQL in your question is incomplete
)
select year(sampledatecollected), month(sampledatecollected),
max(AvailMemTrend)
from t
group by year(sampledatecollected), month(sampledatecollected)
order by year(sampledatecollected), month(sampledatecollected);
I'm attempting to find rows given a list of values where one of the values is in a range between two of the columns, as an example:
id column1 column2
1 1 5
2 6 10
3 11 15
4 16 20
5 21 25
...
99 491 495
100 496 500
I'd like to give a list of values, e.g. (23, 83, 432, 334, 344) which would return the rows
id column1 column2
5 21 25
17 81 85
87 431 435
67 331 335
69 341 345
The only way I can think of doing this so far has been to split each into it's own call by doing
SELECT * FROM TableA WHERE (column1 < num1 AND num1 < column2)
However this scales quite poorly when the list of numbers is around several million.
Is there any better way of doing this?
Thanks for the help.
Putting millions of numbers into the SQL command itself would be unwieldy.
Obviously, you have to put the numbers into a (temporary) table.
Then you can just join the two tables:
SELECT *
FROM TableA JOIN TempTable
ON TempTable.Value BETWEEN TableA.column1 AND TableA.column2;
I have a table with data like below :
ID SUMMARY_DATE KEYWORD_ID DATA
123 9/1/2014 5 98
I need to generate 18 more rows with the summary_date + 18 months, something like below :
ID SUMMARY_DATE KEYWORD_ID DATA
123 9/1/2014 5 98
123 10/1/2014 5 98
123 11/1/2014 5 98
123 12/1/2014 5 98
...
123 3/1/2016 5 98
I could do that using UNION but it will be so long. Is there any other ways to do it?
Thanks in advance.
Just generate a list of numbers and use add_months():
with n as (
select level as m
from dual
connect by level <= 18
)
select t.id, add_months(t.summary_date, n.m, keywork_id, data
from table t cross join
n;
I have the below input
PlayerID MatchPlayed RunsMade
-------- ----------- --------
1 10 200
2 5 100
3 8 24
4 30 50
The output will be
Combined Players Combined Match Played Combined runs Made
---------------- --------------------- ------------------
1 10 200
1,2 15 300
1,3 18 224
1,4 40 250
1,2,3 23 324
1,2,4 45 350
1,3,4 48 274
1,2,3,4 53 374
2 5 100
2,3 13 124
2,4 35 150
2,3,4 43 174
3 8 24
3,4 38 74
4 30 50
The Combined Match Played column is the sum of the values of Match Played column of those players. e.g. for Combined Played 1,2 the Combined Match Played value is 10 + 5 = 15.
similarly, Combined Runs Made is the sum of the Runs MAde column of the individual players. e.g. for the same example, the Combined Runs MAde column is 200 +100 =300.
Thanks
Setup:
create table Input(PlayerId int, MatchPlayed int, RunsMade int)
insert Input
select 1, 10, 200
union all select 2, 5, 100
union all select 3, 8, 24
union all select 4, 30, 50
Query:
with cte(Combined, PlayerId, MatchPlayed, RunsMade)
as
(
select cast(PlayerId as varchar(500)), PlayerId, MatchPlayed, RunsMade
from Input
union all
select cast(cte.Combined + ',' + cast(inp.PlayerId as varchar) as varchar(500)), inp.PlayerId, inp.MatchPlayed + cte.MatchPlayed, inp.RunsMade + cte.RunsMade
from cte
join Input inp on
cte.PlayerId < inp.PlayerId
)
select Combined, MatchPlayed, RunsMade
from cte
order by Combined