Combine results from two SQL Server tables even if they don't have common rows - sql-server

I have two tables in SQL Server:
Table 1:
Silo Material F2601 11 This Month
---------------------------------------
1 Bottle Cullet 0
2 Salt Cake 832
2 Sand 0
3 03 Soda Ash 466430
4 Fluorspar 2820
5 Barytes 2393
6 06 Borax 297405
7 Mixed Cullet 12377
8 Syenite 179880
9 09 Bottle Cullet 0
10 10 Sand 1356551
11 11 BD Lime 164171
12 12 EP Dust 13745
13 Fluorspar 14236
14 Bottle Cullet 0
15 Bottle Cullet 0
16 Syenite 0
Table 2:
Silo Material F2601 11 Last Month
---------------------------------------
1 Bottle Cullet 0
2 Sand 0
3 03 Soda Ash 467382
4 Fluorspar 3758
5 Barytes 5028
6 06 Borax 318729
7 Mixed Cullet 20955
8 Syenite 151361
9 09 Bottle Cullet 0
10 10 Sand 1393186
11 11 BD Lime 169514
12 12 EP Dust 11905
13 Fluorspar 0
14 Bottle Cullet 0
14 Bottle Cullet 14 0
15 Bottle Cullet 0
16 Syenite 0
and I am looking for a query result of...
Silo Material F2601 11 This Month F2601 11 Last Month
------------------------------------------------------------
1 Bottle Cullet 0 0
2 Salt Cake 832 0
2 Sand 0 0
3 03 Soda Ash 466430 467382
4 Fluorspar 2820 3758
5 Barytes 2393 5028
6 06 Borax 297405 318729
7 Mixed Cullet 12377 20955
8 Syenite 179880 151361
9 09 Bottle Cullet 0 0
10 10 Sand 1356551 1393186
11 11 BD Lime 164171 169514
12 12 EP Dust 13745 11905
13 Fluorspar 14236 0
14 Bottle Cullet 0 0
14 Bottle Cullet 14 0 0
15 Bottle Cullet 0 0
16 Syenite 0 0
Any help is much appreciated. It seems as though I want to combine columns but need columns that exist in one and not the other put into result set with a zero in place for the column that does not exist in that respective table.

I'm not sure of well understanding the question. But I'd go with a LEFT JOIN on the IDs, and using IFNULL.
Something like the following query will set you in the right track:
SELECT
t1.id, t1.name, t1.number, t2.number
FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id
ORDER BY t1.id;
Then you can play with IFNULL() to better format the results.

Related

Additional space after the end of a string in MS-SQL. Trimming does nothing [duplicate]

This question already has answers here:
How can I make SQL Server return FALSE for comparing varchars with and without trailing spaces?
(6 answers)
Why would SqlServer select statement select rows which match and rows which match and have trailing spaces
(4 answers)
Closed 5 months ago.
What's this? The last char is a space (ascii 32), but SQL says the trimmed version is exactly the same as the not trimmed one. Moreover, it turns out that the length of the keyword corresponds to the trimmed length of the keyword and that the space comes after what SQL considers to be the last character.
Note that there are only these 143 results in a table of billions (>1,000,000,000)
select k.Keyword, len(k.Keyword) LenKeyword, trim(k.Keyword) TrimKeyword,
case when k.Keyword=trim(k.Keyword) then 1 else 0 end isSame,
len(trim(k.Keyword)) LenTrimKeyword, SUBSTRING(k.Keyword,len(k.keyword)+1,1) LastCharPlus1
, ASCII (SUBSTRING(k.Keyword,len(k.keyword)+1,1)) AsciiLastCharPlus1
from #tk AdditionalSpaceAfterEndOfString
inner join SE_Keywords k
on AdditionalSpaceAfterEndOfString.Keyword=k.Keyword and AdditionalSpaceAfterEndOfString.Lang=k.Lang and AdditionalSpaceAfterEndOfString.Country=k.Country
order by k.DateAdded desc
Keyword LenKeyword TrimKeyword isSame LenTrimKeyword LastCharPlus1 AsciiLastCharPlus1
------------------------------------------------------------------------------------------------------------------------------------------------------ ----------- ------------------------------------------------------------------------------------------------------------------------------------------------------ ----------- -------------- ------------- ------------------
bio arganöl 11 bio arganöl 1 11 32
assurance retrait de permis 27 assurance retrait de permis 1 27 32
call center, centre d'appel, algerie, alger, fran?ais, francophone, ogs, ogs, ogsolution, 89 call center, centre d'appel, algerie, alger, fran?ais, francophone, ogs, ogs, ogsolution, 1 89 32
esta 4 esta 1 4 32
google périsprit 16 google périsprit 1 16 32
huizen aanbod den bosch 23 huizen aanbod den bosch 1 23 32
recruiting pflege 17 recruiting pflege 1 17 32
test by keywords 17 test by keywords 1 17 32
employer branding pflege 24 employer branding pflege 1 24 32
employer branding pflege 24 employer branding pflege 1 24 32
lepelboom 9 lepelboom 1 9 32
sun plaisance 13 sun plaisance 1 13 32
keyboost 4 10 keyboost 4 1 10 32
morocco desert tours 20 morocco desert tours 1 20 32
vraag aanbod reclame 20 vraag aanbod reclame 1 20 32
bedrijfskleding drukwerk 24 bedrijfskleding drukwerk 1 24 32
bedrijfstuitje zeilen 21 bedrijfstuitje zeilen 1 21 32
bmw occasion 12 bmw occasion 1 12 32
marketing altenheim 19 marketing altenheim 1 19 32
marketing pflegeeinrichtung 27 marketing pflegeeinrichtung 1 27 32
marketing prijsbeker 20 marketing prijsbeker 1 20 32
... 1 41 32
abonnement culture paris 24 abonnement culture paris 1 24 32
kommunikationsagentur pflege 28 kommunikationsagentur pflege 1 28 32
personalmarketing pflege 24 personalmarketing pflege 1 24 32
personalmarketing pflege 24 personalmarketing pflege 1 24 32
salon de beauté pavillons-sous-bois 35 salon de beauté pavillons-sous-bois 1 35 32
sophos safeguard 16 sophos safeguard 1 16 32
bon achat spectacles fnac 25 bon achat spectacles fnac 1 25 32
catering alphen-aan-den-rijn 28 catering alphen-aan-den-rijn 1 28 32
drone electronics 17 drone electronics 1 17 32
pflege marketing 16 pflege marketing 1 16 32
pop! vinyl 10 pop! vinyl 1 10 32
prêt sans banque 16 prêt sans banque 1 16 32
sebastien izambard 18 sebastien izambard 1 18 32
carte scenes et sorties 23 carte scenes et sorties 1 23 32
dongen nieuws delen 19 dongen nieuws delen 1 19 32
newerkkabels 12 newerkkabels 1 12 32
quad and camel in marrakech 27 quad and camel in marrakech 1 27 32
showroom marques de luxe 24 showroom marques de luxe 1 24 32
Trimming does not help, the only thing is looking if the char after the total of the string is a space:
select Keyword, Lang, Country into #tk from se_keywords where ascii (SUBSTRING(Keyword,len(keyword)+1,1)) is not null

Aggregate function with window function filtered by time

I have a table with data about buses while making their routes. There are columns for:
bus trip id (different each time a bus starts the route from the first stop)
bus stop id
datetime column that indicates the moment that the bus leaves each bus stop
integer that indicates how many passengers entered the bus in that stop
There is no information about how many passengers get off the bus on each stop, so I have to make an estimation supposing that once they get on the bus, they stay on it for 30 minutes. The trip lasts about 70 minutes from the first to the last stop.
I am trying to aggregate results on each stop using
SUM(iPassengersIn) OVER (
PARTITION BY tripDate, tripId
ORDER BY busStopOrder
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) total_passengers
The problem is that I can add passengers since the beginning of the trip, but not since "30 minutes ago" on each stop. How could I limit the aggregation to "the last 30 minutes" on each row in order to estimate the occupation between stops?
This is a subset of my data:
trip_date trip_id bus_stop_order minutes_since_trip_start passengers_in trip_total_passengers
2020-06-08 374910 0 0 0 0
2020-06-08 374910 1 3 0 0
2020-06-08 374910 2 5 1 1
2020-06-08 374910 3 8 0 1
2020-06-08 374910 4 9 0 1
2020-06-08 374910 5 12 0 1
2020-06-08 374910 6 13 0 1
2020-06-08 374910 7 13 0 1
2020-06-08 374910 8 15 0 1
2020-06-08 374910 9 16 0 1
2020-06-08 374910 10 16 0 1
2020-06-08 374910 11 17 0 1
2020-06-08 374910 12 18 2 3
2020-06-08 374910 13 20 0 3
2020-06-08 374910 14 22 0 3
2020-06-08 374910 15 24 0 3
2020-06-08 374910 16 25 0 3
2020-06-08 374910 17 28 2 5
2020-06-08 374910 18 30 1 6
2020-06-08 374910 19 31 0 6
2020-06-08 374910 20 33 0 6
2020-06-08 374910 21 41 3 9
2020-06-08 374910 22 44 3 12
2020-06-08 374910 23 45 4 16
2020-06-08 374910 24 48 2 18
2020-06-08 374910 25 48 2 20
2020-06-08 374910 26 50 0 20
2020-06-08 374910 27 51 0 20
2020-06-08 374910 28 51 0 20
2020-06-08 374910 29 53 0 20
2020-06-08 374910 30 55 0 20
2020-06-08 374910 31 58 0 20
For the row with bus_stop_order 21 (41 minutes into the bus trip), where 3 passengers enter the bus, I have to sum only the passengers that entered the bus between minute 11 and 41. Thus, the passenger that entered the bus in the 2nd bus stop (5 minutes into the trip) should be excluded.
That should be applied for every row.
The only thing I can think of is:
select
trip_date,
trip_id,
minutes_since_trip_start,
v.total_passengers
from
#t t1
outer apply (
select sum(passengers_in)
from #t t2
where
t1.trip_date = t2.trip_date
and t1.trip_id = t2.trip_id
and t2.bus_stop_order <= t1.bus_stop_order
and t2.minutes_since_trip_start >= t1.minutes_since_trip_start - 30
) v(total_passengers)
order by
trip_date,
trip_id,
minutes_since_trip_start
;

reading and printing a .csv file like a 2D matrix with both integer and float values in c

Reading a file in c with .csv as extension. The file consisting of both integer and float type data values. Is there any way to read the csv file. Any help is appreciated.
The data is as follows:
Application_No. Actual_Effort (in PM) No of Processes No of Tasks No of partnerLinks Task Variables Element Variables Event Variables Script Developer's Skills Developer's Confidence TPSS TS TCC
1 918.28 1 3 5 33 7 2 3 3.5 1 8 135 143
2 8891.513 3 9 3 100 15 6 12 3 1 36 1197 1233
3 22479.261 5 15 23 125 25 10 20 3 1 190 2700 2890
4 2961.131 2 4 9 70 13 4 17 2 0 72 416 488
5 19650.198 7 14 19 130 28 12 5 2.5 0 231 2450 2681
6 377.75 1 2 4 22 8 2 2 3 1 6 68 74
7 2671.93 1 5 12 55 12 6 4 2 0 17 385 402
8 966.15 3 3 6 31 8 5 7 2.5 0 27 153 180
9 3765.81 2 6 17 73 14 2 3 3.5 1 46 552 590
10 7467.11 4 8 21 87 19 13 1 2 0 116 960 1076

Tracking "trailing" daily stats in Appengine Datastore

I am trying to present 7-day trailing stats from a certain Appengine Datastore field. If my model has an integer field "Foo", I want to be able to find the top N models by the increase in Foo over the last 7 days.
I think it is best to explain by example. Suppose we have two entities with a Foo field and we sample the value of Foo every day at midnight:
Day 01 02 03 04 05 06 07 08 09 10 11 12 13
User1.Foo 1 2 3 4 5 6 7 8 9 8 7 6 5
User2.Foo 50 55 50 45 50 55 50 45 50 55 50 45 50
I want to be able to construct (for each day) the 7-day delta:
Day 01 02 03 04 05 06 07 08 09 10 11 12 13
User1.FooDelta7d 6 6 6 4 2 0 -2
User2.FooDelta7d 0 -10 0 10 0 -10 0
And be able to query for users ORDER BY FooDelta7d.
I could solve this problem by keeping 7 extra entries on each user, like Foo1 Foo2 Foo3 ... Foo7 and take the day of year modulo 7 to decide which entry to write into at midnight:
Day 01 02 03 04 05 06 07 08 09 10 11 12 13
User1.Foo 1 2 3 4 5 6 7 8 9 8 7 6 5
User1.Foo1 1 1 1 1 1 1 1 8 8 8 8 8 8
User1.Foo2 2 2 2 2 2 2 2 9 9 9 9 9
User1.Foo3 3 3 3 3 3 3 3 8 8 8 8
User1.Foo4 4 4 4 4 4 4 4 7 7 7
User1.Foo5 5 5 5 5 5 5 5 6 6
User1.Foo6 6 6 6 6 6 6 6 5
User1.Foo7 7 7 7 7 7 7 7
So on (say) day 10, the delta is User1.Foo3 - User1.Foo4 = 8 - 4 = 4. This works but it seems really gross to me, especially since it means 8x storage per field I want to track.
Does anyone have a better idea? Thanks!
Since you need to have a daily rolling 7-day-delta, you need to keep data for all past seven days. So it seems that what you are already doing is the right way.
To reduce "grossness" you might want to make Foo1..Foo7 a list property.

need hint with a custom Linux/UNIX command line utlity "cal" in C

Ok I need to make this program to display "cal" 3 month(one month before and one month after) side by side, rather than just one single month it displays in any Linux/UNIX. I got it working to display 3 calendar by using "system(customCommand)" three times; but then it's not side by side.
I got some hint to use the following system calls:
close(..) pipe(..) dup2(..) read(..) and write(..)
my question is what should I start with? Do I need to create child process and than catch it in pipe(..)?
How can I display three calendar side by side.
ex.
February 2009 March 2009 April 2009
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4
8 9 10 11 12 13 14 8 9 10 11 12 13 14 5 6 7 8 9 10 11
15 16 17 18 19 20 21 15 16 17 18 19 20 21 12 13 14 15 16 17 18
22 23 24 25 26 27 28 22 23 24 25 26 27 28 19 20 21 22 23 24 25
29 30 31 26 27 28 29 30
Assuming you want to write it yourself instead of using "cal -3", what I'd do (in psuedo code):
popen three calls to "cal" with the appropriate args
while (at least one of the three pipes hasn't hit EOF yet)
{
read a line from the first if it isn't at EOF
pad the results out to a width W, print it
read a line from the second if it isn't at EOF
pad the results out to a width W, print it
read a line from the third if it isn't at EOF
print it
print "\n"
}
pclose all three.
if "cal -3" doesn't work, just use paste :)
$ TERM=linux setterm -regtabs 24
$ paste <(cal 2 2009) <(cal 3 2009) <(cal 4 2009)
febbraio 2009 marzo 2009 aprile 2009
do lu ma me gi ve sa do lu ma me gi ve sa do lu ma me gi ve sa
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4
8 9 10 11 12 13 14 8 9 10 11 12 13 14 5 6 7 8 9 10 11
15 16 17 18 19 20 21 15 16 17 18 19 20 21 12 13 14 15 16 17 18
22 23 24 25 26 27 28 22 23 24 25 26 27 28 19 20 21 22 23 24 25
29 30 31 26 27 28 29 30
$
(setterm ignores -regtabs unless TERM=linux or TERM=con.)
just do
cal -3
Does this not work?
cal -3
Ok, how about cal -3?
cal -3 12 2120 to make it a special month and year, with one before and one after.
The approach I would use for this would be to capture the output, split it into lines, and printf the lines out next to each other. I'd probably do it in Perl, though, rather than C.
Or just use cal -3, if your cal has it.

Resources