Getting market data from different markets with yahoo and pandas_datareader in python - pandas-datareader

So I have a bit of an issue with redundant tag names between markets. For example when using the following code to get data:
from pandas_datareader import data as pdr
from datetime import datetime
data = pdr.get_data_yahoo(['AC'], start=datetime(2020,1,6), end=datetime(2020,2,6))
Is the data I'm getting back is NYSE:AC (Associated Capitol Group Incorporated) or TSE:AC (Air Canada)?

It's my understanding that the default with no dot and extension name is from US market. The extension name of a ticker means the market other than US. Here are some examples.
AC.TO : Toronto
AC.PA : Paris
AC.MX : Mexico
AC.VI : Vienna
FPI1.BE : Berlin
F7TB.MU : Munich
The follows can get the longName of a ticker.
from datetime import datetime
import pandas_datareader.data as web
tickers = ['AC', 'AC.TO']
for ticker in tickers:
print('\n---------------------------------------------------------------\n')
data = web.get_data_yahoo([ticker], start=datetime(2020, 1, 6), end=datetime(2020, 2, 6))
df = web.get_quote_yahoo([ticker])
print(df[['longName', 'exchange', 'fullExchangeName', 'currency', 'quoteType', ]].T)
print()
print(data)
Here is the output of the above code:
---------------------------------------------------------------
AC
longName Associated Capital Group, Inc.
exchange NYQ
fullExchangeName NYSE
currency USD
quoteType EQUITY
Attributes High Low Open Close Volume Adj Close
Symbols AC AC AC AC AC AC
Date
2020-01-06 39.790001 38.490002 38.490002 39.790001 3300 39.790001
2020-01-07 39.669998 38.590000 39.540001 39.160000 6200 39.160000
2020-01-08 39.790001 38.560001 38.889999 39.790001 6400 39.790001
2020-01-09 39.830002 39.750000 39.830002 39.750000 1800 39.750000
2020-01-10 39.520000 39.099998 39.520000 39.500000 5100 39.500000
2020-01-13 42.759998 39.250000 39.250000 42.759998 23000 42.759998
2020-01-14 47.779999 43.029999 43.160000 47.770000 13000 47.770000
2020-01-15 48.501999 47.049999 48.299999 48.419998 21400 48.419998
2020-01-16 52.820000 48.134998 48.160000 50.410000 10200 50.410000
2020-01-17 52.595001 50.160000 51.200001 51.080002 21400 51.080002
2020-01-21 65.459999 50.549999 50.709999 63.580002 42700 63.580002
2020-01-22 64.900002 56.000000 64.900002 57.220001 30700 57.220001
2020-01-23 57.480000 46.959999 57.480000 47.340000 26100 47.340000
2020-01-24 47.779999 42.299999 47.340000 42.349998 30000 42.349998
2020-01-27 48.540001 42.500000 42.500000 43.759998 28100 43.759998
2020-01-28 45.000000 43.040001 43.349998 43.880001 11700 43.880001
2020-01-29 43.689999 41.099998 43.689999 41.459999 15200 41.459999
2020-01-30 43.700001 41.730000 42.110001 42.790001 21900 42.790001
2020-01-31 42.730000 41.189999 42.299999 41.770000 20300 41.770000
2020-02-03 43.410000 42.020000 42.320000 42.279999 10900 42.279999
2020-02-04 43.299999 41.799999 42.490002 41.799999 16300 41.799999
2020-02-05 44.299999 41.730000 42.160000 43.889999 15200 43.889999
2020-02-06 45.430000 43.590000 43.937000 43.720001 19700 43.720001
2020-02-07 44.158001 41.290001 43.700001 41.290001 12600 41.290001
---------------------------------------------------------------
AC.TO
longName Air Canada
exchange TOR
fullExchangeName Toronto
currency CAD
quoteType EQUITY
Attributes High Low Open Close Volume Adj Close
Symbols AC.TO AC.TO AC.TO AC.TO AC.TO AC.TO
Date
2020-01-06 47.820000 47.110001 47.610001 47.299999 1134902 47.299999
2020-01-07 48.270000 47.200001 47.299999 48.240002 863739 48.240002
2020-01-08 49.279999 47.750000 47.750000 49.040001 1193122 49.040001
2020-01-09 50.389999 49.070000 49.119999 49.599998 1388006 49.599998
2020-01-10 51.014999 49.200001 49.590000 50.900002 1443019 50.900002
2020-01-13 52.180000 50.470001 50.849998 52.090000 1388026 52.090000
2020-01-14 52.709999 51.490002 52.029999 52.009998 1352906 52.009998
2020-01-15 52.459999 51.340000 51.930000 51.540001 1108539 51.540001
2020-01-16 52.220001 51.560001 51.700001 51.790001 930670 51.790001
2020-01-17 51.520000 50.160000 50.480000 51.080002 1767395 51.080002
2020-01-20 51.410000 50.660000 50.820000 51.150002 310712 51.150002
2020-01-21 50.610001 47.680000 50.500000 48.840000 3095071 48.840000
2020-01-22 49.680000 48.639999 49.299999 48.680000 1282190 48.680000
2020-01-23 49.000000 47.009998 48.459999 48.419998 2001993 48.419998
2020-01-24 48.580002 45.770000 48.450001 47.180000 2416449 47.180000
2020-01-27 45.529999 43.400002 43.410000 44.740002 2709248 44.740002
2020-01-28 45.770000 44.689999 45.119999 45.099998 2257306 45.099998
2020-01-29 45.959999 44.709999 45.279999 45.599998 1916354 45.599998
2020-01-30 45.200001 44.180000 45.099998 44.930000 1586113 44.930000
2020-01-31 45.139999 44.154999 44.630001 44.330002 1529131 44.330002
2020-02-03 45.180000 44.119999 44.369999 44.779999 1499613 44.779999
2020-02-04 47.250000 45.520000 45.590000 46.509998 1812547 46.509998
2020-02-05 48.060001 46.400002 47.250000 46.700001 1971011 46.700001
2020-02-06 47.130001 46.330002 47.000000 46.349998 1373740 46.349998
2020-02-07 46.250000 45.040001 46.250000 45.459999 1316269 45.459999

Related

Edit json to csv output files

I am trying to get JSON data by ‘curl’ and convert it to ‘csv’ with the ‘jq’ command.
Code:
#!/bin/bash
#Remove previously generated output files
rm datacsv.csv output.json
#Get crypto pairs data from the server (curl) in JSON from the COINAMRKET.COM server, save output.json
curl -H "X-CMC_PRO_API_KEY: 45f65521-ad1b-4dbb-9c33-743ba7a63e68" -H "Accept: application/json" -d "start=1&limit=50&convert=USD" -G https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest>output.json
#Convert JSON to CSV, Outputfile: datacsv.csv
jq -r '.data[]| [paths(scalars) | select( all(.[]; type=="string")) ] as $paths| ($paths | map(["data"] + . | join("__"))),[ getpath($paths[]) ]|#tsv' output.json >datacsv.csv
Output csv file (datacsv.csv) has repeated header for each record:
data__id data__name data__symbol data__slug data__num_market_pairs data__date_added data__max_supply data__circulating_supply data__total_supply data__cmc_rank data__last_updated data__quote__USD__price data__quote__USD__volume_24h data__quote__USD__percent_change_1h data__quote__USD__percent_change_24h data__quote__USD__percent_change_7d data__quote__USD__percent_change_30d data__quote__USD__percent_change_60d data__quote__USD__percent_change_90d data__quote__USD__market_cap data__quote__USD__last_updated
1 Bitcoin BTC bitcoin 9547 2013-04-28T00:00:00.000Z 21000000 18691931 18691931 1 2021-04-26T16:21:02.000Z 53285.1129054482 59530205038.6485 -0.6500853 5.53203621 -3.31916623 -3.33187743 4.07075949 69.03589533 996001653755.848 2021-04-26T16:21:02.000Z
data__id data__name data__symbol data__slug data__num_market_pairs data__date_added data__circulating_supply data__total_supply data__cmc_rank data__last_updated data__quote__USD__price data__quote__USD__volume_24h data__quote__USD__percent_change_1h data__quote__USD__percent_change_24h data__quote__USD__percent_change_7d data__quote__USD__percent_change_30d data__quote__USD__percent_change_60d data__quote__USD__percent_change_90d data__quote__USD__market_cap data__quote__USD__last_updated
1027 Ethereum ETH ethereum 6312 2015-08-07T00:00:00.000Z 115629413.749 115629413.749 2 2021-04-26T16:21:02.000Z 2482.06593555852 36892666790.3224 -0.23795873 5.43647682 16.16445289 46.51867774 51.11877441 94.25117671 286999829014.995 2021-04-26T16:21:02.000Z
1839 Binance Coin BNB binance-coin 579 2017-07-25T00:00:00.000Z 170532785 153432897 169432897 3 2021-04-26T16:20:10.000Z 533.23090641431 4373867739.9739 0.03999376 3.29006408 8.84917842 103.69391599 108.02152044 1217.45571414 81815162741.0835 2021-04-26T16:20:10.000Z
52 XRP XRP xrp 664 2013-08-04T00:00:00.000Z 100000000000 45404028640 99990831162 4 2021-04-26T16:21:03.000Z 1.24569103514058 13547433610.3615 0.9120132 9.79015802 -4.60461388 125.54110459 162.86485803 372.19710035 56559391436.1141 2021-04-26T16:21:03.000Z
data__id data__name data__symbol data__slug data__num_market_pairs data__date_added data__circulating_supply data__total_supply data__platform__id data__platform__name data__platform__symbol data__platform__slug data__platform__token_address data__cmc_rank data__last_updated data__quote__USD__price data__quote__USD__volume_24h data__quote__USD__percent_change_1h data__quote__USD__percent_change_24h data__quote__USD__percent_change_7d data__quote__USD__percent_change_30d data__quote__USD__percent_change_60d data__quote__USD__percent_change_90d data__quote__USD__market_cap data__quote__USD__last_updated
825 Tether USDT tether 12325 2015-02-25T00:00:00.000Z 50006254438.5217 51866290994.3298 1027 Ethereum ETH ethereum 0xdac17f958d2ee523a2206206994597c13d831ec7 5 2021-04-26T16:20:10.000Z 0.99997577777133 117822325986.94 -0.01985404 0.00891016 0.00656415 -0.1262222 -0.05070907 -0.14745816 50005043175.5917 2021-04-26T16:20:10.000Z
2010 Cardano ADA cardano 259 2017-10-01T00:00:00.000Z 45000000000 31948309440.7478 45000000000 6 2021-04-26T16:20:10.000Z 1.2163725547586 3494400505.85215 -0.63516459 6.44253956 1.77358151 2.5727442 8.69333792 266.42366354 38861046774.6607 2021-04-26T16:20:10.000Z
74 Dogecoin DOGE dogecoin 331 2013-12-15T00:00:00.000Z 129348760639.944 129348760639.944 7 2021-04-26T16:21:03.000Z 0.26639640673017 6963087140.98019 -0.74842096 -1.96582346 -25.83236676 387.41281461 386.8290754 3173.67302946 34458045049.482 2021-04-26T16:21:03.000Z
6636 Polkadot DOT polkadot-new 186 2020-08-19T00:00:00.000Z 932996847.726976 1068520962.58116 8 2021-04-26T16:21:06.000Z 32.3141434419601 2146407823.73721 -0.76428061 3.64699209 -7.2706416 0.41872077 -5.75544689 96.74696139 30148993968.3461 2021-04-26T16:21:06.000Z
data__id data__name data__symbol data__slug data__num_market_pairs data__date_added data__max_supply data__circulating_supply data__total_supply data__platform__id data__platform__name data__platform__symbol data__platform__slug data__platform__token_address data__cmc_rank data__last_updated data__quote__USD__price data__quote__USD__volume_24h data__quote__USD__percent_change_1h data__quote__USD__percent_change_24h data__quote__USD__percent_change_7d data__quote__USD__percent_change_30d data__quote__USD__percent_change_60d data__quote__USD__percent_change_90d data__quote__USD__market_cap data__quote__USD__last_updated
7083 Uniswap UNI uniswap 235 2020-09-17T00:00:00.000Z 1000000000 523384243.630469 1000000000 1027 Ethereum ETH ethereum 0x1f9840a85d5af5bf1d1762f925bdaddc4201f984 9 2021-04-26T16:20:10.000Z 35.2645393307245 1159198004.59251 -0.72129315 3.06863351 17.86724054 24.69707747 36.43669393 187.42707789 18456904244.5882 2021-04-26T16:20:10.000Z
2 Litecoin LTC litecoin 741 2013-04-28T00:00:00.000Z 84000000 66752414.5153875 66752414.5153875 10 2021-04-26T16:21:02.000Z 241.772049644181 4873850130.55793 0.08569225 3.079198 -6.01020181 32.7958836 20.24742388 84.55115332 16138868076.0832 2021-04-26T16:21:02.000Z
1831 Bitcoin Cash BCH bitcoin-cash 586 2017-07-23T00:00:00.000Z 21000000 18719506.25 18719506.25 11 2021-04-26T16:20:08.000Z 825.995177012163 4098896768.46408 -0.62457646 3.13607248 -8.10372907 65.63228051 51.71459366 96.05126285 15462221878.549 2021-04-26T16:20:08.000Z
1975 Chainlink LINK chainlink 495 2017-09-20T00:00:00.000Z 1000000000 419009556.434445 1000000000 1027 Ethereum ETH ethereum 0x514910771af9ca656af840dff83e8264ecf986ca 12 2021-04-26T16:20:10.000Z 34.4665578984855 1711065668.01118 0.39416876 3.37393637 -5.31781365 29.84576669 22.2383717 55.53806504 14441817136.8665 2021-04-26T16:20:10.000Z
5426 Solana SOL solana 41 2020-04-10T00:00:00.000Z 269856622.936333 493886983.051495 13 2021-04-26T16:21:05.000Z 46.722213592056 1743822879.60336 -1.09733007 3.84673072 46.93695628 195.9965083 184.71221177 1146.97970713 12608298776.0623 2021-04-26T16:21:05.000Z
I want to filter output ‘csv’ file, get interested fields (data__id,data__name,data__symbol,data__slug,data__num_market_pairs,data__date_added,data__max_supply,data__circulating_supply,data__total_supply )and exclude rest.
Desired output:
data__id data__name data__symbol data__slug data__num_market_pairs data__date_added data__max_supply data__circulating_supply data__total_supply
1 Bitcoin BTC bitcoin 9547 2013-04-28T00:00:00.000Z 21000000 18691931 18691931
1027 Ethereum ETH ethereum 6312 2015-08-07T00:00:00.000Z 115629413.749 115629413.749 2
1839 Binance Coin BNB binance-coin 579 2017-07-25T00:00:00.000Z 170532785 153432897 169432897
52 XRP XRP xrp 664 2013-08-04T00:00:00.000Z 100000000000 45404028640 99990831162
825 Tether USDT tether 12325 2015-02-25T00:00:00.000Z 50006254438.5217 51866290994.3298 1027
2010 Cardano ADA cardano 259 2017-10-01T00:00:00.000Z 45000000000 31948309440.7478 45000000000
74 Dogecoin DOGE dogecoin 331 2013-12-15T00:00:00.000Z 129348760639.944 129348760639.944 7
6636 Polkadot DOT polkadot-new 186 2020-08-19T00:00:00.000Z 932996847.726976 1068520962.58116 8
7083 Uniswap UNI uniswap 235 2020-09-17T00:00:00.000Z 1000000000 523384243.630469 1000000000
2 Litecoin LTC litecoin 741 2013-04-28T00:00:00.000Z 84000000 66752414.5153875 66752414.5153875
1831 Bitcoin Cash BCH bitcoin-cash 586 2017-07-23T00:00:00.000Z 21000000 18719506.25 18719506.25
1975 Chainlink LINK chainlink 495 2017-09-20T00:00:00.000Z 1000000000 419009556.434445 1000000000
5426 Solana SOL solana 41 2020-04-10T00:00:00.000Z 269856622.936333 493886983.051495 13
To emit the headers just once, you would use the template: HEADERS, TABLE:
def relevantPaths:
"id,name,symbol,slug,num_market_pairs,date_added,max_supply,circulating_supply,total_supply"
| split(",")
| map([.]) ;
relevantPaths as $paths
| ([["data"] + $paths[]] | map(join("__"))),
(.data[] | [ getpath($paths[]) ])
| #tsv

Custom datetimeparsing to combine date and time after reading csv - Pandas

upon reading text file, I am presented with an odd format, where date and time are contained in separate columns, as follows (files is tabs as separators).
temp
room 1
Date Time simulation
Fri, 01/Jan 00:30 11.94
01:30 12
02:30 12.04
03:30 12.06
04:30 12.08
05:30 12.09
06:30 11.99
07:30 12.01
08:30 12.29
09:30 12.46
10:30 12.35
11:30 12.25
12:30 12.19
13:30 12.12
14:30 12.04
15:30 11.96
16:30 11.9
17:30 11.92
18:30 11.87
19:30 11.79
20:30 12
21:30 12.16
22:30 12.27
23:30 12.3
Sat, 02/Jan 00:30 12.25
01:30 12.19
02:30 12.14
03:30 12.11
etc.
I would like to:
parse date and time over two columns ([0],[1]);
shift all timestamps 30minutes early, that is replacing :30 with :00;
I have used the following code:
timeparse = lambda x: pd.datetime.strptime(x.replace(':30',':00'), '%H:%M')
df = pd.read_csv('Chart_1.txt',
sep='\t',
skiprows=1,
date_parser=timeparse,
parse_dates=['Time'],
header=1)
Which does seem to be parsing time not dates (obviously, as this is what I told it to do).
Also, skipping rows is useful for finding the Date and Time headers, but it discards the headers temp and room 1, that I need.
You can use:
import pandas as pd
df = pd.read_csv('Chart_1.txt', sep='\t')
#get temperature to variable tempfrom third column
temp = df.columns[2]
print (temp)
Dry resultant temperature (°C)
#get aps to variable aps from second row and third column
aps = df.iloc[1, 2]
print (aps)
AE4854c_Campshill_openings reduced_communal areas increased openings2.aps
#create mask from first column - all values contains / - dates
mask = df.iloc[:, 0].str.contains('/',na=False)
#shift all rows to right NOT contain dates
df1 = df[~mask].shift(1, axis=1)
#get rows with dates
df2 = df[mask]
#concat df1 and df2, sort unsorted indexes
df = pd.concat([df1, df2]).sort_index()
#create new column names by assign
#first 3 are custom, other are from first row and fourth to end columns
df.columns = ['date','time','no name'] + df.iloc[0, 3:].tolist()
#remove first 2 row
df = df[2:]
#fill NaN values in column date by forward filling
df.date = df.date.ffill()
#convert column to datetime
df.date = pd.to_datetime(df.date, format='%a, %d/%b')
#replace 30 minutes to 00
df.time = df.time.str.replace(':30', ':00')
print (df.head())
date time no name 3F_T09_SE_SW_Bed1 GF_office_S GF_office_W_tea \
2 1900-01-01 00:00 11.94 11.47 14.72 16.66
3 1900-01-01 01:00 12.00 11.63 14.83 16.69
4 1900-01-01 02:00 12.04 11.73 14.85 16.68
5 1900-01-01 03:00 12.06 11.80 14.83 16.65
6 1900-01-01 04:00 12.08 11.84 14.79 16.62
GF_Act.Room GF_Communal areas GF_Reception GF_Ent Lobby ... \
2 17.41 12.74 12.93 10.85 ...
3 17.45 12.74 13.14 11.00 ...
4 17.44 12.71 13.23 11.09 ...
5 17.41 12.68 13.27 11.16 ...
6 17.36 12.65 13.28 11.21 ...
2F_S01_SE_SW_Bedroom 2F_S01_SE_SW_Int Circ 2F_S01_SE_SW_Storage_int circ \
2 12.58 12.17 12.54
3 12.64 12.22 12.49
4 12.68 12.27 12.48
5 12.70 12.30 12.49
6 12.71 12.31 12.51
GF_G01_SE_SW_Bedroom GF_G01_SE_SW_Storage_Bed 3F_T09_SE_SW_Bathroom \
2 14.51 14.61 11.49
3 14.55 14.59 11.50
4 14.56 14.59 11.52
5 14.55 14.58 11.54
6 14.54 14.57 11.56
3F_T09_SE_SW_Circ 3F_T09_SE_SW_Storage_int circ GF_Lounge GF_Cafe
2 11.52 11.38 12.83 12.86
3 11.56 11.35 13.03 13.03
4 11.61 11.36 13.13 13.13
5 11.65 11.39 13.17 13.17
6 11.68 11.42 13.18 13.18
[5 rows x 31 columns]

Unable to get a JOIN work

I have these two tables. 1st table contains bus information. 2nd table is multi-values and contains ALL the stations for the above mentioned bus, where the bus will stop to drop/pick passengers.
Problem Statement
Suppose there are two buses b1 and b2 with same destination. If a user selects station s1, I want both above buses to be shown. Below is the table structure and the query that I am using (query sometimes works but other times not)
Table Designs
Table 1
=======
BusId Source Destination DepartureTime
Table 2
=======
BusId StationName ArrivalTime DepartureTime
SELECT
b.BusId, b.BusNo, b.Source, b.Destination, b.SrcDepartureTime
AS SrcDepTime, b.DstArrivalTime AS DstArrTime, bs.StationName,
bs.ArrivalTime AS StationArrTime, bs.DepartureTime AS StationDepTime,
FROM
Buses b, BusStations bs
WHERE (b.BusId = bs.BusId) AND (b.Source = **passenger_source** OR bs.StationName = **passenger_source** OR bs.StationName = **passenger_dest**) AND ((DATE(b.SrcDepartureTime) = '2015-10-17') AND (DATE(bs.DepartureTime) = '2015-10-17')) GROUP BY bs.BusId;
As I said, query sometimes works but mostly not. What am I doing wrong?? Thanks for any input.
I have written the following:
My scenario is person departing from station x and wants to go to destination y then show all buses that stop at station x and go to destination y at the specified time.
I have used the same column names so should be easy to see what I have done.
declare #goingto as nvarchar(50)
declare #goingfrom as nvarchar(50)
declare #time as datetime
set #goingto = 'Basingstoke'
set #goingfrom = 'Winchester'
set #time = '2015-10-17 18:50:00.000'
select i.BusID,i.Source,i.Destination,s.StationName,s.DepartureTime from BusInfo i
left join BusStops s on i.BusID = s.BusID
where i.Destination = #goingto and s.StationName = #goingfrom
and s.DepartureTime = #time
Data:
Table Bus Info
1 Southampton Basingstoke 2015-10-17 00:00:00.000
2 Portsmouth Basingstoke 2015-10-17 00:00:00.000
3 Bristol Winchester 2015-10-16 00:00:00.000
4 Winchester Bristol 2015-10-16 00:00:00.000
Table Bus Stops
1 Winchester 2015-10-17 12:00:00.000 2015-10-17 18:50:00.000
1 Basingstoke 2015-10-17 19:00:00.000 2015-10-17 19:10:00.000
1 RedBridge 2015-10-17 21:00:00.000 2015-10-17 21:10:00.000
2 Winchester 2015-10-17 12:00:00.000 2015-10-17 18:50:00.000
3 Basingstoke 2015-10-17 19:00:00.000 2015-10-17 19:10:00.000
2 Southampton 2015-10-17 17:50:00.000 2015-10-17 18:50:00.000

Using 2 worksheets to calculate in sql

I have data that goes back to 2004 to deal with so have to simplify calculations moving from using Excel to using SQL to save processing time & pressure on our servers.
My data is:
Period Employee EmOrg EmType Total Hours Mode
201306 GOVINP1 RSA/PZB/T00 S 180 66
201306 LANDCJ1 RSA/PZB/T00 S 200 35
201306 WOODRE RSA/PZB/T00 S 180 34
201306 MOKOHM1 RSA/JNB/T00 S 160 33
201306 KAPPPJ RSA/PLZ/T00 S 160 32
201306 CAHISJ RSA/PZB/T00 S 187 31
201306 ZEMUN RSA/PZB/T00 S 180 31
201306 SAULDD1 RSA/PZB/T00 S 190 28
201306 JEROP1 RSA/DUR/T00 S 188 26
201306 NGOBS1 RSA/PZB/T00 S 204 24
201306 ZONDNS2 RSA/PZB/T00 S 192 23
201306 DLAMMP RSA/PZB/T00 S 201 23
201306 MPHURK RSA/PLZ/T00 S 160 22
201306 MNDAMB RSA/PZB/T00 S 188 21
My desired outcome is:
Period EmOrg EmType TotalHours FTE S
201308 RSA/BFN/T00 S 198 1
201308 RSA/CPT/T00 S 744 3.757575
201308 RSA/DUR/T00 S 805 4.065656
201308 RSA/JNB/T00 S 396 2
201308 RSA/PLZ/T00 S 563 2.843434
201308 RSA/PTA/T00 S 594 3
201308 RSA/PZB/T00 S 4882 24.656565
And my query:
SELECT
LD.Period,
LD.EmOrg,
LD.EmType,
Sum(LD.RegHrs) AS 'Total Hours',
Sum(LD.RegHrs) / 198 As 'FTE_S'
FROM
SSI.dbo.LD LD
GROUP BY LD.Period , LD.EmOrg , LD.EmType
HAVING (LD.EmOrg Like '%T00')
AND (LD.EmType = 'S')
How do I refer to a column in a different worksheet to use as my Mode rather than dividing with an actual number? Because different months have a different mode and using an actual number will give wrong output in other months.
You need to create a separate table for your Mode per month and than use JOIN to get that value and use it.
Something like this:
SELECT
LD.Period,
LD.EmOrg,
LD.EmType,
Sum(LD.RegHrs) AS 'Total Hours',
Sum(LD.RegHrs) / M.Mode As 'FTE_S'
FROM
SSI.dbo.LD LD
INNER JOIN SSI.dbo.Mode M
ON LD.Period = M.Period -- Not sure its should be Period or Month
GROUP BY LD.Period , LD.EmOrg , LD.EmType
HAVING (LD.EmOrg Like '%T00')
AND (LD.EmType = 'S')

SSRS returns #Error when my count is zero

I have a demographics report with a variety of breakdowns by age and gender. One of my fields is returning #Error when I hit a department with no female employees. I've tried everything I can think of to avoid this, but have yet to hit on a solution.
The pseudo-logic is, if the count of female employees = 0 return "N/A" otherwise give me the average age of female employees for this department. I've also tried returning a zero, or a blank space. All efforts return #Error when the count is zero.
My latest attempts are below:
=iif(sum(iif(Fields!CustGender.Value = "F",1D,0D),"ReportDataset") = 0D,"N/A",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset")))
=iif(sum(iif(Fields!CustGender.Value = "F",1,0),"ReportDataset") = 0,"N/A",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset")))
=iif(sum(iif(Fields!CustGender.Value = "F",1D,0D),"ReportDataset") = 0D," ",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset")))
=iif(sum(iif(Fields!CustGender.Value = "F",1,0),"ReportDataset") = 0," ",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset")))
New Information:
Changed "Nothing" to 0D and the error disappears, but the calculation is incorrect, because I'm adding a bunch of ZERO values to my Avg calculation, thus Average Age of 37 becomes 10 or something like that.
=iif(sum(iif(Fields!CustGender.Value = "F",1D,0D),"ReportDataset") = 0D,"N/A",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset"))) -- Fails
=iif(sum(iif(Fields!CustGender.Value = "F",1D,0D),"ReportDataset") = 0D,"N/A",Int(Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),0D),"ReportDataset"))) -- Works
Data for Ian:
This is department 05 of 26. The fifth field is the gender & the last field is the age I'm wanting to average.
1142 B Bellingham WA M 05 - Business Services 1948-03-06 00:00:00.000 65
2620 A Ferndale WA F 05 - Business Services 1955-02-28 00:00:00.000 58
2626 A Bellingham WA M 05 - Business Services 1948-04-09 00:00:00.000 65
3164 A Bellingham WA M 05 - Business Services 1955-01-07 00:00:00.000 58
3376 A Bellingham WA F 05 - Business Services 1960-04-02 00:00:00.000 53
3867 A Maple Falls WA F 05 - Business Services 1958-06-11 00:00:00.000 55
4294 A Blaine WA F 05 - Business Services 1981-08-09 00:00:00.000 32
4580 A Bellingham WA M 05 - Business Services 1956-02-04 00:00:00.000 57
4702 A Bellingham WA M 05 - Business Services 1967-12-30 00:00:00.000 45
4709 A Lynden WA M 05 - Business Services 1961-04-27 00:00:00.000 52
4764 A Blaine WA F 05 - Business Services 1957-04-28 00:00:00.000 56
4892 A Ferndale WA F 05 - Business Services 1976-10-19 00:00:00.000 36
4971 A Bellingham WA F 05 - Business Services 1983-10-16 00:00:00.000 29
4986 C Bellingham WA M 05 - Business Services 1974-03-31 00:00:00.000 39
5085 A Bellingham WA F 05 - Business Services 1994-10-18 00:00:00.000 18
5094 A Bellingham WA F 05 - Business Services 1986-04-22 00:00:00.000 27
SOLVED!!
I could not include the ZERO values as the return value for a "false" evaluation when asking "is this a female" because then my sample size increased with a dozen or so male employees, and then the Avg function was thrown way off because I had all these males who had an age of 0. Thus my female employees were showing up as 8, 11, 14 years old. The key is that in the "true" portion of my iif, I was converting to an Int, and that meant passing back "Nothing" was an invalid practice. By removing my Int, I can put the "Nothing" back in, thus avoiding the bloated sample size, and getting a valid value back from Avg.
=iif(sum(iif(Fields!CustGender.Value = "F",1D,0D),"ReportDataset") = 0D,"N/A",Avg(IIF(Fields!CustGender.Value = "F", CDec(Fields!Age.Value),Nothing),"ReportDataset"))